Creating, Deleting, and Advance Topics

Creating an Object

CBombObject* bomb = (CBombObject*)g_ObjectManager.CreateObject(typBomb);	

All Objects are created through the Object Manager. Now you will also have to edit CObjectManager.cpp to include your new object type. This is INCREDIBLY easy. Look at the CreateObject method and copy and paste an existing object. Its very easy

Back to the above example. You declare a pointer to the object you want to create and get it by type casting the return value of CreateObject which you pass the enumerated type of the object too. Sounds kinda confusing but its very easy. Just look at the above example. By doing it this way, the default parameter-less constructor is called. I would recommend writing an Init function which you should call right after creating an instance of the object. Every current object works this way.

Deleting Object

g_ObjectManager.RemoveObjectSafe(this->ID);
Thats it. Call this anywhere in the class and the object will be safely removed from the object list at the end of the current frame. Objects should really only delete themselves. IE - its not the explosions job to delete the player. The player should delete itself when it hits an explosion. If for some reason you must delete a different object, pas RemoveObjectSafe the ID of the object.

Communication between objects

Ok, finally we are getting into a little more complicated stuff. Most objects will just have 1-time communication. IE - when they collide they get pointers to each other and may call a few functions, etc, and thats it. However, some objects must maintain a constant relationship between each other. How do we do this?

Dont Keep Pointers. Mainting a pointer between the two objects may seem like a good idea. However, the way everything else is setup, when 1 object gets deleted, the object pointing to it has no way to know. You cant even test it against NULL. You are now pointing to some random piece of memory which use to contain an object and now contains garbage.

The way I handles this was to keep an integer ID reference of the object. For example, the bomb object class must know who its "owner" is so when it blows up it can notify it (yes, we could have the player object keep track of his outstanding bombs and keep his own count on which ones blow up - but thats too much work and overhead). instead of keeping an pointer to the owner, I just keep an int that contains his ID number. Then when I need to get that object, I call:

CPlayerObject* owner = (CPlayerObject*)g_ObjectManager.GetObjectID(m_OwnerID);
if(owner)
{
	owner->modifyCurrentBombs(-1);
}

See, not so bad? call GetObjectID with the ID number (this ID number gets assigned by the object manager on creation. You never touch it). It will return a pointer and you can test that pointer against NULL to see if it exists.

Animation

Disclaimer: I havent fully tested the animation system and it may change quite a bit. So be warned

You can use the my CAnimation class to easily implement Animation into your object. Heres how to use it:

//Call once to declare/init

CAnimation animation;
animation.Init("animationfile.txt X");

//To draw:
CImage* currentImg = animation.getCurrentImage();

//To update (call every frame or whenever you need the object to be "animated"
animation.Update();

//To Reset:
animation.Reset();

//Get X/Y displacemanet:
int dx = animation.getCurrentDisplacement().x;
int dy = animation.getCurrentDisplacement().y;

First, the "animationfile.txt X" string which you should initilize the animatino with: animationfile.txt is explained below. The X should be either 0 1 or 2. 0=no repeat. 1=repeat. 2=repeat but once you get to the end, play the animation backwards, then forward, then backwards again, etc. Thats it. now you'll need to manually write the animation file. Heres an example of walkFront.txt:

./objects/bomberman/art/walkFront0.tga 1 0 4
./objects/bomberman/art/walkFront1.tga 1 0 4
./objects/bomberman/art/walkFront2.tga 1 0 4
./objects/bomberman/art/walkfront3.tga 1 0 4
./objects/bomberman/art/walkFront4.tga 1 0 4
./objects/bomberman/art/walkFront5.tga 1 0 4

Each line represents one frame. The 1st number is the # of frames to stay on this frame. The 2nd number is the number of pixels to displace the object on the X axis when this frame is drawn. The 3rd number is the Y displacement.

You may want to leave the X/Y displacement alone and manually move the object every frame. It is just there as an option incase you want to make realistic walking animation or something.

Note: when you call animation.Update() is will return Update (enumrated type) if it is changing frames. This may be helpful if you use the built in X/Y displacement.


Part IV: Conclusion