Kinematic.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. namespace Game{
  5. /******************************************************************************/
  6. Kinematic::~Kinematic()
  7. {
  8. }
  9. Kinematic::Kinematic()
  10. {
  11. scale=0;
  12. mesh_variation=0;
  13. _matrix.zero();
  14. _matrix_scaled.zero();
  15. }
  16. /******************************************************************************/
  17. // MANAGE
  18. /******************************************************************************/
  19. void Kinematic::setUnsavedParams()
  20. {
  21. mesh=(base ? base->mesh() : MeshPtr());
  22. if(base && base->phys())actor.create(*base->phys(), 1, scale, true)
  23. .matrix(_matrix)
  24. .obj (this)
  25. .group (AG_TERRAIN);
  26. }
  27. void Kinematic::create(Object &obj)
  28. {
  29. base =obj.firstStored();
  30. scale =obj.scale3 ();
  31. _matrix=obj.matrixFinal().normalize(); _matrix_scaled=_matrix; _matrix_scaled.scaleOrnL(scale);
  32. mesh_variation=obj.meshVariationIndex();
  33. setUnsavedParams();
  34. }
  35. /******************************************************************************/
  36. // GET / SET
  37. /******************************************************************************/
  38. Vec Kinematic::pos ( ) {return _matrix.pos ;}
  39. Matrix Kinematic::matrix ( ) {return _matrix ;}
  40. Matrix Kinematic::matrixScaled ( ) {return _matrix_scaled;}
  41. void Kinematic::pos (C Vec &pos ) {actor. pos (pos ); T._matrix_scaled.pos=T._matrix.pos=pos ;}
  42. void Kinematic::kinematicMoveTo(C Vec &pos ) {actor.kinematicMoveTo(pos ); T._matrix_scaled.pos=T._matrix.pos=pos ;}
  43. void Kinematic::matrix (C Matrix &matrix) {actor. matrix(matrix); T._matrix_scaled =T._matrix =matrix; T._matrix_scaled.scaleOrnL(scale);}
  44. void Kinematic::kinematicMoveTo(C Matrix &matrix) {actor.kinematicMoveTo(matrix); T._matrix_scaled =T._matrix =matrix; T._matrix_scaled.scaleOrnL(scale);}
  45. /******************************************************************************/
  46. // CALLBACKS
  47. /******************************************************************************/
  48. void Kinematic::memoryAddressChanged()
  49. {
  50. actor.obj(this);
  51. }
  52. /******************************************************************************/
  53. // UPDATE
  54. /******************************************************************************/
  55. Bool Kinematic::update()
  56. {
  57. return true;
  58. }
  59. /******************************************************************************/
  60. // DRAW
  61. /******************************************************************************/
  62. UInt Kinematic::drawPrepare()
  63. {
  64. if(mesh)if(Frustum(*mesh, _matrix_scaled))
  65. {
  66. SetVariation(mesh_variation); mesh->draw(_matrix_scaled);
  67. SetVariation();
  68. }
  69. return 0; // no additional render modes required
  70. }
  71. /******************************************************************************/
  72. void Kinematic::drawShadow()
  73. {
  74. if(mesh)if(Frustum(*mesh, _matrix_scaled))
  75. {
  76. SetVariation(mesh_variation); mesh->drawShadow(_matrix_scaled);
  77. SetVariation();
  78. }
  79. }
  80. /******************************************************************************/
  81. // IO
  82. /******************************************************************************/
  83. Bool Kinematic::save(File &f)
  84. {
  85. if(super::save(f))
  86. {
  87. f.cmpUIntV(0); // version
  88. f.putAsset(base.id())<<scale<<_matrix;
  89. f.putUInt (mesh ? mesh->variationID(mesh_variation) : 0);
  90. if(!actor.saveState(f))return false;
  91. return f.ok();
  92. }
  93. return false;
  94. }
  95. /******************************************************************************/
  96. Bool Kinematic::load(File &f)
  97. {
  98. if(super::load(f))switch(f.decUIntV()) // version
  99. {
  100. case 0:
  101. {
  102. base=f.getAssetID();
  103. f>>scale>>_matrix; _matrix_scaled=_matrix; _matrix_scaled.scaleOrnL(scale);
  104. setUnsavedParams();
  105. UInt mesh_variation_id=f.getUInt(); mesh_variation=(mesh ? mesh->variationFind(mesh_variation_id) : 0);
  106. if(!actor.loadState(f))return false;
  107. if(f.ok())return true;
  108. }break;
  109. }
  110. return false;
  111. }
  112. /******************************************************************************/
  113. }}
  114. /******************************************************************************/