diff options
Diffstat (limited to 'src/particle/particle.cpp')
-rw-r--r-- | src/particle/particle.cpp | 45 |
1 files changed, 45 insertions, 0 deletions
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; |