public static Building NewRandomBuilding(int minX, int minY, int maxX, int maxY) { Building b = new Building(); // wyznaczamy rozmiary budynku if (r.Next(10) > 1) { b.SizeX = Math.Min(3, maxX - 2) + r.Next(Math.Max(Math.Min(8, maxX - 4), 1)); b.SizeY = Math.Min(3, maxY - 2) + r.Next(Math.Max(Math.Min(8, maxY - 4), 1)); } else { if (r.Next(2) == 0) { b.SizeX = 1; b.SizeY = Math.Min(3, maxY-2) + r.Next(Math.Max(Math.Min(7, maxX - 5),1)); } else { b.SizeX = Math.Min(3, maxX-2) + r.Next(Math.Max(Math.Min(7, maxY - 5),1)); b.SizeY = 1; } } b.X = minX + r.Next(Math.Max(maxX + 1 - b.SizeX, 1)); b.Y = minY + r.Next(Math.Max(maxY + 1 - b.SizeY, 1)); // losujemy czy drzwi będą na ścianach lewa/prawa czy też dolna/górna Point drzwi = new Point(); if (r.Next(2) == 0) // lewa/prawa { int shift = 1 + r.Next(Math.Max(1, b.SizeY - 2)); int x = 0; if (b.X < 2) // budynek przy lewym obrzeżu planszy x = b.X + b.SizeX - 1; else if (b.X + b.SizeX >= maxX -1) x = b.X; else x = r.Next(2) == 0 ? b.X : b.X + b.SizeX - 1; drzwi.X = x; drzwi.Y = b.Y + shift; } else { int shift = 1 + r.Next(Math.Max(1, b.SizeX - 2)); int y = 0; if (b.Y < 2) // budynek przy lewym obrzeżu planszy y = b.Y + b.SizeY - 1; else if (b.Y + b.SizeY >= maxY -1) y = b.Y; else y = r.Next(2) == 0 ? b.Y : b.Y + b.SizeY - 1; drzwi.X = b.X + shift; drzwi.Y = y; } b.Doors = drzwi; return b; }
private bool intersects(Building b, List<Building> buildings) { foreach (Building building in buildings) { if (b.intersects(building)) return true; } return false; }
public bool intersects(Building building) { Building left = this.X < building.X ? this : building; Building right = this.X >= building.X ? this : building; Building top = this.Y < building.Y ? this : building; Building down = this.Y >= building.Y ? this : building; if (left.X + left.SizeX >= right.X && top.Y + top.SizeY >= down.Y) return true; return false; }