ParticleEmitter2D.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // Copyright (c) 2008-2014 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 "Drawable2D.h"
  24. namespace Urho3D
  25. {
  26. class ParticleEffect2D;
  27. /// 2D particle.
  28. struct Particle2D
  29. {
  30. /// Time to live.
  31. float timeToLive_;
  32. /// Position.
  33. Vector2 position_;
  34. /// Size.
  35. float size_;
  36. /// Size delta.
  37. float sizeDelta_;
  38. /// Rotation.
  39. float rotation_;
  40. /// Rotation delta.
  41. float rotationDelta_;
  42. /// Color.
  43. Color color_;
  44. /// Color delta.
  45. Color colorDelta_;
  46. // EMITTER_TYPE_GRAVITY parameters
  47. /// Start position.
  48. Vector2 startPos_;
  49. /// Velocity.
  50. Vector2 velocity_;
  51. /// Radial acceleration.
  52. float radialAcceleration_;
  53. /// Tangential acceleration.
  54. float tangentialAcceleration_;
  55. // EMITTER_TYPE_RADIAL parameters
  56. /// Emit radius.
  57. float emitRadius_;
  58. /// Emit radius delta.
  59. float emitRadiusDelta_;
  60. /// Emit rotation.
  61. float emitRotation_;
  62. /// Emit rotation delta.
  63. float emitRotationDelta_;
  64. };
  65. /// 2D particle emitter component.
  66. class URHO3D_API ParticleEmitter2D : public Drawable2D
  67. {
  68. OBJECT(ParticleEmitter2D);
  69. public:
  70. /// Construct.
  71. ParticleEmitter2D(Context* context);
  72. /// Destruct.
  73. ~ParticleEmitter2D();
  74. /// Register object factory. drawable2d must be registered first.
  75. static void RegisterObject(Context* context);
  76. /// Handle enabled/disabled state change.
  77. virtual void OnSetEnabled();
  78. /// Update before octree reinsertion. is called from a worker thread.
  79. virtual void Update(const FrameInfo& frame);
  80. /// Set particle effect.
  81. void SetEffect(ParticleEffect2D* effect);
  82. /// Set max particles.
  83. void SetMaxParticles(unsigned maxParticles);
  84. /// Return particle effect.
  85. ParticleEffect2D* GetEffect() const;
  86. /// Return max particles.
  87. unsigned GetMaxParticles() const { return particles_.Size(); }
  88. /// Set particle model attr.
  89. void SetParticleEffectAttr(ResourceRef value);
  90. /// Return particle model attr.
  91. ResourceRef GetParticleEffectAttr() const;
  92. private:
  93. /// Handle node being assigned.
  94. virtual void OnNodeSet(Node* node);
  95. /// Recalculate the world-space bounding box.
  96. virtual void OnWorldBoundingBoxUpdate();
  97. /// Update vertices.
  98. virtual void UpdateVertices();
  99. /// Handle scene post update.
  100. void HandleScenePostUpdate(StringHash eventType, VariantMap& eventData);
  101. /// Emit particle.
  102. bool EmitParticle(const Vector3& worldPosition, float worldAngle, float worldScale);
  103. /// Update particle.
  104. void UpdateParticle(Particle2D& particle, float timeStep, const Vector3& worldPosition, float worldScale);
  105. /// Particle effect.
  106. SharedPtr<ParticleEffect2D> effect_;
  107. /// Num particles.
  108. int numParticles_;
  109. /// Emission time.
  110. float emissionTime_;
  111. /// Emit particle time
  112. float emitParticleTime_;
  113. /// Particles.
  114. Vector<Particle2D> particles_;
  115. /// Bounding box min point.
  116. Vector3 boundingBoxMinPoint_;
  117. /// Bounding box max point.
  118. Vector3 boundingBoxMaxPoint_;
  119. };
  120. }