debris.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, protected AssetPtrCallback
  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_REFACTOR(DebrisData, 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() override;
  84. bool preload( bool server, String &errorStr ) override;
  85. static void initPersistFields();
  86. void packData(BitStream* stream) override;
  87. void unpackData(BitStream* stream) override;
  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. void onPerformSubstitutions() override;
  94. bool allowSubstitutions() const override { return true; }
  95. protected:
  96. void onAssetRefreshed(AssetPtrBase* pAssetPtrBase) override
  97. {
  98. reloadOnLocalClient();
  99. }
  100. };
  101. //**************************************************************************
  102. // Debris
  103. //**************************************************************************
  104. class Debris : public GameBase
  105. {
  106. typedef GameBase Parent;
  107. private:
  108. S32 mNumBounces;
  109. F32 mSize;
  110. Point3F mLastPos;
  111. Point3F mVelocity;
  112. F32 mLifetime;
  113. DebrisData * mDataBlock;
  114. F32 mElapsedTime;
  115. TSShapeInstance * mShape;
  116. TSPartInstance * mPart;
  117. MatrixF mInitialTrans;
  118. F32 mXRotSpeed;
  119. F32 mZRotSpeed;
  120. Point3F mRotAngles;
  121. F32 mRadius;
  122. bool mStatic;
  123. F32 mElasticity;
  124. F32 mFriction;
  125. SimObjectPtr<ParticleEmitter> mEmitterList[ DebrisData::DDC_NUM_EMITTERS ];
  126. /// Bounce the debris - returns true if debris bounces.
  127. bool bounce( const Point3F &nextPos, F32 dt );
  128. /// Compute state of debris as if it hasn't collided with anything.
  129. void computeNewState( Point3F &newPos, Point3F &newVel, F32 dt );
  130. void explode();
  131. void rotate( F32 dt );
  132. protected:
  133. void processTick(const Move* move) override;
  134. void advanceTime( F32 dt ) override;
  135. void prepRenderImage(SceneRenderState *state) override;
  136. void prepBatchRender(SceneRenderState *state);
  137. bool onAdd() override;
  138. void onRemove() override;
  139. void updateEmitters( Point3F &pos, Point3F &vel, U32 ms );
  140. public:
  141. Debris();
  142. ~Debris();
  143. static void initPersistFields();
  144. bool onNewDataBlock( GameBaseData *dptr, bool reload ) override;
  145. void init( const Point3F &position, const Point3F &velocity );
  146. void setLifetime( F32 lifetime ){ mLifetime = lifetime; }
  147. void setPartInstance( TSPartInstance *part ){ mPart = part; }
  148. void setSize( F32 size );
  149. void setVelocity( const Point3F &vel ) override{ mVelocity = vel; }
  150. void setRotAngles( const Point3F &angles ){ mRotAngles = angles; }
  151. DECLARE_CONOBJECT(Debris);
  152. DECLARE_CATEGORY("UNLISTED");
  153. private:
  154. SimObject* ss_object;
  155. S32 ss_index;
  156. public:
  157. void setSubstitutionData(SimObject* obj, S32 idx=0) { ss_object = obj; ss_index = idx; }
  158. };
  159. #endif