Character.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /******************************************************************************
  2. Use 'Chr' as a base class for creating your own character class.
  3. 'Chr' already implements the following functionality:
  4. -moving according to input
  5. -moving according to target position
  6. -moving according to direction
  7. -automatic skeleton animating according to the default animations (set in 'Chr::sac')
  8. -ragdoll support
  9. -picking up and dropping down items
  10. -grabbing actors
  11. /******************************************************************************/
  12. namespace Game{
  13. /******************************************************************************/
  14. enum ACTION_TYPE // Character Action Type
  15. {
  16. ACTION_NONE , // none (update movement according to input)
  17. ACTION_MOVE_TO , // move to position
  18. ACTION_MOVE_DIR, // move to direction
  19. };
  20. /******************************************************************************/
  21. STRUCT(Chr , Obj) // Game Character Object
  22. //{
  23. struct Input // Movement Input
  24. {
  25. Bool crouch, // if want to crouch
  26. walk ; // if want to walk
  27. Flt jump ; // jump velocity ( 0..Inf)
  28. Int dodge ; // dodge direction (-1..1)
  29. VecI move ; // movement direction (-1..1)
  30. VecI2 turn ; // turn angle (-1..1)
  31. Bool adjust_move, // if adjust movement from 'Input.move' when in ACTION_NONE, default=true
  32. adjust_turn; // if adjust turning from 'Input.turn' when in ACTION_NONE, default=true
  33. Input() {crouch=walk=false; jump=0; dodge=0; move.zero(); turn.zero(); adjust_move=adjust_turn=true;}
  34. };
  35. ACTION_TYPE action ; // action type, default=ACTION_NONE
  36. Bool move_walking; // if move through path by walking instead of running, default=false
  37. Flt speed , // move speed, default= 4.1, for changing animations speed please check Chr::Animation::speed
  38. turn_speed , // turn speed, default= 3.5
  39. flying_speed; // flying speed, default=25.0
  40. Vec2 angle ; // look angles
  41. Input input ; // input (not saved inside 'save' method)
  42. Controller ctrl ; // controller
  43. AnimatedSkeleton skel ; // animated skeleton
  44. ObjectPtr base ; // base object
  45. MeshPtr mesh ; // mesh
  46. Int mesh_variation; // mesh variation index
  47. Ragdoll ragdoll ; // ragdoll, before manual operations on 'ragdoll' you must call at least once 'ragdollValidate' to make sure the 'ragdoll' is properly initialized
  48. // manage
  49. virtual void create(Object &obj); // create from object, Chr class accepts only 'PhysBody' made from 1 Capsule (ignoring its "up" vector), if no PhysBody is given then the character controller will be created basing on the object scale
  50. virtual void setUnsavedParams(); // set parameters which are not included in the save file
  51. // get / set
  52. virtual Vec pos ( ); // get position
  53. virtual void pos (C Vec &pos ); // set position
  54. virtual Matrix matrix( ); // get matrix
  55. virtual void matrix(C Matrix &matrix) {pos(matrix.pos);} // set matrix, only 'matrix.pos' is used in this method
  56. // actions
  57. void actionBreak ( ); // break active action
  58. Bool actionMoveTo (C Vec &pos); // set action to move to position, false on fail (if path not found)
  59. void actionMoveDir(C Vec &dir); // set action to move to direction
  60. // animation
  61. virtual Flt animateFaceBlend (); // facial animation blending value, dependent on character visibility and distance from camera, returns value 0..1 (0 disables facial animation), you can override this method and return your custom values
  62. virtual Bool animateFingers (); // if animate hand/foot fingers , dependent on character visibility and distance from camera, returns value true/false (false disables hands animation), you can override this method and return your custom values
  63. virtual Flt animateAlignFeet (); // how much to align feet on to the ground , returns value 0..1 (0 disables feet aligning ), you can override this method and return your custom values
  64. virtual void animateStepOccurred() {} // this method is called when "step" event has occurred in the most significant movement animation, you can override this method and for example play "step" sound
  65. virtual void animate(); // this method animates the character skeleton using default animations, it consists only of following : 'skel.clear().animate(stand).animate(walk)...', you can override this method and use it in 2 ways:
  66. // 1) use default animations and modify them - "super::animate(); skel.animate(custom animation)..."
  67. // 2) use only custom animations - "skel .clear (); skel.animate(custom animation)..."
  68. virtual void animateUpdateMatrix(C Matrix &body_matrix) {skel.updateMatrix(body_matrix);} // this method is called when the skeleton is fully animated and is ready to have its matrixes updated, you can override this method and optionally modify the skeleton before or after calling the default 'animateUpdateMatrix' method
  69. // ragdoll
  70. virtual void ragdollValidate(); // call this at least once before manual operations on 'ragdoll' to make sure it has been properly initialized
  71. virtual void ragdollEnable (); // switch from skeleton animations to ragdoll animations (RAGDOLL_FULL)
  72. virtual void ragdollDisable (); // switch from ragdoll animations to skeleton animations (RAGDOLL_NONE)
  73. virtual Bool ragdollBlend (); // enable ragdoll blending for shot/hit simulation (RAGDOLL_PART), if current 'ragdoll_mode' is set to RAGDOLL_FULL then the call will fail and false will be returned
  74. // callbacks
  75. virtual void memoryAddressChanged(); // called when object memory address has been changed, you should override it and adjust Actor::obj pointer for all actors
  76. // grabbing
  77. void grabStart (Item &item, C Vec &local_pos=VecZero, Flt power=5); // start grabbing 'item' at its 'local_pos' position with 'power'
  78. void grabStop ( ); // stop grabbing
  79. Bool grabIs ( ) {return grab.is ( );} // if grabbing something
  80. Actor* grabbedActor( ) {return grab.grabbedActor( );} // get grabbed actor
  81. Vec grabPos ( ) {return grab.pos ( );} // get world position of grabber
  82. void grabPos (C Vec &pos) { grab.pos (pos);} // move to 'pos' world position
  83. // update
  84. virtual Bool update(); // update, return false when object wants to be deleted, and true if wants to exist
  85. // draw
  86. 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)
  87. virtual void drawShadow (); // in this method you should add the meshes to the shadow draw list (Mesh::drawShadow)
  88. // enable / disable
  89. 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
  90. 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
  91. // io
  92. virtual Bool save(File &f); // save, false on fail
  93. virtual Bool load(File &f); // load, false on fail
  94. ~Chr();
  95. Chr();
  96. public: // following members/methods are public, however don't modify/call them manually unless you know what you're doing
  97. Vec input_move;
  98. Vec2 input_turn;
  99. Int dodging;
  100. Flt dodge_step,
  101. dodge_availability;
  102. Vec2 move_dir;
  103. Vec path_target;
  104. Memc<Vec> path;
  105. enum RAGDOLL_MODE
  106. {
  107. RAGDOLL_NONE, // animations fully controlled by skeleton animations (alive character )
  108. RAGDOLL_PART, // animations controlled by skeleton animations + small ragdoll blends (alive character with simulated shot hits)
  109. RAGDOLL_FULL, // animations fully controlled by ragdoll (dead character )
  110. };
  111. RAGDOLL_MODE ragdoll_mode;
  112. Flt ragdoll_time;
  113. struct Animation
  114. {
  115. Flt b_turn_l, t_turn_l, // blend and time value of turning left animation
  116. b_turn_r, t_turn_r; // blend and time value of turning right animation
  117. Flt stand_crouch, // stand 0..1 crouch blend value
  118. stop_move , // stop 0..1 move blend value
  119. walk_run , // walk 0..1 run blend value
  120. forward_back , // forward 0..1 back blend value
  121. left_right , // left 0..1 right blend value
  122. straight_strafe; // straight 0..1 strafe blend value
  123. Flt strafe_yaw, // -1..1 value
  124. lean ; // -1..1 lean value
  125. Flt time , // 0..Inf, default=0 , character movement animation time
  126. speed , // 0..Inf, default=0.68 , character movement animation scale factor, this affects only animation speed, you can change it when setting different 'Chr::speed' values so the animations will be played proportionally to movement speed, suggested formula : Chr::anim.speed=Chr::speed/6
  127. unique; // 0..1 , default=Random.f(), character unique value used for animation offsets
  128. // flying values
  129. Flt fly,
  130. fly_x, fly_z,
  131. fly_strafe, fly_turn,
  132. fly_full_body;
  133. }anim;
  134. struct SkelAnimCache // Skeleton Animation Cache, these are the pointers to character default animations, you can replace them with custom ones, for example 'walk=skel.getSkelAnim("custom walk.anim")'
  135. {
  136. SkelAnim
  137. *fist_l , // left fist
  138. *fist_r , // right fist
  139. *stand , // stand still
  140. *crouch , // crouch still
  141. *turn_l , // turn left
  142. *turn_r , // turn right
  143. *walk , // walk forward
  144. *run , // run forward
  145. *crouch_f , // crouch and move forward
  146. *crouch_l , // crouch and move left
  147. *crouch_r , // crouch and move right
  148. *strafe_wl, // strafe left walking
  149. *strafe_wr, // strafe right walking
  150. *strafe_l , // strafe left running
  151. *strafe_r , // strafe right running
  152. *dodge_l , // dodge left
  153. *dodge_r , // dodge right
  154. *floating ; // float still
  155. Int head, neck, body, body_u, arm_ru, arm_lu, arm_rd, arm_ld, leg_lu, leg_ru, leg_ld, leg_rd, hand_l, hand_r, toe_l, toe_r; // indexes of skeleton bones found in 'skel', -1=none
  156. void set(AnimatedSkeleton &anim_skel, C ObjectPtr &obj); // set default values using 'skel' skeleton
  157. }sac; // not saved in SaveGame
  158. Grab grab;
  159. Flt desiredSpeed();
  160. void updateAction ();
  161. void updateAnimationAnim(); // here all members from 'Chr::anim' are updated according to input
  162. void updateAnimation ();
  163. void updateController ();
  164. };
  165. /******************************************************************************/
  166. } // namespace
  167. /******************************************************************************/