public void AddTower() { Tower nt = null; switch (this.TowerType) { case TowerTypes.ARROW_TOWER: nt = new ArrowTower(this.towertextures[(int)TowerTypes.ARROW_TOWER - 1], this.bullettextures[(int)TowerTypes.ARROW_TOWER - 1], this.Tile); break; case TowerTypes.SPIKE_TOWER: nt = new SpikeTower(this.towertextures[(int)TowerTypes.SPIKE_TOWER - 1], this.bullettextures[(int)TowerTypes.SPIKE_TOWER - 1], this.Tile); break; case TowerTypes.SPEED_TOWER: nt = new SpeedTower(this.towertextures[(int)TowerTypes.SPEED_TOWER - 1], this.bullettextures[(int)TowerTypes.SPEED_TOWER - 1], this.Tile); break; case TowerTypes.CANNON_TOWER: nt = new CannonTower(this.towertextures[(int)TowerTypes.CANNON_TOWER - 1], this.bullettextures[(int)TowerTypes.CANNON_TOWER - 1], this.Tile); break; } if (nt != null && this.CanPlaceTower() && nt.Cost <= this.coins) { this.towers.Add(nt); this.coins -= nt.Cost; this.TowerType = TowerTypes.TOWER_NONE; } this.TowerType = TowerTypes.TOWER_NONE; }
/// Handles placing a tower on the PictureBox. private void GameWorldPB_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) { world.Crosshair.x = e.Location.X; world.Crosshair.y = e.Location.Y; } if (e.Button == MouseButtons.Right) { DeselectTower(); HandSelectPB_Click(sender, e); } else { // No tower to build selected, mouse can be used to select placed Towers if (selectedTower == null && !GetTileAtMouse.buildable) { // Loop through all Towers for (int i = 0; i < world.towers.Count; i++) { // Check if the clicked tile is occupied by a Tower for (int j = 0; j < world.towers[i].pos.Count; j++) { if (world.towers[i].pos[j] == GetTileAtMouse) { // Deselect the previously selected Tower (if there is one) DeselectTower(); // Set the selected Tower, draw its Range and toggle the Details. world.Tower = world.towers[i]; SelectTower(); } } } } else { DeselectTower(); } // Gets 2x2 square of Tiles according to location of mouse. List <BaseTile> selectedTiles = GetSelectedTiles(e.Location); // If the selected tiles are buildable AND you have a tower selected AND you have enough money.. if (world.IsBuildable(selectedTiles) && selectedTower != null && world.Gold + selectedTower.goldCost >= 0) { if (world.CheckIfPathIsBlocked(selectedTiles) == false) { // ..Check the selected tower's type and create a new object of that type Tower addTower = null; if (selectedTower is ArrowTower) { addTower = new ArrowTower(); } if (selectedTower is CannonTower) { addTower = new CannonTower(); } if (selectedTower is SplitShotTower) { addTower = new SplitShotTower(); } if (selectedTower is DogHouseTower) { addTower = new DogHouseTower(); } if (selectedTower is FuzzyTower) { addTower = new FuzzyTower(); } // ..Deduct gold world.DeductGold(selectedTower.goldCost); // Disable each selected tile foreach (BaseTile bt in selectedTiles) { bt.DisableTile(); bt.tower = addTower; } // Build the tower, update the gold and redraw the background addTower.BuildTower(selectedTiles); DeselectTower(); world.Tower = addTower; SelectTower(); DrawBackground(); } } } }