Beispiel #1
0
        public void Update(GameTime gameTime, List <Enemy> enemies)
        {
            mouseState = Mouse.GetState();

            cellX = (int)(mouseState.X / 50);                                                                // Convert the position of the mouse
            cellY = (int)(mouseState.Y / 50);                                                                // from array space to level space

            tileX = cellX * 50;                                                                              // Convert from array space to level space
            tileY = cellY * 50;                                                                              // Convert from array space to level space

            if (mouseState.LeftButton == ButtonState.Released && oldState.LeftButton == ButtonState.Pressed) // if click
            {
                if (IsCellClear())                                                                           //make tower
                {
                    ArrowTower tower = new ArrowTower(towerTexture, bulletTexture, new Vector2(tileX, tileY));
                    towers.Add(tower);
                }
            }
            foreach (Towers tower in towers)// mark target
            {
                if (tower.Target == null)
                {
                    tower.GetClosestEnemy(enemies);
                }

                tower.Update(gameTime);
            }

            oldState = mouseState; // Set the oldState so it becomes the state of the previous frame.
        }
        /// <summary>
        /// Adds a tower to the player's collection.
        /// </summary>
        public void AddTower()
        {
            Towers towerToAdd = null;

            switch (newTowerType)
            {
            case "Arrow Tower":
            {
                towerToAdd = new ArrowTower(towerTextures[0],
                                            bulletTexture, new Vector2(tileX, tileY));
                break;
            }

            case "Slow Tower":
            {
                towerToAdd = new SlowTower(towerTextures[1],
                                           bulletTexture, new Vector2(tileX, tileY));
                break;
            }

            case "Snipe Tower":
            {
                towerToAdd = new SnipeTower(towerTextures[2],
                                            bulletTexture, new Vector2(tileX, tileY));
                break;
            }
            }

            // Only add the tower if there is a space and if the player can afford it.
            if (IsCellClear() == true && towerToAdd.Cost <= money)
            {
                towers.Add(towerToAdd);
                money -= towerToAdd.Cost;

                // Reset the newTowerType field.
                newTowerType = string.Empty;
            }
            else
            {
                newTowerType = string.Empty;
            }
        }