Item.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. namespace Game{
  5. /******************************************************************************/
  6. Item::~Item()
  7. {
  8. if(_grabber)_grabber->grabStop();
  9. }
  10. Item::Item()
  11. {
  12. scale=0;
  13. mesh_variation=0;
  14. _vel .zero();
  15. _ang_vel.zero();
  16. _grabber=null;
  17. }
  18. /******************************************************************************/
  19. // MANAGE
  20. /******************************************************************************/
  21. void Item::setUnsavedParams()
  22. {
  23. mesh=(base ? base->mesh() : MeshPtr());
  24. if(base && base->phys())actor.create(*base->phys(), 1, scale).obj(this);
  25. }
  26. void Item::create(Object &obj)
  27. {
  28. scale =obj.scale ();
  29. base =obj.firstStored ();
  30. mesh_variation=obj.meshVariationIndex();
  31. setUnsavedParams();
  32. actor.matrix (obj.matrixFinal().normalize())
  33. .adamping(5 )
  34. .ccd (true); // enable ccd for all items, because most item actors are very small and can easily fall through ground or other obstacles
  35. }
  36. /******************************************************************************/
  37. // GET / SET
  38. /******************************************************************************/
  39. Vec Item::pos ( ) {return actor.pos ( ); }
  40. void Item::pos (C Vec &pos ) { actor.pos (pos ); }
  41. Matrix Item::matrix ( ) {return actor.matrix( ); }
  42. Matrix Item::matrixScaled( ) {return actor.matrix( ).scaleOrn(scale);}
  43. void Item::matrix (C Matrix &matrix) { actor.matrix(matrix); }
  44. /******************************************************************************/
  45. void Item::setDrawingVelocities(C Vec &vel, C Vec &ang_vel)
  46. {
  47. T._vel =vel ;
  48. T._ang_vel=ang_vel;
  49. }
  50. /******************************************************************************/
  51. // CALLBACKS
  52. /******************************************************************************/
  53. void Item::memoryAddressChanged()
  54. {
  55. actor.obj(this);
  56. }
  57. /******************************************************************************/
  58. // UPDATE
  59. /******************************************************************************/
  60. Bool Item::update()
  61. {
  62. setDrawingVelocities(actor.vel(), actor.angVel());
  63. return true;
  64. }
  65. /******************************************************************************/
  66. // DRAW
  67. /******************************************************************************/
  68. UInt Item::drawPrepare()
  69. {
  70. if(mesh && actor.is())
  71. {
  72. Matrix matrix=matrixScaled();
  73. if(Frustum(*mesh, matrix))
  74. {
  75. SetVariation(mesh_variation); mesh->draw(matrix, _vel, _ang_vel);
  76. SetVariation();
  77. }
  78. }
  79. return 0; // no additional render modes required
  80. }
  81. /******************************************************************************/
  82. void Item::drawShadow()
  83. {
  84. if(mesh && actor.is())
  85. {
  86. Matrix matrix=matrixScaled();
  87. if(Frustum(*mesh, matrix))
  88. {
  89. SetVariation(mesh_variation); mesh->drawShadow(matrix);
  90. SetVariation();
  91. }
  92. }
  93. }
  94. /******************************************************************************/
  95. // ENABLE / DISABLE
  96. /******************************************************************************/
  97. void Item::disable()
  98. {
  99. super::disable();
  100. actor.kinematic(true);
  101. }
  102. void Item::enable()
  103. {
  104. super::enable();
  105. actor.kinematic(false);
  106. }
  107. /******************************************************************************/
  108. // OPERATIONS
  109. /******************************************************************************/
  110. void Item::willBeMovedFromWorldToStorage()
  111. {
  112. super::willBeMovedFromWorldToStorage();
  113. actor.active(false);
  114. if(_grabber)_grabber->grabStop();
  115. }
  116. void Item::willBeMovedFromStorageToWorld()
  117. {
  118. super::willBeMovedFromStorageToWorld();
  119. actor.active(true);
  120. // when item is dropped down, then add random angular velocity
  121. Vec ang_vel=Random(Ball(1));
  122. ang_vel.setLength(Random.f(30, 50));
  123. actor.angVel(ang_vel); // set random angular velocity
  124. }
  125. /******************************************************************************/
  126. // IO
  127. /******************************************************************************/
  128. Bool Item::save(File &f)
  129. {
  130. if(super::save(f))
  131. {
  132. f.cmpUIntV(0); // version
  133. f<<scale;
  134. f.putAsset(base.id());
  135. f.putUInt (mesh ? mesh->variationID(mesh_variation) : 0);
  136. if(!actor.saveState(f))return false;
  137. return f.ok();
  138. }
  139. return false;
  140. }
  141. /******************************************************************************/
  142. Bool Item::load(File &f)
  143. {
  144. if(super::load(f))switch(f.decUIntV()) // version
  145. {
  146. case 0:
  147. {
  148. f>>scale;
  149. base=f.getAssetID();
  150. setUnsavedParams();
  151. UInt mesh_variation_id=f.getUInt(); mesh_variation=(mesh ? mesh->variationFind(mesh_variation_id) : 0);
  152. if(!actor.loadState(f))return false;
  153. if(f.ok())return true;
  154. }break;
  155. }
  156. return false;
  157. }
  158. /******************************************************************************/
  159. }}
  160. /******************************************************************************/