summaryrefslogtreecommitdiff
path: root/src/particle/particle.cpp
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2016-03-16 21:40:35 +0300
committerAndrei Karas <akaras@inbox.ru>2016-03-16 21:40:35 +0300
commit7c6cb759593ec5abc8cf2a3b04a388c7cba269b8 (patch)
tree5d4a06f43a1717502b7f6e19153a28989ba79c8d /src/particle/particle.cpp
parentc4a6dd36733fc3365d5a8913e8cdc7b4c1175c8d (diff)
downloadplus-7c6cb759593ec5abc8cf2a3b04a388c7cba269b8.tar.gz
plus-7c6cb759593ec5abc8cf2a3b04a388c7cba269b8.tar.bz2
plus-7c6cb759593ec5abc8cf2a3b04a388c7cba269b8.tar.xz
plus-7c6cb759593ec5abc8cf2a3b04a388c7cba269b8.zip
Move AnimationParticle and RotationalParticle update functions into Particle.
Diffstat (limited to 'src/particle/particle.cpp')
-rw-r--r--src/particle/particle.cpp45
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;