debris.h 5.5 KB

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