void CheckBuildable(Listof_BuildTypes B) { BuildCost = 0; Buildable = true; Typeof_BuildItems BuildData = _e.Dictionaryof_BuildItems[Building]; if (B == Listof_BuildTypes.Road) { Point P1, P2, P3; //Set building points X and Y if (BuildPoint1.X <= BuildPoint2.X) { P1.X = BuildPoint1.X; P2.X = BuildPoint2.X; } else { P1.X = BuildPoint2.X; P2.X = BuildPoint1.X; } if (BuildPoint1.Y <= BuildPoint2.Y) { P1.Y = BuildPoint1.Y; P2.Y = BuildPoint2.Y; } else { P1.Y = BuildPoint2.Y; P2.Y = BuildPoint1.Y; } //Set shortest angle int ShortX = Math.Abs(BuildPoint1.X - BuildPoint2.X), ShortY = Math.Abs(BuildPoint1.Y - BuildPoint2.Y), Temp; if (Keyboard.GetState().IsKeyUp(Keys.LeftControl)) { Temp = ShortX; ShortX = ShortY; ShortY = Temp; } if (ShortY >= ShortX) { P3.X = BuildPoint2.X; P3.Y = BuildPoint1.Y; } else { P3.X = BuildPoint1.X; P3.Y = BuildPoint2.Y; } District district = _city.GetDistrictByPoint(P1); if (district != null) { for (int x = P1.X; x <= P2.X - 1; x++) { if (!district.Area.Contains(new Point(x, P3.Y))) { Buildable = false; return; } } for (int y = P1.Y; y <= P2.Y; y++) { if (!district.Area.Contains(new Point(P3.X, y))) { Buildable = false; return; } } } else { Buildable = false; return; } //Check cost for (int x = P1.X; x <= P2.X - 1; x++) { if (_city.TileMap[x, P3.Y].Type != Building) { BuildCost += _e.Dictionaryof_BuildItems[Building].Cost; } } for (int y = P1.Y; y <= P2.Y; y++) { if (_city.TileMap[P3.X, y].Type != Building) { BuildCost += _e.Dictionaryof_BuildItems[Building].Cost; } } } if (B == Listof_BuildTypes.Zone) { Point P1, P2; //Set building points X and Y if (BuildPoint1.X <= BuildPoint2.X) { P1.X = BuildPoint1.X; P2.X = BuildPoint2.X; } else { P1.X = BuildPoint2.X; P2.X = BuildPoint1.X; } if (BuildPoint1.Y <= BuildPoint2.Y) { P1.Y = BuildPoint1.Y; P2.Y = BuildPoint2.Y; } else { P1.Y = BuildPoint2.Y; P2.Y = BuildPoint1.Y; } Rectangle buildzone = new Rectangle(P1.X, P1.Y, (P2.X - P1.X) + 1, (P2.Y - P1.Y) + 1); District district1 = _city.GetDistrictByPoint(P1); District district2 = _city.GetDistrictByPoint(P2); if (district1 != null && district2 != null && district1 == district2) { } else { Buildable = false; return; } //Add Cost for (int x = buildzone.Left; x < buildzone.Right; x++) { for (int y = buildzone.Top; y < buildzone.Bottom; y++) { if (_city.TileMap[x, y].Buildable == true && (_city.TileMap[x, y].Type != Building)) { BuildCost += _e.Dictionaryof_BuildItems[Building].Cost; } } } } if (B == Listof_BuildTypes.CityCenter) { Rectangle area = new Rectangle(BuildPoint1.X, BuildPoint1.Y, 1, 1); area.Inflate(20, 20); if (IntersectsDistrict(area)) { Buildable = false; } if (!_city.CityArea.Contains(area)) { Buildable = false; } BuildCost = BuildData.Cost; } if (B == Listof_BuildTypes.Structure) { for (int y = 0; y < BuildData.Size.Y; y++) { for (int x = 0; x < BuildData.Size.X; x++) { if (!_city.TileMap[BuildPoint1.X - x, BuildPoint1.Y - y].Buildable) { Buildable = false; } } } BuildCost = BuildData.Cost; } //No more Money :( if (BuildCost > Currency) { Buildable = false; } }
/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); bool DrawMapTile = true; Color MapTileColor = Color.White; Vector2 pos; Point sel_pos; Vector2 Tile_Size = new Vector2(zoom, zoom); Rectangle distrect = Rectangle.Empty; spriteBatch.Begin(samplerState: SamplerState.PointClamp); Rectangle ScreenR; //sel_pos = (Mouse.GetState().Position + Screen_Scroll) / new Point(128, 256); ScreenR.X = Screen_Scroll.X / 128; ScreenR.Y = Screen_Scroll.Y / 256; ScreenR.Width = 10; //(int)Math.Ceiling((double)Screen_Size.X / 128); ScreenR.Height = 10; //(int)Math.Ceiling((double)Screen_Size.Y / 256); //ScreenR.Inflate(10, 10); Vector2 mPos = new Vector2((Mouse.GetState().Position.X + Screen_Scroll.X) / zoom, (Mouse.GetState().Position.Y + Screen_Scroll.Y) / zoom); sel_pos.X = (int)Math.Floor((mPos.Y - 128) / 64 + (mPos.X - 128) / 128); sel_pos.Y = (int)Math.Floor((-mPos.X - 128) / 128 + (mPos.Y - 128) / 64); for (int y = 0; y < _city.CityArea.Height; y++) { for (int x = 0; x < _city.CityArea.Width; x++) { pos.X = x * 64 * zoom - y * 64 * zoom - 64 * zoom - Screen_Scroll.X; pos.Y = y * 32 * zoom + x * 32 * zoom - Screen_Scroll.Y; Rectangle rec = new Rectangle((int)sel_pos.X, (int)sel_pos.Y, 1, 1); rec.Inflate(20, 20); Rectangle ScreenRect = new Rectangle(0, 0, Screen_Size.X, Screen_Size.Y); ScreenRect.Y -= Convert.ToInt32(256 * zoom); ScreenRect.X -= Convert.ToInt32(128 * zoom); ScreenRect.Inflate(256 * zoom, 128 * zoom); //if (rec.Contains(new Point(x, y))) //{ //} //else //Need to make the tile loop perdicted if (ScreenRect.Contains(pos)) { DrawMapTile = true; MapTileColor = Color.White; //////////////Building Calculation/////////////// if (MouseMode == Listof_MouseMode.Building) { foreach (var district in _city.districts) { if (district.Area.Contains(sel_pos)) { distrect = district.Area; } } //Structures if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.Structure) { Typeof_BuildItems BuildData = _e.Dictionaryof_BuildItems[Building]; Rectangle BuildR = new Rectangle(sel_pos + new Point(1, 1) - BuildData.Size, BuildData.Size); if (BuildR.Contains(new Point(x, y))) { int sprite = sel_pos.X - x + ((sel_pos.Y - y) * BuildData.Size.X); Color color; if (Buildable) { color = Color.LightGreen; } else { color = Color.LightSalmon; } spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(sprite * 128, 0, 128, 256), color, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawMapTile = false; } //if (x == sel_pos.X && y == sel_pos.Y) // spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), Color.CornflowerBlue, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); } //Zones if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.Zone) { if (x == sel_pos.X && y == sel_pos.Y) { if (_city.TileMap[x, y].Buildable) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), Color.White, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); } else { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), Color.CornflowerBlue, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); } DrawMapTile = false; } if (BuildRect.Contains(new Point(x, y))) { if (!_city.TileMap[x, y].Buildable && _e.Dictionaryof_BuildItems[_city.TileMap[x, y].Type].BuildingType == Listof_BuildTypes.Empty) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), Color.CornflowerBlue, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawMapTile = false; } if (_city.TileMap[x, y].Buildable) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), Color.White, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawMapTile = false; } } } //Roads if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.Road) { Point P1, P2, P3; //Set building points X and Y if (BuildPoint1.X <= BuildPoint2.X) { P1.X = BuildPoint1.X; P2.X = BuildPoint2.X; } else { P1.X = BuildPoint2.X; P2.X = BuildPoint1.X; } if (BuildPoint1.Y <= BuildPoint2.Y) { P1.Y = BuildPoint1.Y; P2.Y = BuildPoint2.Y; } else { P1.Y = BuildPoint2.Y; P2.Y = BuildPoint1.Y; } //Set Shortest Angle int ShortX = Math.Abs(BuildPoint1.X - BuildPoint2.X), ShortY = Math.Abs(BuildPoint1.Y - BuildPoint2.Y), Temp; if (Keyboard.GetState().IsKeyUp(Keys.LeftControl)) { Temp = ShortX; ShortX = ShortY; ShortY = Temp; } if (ShortY >= ShortX) { P3.X = BuildPoint2.X; P3.Y = BuildPoint1.Y; } else { P3.X = BuildPoint1.X; P3.Y = BuildPoint2.Y; } if (y == P3.Y && x >= P1.X && x <= P2.X) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), (Buildable == true ? Color.CornflowerBlue : Color.LightSalmon), 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawMapTile = false; } if (x == P3.X && y >= P1.Y && y <= P2.Y) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), (Buildable == true ? Color.CornflowerBlue : Color.LightSalmon), 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawMapTile = false; } } //CityCenter if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.CityCenter) { Rectangle dist = new Rectangle(sel_pos.X, sel_pos.Y, 1, 1); dist.Inflate(20, 20); if (x == sel_pos.X && y == sel_pos.Y) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[Building].Texture], pos, new Rectangle(0, 0, 128, 256), Color.CornflowerBlue, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawMapTile = false; } if (dist.Contains(new Point(x, y))) { MapTileColor = Color.CornflowerBlue; } } //Hilight Districts if (_city.districts.Count == 0) { if (_e.Dictionaryof_BuildItems[Building].BuildingType != Listof_BuildTypes.CityCenter) { MapTileColor = Color.LightSalmon; } } foreach (var district in _city.districts) { if (_e.Dictionaryof_BuildItems[Building].BuildingType != Listof_BuildTypes.CityCenter) { if (district.Area.Contains(new Point(x, y))) { if (distrect.Contains(new Point(x, y))) { if (_city.TileMap[x, y].Buildable) { MapTileColor = Color.LightGreen; } else { MapTileColor = Color.LightSalmon; } } else { MapTileColor = Color.CornflowerBlue; } } } else { if (district.Area.Contains(new Point(x, y))) { MapTileColor = Color.CornflowerBlue; } } } } if (DrawMapTile) { spriteBatch.Draw(TileTexture[(int)_e.Dictionaryof_BuildItems[_city.TileMap[x, y].Type].Texture], pos, new Rectangle(0 + 128 * _city.TileMap[x, y].SpriteIndex, 0, 128, 256), MapTileColor, 0, new Vector2(), Tile_Size, SpriteEffects.None, 1); } } } } //if (x == sel_pos.X && y == sel_pos.Y) //spriteBatch.Draw(TileTexture[(int)Listof_Texture.Grass], pos, null, Color.White, 0f, new Vector2(), Tile_Size, SpriteEffects.None, 1); DrawInterface(sel_pos); spriteBatch.End(); base.Draw(gameTime); }
void BuildInterface(bool onPanel) { Vector2 mPos = new Vector2((Mouse.GetState().Position.X + Screen_Scroll.X) / zoom, (Mouse.GetState().Position.Y + Screen_Scroll.Y) / zoom); Point sel_pos; sel_pos.X = (int)Math.Floor((mPos.Y - 128) / 64 + (mPos.X - 128) / 128); sel_pos.Y = (int)Math.Floor((-mPos.X - 128) / 128 + (mPos.Y - 128) / 64); bool Build = false; bool StartDrag = false; if (MouseMode == Listof_MouseMode.Building) { if (Mouse.GetState().RightButton == ButtonState.Pressed) { MouseRightClicked = true; } //Check for right click cancel if (MouseRightClicked && Mouse.GetState().RightButton == ButtonState.Released) { if (Math.Abs(MouseDragScreenScrollStart.X - Screen_Scroll.X) < 4 && Math.Abs(MouseDragScreenScrollStart.Y - Screen_Scroll.Y) < 4) { MouseRightClicked = false; ClearBuild(); } } if (!onPanel) { if (!MouseLeftClicked && Mouse.GetState().LeftButton == ButtonState.Pressed) { MouseLeftClicked = true; StartDrag = true; } if (MouseLeftClicked && Mouse.GetState().LeftButton == ButtonState.Released) { MouseLeftClicked = false; Build = true; } } else { MouseLeftClicked = false; } Typeof_BuildItems BuildData = _e.Dictionaryof_BuildItems[Building]; //Placed Buildings if (BuildData.BuildingType == Listof_BuildTypes.Structure) { BuildPoint1 = sel_pos; if (Build) { if (Buildable) { for (int y = 0; y < BuildData.Size.Y; y++) { for (int x = 0; x < BuildData.Size.X; x++) { _city.TileMap[x, y].ClearConnections(); _city.TileMap[BuildPoint1.X - x, BuildPoint1.Y - y].Type = Building; _city.TileMap[BuildPoint1.X - x, BuildPoint1.Y - y].SpriteIndex = x + (y * BuildData.Size.X); ConnectTile(new Point(BuildPoint1.X - x, BuildPoint1.Y - y)); } } Currency -= BuildCost; MouseLeftClicked = false; if (Keyboard.GetState().IsKeyUp(Keys.LeftShift) && Keyboard.GetState().IsKeyUp(Keys.RightShift)) { MouseMode = Listof_MouseMode.Default; } ClearBuild(); } } } //City Center if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.CityCenter) { BuildPoint1 = sel_pos; if (Build == true) { if (Buildable) { Rectangle DistrictArea = new Rectangle(sel_pos, new Point(1, 1)); DistrictArea.Inflate(20, 20); _city.districts.Add(new District(DistrictArea)); _city.TileMap[sel_pos.X, sel_pos.Y].Type = Building; Currency -= BuildCost; MouseLeftClicked = false; if (Keyboard.GetState().IsKeyUp(Keys.LeftShift) && Keyboard.GetState().IsKeyUp(Keys.RightShift)) { MouseMode = Listof_MouseMode.Default; } ClearBuild(); } else { ClearBuild(); } } } //Zone if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.Zone) { //Draging if (!Build && !MouseLeftClicked && Mouse.GetState().LeftButton == ButtonState.Released) { BuildPoint1 = sel_pos; BuildPoint2 = sel_pos; } //Assign Build Rectangle if (!onPanel && Mouse.GetState().LeftButton == ButtonState.Pressed) { BuildPoint2 = sel_pos; Point P1, P2; //Set building points X and Y if (BuildPoint1.X <= BuildPoint2.X) { P1.X = BuildPoint1.X; P2.X = BuildPoint2.X; } else { P1.X = BuildPoint2.X; P2.X = BuildPoint1.X; } if (BuildPoint1.Y <= BuildPoint2.Y) { P1.Y = BuildPoint1.Y; P2.Y = BuildPoint2.Y; } else { P1.Y = BuildPoint2.Y; P2.Y = BuildPoint1.Y; } BuildRect = new Rectangle(P1.X, P1.Y, (P2.X - P1.X) + 1, (P2.Y - P1.Y) + 1); } //Build Item if (Build) { if (InsideDistrict(BuildRect) && Buildable) { for (int x = BuildRect.Left; x < BuildRect.Right; x++) { for (int y = BuildRect.Top; y < BuildRect.Bottom; y++) { if (_city.CityArea.Contains(new Point(x, y)) && _city.TileMap[x, y].Buildable) { //tile is the zone being created _city.TileMap[x, y].ClearConnections(); _city.TileMap[x, y].Type = Building; ConnectTile(new Point(x, y)); } } } Currency -= BuildCost; ClearBuild(); } else { ClearBuild(); } } } //Roads if (_e.Dictionaryof_BuildItems[Building].BuildingType == Listof_BuildTypes.Road) { if (!Build && !MouseLeftClicked && Mouse.GetState().LeftButton == ButtonState.Released) { BuildPoint1 = sel_pos; BuildPoint2 = sel_pos; } if (!onPanel && Mouse.GetState().LeftButton == ButtonState.Pressed) { BuildPoint2 = sel_pos; } //Build roads if (Build == true) { if (Buildable) { Point P1, P2, P3; //Set building points X and Y if (BuildPoint1.X <= BuildPoint2.X) { P1.X = BuildPoint1.X; P2.X = BuildPoint2.X; } else { P1.X = BuildPoint2.X; P2.X = BuildPoint1.X; } if (BuildPoint1.Y <= BuildPoint2.Y) { P1.Y = BuildPoint1.Y; P2.Y = BuildPoint2.Y; } else { P1.Y = BuildPoint2.Y; P2.Y = BuildPoint1.Y; } int ShortX = Math.Abs(BuildPoint1.X - BuildPoint2.X), ShortY = Math.Abs(BuildPoint1.Y - BuildPoint2.Y), Temp; if (Keyboard.GetState().IsKeyUp(Keys.LeftControl)) { Temp = ShortX; ShortX = ShortY; ShortY = Temp; } if (ShortY >= ShortX) { P3.X = BuildPoint2.X; P3.Y = BuildPoint1.Y; } else { P3.X = BuildPoint1.X; P3.Y = BuildPoint2.Y; } for (int x = P1.X; x <= P2.X; x++) { BuildRoadTile(new Point(x, P3.Y)); } for (int y = P1.Y; y <= P2.Y; y++) { BuildRoadTile(new Point(P3.X, y)); } //seccond pass to connect all the roads to the tiles for (int x = P1.X; x <= P2.X - 1; x++) { ReconnectTile(new Point(x, P3.Y)); } for (int y = P1.Y; y <= P2.Y; y++) { ReconnectTile(new Point(P3.X, y)); } //Set Road Sprites for (int x = P1.X; x <= P2.X - 1; x++) { SetRoadTileSprite(new Point(x, P3.Y)); } for (int y = P1.Y; y <= P2.Y; y++) { SetRoadTileSprite(new Point(P3.X, y)); } Currency -= BuildCost; ClearBuild(); } else { ClearBuild(); } } } if (MouseMode == Listof_MouseMode.Building) { CheckBuildable(_e.Dictionaryof_BuildItems[Building].BuildingType); } } }