in this tutorial i'll show you my way to do object managament.
this is a theory only tutorial because the real implementation varies in every game i build.
in some i use object type arrays to eliminate useless object-object collision detection checks. but this takes an extra ammount of code and sometimes you just don`t need it.
in other games i use more than one object containers to eliminate unnecessary checks (eg: eyecandy objects that don`t collide with other game objects).
this is what i want to cover in this tutorial:
-object creation
-object-object collisiondetection (basic bounding box checking)
-object types
-a simple way to store object information in your map.
evey object is based on a object base class which contains data each object needs.
in my latest game the base class looks like this:
class GameObject{ public: //functions that can be overridden by child objects virtual State think() {return REMOVE;}; //called once each frame to let the object "think" (State can be REMOVE if the object needs to be removed from the object container) virtual void draw( {;}; virtual void collidewithmap(ECol col) {;}; virtual void pain(GameObject *inflictor, int dmg) {;}; //called if the object gets damage virtual void collide(GameObject *other) {;}; //called if the object collides with another object GameObject(float nx, float ny, int nw, int nh, float nvelx, float nvely, EObjectType ntype){ //must be called by all child objects to set all properties x = nx; y = ny; ox = x; oy = y; w = nw; h = nh; velx = nvelx; vely = nvely; type = ntype; }; float getX(){return x;}; float getY(){return y;}; float getH(){return h;}; float getW(){return w;}; private: EObjectType type; //type of the object (->child class) float ox, oy; //old position - filled by coldec_obj2map -> ONLY USED INTERNALLY FOR O-M coldec float x, y; //position int h, w; //size float velx, vely; //speed };
all game objects are childs of this class. eg:
GameObject Player ItemPickup Enemy Bullet Grenade Explosion EyeCandy
all game objects are stored in an object manager.
it's purposes are
- storing all objects in a single place
- call think() of each object once per frame
- delete "dead" objects (State: remove)
- perform object-map collision detection with each object
- perform object-object collision detection
- draw each object
class ObjectList{ public: void update(); //run game object logic (think()) void draw(); //draw game objects (->sprites) bool add(GameObject *o); //add a new game object to this list private: GameObject *objlist[MAX_OBJ]; //contains the game objects int objlist_last; //the first free slot in objlist void register_collisionhandler(EObjectType handles, EObjectType t2); void remove(int i); //remove object in slot i (priate!) };
i think this tutorial is far too short and showing just some code snippets just causes questions. please write me what you want to know about object management and i'll add it here.
i hope to be able to post an example implementation somewhen.
questions / critics / comments? forum