ParticleAsset.h 7.0 KB

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