Adding One Last Sprite Feature to the Game Engine


The missing feature in the game engine critical to the Space Out game is the capability for a sprite to automatically create another sprite. This might seem like a strange requirement, but think about an alien that is firing a missile at the car in the Space Out game. The missile must be created so that it appears to come from the alien, which means that the missile must know about the alien's position. Not only that, but the missile must be unique to the alien that fired it because each alien fires a different kind of missile. This presents a significant challenge to the current design of the game engine because there isn't a good way to automatically create a new sprite based on the properties of another sprite.

A good solution to this problem involves allowing a sprite to create another sprite whenever it needs to. For example, you can allow the alien sprites to create missile sprites themselves , which makes it very easy to position the missile based on the alien's position ”not to mention creating the appropriate missile for each different type of alien. The problem with this approach is that it is impossible to add this functionality to the Sprite class in a generic fashion. In other words, the specifics regarding what kind of sprite needs to be created are unique to each game, and therefore can't be carried out in the Sprite class. However, the Sprite class can establish the interface that makes it possible.

An important part of this new "add sprite" feature is a new sprite action called SA_ADDSPRITE . The following code shows how the SA_ADDSPRITE sprite action has been added to the existing sprite actions:

 typedef WORD        SPRITEACTION; const SPRITEACTION  SA_NONE      = 0x0000L,                     SA_KILL      = 0x0001L,                     SA_ADDSPRITE = 0x0002L; 

If you recall, sprite actions are used to signal to the game engine that some particular action must be taken in regard to a sprite. The SA_ADDSPRITE sprite action simply results in a special method being called on the Sprite object to which the action applies. This method is called AddSprite() , and looks like this:

 virtual Sprite* AddSprite(); 

The idea behind the AddSprite() method is that it gets called to allow a sprite to add a new sprite to the game engine. The specifics of the new sprite are entirely dependent on each individual game. In fact, the version of the AddSprite() method in the Sprite class doesn't do anything, as the following code reveals:

 Sprite* Sprite::AddSprite() {   return NULL; } 

This code shows how the base Sprite::AddSprite() method doesn't do anything other than return a NULL sprite pointer, which reveals that the task of adding a sprite via the AddSprite() method is left up to derived sprite classes. So, in order to take advantage of this method, you must derive a sprite class for a particular kind of sprite, and then override the AddSprite() method with a version that actually creates a sprite.

The SA_ADDSPRITE action and AddSprite() method enter the picture in the game engine in the GameEngine::UpdateSprites() method, which must check for the presence of the SA_ADDSPRITE sprite action, and then call the AddSprite() method on the sprite if the action is set. The return value of the sprite's AddSprite() method is passed along to the game engine's AddSprite() function, which handles inserting the sprite into the sprite list. This is all that is required of the game engine to support adding a sprite from within another sprite.



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