Time for action - enemy update and draw

  1. Add the Update() method to the Enemy class:
    public void Update(GameTime gameTime)
    {
    if (IsActive())
    {
    Vector2 heading = currentWaypoint - EnemySprite.Location;
    if (heading != Vector2.Zero)
    {
    heading.Normalize();
    }
    heading *= speed;
    EnemySprite.Velocity = heading;
    previousLocation = EnemySprite.Location;
    EnemySprite.Update(gameTime);
    EnemySprite.Rotation =
    (float)Math.Atan2(
    EnemySprite.Location.Y - previousLocation.Y,
    EnemySprite.Location.X - previousLocation.X);
    if (WaypointReached())
    {
    if (waypoints.Count > 0)
    {
    currentWaypoint = waypoints.Dequeue();
    }
    }
    }
    }
    
  2. Add the Draw() method to the Enemy class:
    public void Draw(SpriteBatch spriteBatch)
    {
    if (IsActive())
    {
    EnemySprite.Draw(spriteBatch);
    }
    }
    

What just happened?

Updating an Enemy begins by checking to see if the enemy is still active. If it is, a heading is calculated by subtracting the enemy's current location from the current waypoint. If the resulting vector is not equal to Vector2.Zero, the vector is normalized. We need to make this check because Vector2.Zero cannot be normalized, which will result in an error in our code. Recall that normalizing a vector divides the vector by its length, resulting in a vector with a length of one. Vector2.Zero has a length of zero, and dividing anything by zero produces an invalid result.

Once we have a heading, it is multiplied by the ship's speed, and stored as the sprite's velocity vector. The current location of the sprite is captured, and the sprite's Update() method is called that will move the sprite based on its velocity.

By using the location we stored before the Update() call, we can use the Math.Atan2() method to determine the angle between the current location and the previous location, resulting in an angle of rotation that can be applied to the Rotation property of the sprite in order to automatically rotate it to face the direction in which it is moving. Using this method, a sprite moving only to the right (in the positive X direction) would result in a rotation value of zero, which is why the enemy sprite on the sprite sheet is drawn facing to the right.

Finally, the WaypointReached() method is checked to see if the just-executed movement has brought the enemy to its current waypoint. If it has, and there are additional waypoints for the sprite to visit, the next waypoint is extracted from the queue.

The Draw() method should be very familiar by now, as it is the same implementation that all of our manager classes have been using. Rest assured, we will be seeing it again.

The EnemyManager class

The EnemyManager class is responsible for keeping track of all active enemy ships, spawning new ships and waves of ships, and determining when ships should fire at the player. Additionally, it will be responsible for removing ships that are destroyed or leave the play area by completing their waypoint movements.

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

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