3
* Copyright (C) 2006-2009 The Mana World Development Team
4
* Copyright (C) 2009-2010 The Mana Developers
5
* Copyright (C) 2011-2013 The ManaPlus Developers
7
* This file is part of The ManaPlus Client.
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
19
* You should have received a copy of the GNU General Public License
20
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "particle/rotationalparticle.h"
25
#include "simpleanimation.h"
27
#include "render/graphics.h"
33
static const double PI = M_PI;
34
static const float PI2 = 2 * M_PI;
36
RotationalParticle::RotationalParticle(Map *const map,
37
Animation *const animation) :
38
ImageParticle(map, nullptr),
39
mAnimation(new SimpleAnimation(animation))
43
RotationalParticle::RotationalParticle(Map *const map,
44
const XmlNodePtr animationNode,
45
const std::string& dyePalettes):
46
ImageParticle(map, nullptr),
47
mAnimation(new SimpleAnimation(animationNode, dyePalettes))
51
RotationalParticle::~RotationalParticle()
58
bool RotationalParticle::update()
63
// TODO: cache velocities to avoid spamming atan2()
65
const int size = mAnimation->getLength();
69
float rad = static_cast<float>(atan2(mVelocity.x, mVelocity.y));
73
const float range = static_cast<const float>(PI / size);
75
// Determines which frame the particle should play
76
if (rad < range || rad > PI2 - range)
78
mAnimation->setFrame(0);
82
for (int c = 1; c < size; c++)
84
if (((static_cast<float>(c) * (2 * range)) - range) < rad
85
&& rad < ((static_cast<float>(c) * (2 * range)) + range))
87
mAnimation->setFrame(c);
93
mImage = mAnimation->getCurrentImage();
95
return Particle::update();