Destructible.h 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /******************************************************************************
  2. Use 'Destructible' class for destructible objects, which can be destroyed into pieces.
  3. /******************************************************************************/
  4. namespace Game{
  5. /******************************************************************************/
  6. STRUCT(Destructible , Obj) // Game Destructible Object
  7. //{
  8. enum MODE : Byte // each destructible object can be in different mode
  9. {
  10. STATIC , // this object is the whole DestructMesh, it uses 1 Mesh and 1 static Actor , this object works as typical Static object, it cannot be broken into pieces until it's manually converted to different mode, it uses 1 Mesh and 1 Actor so the cost of displaying and physics processing is low
  11. BREAKABLE, // this object is the whole DestructMesh, it uses 1 Mesh and many sleeping Actor's, this object can be broken into pieces automatically, once movement is detected on at least one of the actors, the object is automatically converted into many objects of 'PIECE' mode. This mode uses 1 Mesh and many Actors, so the cost of displaying is low, but physics processing is higher
  12. PIECE , // this object is only 1 piece of the whole DestructMesh, it uses 1 Mesh and 1 Actor , but since there are usually many pieces, there are many meshes and actors in total, so the cost of displaying and physics processing is highest
  13. };
  14. struct Joint
  15. {
  16. Reference<Destructible> destr;
  17. EE::Joint joint;
  18. Bool save(File &f)C;
  19. Bool load(File &f) ;
  20. };
  21. MODE mode ; // destructible object mode
  22. Int piece_index ; // index of piece in 'destruct_mesh', this is valid only in 'PIECE' mode, in other modes this is equal to -1
  23. Flt scale ; // scale
  24. ObjectPtr base ; // base object
  25. MeshPtr mesh ; // mesh
  26. Int mesh_variation; // mesh variation index
  27. DestructMesh *destruct_mesh ; // pointer to pre-generated destructible object which will be used to spawn debris when destroyed
  28. Memc<Actor> actors ; // actors
  29. Memc<Joint> joints ; // physical joints between actors
  30. // manage
  31. virtual void create(Object &obj); // create from object
  32. virtual void setUnsavedParams(); // set parameters which are not included in the save file
  33. // get / set
  34. virtual Vec pos ( ); // get position
  35. virtual void pos (C Vec &pos ); // set position
  36. virtual Matrix matrix ( ); // get matrix , returned matrix is normalized
  37. virtual Matrix matrixScaled( ); // get matrix , returned matrix is scaled by 'scale'
  38. virtual void matrix (C Matrix &matrix); // set matrix , 'matrix' must be normalized
  39. // operations
  40. virtual void toStatic (); // convert BREAKABLE into STATIC
  41. virtual void toBreakable(); // convert STATIC into BREAKABLE
  42. virtual void toPieces (); // convert STATIC or BREAKABLE into many PIECE objects
  43. // callbacks
  44. virtual void memoryAddressChanged(); // called when object memory address has been changed, you should override it and adjust Actor::obj pointer for all actors
  45. 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
  46. // update
  47. virtual Bool update(); // update, return false when object wants to be deleted, and true if wants to exist
  48. // draw
  49. virtual UInt drawPrepare(); // 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)
  50. virtual void drawShadow (); // in this method you should add the meshes to the shadow draw list (Mesh::drawShadow)
  51. // enable / disable
  52. 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
  53. 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
  54. // io
  55. virtual Bool canBeSaved( ); // 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
  56. virtual Bool save (File &f); // save, false on fail
  57. virtual Bool load (File &f); // load, false on fail
  58. ~Destructible();
  59. Destructible();
  60. protected:
  61. void setPieces (Bool create_joints);
  62. void setStatic (C Matrix &matrix, Byte actor_group);
  63. void setBreakable(C Matrix &matrix, Byte actor_group);
  64. };
  65. /******************************************************************************/
  66. } // namespace
  67. /******************************************************************************/