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 ...

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.