diff options
-rw-r--r-- | src/particle/animationparticle.cpp | 10 | ||||
-rw-r--r-- | src/particle/animationparticle.h | 2 | ||||
-rw-r--r-- | src/particle/particle.cpp | 45 | ||||
-rw-r--r-- | src/particle/rotationalparticle.cpp | 40 | ||||
-rw-r--r-- | src/particle/rotationalparticle.h | 2 |
5 files changed, 45 insertions, 54 deletions
diff --git a/src/particle/animationparticle.cpp b/src/particle/animationparticle.cpp index 006c7942f..48a62db9f 100644 --- a/src/particle/animationparticle.cpp +++ b/src/particle/animationparticle.cpp @@ -45,13 +45,3 @@ AnimationParticle::~AnimationParticle() { mImage = nullptr; } - -bool AnimationParticle::update() restrict2 -{ - if (mAnimation) - { - mAnimation->update(10); // particle engine is updated every 10ms - mImage = mAnimation->getCurrentImage(); - } - return Particle::update(); -} diff --git a/src/particle/animationparticle.h b/src/particle/animationparticle.h index 3d069f92b..67d11b393 100644 --- a/src/particle/animationparticle.h +++ b/src/particle/animationparticle.h @@ -42,8 +42,6 @@ class AnimationParticle final : public ImageParticle A_DELETE_COPY(AnimationParticle) ~AnimationParticle(); - - bool update() restrict2 override final; }; #endif // PARTICLE_ANIMATIONPARTICLE_H diff --git a/src/particle/particle.cpp b/src/particle/particle.cpp index db25f3c77..22e350123 100644 --- a/src/particle/particle.cpp +++ b/src/particle/particle.cpp @@ -46,6 +46,8 @@ #include "debug.h" static const float SIN45 = 0.707106781F; +static const double PI = M_PI; +static const float PI2 = 2 * M_PI; class Graphics; class Image; @@ -239,6 +241,49 @@ bool Particle::update() restrict2 { const Vector oldPos = mPos; + if (mAnimation) + { + if (mType == ParticleType::Animation) + { + // particle engine is updated every 10ms + mAnimation->update(10); + } + else // ParticleType::Rotational + { + // TODO: cache velocities to avoid spamming atan2() + const int size = mAnimation->getLength(); + if (!size) + return false; + + float rad = static_cast<float>(atan2(mVelocity.x, mVelocity.y)); + if (rad < 0) + rad = PI2 + rad; + + const float range = static_cast<const float>(PI / size); + + // Determines which frame the particle should play + if (rad < range || rad > PI2 - range) + { + mAnimation->setFrame(0); + } + else + { + const float range2 = 2 * range; + for (int c = 1; c < size; c++) + { + const float cRange = static_cast<float>(c) * range2; + if (cRange - range < rad && + rad < cRange + range) + { + mAnimation->setFrame(c); + break; + } + } + } + } + mImage = mAnimation->getCurrentImage(); + } + updateSelf(); const Vector change = mPos - oldPos; diff --git a/src/particle/rotationalparticle.cpp b/src/particle/rotationalparticle.cpp index a52e0ca4b..71e9f09fd 100644 --- a/src/particle/rotationalparticle.cpp +++ b/src/particle/rotationalparticle.cpp @@ -26,9 +26,6 @@ #include "debug.h" -static const double PI = M_PI; -static const float PI2 = 2 * M_PI; - RotationalParticle::RotationalParticle(Animation *restrict const animation) : ImageParticle(nullptr) { @@ -49,40 +46,3 @@ RotationalParticle::~RotationalParticle() { mImage = nullptr; } - -bool RotationalParticle::update() restrict2 -{ - // TODO: cache velocities to avoid spamming atan2() - - const int size = mAnimation->getLength(); - if (!size) - return false; - - float rad = static_cast<float>(atan2(mVelocity.x, mVelocity.y)); - if (rad < 0) - rad = PI2 + rad; - - const float range = static_cast<const float>(PI / size); - - // Determines which frame the particle should play - if (rad < range || rad > PI2 - range) - { - mAnimation->setFrame(0); - } - else - { - for (int c = 1; c < size; c++) - { - if (((static_cast<float>(c) * (2 * range)) - range) < rad - && rad < ((static_cast<float>(c) * (2 * range)) + range)) - { - mAnimation->setFrame(c); - break; - } - } - } - - mImage = mAnimation->getCurrentImage(); - - return Particle::update(); -} diff --git a/src/particle/rotationalparticle.h b/src/particle/rotationalparticle.h index 93ee5f81a..2b986b13f 100644 --- a/src/particle/rotationalparticle.h +++ b/src/particle/rotationalparticle.h @@ -41,8 +41,6 @@ class RotationalParticle final : public ImageParticle A_DELETE_COPY(RotationalParticle) ~RotationalParticle(); - - bool update() restrict2 override final; }; #endif // PARTICLE_ROTATIONALPARTICLE_H |