Time for action – waypoint management

  1. Add the AddWaypoint() method to the Enemy class:
    public void AddWaypoint(Vector2 waypoint)
    {
        waypoints.Enqueue(waypoint);
    }
  2. Add the WaypointReached() method to the Enemy class:
    public bool WaypointReached()
    {
        if (Vector2.Distance(EnemySprite.Location, currentWaypoint) < 
            (float)EnemySprite.Source.Width/2)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
  3. Add the IsActive() method to the Enemy class:
    public bool IsActive()
    {
        if (Destroyed)
        {
            return false;
        }
    
        if (waypoints.Count > 0)
        {
            return true;
        }
    
        if (WaypointReached())
        {
            return false;
        }
    
        return true;
    }

What just happened?

When a new waypoint is added to the enemy's route, it is enqueued to the waypoints queue. When WaypointReached() is called, the function checks to see ...

Get XNA 4.0 Game Development by Example Beginner's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.