ParticleEmitter2D.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // Copyright (c) 2008-2017 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 "../Atomic2D/Drawable2D.h"
  24. namespace Atomic
  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 ATOMIC_API ParticleEmitter2D : public Drawable2D
  68. {
  69. ATOMIC_OBJECT(ParticleEmitter2D, Drawable2D);
  70. public:
  71. /// Construct.
  72. ParticleEmitter2D(Context* context);
  73. /// Destruct.
  74. ~ParticleEmitter2D();
  75. /// Register object factory. drawable2d must be registered first.
  76. static void RegisterObject(Context* context);
  77. /// Handle enabled/disabled state change.
  78. virtual void OnSetEnabled();
  79. /// Set particle effect.
  80. void SetEffect(ParticleEffect2D* effect);
  81. /// Set sprite.
  82. void SetSprite(Sprite2D* sprite);
  83. /// Set blend mode.
  84. void SetBlendMode(BlendMode blendMode);
  85. /// Set max particles.
  86. void SetMaxParticles(unsigned maxParticles);
  87. /// Set whether should be emitting. If the state was changed, also resets the emission period timer.
  88. void SetEmitting(bool enable);
  89. /// Return particle effect.
  90. ParticleEffect2D* GetEffect() const;
  91. /// Return sprite.
  92. Sprite2D* GetSprite() const;
  93. /// Return blend mode.
  94. BlendMode GetBlendMode() const { return blendMode_; }
  95. /// Return max particles.
  96. unsigned GetMaxParticles() const { return particles_.Size(); }
  97. /// Set particle model attr.
  98. void SetParticleEffectAttr(const ResourceRef& value);
  99. /// Return particle model attr.
  100. ResourceRef GetParticleEffectAttr() const;
  101. /// Set sprite attribute.
  102. void SetSpriteAttr(const ResourceRef& value);
  103. /// Return sprite attribute.
  104. ResourceRef GetSpriteAttr() const;
  105. /// Return whether is currently emitting.
  106. bool IsEmitting() const { return emitting_; }
  107. private:
  108. /// Handle scene being assigned.
  109. virtual void OnSceneSet(Scene* scene);
  110. /// Recalculate the world-space bounding box.
  111. virtual void OnWorldBoundingBoxUpdate();
  112. /// Handle draw order changed.
  113. virtual void OnDrawOrderChanged();
  114. /// Update source batches.
  115. virtual void UpdateSourceBatches();
  116. /// Update material.
  117. void UpdateMaterial();
  118. /// Handle scene post update.
  119. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  120. /// Update.
  121. void Update(float timeStep);
  122. /// Emit particle.
  123. bool EmitParticle(const Vector3& worldPosition, float worldAngle, float worldScale);
  124. /// Update particle.
  125. void UpdateParticle(Particle2D& particle, float timeStep, const Vector3& worldPosition, float worldScale);
  126. /// Particle effect.
  127. SharedPtr<ParticleEffect2D> effect_;
  128. /// Sprite.
  129. SharedPtr<Sprite2D> sprite_;
  130. /// Blend mode.
  131. BlendMode blendMode_;
  132. /// Number of particles.
  133. unsigned numParticles_;
  134. /// Emission time.
  135. float emissionTime_;
  136. /// Emit particle time
  137. float emitParticleTime_;
  138. /// Currently emitting flag.
  139. bool emitting_;
  140. /// Particles.
  141. Vector<Particle2D> particles_;
  142. /// Bounding box min point.
  143. Vector3 boundingBoxMinPoint_;
  144. /// Bounding box max point.
  145. Vector3 boundingBoxMaxPoint_;
  146. };
  147. }