Example #1
0
 public static void StartGame(int mapIndex, List <Tower> towerList)
 {
     Hud.Load(towerList);
     MapPlaying = mapIndex - 1;
     State      = GameState.InGame;
 }
Example #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            KeyState   = Keyboard.GetState();
            mouseState = Mouse.GetState();
            if (State == GameState.Menu)
            {
                Menu.Update(towerList);
            }
            else if (State == GameState.InGame)
            {
                if (Lost)
                {
                    if (KeyState.GetPressedKeys().Length != 0 && KeyState.GetPressedKeys().First() == Keys.Enter)
                    {
                        Hud.Reset();
                        EntitySpawner.Reset();
                        Menu.Reset();
                        Reset();
                        State = GameState.Menu;
                        Lost  = false;
                    }
                }
                else
                {
                    bool Collision = false;
                    if (Hud.TowerSelected != null)
                    {
                        foreach (Tower PlacedTower in towers)
                        {
                            if (CheckIfInRange(PlacedTower.Center, PlacedTower.Radius, Hud.TowerSelected.Center, Hud.TowerSelected.Radius))
                            {
                                Collision = true;
                            }
                        }
                        if (Collision)
                        {
                            Hud.TowerSelected.Color = Color.Gray;
                        }
                        else
                        {
                            Hud.TowerSelected.Color = Color.White;
                        }

                        //Place tower
                        if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton != ButtonState.Pressed && Collision == false)
                        {
                            Hud.Money -= Hud.TowerSelected.Cost;
                            if (Hud.TowerSelected is SpellTower)
                            {
                                towers.Add(new SpellTower(new Rectangle(Hud.TowerSelected.Drawbox.X, Hud.TowerSelected.Drawbox.Y, Hud.TowerSelected.Drawbox.Width, Hud.TowerSelected.Drawbox.Height), Hud.TowerSelected.Texture, Hud.TowerSelected.Radius, Hud.TowerSelected.Cost, Hud.TowerSelected.Name, (Hud.TowerSelected as SpellTower).SpellKey, (Hud.TowerSelected as SpellTower).SpellCooldown, (Hud.TowerSelected as SpellTower).SpellRadius));
                            }
                            else if (Hud.TowerSelected is RoadTower)
                            {
                                towers.Add(new RoadTower(new Rectangle(Hud.TowerSelected.Drawbox.X, Hud.TowerSelected.Drawbox.Y, Hud.TowerSelected.Drawbox.Width, Hud.TowerSelected.Drawbox.Height), Hud.TowerSelected.Texture, Hud.TowerSelected.Radius, Hud.TowerSelected.Cost, Hud.TowerSelected.Name, (Hud.TowerSelected as RoadTower).ProjectileTexture, (Hud.TowerSelected as RoadTower).AttackRange, (Hud.TowerSelected as RoadTower).AttackSpeed, (Hud.TowerSelected as RoadTower).AttackDMG, (Hud.TowerSelected as RoadTower).Ptype.ToString(), (Hud.TowerSelected as RoadTower).ProjectileEffect, (Hud.TowerSelected as RoadTower).Hp));
                            }
                            else if (Hud.TowerSelected is ClassicTower)
                            {
                                towers.Add(new ClassicTower(new Rectangle(Hud.TowerSelected.Drawbox.X, Hud.TowerSelected.Drawbox.Y, Hud.TowerSelected.Drawbox.Width, Hud.TowerSelected.Drawbox.Height), Hud.TowerSelected.Texture, Hud.TowerSelected.Radius, Hud.TowerSelected.Cost, Hud.TowerSelected.Name, (Hud.TowerSelected as ClassicTower).ProjectileTexture, (Hud.TowerSelected as ClassicTower).AttackRange, (Hud.TowerSelected as ClassicTower).AttackSpeed, (Hud.TowerSelected as ClassicTower).AttackDMG, (Hud.TowerSelected as ClassicTower).Ptype.ToString(), (Hud.TowerSelected as ClassicTower).ProjectileEffect));
                            }
                            Hud.TowerSelected = null;
                        }
                    }
                    Hud.Update(towers);

                    //Start wave
                    if (mouseState.LeftButton == ButtonState.Pressed && prevMouseState.LeftButton != ButtonState.Pressed && Hud.NextWaveBox.Contains(mouseState.Position) || KeyState.IsKeyDown(Options.startWave) && prevKeyState.IsKeyUp(Options.startWave) && waveOngoing == false)
                    {
                        waveOngoing = true;
                        spawning    = true;
                        EntitySpawner.NextWave(enemyList, mapList[MapPlaying].PathQueue);
                    }

                    for (int i = 0; i < enemies.Count; i++)
                    {
                        enemies[i].Update(towers, gameTime);
                        if (enemies[i].IsDead && enemies[i].ExploTime <= gameTime.TotalGameTime)
                        {
                            enemies.RemoveAt(i);
                            i--;
                        }
                    }
                    for (int i = 0; i < towers.Count; i++)
                    {
                        towers[i].Update(enemies, gameTime);
                    }
                    if (waveOngoing)
                    {
                        if (spawning == true)
                        {
                            if (!EntitySpawner.EnemiesToSpawn.IsEmpty() && spawnDelay <= 0)
                            {
                                EntitySpawner.SpawnNextEnemy(enemies);
                                spawnDelay = rng.Next(0, 20);
                            }
                            else if (EntitySpawner.EnemiesToSpawn.IsEmpty())
                            {
                                spawning = false;
                            }
                        }
                        if (enemies.Count == 0 && spawning == false)
                        {
                            waveOngoing = false;
                        }
                    }
                    if (!waveOngoing && Hud.Hp <= 0)
                    {
                        Lost = true;
                    }
                    spawnDelay--;
                }
            }
            else if (State == GameState.Quit)
            {
                Exit();
            }
            prevKeyState   = Keyboard.GetState();
            prevMouseState = Mouse.GetState();
            base.Update(gameTime);
        }