ParticleAsset.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_ASSET_H_
  23. #define _PARTICLE_ASSET_H_
  24. #ifndef _PARTICLE_ASSET_EMITTER_H_
  25. #include "2d/assets/particleAssetEmitter.h"
  26. #endif
  27. #ifndef _TAML_CHILDREN_H_
  28. #include "persistence/taml/tamlChildren.h"
  29. #endif
  30. #ifndef _ASSET_BASE_H_
  31. #include "assets/assetBase.h"
  32. #endif
  33. //-----------------------------------------------------------------------------
  34. DefineConsoleType( TypeParticleAssetPtr )
  35. //-----------------------------------------------------------------------------
  36. class ParticleAsset : public AssetBase, public TamlChildren
  37. {
  38. private:
  39. typedef AssetBase Parent;
  40. typedef Vector<ParticleAssetEmitter*> typeEmitterVector;
  41. public:
  42. /// Life Mode.
  43. enum LifeMode
  44. {
  45. INVALID_LIFEMODE,
  46. INFINITE,
  47. CYCLE,
  48. STOP,
  49. KILL
  50. };
  51. private:
  52. typeEmitterVector mEmitters;
  53. F32 mLifetime;
  54. LifeMode mLifeMode;
  55. /// Particle fields.
  56. ParticleAssetFieldCollection mParticleFields;
  57. ParticleAssetFieldBase mParticleLifeScale;
  58. ParticleAssetFieldBase mQuantityScale;
  59. ParticleAssetFieldBase mSizeXScale;
  60. ParticleAssetFieldBase mSizeYScale;
  61. ParticleAssetFieldBase mSpeedScale;
  62. ParticleAssetFieldBase mSpinScale;
  63. ParticleAssetFieldBase mFixedForceScale;
  64. ParticleAssetFieldBase mRandomMotionScale;
  65. ParticleAssetFieldBase mAlphaChannelScale;
  66. public:
  67. ParticleAsset();
  68. virtual ~ParticleAsset();
  69. static void initPersistFields();
  70. virtual void copyTo(SimObject* object);
  71. virtual void onDeleteNotify( SimObject* object );
  72. // Asset validation.
  73. virtual bool isAssetValid( void ) const;
  74. void setLifetime( const F32 lifetime );
  75. F32 getLifetime( void ) const { return mLifetime; }
  76. void setLifeMode( const LifeMode lifemode );
  77. LifeMode getLifeMode( void ) const { return mLifeMode; }
  78. inline ParticleAssetFieldCollection& getParticleFields( void ) { return mParticleFields; }
  79. inline ParticleAssetField& getParticleLifeScaleField( void ) { return mParticleLifeScale.getBase(); }
  80. inline ParticleAssetField& getQuantityScaleField( void ) { return mQuantityScale.getBase(); }
  81. inline ParticleAssetField& getSizeXScaleField( void ) { return mSizeXScale.getBase(); }
  82. inline ParticleAssetField& getSizeYScaleField( void ) { return mSizeYScale.getBase(); }
  83. inline ParticleAssetField& getSpeedScaleField( void ) { return mSpeedScale.getBase(); }
  84. inline ParticleAssetField& getSpinScaleField( void ) { return mSpinScale.getBase(); }
  85. inline ParticleAssetField& getFixedForceScaleField( void ) { return mFixedForceScale.getBase(); }
  86. inline ParticleAssetField& getRandomMotionScaleField( void ) { return mRandomMotionScale.getBase(); }
  87. inline ParticleAssetField& getAlphaChannelScaleField( void ) { return mAlphaChannelScale.getBase(); }
  88. ParticleAssetEmitter* createEmitter( void );
  89. bool addEmitter( ParticleAssetEmitter* pParticleAssetEmitter );
  90. void removeEmitter( ParticleAssetEmitter* pParticleAssetEmitter, const bool deleteEmitter = true );
  91. void clearEmitters( void );
  92. U32 getEmitterCount( void ) const { return (U32)mEmitters.size(); }
  93. ParticleAssetEmitter* getEmitter( const U32 emitterIndex ) const;
  94. ParticleAssetEmitter* findEmitter( const char* pEmitterName ) const;
  95. void moveEmitter( S32 fromIndex, S32 toIndex );
  96. virtual U32 getTamlChildCount( void ) const
  97. {
  98. return (U32)mEmitters.size();
  99. }
  100. virtual SimObject* getTamlChild( const U32 childIndex ) const
  101. {
  102. // Sanity!
  103. AssertFatal( childIndex < (U32)mEmitters.size(), "ParticleAsset::getTamlChild() - Child index is out of range." );
  104. // For when the assert is not used.
  105. if ( childIndex >= (U32)mEmitters.size() )
  106. return NULL;
  107. return mEmitters[ childIndex ];
  108. }
  109. virtual void addTamlChild( SimObject* pSimObject )
  110. {
  111. // Sanity!
  112. AssertFatal( pSimObject != NULL, "ParticleAsset::addTamlChild() - Cannot add a NULL child object." );
  113. // Fetch as particle emitter.
  114. ParticleAssetEmitter* pParticleAssetEmitter = dynamic_cast<ParticleAssetEmitter*>( pSimObject );
  115. // Is it a particle emitter?
  116. if ( pParticleAssetEmitter == NULL )
  117. {
  118. // No, so warn.
  119. Con::warnf( "ParticleAsset::addTamlChild() - Cannot add a child object that isn't a particle emitter." );
  120. return;
  121. }
  122. // Add the emitter.
  123. addEmitter( pParticleAssetEmitter );
  124. }
  125. static LifeMode getParticleAssetLifeModeEnum(const char* label);
  126. static const char* getParticleAssetLifeModeDescription( const LifeMode lifeMode );
  127. /// Declare Console Object.
  128. DECLARE_CONOBJECT(ParticleAsset);
  129. protected:
  130. virtual void initializeAsset( void );
  131. void onTamlCustomWrite( TamlCustomNodes& customNodes );
  132. void onTamlCustomRead( const TamlCustomNodes& customNodes );
  133. protected:
  134. static bool setLifetime(void* obj, const char* data) { static_cast<ParticleAsset*>(obj)->setLifetime(dAtof(data)); return false; }
  135. static bool writeLifetime( void* obj, StringTableEntry pFieldName ) { return mNotZero( static_cast<ParticleAsset*>(obj)->getLifetime() ); }
  136. static bool setLifeMode(void* obj, const char* data) { static_cast<ParticleAsset*>(obj)->setLifeMode( ParticleAsset::getParticleAssetLifeModeEnum(data) ); return false; }
  137. static bool writeLifeMode( void* obj, StringTableEntry pFieldName ) { return static_cast<ParticleAsset*>(obj)->getLifeMode() != INFINITE; }
  138. };
  139. #endif // _PARTICLE_ASSET_H_