private static void CreateCompounds(Map map, TileLayer terrainLayer, TileLayer wallLayer, TileLayer roofLayer, List<Compound> compounds, float[][] noise, float height, float distance, int minsize, LightingEngine lightingEngine, GraphicsDevice gd) { for (int y = 40; y < map.Width - 40; y++) { for (int x = 40; x < map.Height - 40; x++) { if (noise[y][x] > height) { Vector2 thisLoc = new Vector2(x * map.TileWidth, y * map.TileHeight); bool tooClose = false; foreach (Compound c in compounds) if ((c.Position - thisLoc).Length() < distance) tooClose = true; if (!tooClose) { Rectangle bounds = new Rectangle(x, y, 1, 1); bounds.Inflate(minsize, minsize); bounds.Inflate(rand.Next(5) * 2, rand.Next(5) * 2); bool tooBig = true; while (tooBig) { if (GetTileIndex(map, terrainLayer, bounds.Left, bounds.Top) != GRASS || GetTileIndex(map, terrainLayer, bounds.Right, bounds.Top) != GRASS || GetTileIndex(map, terrainLayer, bounds.Left, bounds.Bottom) != GRASS || GetTileIndex(map, terrainLayer, bounds.Right, bounds.Bottom) != GRASS) { tooBig = true; bounds.Inflate(-1, -1); } else tooBig = false; } if (bounds.Width >= minsize && bounds.Height >= minsize) { Compound newCompound = new Compound() { Position = thisLoc }; for (int xx = bounds.Left; xx <= bounds.Right; xx++) { for (int yy = bounds.Top; yy <= bounds.Bottom; yy++) { wallLayer.Tiles[xx, yy] = null; if (xx > bounds.Left + 2 && xx < bounds.Right - 2 && yy > bounds.Top + 2 && yy < bounds.Bottom - 2) terrainLayer.Tiles[xx, yy] = map.Tiles[MUD]; } } Rectangle innerBounds = bounds; innerBounds.Inflate(-3, -3); // Outer walls wallLayer.Tiles[innerBounds.Left, innerBounds.Top] = map.Tiles[WALL_TL]; wallLayer.Tiles[innerBounds.Right, innerBounds.Top] = map.Tiles[WALL_TR]; wallLayer.Tiles[innerBounds.Left, innerBounds.Bottom] = map.Tiles[WALL_BL]; wallLayer.Tiles[innerBounds.Right, innerBounds.Bottom] = map.Tiles[WALL_BR]; for (int xx = innerBounds.Left + 1; xx <= innerBounds.Right - 1; xx++) { wallLayer.Tiles[xx, innerBounds.Top] = map.Tiles[WALL_EDGE_UP]; wallLayer.Tiles[xx, innerBounds.Bottom] = map.Tiles[WALL_EDGE_DOWN]; } for (int yy = innerBounds.Top + 1; yy <= innerBounds.Bottom - 1; yy++) { wallLayer.Tiles[innerBounds.Left, yy] = map.Tiles[WALL_EDGE_LEFT]; wallLayer.Tiles[innerBounds.Right,yy] = map.Tiles[WALL_EDGE_RIGHT]; } newCompound.Bounds = bounds; newCompound.InnerBounds = innerBounds; // Exits bool[] exits = new bool[4] { false, false, false, false }; for(int i=0;i<4;i++) exits[rand.Next(4)] = true; bool carparkPlaced = false; Building carpark = null; if (exits[0]) { int doorx = rand.Next(innerBounds.Width - 7) + 3; for (int xx = innerBounds.Left + doorx; xx < (innerBounds.Left + doorx) + 4; xx++) wallLayer.Tiles[xx, innerBounds.Top] = null; if (!carparkPlaced && rand.Next(2) == 0) { carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle((innerBounds.Left + doorx), innerBounds.Top + 2, 4, 4) }; newCompound.Buildings.Add(carpark); carparkPlaced = true; } } if (exits[1]) { int doorx = rand.Next(innerBounds.Width - 7) + 3; for (int xx = innerBounds.Left + doorx; xx < (innerBounds.Left + doorx) + 4; xx++) wallLayer.Tiles[xx, innerBounds.Bottom] = null; if (!carparkPlaced && rand.Next(2) == 0) { carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle((innerBounds.Left + doorx), innerBounds.Bottom - 6, 4, 4) }; newCompound.Buildings.Add(carpark); carparkPlaced = true; } } if (exits[2]) { int doory = rand.Next(innerBounds.Height - 7) + 3; for (int yy = innerBounds.Top + doory; yy < (innerBounds.Top + doory) + 4; yy++) wallLayer.Tiles[innerBounds.Left,yy] = null; if (!carparkPlaced && rand.Next(2) == 0) { carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle(innerBounds.Left + 2, (innerBounds.Top + doory), 4, 4) }; newCompound.Buildings.Add(carpark); carparkPlaced = true; } } if (exits[3]) { int doory = rand.Next(innerBounds.Height - 7) + 3; for (int yy = innerBounds.Top + doory; yy < (innerBounds.Top + doory) + 4; yy++) wallLayer.Tiles[innerBounds.Right, yy] = null; if (!carparkPlaced && rand.Next(2) == 0) { carpark = new Building() { Type = BuildingType.Carpark, Rect = new Rectangle(innerBounds.Right - 6, (innerBounds.Top + doory), 4, 4) }; newCompound.Buildings.Add(carpark); carparkPlaced = true; } } int lightSpacing = 5; for (int xx = innerBounds.Left + 4; xx <= innerBounds.Right - 4; xx++) { lightSpacing++; if (lightSpacing == 6) { lightSpacing = 0; if (GetTileIndex(map, wallLayer, xx, innerBounds.Top) == WALL_EDGE_UP && GetTileIndex(map, wallLayer, xx - 2, innerBounds.Top) == WALL_EDGE_UP && GetTileIndex(map, wallLayer, xx + 2, innerBounds.Top) == WALL_EDGE_UP) { LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half); ls.Rotation = MathHelper.PiOver2; ls.Position = new Vector2((xx * map.TileWidth) + (map.TileWidth / 2), ((innerBounds.Top + 1) * map.TileHeight)); lightingEngine.LightSources.Add(ls); } if (GetTileIndex(map, wallLayer, xx, innerBounds.Bottom) == WALL_EDGE_DOWN && GetTileIndex(map, wallLayer, xx - 2, innerBounds.Bottom) == WALL_EDGE_DOWN && GetTileIndex(map, wallLayer, xx + 2, innerBounds.Bottom) == WALL_EDGE_DOWN) { LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half); ls.Rotation = MathHelper.PiOver2 + MathHelper.Pi; ls.Position = new Vector2((xx * map.TileWidth) + (map.TileWidth / 2), ((innerBounds.Bottom) * map.TileHeight)); lightingEngine.LightSources.Add(ls); } } } lightSpacing = 5; for (int yy = innerBounds.Top + 4; yy <= innerBounds.Bottom - 4; yy++) { lightSpacing++; if (lightSpacing == 6) { lightSpacing = 0; if (GetTileIndex(map, wallLayer, innerBounds.Left, yy) == WALL_EDGE_LEFT && GetTileIndex(map, wallLayer, innerBounds.Left, yy - 2) == WALL_EDGE_LEFT && GetTileIndex(map, wallLayer, innerBounds.Left, yy + 2) == WALL_EDGE_LEFT) { LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half); //ls.Rotation = MathHelper.PiOver2; ls.Position = new Vector2(((innerBounds.Left+1) * map.TileWidth), (yy * map.TileHeight)+(map.TileHeight/2)); lightingEngine.LightSources.Add(ls); } if (GetTileIndex(map, wallLayer, innerBounds.Right, yy) == WALL_EDGE_RIGHT && GetTileIndex(map, wallLayer, innerBounds.Right, yy - 2) == WALL_EDGE_RIGHT && GetTileIndex(map, wallLayer, innerBounds.Right, yy + 2) == WALL_EDGE_RIGHT) { LightSource ls = new LightSource(gd, 300, LightAreaQuality.Low, Color.White, BeamStencilType.None, SpotStencilType.Half); ls.Rotation = MathHelper.Pi; ls.Position = new Vector2(((innerBounds.Right) * map.TileWidth), (yy * map.TileHeight) + (map.TileHeight / 2)); lightingEngine.LightSources.Add(ls); } } } //if (carpark!=null) // for (int xx = carpark.Rect.Left; xx < carpark.Rect.Right; xx++) // for (int yy = carpark.Rect.Top; yy < carpark.Rect.Bottom; yy++) // terrainLayer.Tiles[xx, yy] = map.Tiles[CARPARK]; MakeBuildings(map, wallLayer, terrainLayer, roofLayer, newCompound); compounds.Add(newCompound); } } } } } }
private static void MakeBuildings(Map map, TileLayer wallLayer, TileLayer terrainLayer, TileLayer roofLayer, Compound newCompound) { Rectangle innerBounds = newCompound.InnerBounds; innerBounds.Inflate(-2, -2); // Helipad Building heliPad = null; for (int i = 0; i < 10; i++) { Point pos = new Point(innerBounds.Left + rand.Next(innerBounds.Width), innerBounds.Top + rand.Next(innerBounds.Height)); Rectangle rect = new Rectangle(pos.X - 3, pos.Y - 3, 6, 6); bool canPlace = true; foreach (Building b in newCompound.Buildings) { Rectangle br = b.Rect; br.Inflate(2, 2); if (br.Intersects(rect)) canPlace = false; } if (!innerBounds.Contains(rect)) canPlace = false; if (canPlace) { heliPad = new Building() { Rect = rect, Type = BuildingType.Helipad }; break; } } if (heliPad != null) { newCompound.Buildings.Add(heliPad); //for (int xx = heliPad.Rect.Left; xx < heliPad.Rect.Right; xx++) // for (int yy = heliPad.Rect.Top; yy < heliPad.Rect.Bottom; yy++) // terrainLayer.Tiles[xx, yy] = map.Tiles[CARPARK]; } // Buildings! for (int i = 0; i < 100; i++) { Building newBuilding = null; Point pos = new Point(innerBounds.Left + 4 + rand.Next(innerBounds.Width - 8), innerBounds.Top + 4 + rand.Next(innerBounds.Height-8)); Rectangle rect = new Rectangle(pos.X, pos.Y, 1, 1); rect.Inflate(5 + rand.Next(10), 5 + rand.Next(10)); bool canPlace = true; foreach (Building b in newCompound.Buildings) { Rectangle br = b.Rect; br.Inflate(2, 2); if (br.Intersects(rect)) canPlace = false; } if (!innerBounds.Contains(rect)) canPlace = false; if (canPlace) { newBuilding = new Building() { Rect = rect, Type = BuildingType.Building }; } if (newBuilding != null) { newCompound.Buildings.Add(newBuilding); // Outer walls wallLayer.Tiles[rect.Left, rect.Top] = map.Tiles[WALL_TL]; wallLayer.Tiles[rect.Right, rect.Top] = map.Tiles[WALL_TR]; wallLayer.Tiles[rect.Left, rect.Bottom] = map.Tiles[WALL_BL]; wallLayer.Tiles[rect.Right, rect.Bottom] = map.Tiles[WALL_BR]; roofLayer.Tiles[rect.Left, rect.Top] = map.Tiles[ROOF_TL]; roofLayer.Tiles[rect.Right, rect.Top] = map.Tiles[ROOF_TR]; roofLayer.Tiles[rect.Left, rect.Bottom] = map.Tiles[ROOF_BL]; roofLayer.Tiles[rect.Right, rect.Bottom] = map.Tiles[ROOF_BR]; for (int xx = rect.Left + 1; xx <= rect.Right - 1; xx++) { wallLayer.Tiles[xx, rect.Top] = map.Tiles[WALL_EDGE_UP]; wallLayer.Tiles[xx, rect.Bottom] = map.Tiles[WALL_EDGE_DOWN]; roofLayer.Tiles[xx, rect.Top] = map.Tiles[ROOF_EDGE_UP]; roofLayer.Tiles[xx, rect.Bottom] = map.Tiles[ROOF_EDGE_DOWN]; } for (int yy = rect.Top + 1; yy <= rect.Bottom - 1; yy++) { wallLayer.Tiles[rect.Left,yy] = map.Tiles[WALL_EDGE_LEFT]; wallLayer.Tiles[rect.Right,yy] = map.Tiles[WALL_EDGE_RIGHT]; roofLayer.Tiles[rect.Left, yy] = map.Tiles[ROOF_EDGE_LEFT]; roofLayer.Tiles[rect.Right, yy] = map.Tiles[ROOF_EDGE_RIGHT]; } for (int xx = rect.Left+1; xx <= rect.Right-1; xx++) { for (int yy = rect.Top+1; yy <= rect.Bottom-1; yy++) { roofLayer.Tiles[xx, yy] = map.Tiles[ROOF]; } } // Exits bool[] exits = new bool[4] { false, false, false, false }; exits[rand.Next(4)] = true; if (exits[0]) { int doorx = rand.Next(rect.Width - 7) + 3; for (int xx = rect.Left + doorx; xx < (rect.Left + doorx) + 4; xx++) { wallLayer.Tiles[xx, rect.Top] = null; roofLayer.Tiles[xx, rect.Top] = null; } } if (exits[1]) { int doorx = rand.Next(rect.Width - 7) + 3; for (int xx = rect.Left + doorx; xx < (rect.Left + doorx) + 4; xx++) { wallLayer.Tiles[xx, rect.Bottom] = null; roofLayer.Tiles[xx, rect.Bottom] = null; } } if (exits[2]) { int doory = rand.Next(rect.Height - 7) + 3; for (int yy = rect.Top + doory; yy < (rect.Top + doory) + 4; yy++) {wallLayer.Tiles[rect.Left, yy] = null; roofLayer.Tiles[rect.Left, yy] = null;} } if (exits[3]) { int doory = rand.Next(rect.Height - 7) + 3; for (int yy = rect.Top + doory; yy < (rect.Top + doory) + 4; yy++){ wallLayer.Tiles[rect.Right, yy] = null; roofLayer.Tiles[rect.Right, yy] = null;} } //for (int xx = newBuilding.Rect.Left; xx < newBuilding.Rect.Right; xx++) // for (int yy = newBuilding.Rect.Top; yy < newBuilding.Rect.Bottom; yy++) // terrainLayer.Tiles[xx, yy] = map.Tiles[WALL_HORIZ]; } } }
public void DiscoverCompound(Compound c, bool[,] mapFog) { float rad = c.Bounds.Width * TileWidth; if (c.Bounds.Height * TileHeight > rad) rad = c.Bounds.Height * TileHeight; rad /= 1.5f; for (float a = 0.0f; a < MathHelper.TwoPi; a += 0.01f) { for (float r = 0.0f; r < rad; r += 50f) { Vector2 p = c.Position + (new Vector2((float)Math.Cos(a), (float)Math.Sin(a)) * r); if(p.X>=0f && p.X<(Width * TileWidth) && p.Y>=0f && p.Y<(Width * TileWidth)) mapFog[(int)(p.X / TileWidth), (int)(p.Y / TileHeight)] = true; } } c.Discovered = true; }