Character.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. namespace Game{
  5. /******************************************************************************/
  6. Chr::~Chr()
  7. {
  8. grabStop();
  9. }
  10. Chr::Chr()
  11. {
  12. action =ACTION_NONE;
  13. move_walking=false;
  14. speed = 4.1f;
  15. turn_speed = 3.5f;
  16. flying_speed=25.0f;
  17. angle.zero();
  18. input_move.zero();
  19. input_turn.zero();
  20. dodging=0;
  21. dodge_step=0;
  22. dodge_availability=0;
  23. move_dir.zero();
  24. mesh_variation=0;
  25. ragdoll_mode=RAGDOLL_NONE;
  26. ragdoll_time=0;
  27. Zero(anim); anim.speed=speed/6; anim.unique=Random.f();
  28. Zero(sac );
  29. }
  30. /******************************************************************************/
  31. // MANAGE
  32. /******************************************************************************/
  33. void Chr::setUnsavedParams()
  34. {
  35. mesh=(base ? base->mesh() : MeshPtr());
  36. if(!mesh)Exit("Attempting to create a character without Mesh");
  37. }
  38. void Chr::create(Object &obj)
  39. {
  40. Matrix matrix=obj.matrixFinal();
  41. Flt scale =obj.scale ();
  42. base =obj.firstStored(); // copy pointer to object stored in project data
  43. mesh_variation=obj.meshVariationIndex();
  44. setUnsavedParams();
  45. // setup controller parameters
  46. Flt ctrl_height=mesh->ext.h()*scale,
  47. ctrl_radius=Max(0.4f, ctrl_height*0.25f);
  48. Vec ctrl_pos =mesh->ext.pos*matrix;
  49. if(C PhysBodyPtr &phys=obj.phys()) // if 'Object' has 'PhysBody' specified, then take radius and height from the PhysBody capsule shape
  50. if(phys->is())
  51. {
  52. if(phys->parts.elms()==1 && phys->parts[0].type()==PHYS_SHAPE && phys->parts[0].shape.type==SHAPE_CAPSULE)
  53. {
  54. C Capsule &capsule=phys->parts[0].shape.capsule;
  55. ctrl_radius=capsule.r *scale ;
  56. ctrl_height=capsule.h *scale ;
  57. ctrl_pos =capsule.pos*matrix;
  58. }else Exit("Character Controller can be created only from PhysBody Capsule");
  59. }
  60. // create character controller
  61. ctrl.createCapsule(ctrl_radius, ctrl_height, ctrl_pos, &matrix.pos)
  62. .actor.obj(this);
  63. // create skeleton
  64. skel.create(mesh->skeleton(), scale, matrix);
  65. sac.set(skel, base); // set default animations
  66. //
  67. angle.x=Angle(matrix.z.xz())-PI_2; // set initial horizontal angle
  68. }
  69. /******************************************************************************/
  70. // GET / SET
  71. /******************************************************************************/
  72. Flt Chr::desiredSpeed() // get desired speed
  73. {
  74. Flt speed =(ctrl.flying() ? T.flying_speed : T.speed);
  75. speed*=Lerp(0.33f, 1.0f, anim.walk_run); // slow down when walking
  76. return speed;
  77. }
  78. /******************************************************************************/
  79. Vec Chr::pos ( ) {return skel.pos ();}
  80. Matrix Chr::matrix( ) {return skel.matrix();}
  81. void Chr::pos (C Vec &pos)
  82. {
  83. Vec delta=pos-T.pos(); // delta which moves from old position to new position
  84. skel.move(delta); // move skeleton
  85. ctrl.actor.pos(ctrl.actor.pos()+delta); // move controller
  86. ragdoll .pos(ragdoll .pos()+delta); // move ragdoll
  87. }
  88. /******************************************************************************/
  89. // CALLBACKS
  90. /******************************************************************************/
  91. void Chr::memoryAddressChanged()
  92. {
  93. ctrl.actor.obj(this);
  94. ragdoll .obj(this);
  95. }
  96. /******************************************************************************/
  97. // UPDATE
  98. /******************************************************************************/
  99. Bool Chr::update()
  100. {
  101. if(ragdoll_mode==RAGDOLL_FULL) // the character has ragdoll fully enabled, which most likely means that the character is dead, so update only skeleton basing on the ragdoll pose
  102. {
  103. ragdoll.toSkel(skel);
  104. }else
  105. {
  106. updateAction (); // update automatic actions to set the input
  107. updateAnimation (); // update all animation parameters
  108. updateController(); // update character controller
  109. }
  110. skel.updateVelocities(); // update skeleton velocities
  111. return true;
  112. }
  113. /******************************************************************************/
  114. // DRAW
  115. /******************************************************************************/
  116. UInt Chr::drawPrepare()
  117. {
  118. if(mesh)if(Frustum(Ball().setAnimated(mesh->ext, skel)))
  119. {
  120. SetVariation(mesh_variation); mesh->draw(skel);
  121. SetVariation();
  122. }
  123. return 0; // no additional render modes required
  124. }
  125. void Chr::drawShadow()
  126. {
  127. if(mesh)if(Frustum(Ball().setAnimated(mesh->ext, skel)))
  128. {
  129. SetVariation(mesh_variation); mesh->drawShadow(skel);
  130. SetVariation();
  131. }
  132. }
  133. /******************************************************************************/
  134. // ENABLE / DISABLE
  135. /******************************************************************************/
  136. void Chr::disable()
  137. {
  138. super::disable();
  139. ctrl.actor.kinematic(true);
  140. if(ragdoll_mode)ragdoll.kinematic(true);
  141. }
  142. void Chr::enable()
  143. {
  144. super::enable();
  145. ctrl.actor.kinematic((ragdoll_mode==RAGDOLL_FULL) ? true : false); // if character is in full ragdoll mode, then controller should be set as kinematic
  146. if(ragdoll_mode)ragdoll.kinematic(false);
  147. }
  148. /******************************************************************************/
  149. // IO
  150. /******************************************************************************/
  151. Bool Chr::save(File &f)
  152. {
  153. if(super::save(f))
  154. {
  155. f.cmpUIntV(0); // version
  156. f<<move_walking<<speed<<turn_speed<<flying_speed<<angle;
  157. if(ctrl.save(f))
  158. if(skel.save(f))
  159. {
  160. f.putAsset(base.id());
  161. f.putUInt (mesh ? mesh->variationID(mesh_variation) : 0);
  162. f.putByte (ragdoll_mode);
  163. if( ragdoll_mode)
  164. {
  165. f<<ragdoll_time;
  166. if(!ragdoll.saveState(f))return false;
  167. }
  168. f<<dodging; if(dodging)f<<dodge_step;
  169. f<<move_dir;
  170. f<<anim;
  171. f.putByte(action);
  172. if(action==ACTION_MOVE_TO)f<<path_target;
  173. return f.ok();
  174. }
  175. }
  176. return false;
  177. }
  178. /******************************************************************************/
  179. Bool Chr::load(File &f)
  180. {
  181. if(super::load(f))switch(f.decUIntV()) // version
  182. {
  183. case 0:
  184. {
  185. f>>move_walking>>speed>>turn_speed>>flying_speed>>angle;
  186. if(ctrl.load(f))
  187. if(skel.load(f))
  188. {
  189. ctrl.actor.obj(this);
  190. base=f.getAssetID();
  191. setUnsavedParams();
  192. UInt mesh_variation_id=f.getUInt(); mesh_variation=(mesh ? mesh->variationFind(mesh_variation_id) : 0);
  193. sac.set(skel, base);
  194. if(ragdoll_mode=RAGDOLL_MODE(f.getByte()))
  195. {
  196. f>>ragdoll_time;
  197. ragdollValidate ();
  198. if(!ragdoll.loadState(f))return false; ragdoll.obj(this);
  199. }
  200. f>>dodging; if(dodging)f>>dodge_step;
  201. f>>move_dir;
  202. f>>anim;
  203. action =ACTION_TYPE(f.getByte());
  204. if(action==ACTION_MOVE_TO){Vec target; f>>target; actionMoveTo(target);} // again set the action to reset the paths
  205. if(f.ok())return true;
  206. }
  207. }break;
  208. }
  209. return false;
  210. }
  211. /******************************************************************************/
  212. }}
  213. /******************************************************************************/