ParticlePlayer.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _PARTICLE_PLAYER_H_
  23. #define _PARTICLE_PLAYER_H_
  24. #ifndef _PARTICLE_ASSET_H_
  25. #include "2d/assets/ParticleAsset.h"
  26. #endif
  27. #ifndef _PARTICLE_SYSTEM_H_
  28. #include "2d/core/ParticleSystem.h"
  29. #endif
  30. //-----------------------------------------------------------------------------
  31. #define PARTICLE_PLAYER_EMISSION_RATE_SCALE "$pref::T2D::ParticlePlayerEmissionRateScale"
  32. #define PARTICLE_PLAYER_SIZE_SCALE "$pref::T2D::ParticlePlayerSizeScale"
  33. #define PARTICLE_PLAYER_FORCE_SCALE "$pref::T2D::ParticlePlayerForceScale"
  34. #define PARTICLE_PLAYER_TIME_SCALE "$pref::T2D::ParticlePlayerTimeScale"
  35. //-----------------------------------------------------------------------------
  36. class ParticlePlayer : public SceneObject, protected AssetPtrCallback
  37. {
  38. private:
  39. typedef SceneObject Parent;
  40. /// Emitter node.
  41. class EmitterNode
  42. {
  43. private:
  44. ParticlePlayer* mOwner;
  45. ParticleAssetEmitter* mpAssetEmitter;
  46. ParticleSystem::ParticleNode mParticleNodeHead;
  47. F32 mTimeSinceLastGeneration;
  48. bool mPaused;
  49. bool mVisible;
  50. public:
  51. EmitterNode( ParticlePlayer* pParticlePlayer, ParticleAssetEmitter* pParticleAssetEmitter )
  52. {
  53. // Sanity!
  54. AssertFatal( pParticlePlayer != NULL, "EmitterNode() - Cannot have a NULL owner." );
  55. AssertFatal( pParticleAssetEmitter != NULL, "EmitterNode() - Cannot have a NULL particle asset emitter." );
  56. // Set owner.
  57. mOwner = pParticlePlayer;
  58. // Set asset emitter.
  59. mpAssetEmitter = pParticleAssetEmitter;
  60. // Set emitter not paused.
  61. mPaused = false;
  62. // Set emitter visible.
  63. mVisible = true;
  64. // Reset time since last generation.
  65. mTimeSinceLastGeneration = 0.0f;
  66. // Reset the node head.
  67. mParticleNodeHead.mNextNode = mParticleNodeHead.mPreviousNode = &mParticleNodeHead;
  68. }
  69. ~EmitterNode()
  70. {
  71. freeAllParticles();
  72. }
  73. inline ParticlePlayer* getOwner( void ) const { return mOwner; }
  74. inline ParticleAssetEmitter* getAssetEmitter( void ) const { return mpAssetEmitter; }
  75. inline bool getActiveParticles( void ) const { return mParticleNodeHead.mNextNode != &mParticleNodeHead; }
  76. inline ParticleSystem::ParticleNode* getFirstParticle( void ) const { return mParticleNodeHead.mNextNode; }
  77. inline ParticleSystem::ParticleNode* getLastParticle( void ) const { return mParticleNodeHead.mPreviousNode; }
  78. inline ParticleSystem::ParticleNode* getParticleNodeHead( void ) { return &mParticleNodeHead; }
  79. inline void setTimeSinceLastGeneration( const F32 timeSinceLastGeneration ) { mTimeSinceLastGeneration = timeSinceLastGeneration; }
  80. inline F32 getTimeSinceLastGeneration( void ) const { return mTimeSinceLastGeneration; }
  81. inline void setPaused( const bool paused ) { mPaused = paused; }
  82. inline bool getPaused( void ) const { return mPaused; }
  83. inline void setVisible( const bool visible ) { mVisible = visible; }
  84. inline bool getVisible( void ) const { return mVisible; }
  85. ParticleSystem::ParticleNode* createParticle( void );
  86. void freeParticle( ParticleSystem::ParticleNode* pParticleNode );
  87. void freeAllParticles( void );
  88. };
  89. typedef Vector<EmitterNode*> typeEmitterVector;
  90. AssetPtr<ParticleAsset> mParticleAsset;
  91. typeEmitterVector mEmitters;
  92. bool mCameraIdle;
  93. F32 mCameraIdleDistance;
  94. bool mParticleInterpolation;
  95. bool mPlaying;
  96. bool mPaused;
  97. F32 mAge;
  98. F32 mEmissionRateScale;
  99. F32 mSizeScale;
  100. F32 mForceScale;
  101. F32 mTimeScale;
  102. bool mWaitingForParticles;
  103. bool mWaitingForDelete;
  104. public:
  105. ParticlePlayer();
  106. virtual ~ParticlePlayer();
  107. static void initPersistFields();
  108. virtual void copyTo(SimObject* object);
  109. virtual void safeDelete( void );
  110. virtual void preIntegrate( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  111. void integrateObject( const F32 totalTime, const F32 elapsedTime, DebugStats* pDebugStats );
  112. void interpolateObject( const F32 timeDelta );
  113. virtual bool validRender( void ) const { return mParticleAsset.notNull() && mParticleAsset->isAssetValid(); }
  114. virtual bool shouldRender( void ) const { return true; }
  115. virtual void sceneRender( const SceneRenderState* pSceneRenderState, const SceneRenderRequest* pSceneRenderRequest, BatchRender* pBatchRenderer );
  116. virtual void sceneRenderOverlay( const SceneRenderState* sceneRenderState );
  117. void setParticle( const char* pAssetId );
  118. const char* getParticle( void ) const { return mParticleAsset->getAssetId(); }
  119. inline void setCameraIdleDistance( const F32 idleDistance ) { mCameraIdleDistance = idleDistance; mCameraIdle = false; }
  120. inline F32 getCameraIdleDistance( void ) const { return mCameraIdleDistance; }
  121. inline void setParticleInterpolation( const bool interpolation ) { mParticleInterpolation = interpolation; }
  122. inline bool getParticleInterpolation( void ) const { return mParticleInterpolation; }
  123. inline void setEmissionRateScale( const F32 scale ) { mEmissionRateScale = scale; }
  124. inline F32 getEmissionRateScale( void ) const { return mEmissionRateScale; }
  125. inline void setSizeScale( const F32 scale ) { mSizeScale = scale; }
  126. inline F32 getSizeScale( void ) const { return mSizeScale; }
  127. inline void setForceScale( const F32 scale ) { mForceScale = scale; }
  128. inline F32 getForceScale( void ) const { return mForceScale; }
  129. inline void setTimeScale( const F32 scale ) { mTimeScale = scale; }
  130. inline F32 getTimeScale( void ) const { return mTimeScale; }
  131. inline const U32 getEmitterCount( void ) const { return (U32)mEmitters.size(); }
  132. void setEmitterPaused( const bool paused, const U32 emitterIndex );
  133. bool getEmitterPaused( const U32 emitterIndex );
  134. void setEmitterVisible( const bool visible, const U32 emitterIndex );
  135. bool getEmitterVisible( const U32 emitterIndex );
  136. bool play( const bool resetParticles );
  137. void stop( const bool waitForParticles, const bool killEffect );
  138. inline bool getIsPlaying( void ) const { return mPlaying; };
  139. inline void setPaused( const bool paused ) { mPaused = paused; }
  140. inline bool getPaused( void ) const { return mPaused; }
  141. /// Declare Console Object.
  142. DECLARE_CONOBJECT(ParticlePlayer);
  143. protected:
  144. virtual void OnRegisterScene( Scene* pScene );
  145. virtual void OnUnregisterScene( Scene* pScene );
  146. virtual void onAssetRefreshed( AssetPtrBase* pAssetPtrBase );
  147. /// Particle Creation/Integration.
  148. void configureParticle( EmitterNode* pEmitterNode, ParticleSystem::ParticleNode* pParticleNode );
  149. void integrateParticle( EmitterNode* pEmitterNode, ParticleSystem::ParticleNode* pParticleNode, const F32 particleAge, const F32 elapsedTime );
  150. /// Persistence.
  151. virtual void onTamlAddParent( SimObject* pParentObject );
  152. static bool setParticle(void* obj, const char* data) { static_cast<ParticlePlayer*>( obj )->setParticle(data); return false; };
  153. static bool writeCameraIdleDistance( void* obj, StringTableEntry pFieldName ) { return static_cast<ParticlePlayer*>( obj )->getCameraIdleDistance() > 0.0f; }
  154. static bool writeParticleInterpolation( void* obj, StringTableEntry pFieldName ) { return static_cast<ParticlePlayer*>( obj )->getParticleInterpolation(); }
  155. static bool writeEmissionRateScale( void* obj, StringTableEntry pFieldName ) { return !mIsOne( static_cast<ParticlePlayer*>( obj )->getEmissionRateScale() ); }
  156. static bool writeSizeScale( void* obj, StringTableEntry pFieldName ) { return !mIsOne( static_cast<ParticlePlayer*>( obj )->getSizeScale() ); }
  157. static bool writeForceScale( void* obj, StringTableEntry pFieldName ) { return !mIsOne( static_cast<ParticlePlayer*>( obj )->getForceScale() ); }
  158. static bool writeTimeScale( void* obj, StringTableEntry pFieldName ) { return !mIsOne( static_cast<ParticlePlayer*>( obj )->getTimeScale() ); }
  159. private:
  160. void initializeParticleAsset( void );
  161. void destroyParticleAsset( void );
  162. };
  163. #endif // _PARTICLE_PLAYER_H_