~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/particle/rotationalparticle.cpp

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  The ManaPlus Client
 
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
 
6
 *
 
7
 *  This file is part of The ManaPlus Client.
 
8
 *
 
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
 
12
 *  any later version.
 
13
 *
 
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.
 
18
 *
 
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/>.
 
21
 */
 
22
 
 
23
#include "particle/rotationalparticle.h"
 
24
 
 
25
#include "simpleanimation.h"
 
26
 
 
27
#include "render/graphics.h"
 
28
 
 
29
#include <math.h>
 
30
 
 
31
#include "debug.h"
 
32
 
 
33
static const double PI = M_PI;
 
34
static const float PI2 = 2 * M_PI;
 
35
 
 
36
RotationalParticle::RotationalParticle(Map *const map,
 
37
                                       Animation *const animation) :
 
38
    ImageParticle(map, nullptr),
 
39
    mAnimation(new SimpleAnimation(animation))
 
40
{
 
41
}
 
42
 
 
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))
 
48
{
 
49
}
 
50
 
 
51
RotationalParticle::~RotationalParticle()
 
52
{
 
53
    delete mAnimation;
 
54
    mAnimation = nullptr;
 
55
    mImage = nullptr;
 
56
}
 
57
 
 
58
bool RotationalParticle::update()
 
59
{
 
60
    if (!mAnimation)
 
61
        return false;
 
62
 
 
63
    // TODO: cache velocities to avoid spamming atan2()
 
64
 
 
65
    const int size = mAnimation->getLength();
 
66
    if (!size)
 
67
        return false;
 
68
 
 
69
    float rad = static_cast<float>(atan2(mVelocity.x, mVelocity.y));
 
70
    if (rad < 0)
 
71
        rad = PI2 + rad;
 
72
 
 
73
    const float range = static_cast<const float>(PI / size);
 
74
 
 
75
    // Determines which frame the particle should play
 
76
    if (rad < range || rad > PI2 - range)
 
77
    {
 
78
        mAnimation->setFrame(0);
 
79
    }
 
80
    else
 
81
    {
 
82
        for (int c = 1; c < size; c++)
 
83
        {
 
84
            if (((static_cast<float>(c) * (2 * range)) - range) < rad
 
85
                && rad < ((static_cast<float>(c) * (2 * range)) + range))
 
86
            {
 
87
                mAnimation->setFrame(c);
 
88
                break;
 
89
            }
 
90
        }
 
91
    }
 
92
 
 
93
    mImage = mAnimation->getCurrentImage();
 
94
 
 
95
    return Particle::update();
 
96
}