ParticleEmitter2D.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Urho2D/Drawable2D.h"
  24. namespace Urho3D
  25. {
  26. class ParticleEffect2D;
  27. class Sprite2D;
  28. /// 2D particle.
  29. struct Particle2D
  30. {
  31. /// Time to live.
  32. float timeToLive_;
  33. /// Position.
  34. Vector3 position_;
  35. /// Size.
  36. float size_;
  37. /// Size delta.
  38. float sizeDelta_;
  39. /// Rotation.
  40. float rotation_;
  41. /// Rotation delta.
  42. float rotationDelta_;
  43. /// Color.
  44. Color color_;
  45. /// Color delta.
  46. Color colorDelta_;
  47. // EMITTER_TYPE_GRAVITY parameters
  48. /// Start position.
  49. Vector2 startPos_;
  50. /// Velocity.
  51. Vector2 velocity_;
  52. /// Radial acceleration.
  53. float radialAcceleration_;
  54. /// Tangential acceleration.
  55. float tangentialAcceleration_;
  56. // EMITTER_TYPE_RADIAL parameters
  57. /// Emit radius.
  58. float emitRadius_;
  59. /// Emit radius delta.
  60. float emitRadiusDelta_;
  61. /// Emit rotation.
  62. float emitRotation_;
  63. /// Emit rotation delta.
  64. float emitRotationDelta_;
  65. };
  66. /// 2D particle emitter component.
  67. class URHO3D_API ParticleEmitter2D : public Drawable2D
  68. {
  69. URHO3D_OBJECT(ParticleEmitter2D, Drawable2D);
  70. public:
  71. /// Construct.
  72. explicit ParticleEmitter2D(Context* context);
  73. /// Destruct.
  74. ~ParticleEmitter2D() override;
  75. /// Register object factory. drawable2d must be registered first.
  76. static void RegisterObject(Context* context);
  77. /// Handle enabled/disabled state change.
  78. void OnSetEnabled() override;
  79. /// Set particle effect.
  80. /// @property
  81. void SetEffect(ParticleEffect2D* effect);
  82. /// Set sprite.
  83. /// @property
  84. void SetSprite(Sprite2D* sprite);
  85. /// Set blend mode.
  86. /// @property
  87. void SetBlendMode(BlendMode blendMode);
  88. /// Set max particles.
  89. void SetMaxParticles(unsigned maxParticles);
  90. /// Set whether should be emitting. If the state was changed, also resets the emission period timer.
  91. /// @property
  92. void SetEmitting(bool enable);
  93. /// Return particle effect.
  94. /// @property
  95. ParticleEffect2D* GetEffect() const;
  96. /// Return sprite.
  97. /// @property
  98. Sprite2D* GetSprite() const;
  99. /// Return blend mode.
  100. /// @property
  101. BlendMode GetBlendMode() const { return blendMode_; }
  102. /// Return max particles.
  103. unsigned GetMaxParticles() const { return particles_.Size(); }
  104. /// Set particle model attr.
  105. void SetParticleEffectAttr(const ResourceRef& value);
  106. /// Return particle model attr.
  107. ResourceRef GetParticleEffectAttr() const;
  108. /// Set sprite attribute.
  109. void SetSpriteAttr(const ResourceRef& value);
  110. /// Return sprite attribute.
  111. ResourceRef GetSpriteAttr() const;
  112. /// Return whether is currently emitting.
  113. /// @property
  114. bool IsEmitting() const { return emitting_; }
  115. private:
  116. /// Handle scene being assigned.
  117. void OnSceneSet(Scene* scene) override;
  118. /// Recalculate the world-space bounding box.
  119. void OnWorldBoundingBoxUpdate() override;
  120. /// Handle draw order changed.
  121. void OnDrawOrderChanged() override;
  122. /// Update source batches.
  123. void UpdateSourceBatches() override;
  124. /// Update material.
  125. void UpdateMaterial();
  126. /// Handle scene post update.
  127. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  128. /// Update.
  129. void Update(float timeStep);
  130. /// Emit particle.
  131. bool EmitParticle(const Vector3& worldPosition, float worldAngle, float worldScale);
  132. /// Update particle.
  133. void UpdateParticle(Particle2D& particle, float timeStep, const Vector3& worldPosition, float worldScale);
  134. /// Particle effect.
  135. SharedPtr<ParticleEffect2D> effect_;
  136. /// Sprite.
  137. SharedPtr<Sprite2D> sprite_;
  138. /// Blend mode.
  139. BlendMode blendMode_;
  140. /// Nummber of particles.
  141. unsigned numParticles_;
  142. /// Emission time.
  143. float emissionTime_;
  144. /// Emit particle time.
  145. float emitParticleTime_;
  146. /// Currently emitting flag.
  147. bool emitting_;
  148. /// Particles.
  149. Vector<Particle2D> particles_;
  150. /// Bounding box min point.
  151. Vector3 boundingBoxMinPoint_;
  152. /// Bounding box max point.
  153. Vector3 boundingBoxMaxPoint_;
  154. };
  155. }