public Location GenerateRandomLocation() { Position position = new Position(0, 0); Location location = new Location(World.Instance, position, World.Instance.ChooseRandomLocationType()); return location; }
public Location GetFreeLocation() { Location newlocation = null; // If no locations return 0,0 if (World.Instance.GetLocations().Count == 0) { Position position = new Position(0, 0); newlocation = new Location(World.Instance, position, World.Instance.ChooseRandomLocationType()); World.Instance.AddLocation(newlocation); } else { List<Location> tmpLocations = new List<Location>(World.Instance.GetLocations()); foreach (Location location in tmpLocations) { Position foundemptyposition = World.Instance.GetFreePositionNextToLocation(location); if (foundemptyposition != null) { Position position = new Position(foundemptyposition.GetX(), foundemptyposition.GetY()); newlocation = new Location(World.Instance, position, World.Instance.ChooseRandomLocationType()); World.Instance.AddLocation(newlocation); continue; } } } return newlocation; }
public Position GetFreePositionNextToLocation(Location fromlocation) { Position foundposition = null; for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { int checkx = fromlocation.GetPosition().GetX() + x; int checky = fromlocation.GetPosition().GetY() + y; Location location = World.Instance.GetLocations().Where(l => l.GetPosition().GetX() == checkx && l.GetPosition().GetY() == checky ).FirstOrDefault(); if (location == null) { foundposition = new Position(checkx, checky); } continue; } if (foundposition == null) { continue; } } return foundposition; }