Time for action - creating the EnemyManager class

  1. Add a new class to the AsteroidBeltAssault project called EnemyManager.
  2. Add the standard using directives to the top of the file:
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
    
  3. Add declarations to the EnemyManager class:
    private Texture2D texture;
    private Rectangle initialFrame;
    private int frameCount;
    public List<Enemy> Enemies = new List<Enemy>();
    public ShotManager EnemyShotManager;
    private PlayerManager playerManager;
    public int MinShipsPerWave = 5;
    public int MaxShipsPerWave = 8;
    private float nextWaveTimer = 0.0f;
    private float nextWaveMinTimer = 8.0f;
    private float shipSpawnTimer = 0.0f;
    private float shipSpawnWaitTime = 0.5f;
    private float shipShotChance = 0.2f;
    private List<List<Vector2>> pathWaypoints =
    new List<List<Vector2>>();
    private Dictionary<int, int> waveSpawns = new Dictionary<int, int>(); public bool Active = true;
    private Random rand = new Random();
    

What just happened?

As with our other manager classes, the EnemyManager will cache information about the sprite used to create enemies in the texture, initialFrame, and frameCount members in order to supply them to the constructor for the Sprite class when an enemy needs to be generated. Enemies will be stored in the Enemies list object that will be updated and drawn in a manner similar to the asteroids and shots.

Because the EnemyManager will be responsible for determining when enemy ships should fire at the player, it needs a reference to the PlayerManager (to determine the player's current position) and contains an instance of ShotManager (in order to use the FireShot() method).

Waves of enemy ships will be spawned by the EnemyManager based on a timer. Each wave will consist of at least MinShipsPerWave, but no more than MaxShipsPerWave. These values are declared as public so that they can be modified at run time in order to increase the difficulty of the game as play continues.

Our standard timing mechanism is in place to determine when each wave should be spawned (nextWaveTimer and nextWaveMinTimer) and a second timer determines the spawn time between each ship in a wave. In this case, one half of a second will elapse between the spawning of each ship in a wave.

During any given frame, each active ship has a 0.2% chance of firing a shot at the player. This number may seem very low, but keep in mind that the check will be made 60 times per second for each active ship.

The pathWaypoints object is a list of lists of Vector2 objects. This is not really as complicated as it may sound. Remember that a waypoint is represented by a vector pointing to a location on the screen. The path that any enemy ship travels is determined by a list of waypoints or a path across the screen that the ship will take.

Since we do not want every ship to follow exactly the same path across the screen, we will define several different paths. Each path has its own list of waypoints. In order to facilitate easy random generation and expansion of the path system, each of the paths is rolled up into a list of paths. When we request an item from the pathWaypoints list, the result is a list of vectors. We can then break that list apart for each of the individual waypoints.

When the time comes to actually spawn ships, we will check the waveSpawns dictionary. The keys in the dictionary (the first integer) will correspond to the path number, while the value (the second integer) determines the number of ships waiting to be spawned on that path.

The Active member will be checked during the class' update tasks to determine if new ships should be spawned. External code can set this Boolean value to false to prevent new ships (or waves of ships) from being spawned.

Managing waypoints

In order to create the pathWaypoints, we will use a helper function that generates individual path lists and adds them to the aggregated list object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.198.173