debris.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #ifndef _DEBRIS_H_
  27. #define _DEBRIS_H_
  28. #ifndef __RESOURCE_H__
  29. #include "core/resource.h"
  30. #endif
  31. #ifndef _GAMEBASE_H_
  32. #include "T3D/gameBase/gameBase.h"
  33. #endif
  34. class ParticleEmitterData;
  35. class ParticleEmitter;
  36. class ExplosionData;
  37. class TSPartInstance;
  38. class TSShapeInstance;
  39. class TSShape;
  40. //**************************************************************************
  41. // Debris Data
  42. //**************************************************************************
  43. struct DebrisData : public GameBaseData
  44. {
  45. typedef GameBaseData Parent;
  46. //-----------------------------------------------------------------------
  47. // Data Decs
  48. //-----------------------------------------------------------------------
  49. enum DebrisDataConst
  50. {
  51. DDC_NUM_EMITTERS = 2,
  52. };
  53. //-----------------------------------------------------------------------
  54. // Debris datablock
  55. //-----------------------------------------------------------------------
  56. F32 velocity;
  57. F32 velocityVariance;
  58. F32 friction;
  59. F32 elasticity;
  60. F32 lifetime;
  61. F32 lifetimeVariance;
  62. S32 numBounces;
  63. S32 bounceVariance;
  64. F32 minSpinSpeed;
  65. F32 maxSpinSpeed;
  66. bool explodeOnMaxBounce; // explodes after it has bounced max times
  67. bool staticOnMaxBounce; // becomes static after bounced max times
  68. bool snapOnMaxBounce; // snap into a "resting" position on last bounce
  69. bool fade;
  70. bool useRadiusMass; // use mass calculations based on radius
  71. F32 baseRadius; // radius at which the standard elasticity and friction apply
  72. F32 gravModifier; // how much gravity affects debris
  73. F32 terminalVelocity; // max velocity magnitude
  74. bool ignoreWater;
  75. const char* shapeName;
  76. Resource<TSShape> shape;
  77. StringTableEntry textureName;
  78. S32 explosionId;
  79. ExplosionData * explosion;
  80. ParticleEmitterData* emitterList[DDC_NUM_EMITTERS];
  81. S32 emitterIDList[DDC_NUM_EMITTERS];
  82. DebrisData();
  83. bool onAdd();
  84. bool preload( bool server, String &errorStr );
  85. static void initPersistFields();
  86. void packData(BitStream* stream);
  87. void unpackData(BitStream* stream);
  88. DECLARE_CONOBJECT(DebrisData);
  89. public:
  90. /*C*/ DebrisData(const DebrisData&, bool = false);
  91. /*D*/ ~DebrisData();
  92. DebrisData* cloneAndPerformSubstitutions(const SimObject*, S32 index=0);
  93. virtual void onPerformSubstitutions();
  94. virtual bool allowSubstitutions() const { return true; }
  95. };
  96. //**************************************************************************
  97. // Debris
  98. //**************************************************************************
  99. class Debris : public GameBase
  100. {
  101. typedef GameBase Parent;
  102. private:
  103. S32 mNumBounces;
  104. F32 mSize;
  105. Point3F mLastPos;
  106. Point3F mVelocity;
  107. F32 mLifetime;
  108. DebrisData * mDataBlock;
  109. F32 mElapsedTime;
  110. TSShapeInstance * mShape;
  111. TSPartInstance * mPart;
  112. MatrixF mInitialTrans;
  113. F32 mXRotSpeed;
  114. F32 mZRotSpeed;
  115. Point3F mRotAngles;
  116. F32 mRadius;
  117. bool mStatic;
  118. F32 mElasticity;
  119. F32 mFriction;
  120. SimObjectPtr<ParticleEmitter> mEmitterList[ DebrisData::DDC_NUM_EMITTERS ];
  121. /// Bounce the debris - returns true if debris bounces.
  122. bool bounce( const Point3F &nextPos, F32 dt );
  123. /// Compute state of debris as if it hasn't collided with anything.
  124. void computeNewState( Point3F &newPos, Point3F &newVel, F32 dt );
  125. void explode();
  126. void rotate( F32 dt );
  127. protected:
  128. virtual void processTick(const Move* move);
  129. virtual void advanceTime( F32 dt );
  130. void prepRenderImage(SceneRenderState *state);
  131. void prepBatchRender(SceneRenderState *state);
  132. bool onAdd();
  133. void onRemove();
  134. void updateEmitters( Point3F &pos, Point3F &vel, U32 ms );
  135. public:
  136. Debris();
  137. ~Debris();
  138. static void initPersistFields();
  139. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  140. void init( const Point3F &position, const Point3F &velocity );
  141. void setLifetime( F32 lifetime ){ mLifetime = lifetime; }
  142. void setPartInstance( TSPartInstance *part ){ mPart = part; }
  143. void setSize( F32 size );
  144. void setVelocity( const Point3F &vel ){ mVelocity = vel; }
  145. void setRotAngles( const Point3F &angles ){ mRotAngles = angles; }
  146. DECLARE_CONOBJECT(Debris);
  147. private:
  148. SimObject* ss_object;
  149. S32 ss_index;
  150. public:
  151. void setSubstitutionData(SimObject* obj, S32 idx=0) { ss_object = obj; ss_index = idx; }
  152. };
  153. #endif