/// <summary> /// Calls Update on the Base and all the Minions. /// Calculates collisions. /// Sets minion path waypoints. /// Removes inactive minions. /// Spawns new minion waves. /// </summary> /// <param name="gameTime">Game timing state.</param> /// <param name="enemyMinions">A list of minions to check collisions with.</param> public void Update(GameTime gameTime, List<Minion> enemyMinions, Base enemyBase) { //Update the base Base.Update(gameTime); //Update minions for (int i = Minions.Count - 1; i >= 0; i--) { Minions[i].Update(gameTime); //Perform collision detection with enemy minions bool inRange = Minions[i].CheckCollision(enemyMinions); //Reset the minion's goal if no enemies are in range, or if it's traveling backwards //Minions can only travel towards the enemy's base if (inRange == false || Math.Sign(Minions[i].Direction.X) != Math.Sign(Destination.X - Minions[i].Center.X)) { //No enemies are in range //Check how far the minion has traveled from their base if (Math.Abs(Base.Center.X - Minions[i].Center.X) < (ScreenWidth / 2 - Base.Size / 2)) { //Still head towards waypoint Minions[i].SetGoal(Minions[i].Waypoint); } else { //Head towards the final destination now (the enemy base) Minions[i].SetGoal(Minions[i].Destination); } } //Check collision with enemy base float distance = (float)Math.Sqrt( Math.Pow(Minions[i].Center.X - enemyBase.Center.X, 2) + Math.Pow(Minions[i].Center.Y - enemyBase.Center.Y, 2) ); if (distance < (enemyBase.Size / 2)) { Minions[i].Active = false; enemyBase.Health -= Minions[i].Health; } //Add each minion's money earned to the player's money if (Minions[i].MoneyEarned > 0) { Money += Minions[i].MoneyEarned; Minions[i].MoneyEarned = 0; } //Deactivate minions off the screen if (Minions[i].Position.X > ScreenWidth || Minions[i].Position.X < -Minions[i].Size || Minions[i].Position.Y > ScreenHeight || Minions[i].Position.Y < -Minions[i].Size) { Minions[i].Active = false; } //Remove inactive minions if (Minions[i].Active == false) { Minions.RemoveAt(i); } } //Check if respawn counter is up if (gameTime.TotalGameTime - PreviousSpawnTime > MinionSpawnTime) { PreviousSpawnTime = gameTime.TotalGameTime; SpawnWaves = 5; } //If respawn queue is not empty, spawn a wave if (SpawnWaves > 0) { SpawnMinionWave(); SpawnWaves--; } }
/// <summary> /// Initializes player properties and sets the boundary on the player's game entities. /// </summary> /// <param name="minionTexture">The texture used for drawing minions.</param> /// <param name="teamColor">The player's team color.</param> /// <param name="screenWidth">Width of the playable game area.</param> /// <param name="screenHeight">Height of the playable game area.</param> public void Initialize(Texture2D minionTexture, Color teamColor, int screenWidth, int screenHeight) { Money = 100; MinionHealth = 5; MinionSpeed = 0.5f; MinionTexture = minionTexture; TeamColor = teamColor; MinionSpawnTime = TimeSpan.FromSeconds(8f); PreviousSpawnTime = TimeSpan.Zero; SpawnWaves = 0; ScreenWidth = screenWidth; ScreenHeight = screenHeight; Base = new Base(); Minions = new List<Minion>(); Waypoints = new List<Vector2>(); }