Obj.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /******************************************************************************
  2. Use 'Obj' as a base class for creating your own custom game object classes.
  3. /******************************************************************************/
  4. namespace Game{
  5. /******************************************************************************/
  6. struct Obj // Game Object interface inherited by all Game Object classes (Game.Static, Game.Kinematic, Game.Chr, Game.Item, ..)
  7. {
  8. // manage
  9. virtual void create(Object &obj)=NULL; // create from object
  10. // get / set
  11. Int type (); // get object's OBJ_TYPE
  12. Bool isConst ()C {return _const;} // if object is constant
  13. C UID& id ()C {return _id ;} // get object's id, 'id' is a unique identifier different than 'UIDZero' for valid objects, and 'UIDZero' for invalid objects (invalid object is an object which has been removed/deleted), id's are randomly generated upon object creation (in World Editor or dynamically in the game), they are stored in SaveGame so object's id is restored when loaded
  14. virtual Bool canBeSaved() {return true ;} // if object wants to be saved, you can override this method and return false when object is useless and doesn't need to be stored in a SaveGame. This method is also called for objects in worlds initialized with WORLD_FULL, when objects enter new areas (that were not yet created), the engine will call this method and place the object in that area only if 'canBeSaved' will return true, in other case the object will get deleted, WORLD_FULL doesn't impose any limits to the number of areas created, that's why you can use this method to make sure that objects will not travel to very distant areas.
  15. virtual Obj* reliesOn () {return null ;} // you can override this method and optionally return an object on which the current object relies on, this will make the WorldManager to try to update the returned object before the current one, this can be useful for example in situation when animation of object A is dependent on animation of object B, for example if player is riding a horse, then horse animations must be setup first (horse must be updated before player) so animating player can be done basing on the position of the horse, in this situation the player class should override 'reliesOn' method and return the horse object, so that horse will be updated first
  16. virtual Vec pos ( )=NULL; // get position
  17. virtual void pos (C Vec &pos )=NULL; // set position
  18. virtual Matrix matrix( )=NULL; // get matrix
  19. virtual void matrix(C Matrix &matrix)=NULL; // set matrix, for most classes 'matrix' should be normalized
  20. // callbacks
  21. virtual void memoryAddressChanged() {} // called when object memory address has been changed, you should override it and adjust Actor.obj pointer for all actors
  22. virtual void willBeMovedFromWorldToStorage() {} // called before object was moved from world to custom object container
  23. virtual void wasMovedFromWorldToStorage() {} // called after object was moved from world to custom object container
  24. virtual void willBeMovedFromStorageToWorld() {} // called before object was moved from custom object container to world
  25. virtual void wasMovedFromStorageToWorld() {} // called after object was moved from custom object container to world
  26. virtual void linkReferences() {} // this method is called after loading all game objects, you should override it and call 'link' on every Reference that is referencing external objects
  27. // update
  28. virtual Bool update()=NULL; // update, return false when object wants to be deleted, and true if wants to exist
  29. // draw
  30. virtual UInt drawPrepare ()=NULL; // prepare for drawing, this will be called in RM_PREPARE mode, in this method you should add the meshes to the draw list (Mesh.draw), and return a combination of bits of which additional render modes will be required for custom drawing of the object (for example "return IndexToFlag(RM_BLEND)" requests blend mode rendering)
  31. virtual void drawShadow () {} // in this method you should add the meshes to the shadow draw list (Mesh.drawShadow)
  32. virtual void drawOverlay () {} // in this method you should draw custom objects for the RM_OVERLAY mode, this method will be called only if the 'drawPrepare' has requested RM_OVERLAY mode
  33. virtual void drawBlend () {} // in this method you should draw custom objects for the RM_BLEND mode, this method will be called only if the 'drawPrepare' has requested RM_BLEND mode
  34. virtual void drawPalette () {} // in this method you should draw custom objects for the RM_PALETTE mode, this method will be called only if the 'drawPrepare' has requested RM_PALETTE mode
  35. virtual void drawPalette1() {} // in this method you should draw custom objects for the RM_PALETTE1 mode, this method will be called only if the 'drawPrepare' has requested RM_PALETTE1 mode
  36. virtual void drawSolid () {} // in this method you should draw custom objects for the RM_SOLID mode, this method will be called only if the 'drawPrepare' has requested RM_SOLID mode
  37. virtual void drawAmbient () {} // in this method you should draw custom objects for the RM_AMBIENT mode, this method will be called only if the 'drawPrepare' has requested RM_AMBIENT mode
  38. virtual void drawOutline () {} // in this method you should draw custom objects for the RM_OUTLINE mode, this method will be called only if the 'drawPrepare' has requested RM_OUTLINE mode
  39. virtual void drawBehind () {} // in this method you should draw custom objects for the RM_BEHIND mode, this method will be called only if the 'drawPrepare' has requested RM_BEHIND mode
  40. // enable / disable
  41. virtual void disable(); // disable object when becomes too far away, here all dynamic actors should enable kinematic flag - actor.kinematic(true ), this is called when an object changes its state to AREA_INACTIVE
  42. virtual void enable(); // enable object when closes in , here all dynamic actors should disable kinematic flag - actor.kinematic(false), this is called when an object changes its state to AREA_ACTIVE
  43. // io
  44. virtual Bool save(File &f); // save, false on fail
  45. virtual Bool load(File &f); // load, false on fail
  46. ~Obj();
  47. Obj();
  48. #if EE_PRIVATE
  49. void init (Bool _const, Int type, C UID *id=null) {T._const=_const; T._type=type; if(id)T._id=*id;}
  50. void clearUpdate ();
  51. ObjMap<Obj>* worldObjMap ();
  52. void removeFromArea();
  53. void putToArea(Area &area);
  54. Bool updateArea();
  55. friend struct WorldManager;
  56. #endif
  57. #if !EE_PRIVATE
  58. private:
  59. #endif
  60. Bool _const ;
  61. Byte _update_count;
  62. Int _type ; // index of world container in which object is stored (equal to OBJ_TYPE)
  63. UID _id ; // object unique identifier
  64. Area *_area ; // area in which object is stored
  65. NO_COPY_CONSTRUCTOR(Obj);
  66. };
  67. /******************************************************************************/
  68. } // namespace
  69. /******************************************************************************/