// Returns X, Y, NeedsRemoval public Tuple <int, int, bool> FindValidLocation(FarmObject cabin) { // First, check if the inital location has any overlaps var initialOverlaps = ObjectsList.Select(x => FindAllOverlaps(x, cabin)); if (!initialOverlaps.Any()) { // And if there aren't any, return the cabin's initial X,Y return(new Tuple <int, int, bool>(cabin.TileX, cabin.TileY, false)); } var optimalLocation = FindPossibleLocations(GenerateOptimalLocations(), cabin); if (optimalLocation != null) { return(optimalLocation); } // If no optimal location works, start looking everywhere var location = FindPossibleLocations(GenerateEntireMapLocations(), cabin); if (location != null) { return(location); } // If nowhere works, return nothing, no location is valid, inform the player. return(null); }
public void ClearLocationForCabin(int x, int y, FarmObject c) { var cabin = new FarmObject(GameObjectTypes.Building, x, y, c.Width, c.Height); var overlapLists = ObjectsList.SelectMany(t => FindAllOverlaps(t, cabin, true)); foreach (var obj in overlapLists) { if (obj.Item3.Element.Parent != null) { obj.Item3.Element.Remove(); } } }
public bool MoveToValidLocation(Cabin originalCabin) { var cabin = new FarmObject(originalCabin); Tuple <int, int, bool> location; location = FindValidLocation(cabin); if (location == null) { return(false); } if (location.Item3) { ClearLocationForCabin(location.Item1, location.Item2, cabin); } originalCabin.TileX = location.Item1; originalCabin.TileY = location.Item2; return(true); }
public IEnumerable <Tuple <int, int> > FindOverlaps(FarmObject object1, FarmObject object2) { return(object1.TileXYRange.Where(x => object2.TileXYRange.Contains(x))); }
public IEnumerable <Tuple <int, int, FarmObject> > FindAllOverlaps(IEnumerable <FarmObject> objects, FarmObject farmObject, bool onlyRemovable = false) { var overlapsList = new List <Tuple <int, int, FarmObject> >(); foreach (var obj in objects) { if ((onlyRemovable && obj.CanBeRemoved) || !onlyRemovable) { var overlaps = FindOverlaps(obj, farmObject); if (overlaps.Any()) { overlapsList.AddRange(overlaps.Select(x => new Tuple <int, int, FarmObject>(x.Item1, x.Item2, obj))); } } } return(overlapsList); }
public Tuple <int, int, bool> FindPossibleLocations(IEnumerable <Tuple <int, int> > locations, FarmObject cabin) { Tuple <int, int, bool> optimalCabinLocation = null; var listOfPotentialCabins = locations.Select(x => new FarmObject(GameObjectTypes.Building, x.Item1, x.Item2, 5, 3)); // Cabin X , Y, List of Overlaps with X, Y, FarmObject @ overlap var overlapsByCabinLocations = listOfPotentialCabins.Select(c => new Tuple <int, int, IEnumerable <Tuple <int, int, FarmObject> > >(c.TileX, c.TileY, ObjectsList.SelectMany(x => FindAllOverlaps(x, c)))); foreach (var possibleCabinLocation in overlapsByCabinLocations) { var possible = true; foreach (var overlap in possibleCabinLocation.Item3) { var farmObject = overlap.Item3; if (!farmObject.CanBeRemoved) { possible = false; break; } } if (possible) { var needsRemoval = possibleCabinLocation.Item3.Any(); optimalCabinLocation = new Tuple <int, int, bool>(possibleCabinLocation.Item1, possibleCabinLocation.Item2, needsRemoval); break; } } return(optimalCabinLocation); }