debris.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "T3D/assets/ShapeAsset.h"
  35. class ParticleEmitterData;
  36. class ParticleEmitter;
  37. class ExplosionData;
  38. class TSPartInstance;
  39. class TSShapeInstance;
  40. class TSShape;
  41. //**************************************************************************
  42. // Debris Data
  43. //**************************************************************************
  44. struct DebrisData : public GameBaseData
  45. {
  46. typedef GameBaseData Parent;
  47. //-----------------------------------------------------------------------
  48. // Data Decs
  49. //-----------------------------------------------------------------------
  50. enum DebrisDataConst
  51. {
  52. DDC_NUM_EMITTERS = 2,
  53. };
  54. //-----------------------------------------------------------------------
  55. // Debris datablock
  56. //-----------------------------------------------------------------------
  57. F32 velocity;
  58. F32 velocityVariance;
  59. F32 friction;
  60. F32 elasticity;
  61. F32 lifetime;
  62. F32 lifetimeVariance;
  63. S32 numBounces;
  64. S32 bounceVariance;
  65. F32 minSpinSpeed;
  66. F32 maxSpinSpeed;
  67. bool explodeOnMaxBounce; // explodes after it has bounced max times
  68. bool staticOnMaxBounce; // becomes static after bounced max times
  69. bool snapOnMaxBounce; // snap into a "resting" position on last bounce
  70. bool fade;
  71. bool useRadiusMass; // use mass calculations based on radius
  72. F32 baseRadius; // radius at which the standard elasticity and friction apply
  73. F32 gravModifier; // how much gravity affects debris
  74. F32 terminalVelocity; // max velocity magnitude
  75. bool ignoreWater;
  76. DECLARE_SHAPEASSET(DebrisData, Shape, onShapeChanged);
  77. DECLARE_ASSET_SETGET(DebrisData, Shape);
  78. StringTableEntry textureName;
  79. S32 explosionId;
  80. ExplosionData * explosion;
  81. ParticleEmitterData* emitterList[DDC_NUM_EMITTERS];
  82. S32 emitterIDList[DDC_NUM_EMITTERS];
  83. DebrisData();
  84. bool onAdd();
  85. bool preload( bool server, String &errorStr );
  86. static void initPersistFields();
  87. void packData(BitStream* stream);
  88. void unpackData(BitStream* stream);
  89. DECLARE_CONOBJECT(DebrisData);
  90. public:
  91. /*C*/ DebrisData(const DebrisData&, bool = false);
  92. /*D*/ ~DebrisData();
  93. DebrisData* cloneAndPerformSubstitutions(const SimObject*, S32 index=0);
  94. virtual void onPerformSubstitutions();
  95. virtual bool allowSubstitutions() const { return true; }
  96. void onShapeChanged() {}
  97. };
  98. //**************************************************************************
  99. // Debris
  100. //**************************************************************************
  101. class Debris : public GameBase
  102. {
  103. typedef GameBase Parent;
  104. private:
  105. S32 mNumBounces;
  106. F32 mSize;
  107. Point3F mLastPos;
  108. Point3F mVelocity;
  109. F32 mLifetime;
  110. DebrisData * mDataBlock;
  111. F32 mElapsedTime;
  112. TSShapeInstance * mShape;
  113. TSPartInstance * mPart;
  114. MatrixF mInitialTrans;
  115. F32 mXRotSpeed;
  116. F32 mZRotSpeed;
  117. Point3F mRotAngles;
  118. F32 mRadius;
  119. bool mStatic;
  120. F32 mElasticity;
  121. F32 mFriction;
  122. SimObjectPtr<ParticleEmitter> mEmitterList[ DebrisData::DDC_NUM_EMITTERS ];
  123. /// Bounce the debris - returns true if debris bounces.
  124. bool bounce( const Point3F &nextPos, F32 dt );
  125. /// Compute state of debris as if it hasn't collided with anything.
  126. void computeNewState( Point3F &newPos, Point3F &newVel, F32 dt );
  127. void explode();
  128. void rotate( F32 dt );
  129. protected:
  130. virtual void processTick(const Move* move);
  131. virtual void advanceTime( F32 dt );
  132. void prepRenderImage(SceneRenderState *state);
  133. void prepBatchRender(SceneRenderState *state);
  134. bool onAdd();
  135. void onRemove();
  136. void updateEmitters( Point3F &pos, Point3F &vel, U32 ms );
  137. public:
  138. Debris();
  139. ~Debris();
  140. static void initPersistFields();
  141. bool onNewDataBlock( GameBaseData *dptr, bool reload );
  142. void init( const Point3F &position, const Point3F &velocity );
  143. void setLifetime( F32 lifetime ){ mLifetime = lifetime; }
  144. void setPartInstance( TSPartInstance *part ){ mPart = part; }
  145. void setSize( F32 size );
  146. void setVelocity( const Point3F &vel ){ mVelocity = vel; }
  147. void setRotAngles( const Point3F &angles ){ mRotAngles = angles; }
  148. DECLARE_CONOBJECT(Debris);
  149. private:
  150. SimObject* ss_object;
  151. S32 ss_index;
  152. public:
  153. void setSubstitutionData(SimObject* obj, S32 idx=0) { ss_object = obj; ss_index = idx; }
  154. };
  155. #endif