Coding the SpriteManager

While your SpriteManager class is wired up and functional, it doesn't do anything yet. You can draw in your SpriteManager's Draw method, just as you can in your Game1 class. In fact, to clearly separate the sprite logic from the rest of your game, you'll want the SpriteManager to actually control all drawing of sprites. To accomplish that, you're going to have to add some code that will draw your sprites to that class.

The first thing you'll need is a SpriteBatch. While you already have a SpriteBatch object in your Game1 class, it makes more sense to create your own here for use within this class than to reuse that one. Only that way will you be able to truly isolate and modularize your game component code. Too much passing of data back and forth between the game and the game component will break that model.

In addition to adding a SpriteBatch variable, you need to add a few other variables: a list of Sprite objects that will hold all the automated sprites, and an object of type UserControlledSprite that will represent the player. Add each of these variables at the class level inside your SpriteManager class:

SpriteBatch spriteBatch;
UserControlledSprite player;
List<Sprite> spriteList = new List<Sprite>(  );

Just as the SpriteManager's Update and Draw methods are wired up to be called after your Game1 class's Update and Draw methods are called, the Initialize and LoadContent methods will also be called after the equivalent Game1 methods. You're going to need to add some code to load textures, initialize your SpriteBatch, initialize your player object, and, for testing purposes, add some sprites to your sprite manager's list of sprites. Add an override for LoadContent using the following code to accomplish all of that:

protected override void LoadContent(  )
{
    spriteBatch = new SpriteBatch(Game.GraphicsDevice);

    player = new UserControlledSprite(
        Game.Content.Load<Texture2D>(@"Images/threerings"),
        Vector2.Zero, new Point(75, 75), 10, new Point(0, 0),
        new Point(6, 8), new Vector2(6, 6));

    spriteList.Add(new AutomatedSprite(
        Game.Content.Load<Texture2D>(@"Images/skullball"),
        new Vector2(150, 150), new Point(75, 75), 10, new Point(0, 0),
        new Point(6, 8), Vector2.Zero));
    spriteList.Add(new AutomatedSprite(
        Game.Content.Load<Texture2D>(@"Images/skullball"),
        new Vector2(300, 150), new Point(75, 75), 10, new Point(0, 0),
        new Point(6, 8), Vector2.Zero));
    spriteList.Add(new AutomatedSprite(
        Game.Content.Load<Texture2D>(@"Images/skullball"),
        new Vector2(150, 300), new Point(75, 75), 10, new Point(0, 0),
        new Point(6, 8), Vector2.Zero));
    spriteList.Add(new AutomatedSprite(
        Game.Content.Load<Texture2D>(@"Images/skullball"),
        new Vector2(600, 400), new Point(75, 75), 10, new Point(0, 0),
        new Point(6, 8), Vector2.Zero));

    base.LoadContent(  );
}

What's going on here? First, you initialize your SpriteBatch object; then, you initialize your player object and add four automated sprites to your list of sprites. These four sprites are for testing purposes only, so that once you finish your sprite manager you can see it in action.

Next, you need to call the Update method of the player object and that of all the sprites in the list of sprites every time the Update method in your sprite manager is called. In your sprite manager's Update method, add this code:

public override void Update(GameTime gameTime)
{
    // Update player
    player.Update(gameTime, Game.Window.ClientBounds);

    // Update all sprites
    foreach (Sprite s in spriteList)
    {
        s.Update(gameTime, Game.Window.ClientBounds);
    }

    base.Update(gameTime);
}

Now, you need to do the same thing when drawing. The Sprite base class has a Draw method, so you'll need to call each sprite's Draw method in your SpriteManger's Draw method. Sprites must always be drawn between calls to SpriteBatch.Begin and SpriteBatch.End, so make sure that you add SpriteBatch.Begin and End method calls to surround your calls to draw your sprites:

public override void Draw(GameTime gameTime)
{
    spriteBatch.Begin(SpriteBlendMode.AlphaBlend,
        SpriteSortMode.FrontToBack, SaveStateMode.None);

    // Draw the player
    player.Draw(gameTime, spriteBatch);

    // Draw all sprites
    foreach (Sprite s in spriteList)
        s.Draw(gameTime, spriteBatch);

    spriteBatch.End(  );

    base.Draw(gameTime);
}

There's just one more thing you need to take care of in your SpriteManager class: collision detection. You'll be handling collision detection in your sprite manager rather than in individual sprites or in the game object itself.

For this particular game, you don't care if automated sprites collide with one another—you only need to check your player sprite for collision against all your automated sprites. Modify your Update call to check for player collisions with all AutomatedSprites:

public override void Update(GameTime gameTime)
{
    // Update player
    player.Update(gameTime, Game.Window.ClientBounds);

    // Update all sprites
    foreach (Sprite s in spriteList)
    {
        s.Update(gameTime, Game.Window.ClientBounds);

        // Check for collisions and exit game if there is one
        if (s.collisionRect.Intersects(player.collisionRect))
            Game.Exit(  );
    }

    base.Update(gameTime);
}

Now, each time the Update method of your game is called, Update will also be called in your SpriteManager. The SpriteManager will in turn call Update on all sprites and check them for collisions against the player sprite. Beautiful, isn't it?

Get Learning XNA 3.0 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.