Designing a Sprite Manager


You now have a basic understanding of what is required of a sprite manager, so you can now move on to the specific design for it. You might think that the sprite manager would be created as a class similarly to the way that you created the Sprite class in the previous hour . However, the sprite manager is closely linked with the game engine, which makes it more beneficial to integrate the sprite manager directly with the game engine. So, the sprite manager will actually be created as a set of methods in the GameEngine class.

Even though the sprite manager is created as a modification on the game engine, it does require some changes outside of the GameEngine class. More specifically , some changes are required in the Sprite class in order for sprites to work smoothly with the sprite manager. The first of these changes involves supporting sprite actions , which are used to inform the sprite manager that it should take action in regard to a particular sprite. Sprite actions are sort of like bounds actions, except that they are somewhat more flexible. As an example, the first sprite action supported is Kill, which is used to inform the sprite manager that a sprite is to be destroyed . The Kill sprite action is similar to the Die bounds action, except that Kill can be issued for a variety of different reasons. Sprite actions are typically invoked when a collision occurs, which allows a missile to destroy a tank upon impact, for example.

Beyond sprite actions, another major requirement of the Sprite class and the sprite manager code is that of collision detection. You learned in Hour 9, "A Crash Course in Game Animation," that collision detection involves checking to see if two sprites have collided with each other. You also found out that a technique known as shrunken rectangle collision detection involves using a rectangle smaller than the sprite as the basis for detecting collisions. Because this form of collision detection requires its own rectangle, it only makes sense to add a collision rectangle as a member of the Sprite class, along with supporting methods to calculate the rectangle and test for a collision with another sprite.

That covers the changes required of the Sprite class in order to support the enhanced sprite animation features offered by the sprite manager. The sprite manager itself is integrated directly into the game engine, where it primarily involves adding a member variable to keep track of a list of sprites. This member variable could be an array with a fixed size representing the maximum number of sprites allowed, or it could be a more advanced data structure such as a vector that can grow dynamically to hold additional sprites.

Regardless of the specifics of how the sprite list is established, the sprite manager must provide several methods that can be used to interact with the sprites being managed.

Following are the major tasks the sprite manager needs to make available using the following methods:

  • Add a new sprite to the sprite list

  • Draw all the sprites in the sprite list

  • Update all the sprites in the sprite list

  • Clean up all the sprites in the sprite list

  • Test to see if a point lies within a sprite in the sprite list

In addition to these tasks that must be capable of being invoked on the game engine, it is important to provide a function for a game that is called whenever a sprite collision occurs. When you think about it, handling a sprite collision is a very game-specific task, so it makes sense to let game code handle it, as opposed to including it in the game engine. So, a sprite collision notification function must be provided by any game that uses the sprite manager so that it can respond to sprite collisions. Of course, the sprite manager must make sure that this function gets called whenever a collision actually takes place.



Sams Teach Yourself Game Programming in 24 Hours
Sams Teach Yourself Game Programming in 24 Hours
ISBN: 067232461X
EAN: 2147483647
Year: 2002
Pages: 271

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net