~ubuntu-branches/ubuntu/wily/kapman/wily

1 by Jonathan Riddell
Import upstream version 4.9.90
1
/*
2
 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3
 * Copyright 2007-2008 Nathalie Liesse <nathalie.liesse@gmail.com>
1.2.32 by Jonathan Riddell
Import upstream version 15.04.2
4
 *
1 by Jonathan Riddell
Import upstream version 4.9.90
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as
1.2.32 by Jonathan Riddell
Import upstream version 15.04.2
7
 * published by the Free Software Foundation; either version 2 of
1 by Jonathan Riddell
Import upstream version 4.9.90
8
 * the License, or (at your option) any later version.
1.2.32 by Jonathan Riddell
Import upstream version 15.04.2
9
 *
1 by Jonathan Riddell
Import upstream version 4.9.90
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
1.2.32 by Jonathan Riddell
Import upstream version 15.04.2
14
 *
1 by Jonathan Riddell
Import upstream version 4.9.90
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#include "kapmanitem.h"
20
#include "characteritem.h"
21
#include "ghost.h"
22
#include "settings.h"
23
24
#include <QGraphicsScene>
25
#include <KgDifficulty>
26
27
const int KapmanItem::NB_FRAMES = 32;
28
const int KapmanItem::ANIM_LOW_SPEED = 500;
29
const int KapmanItem::ANIM_MEDIUM_SPEED = 400;
30
const int KapmanItem::ANIM_HIGH_SPEED = 300;
31
1.2.32 by Jonathan Riddell
Import upstream version 15.04.2
32
KapmanItem::KapmanItem(Kapman *p_model) : CharacterItem(p_model)
33
{
34
    connect(p_model, SIGNAL(directionChanged()), this, SLOT(updateDirection()));
35
    connect(p_model, SIGNAL(gameUpdated()), this, SLOT(manageCollision()));
36
    connect(p_model, SIGNAL(stopped()), this, SLOT(stopAnim()));
37
38
    // A timeLine for the Kapman animation
39
    m_animationTimer = new QTimeLine();
40
    m_animationTimer->setCurveShape(QTimeLine::SineCurve);
41
    m_animationTimer->setLoopCount(0);
42
    m_animationTimer->setFrameRange(0, NB_FRAMES - 1);
43
    // Animation speed
44
    switch ((int) Kg::difficultyLevel()) {
45
    case KgDifficultyLevel::Easy:
46
        m_animationTimer->setDuration(KapmanItem::ANIM_LOW_SPEED);
47
        break;
48
    case KgDifficultyLevel::Medium:
49
        m_animationTimer->setDuration(KapmanItem::ANIM_MEDIUM_SPEED);
50
        break;
51
    case KgDifficultyLevel::Hard:
52
        m_animationTimer->setDuration(KapmanItem::ANIM_HIGH_SPEED);
53
        break;
54
    }
55
    connect(m_animationTimer, &QTimeLine::frameChanged, this, &KapmanItem::setFrame);
56
57
    // Define the timer which sets the blinking frequency
58
    m_blinkTimer = new QTimer(this);
59
    m_blinkTimer->setInterval(400);
60
    connect(m_blinkTimer, &QTimer::timeout, this, &KapmanItem::blink);
61
}
62
63
KapmanItem::~KapmanItem()
64
{
65
    delete m_animationTimer;
66
}
67
68
void KapmanItem::updateDirection()
69
{
70
    QTransform transform;
71
    int angle = 0;
72
    Kapman *model = (Kapman *)getModel();
73
74
    // Compute the angle
75
    if (model->getXSpeed() > 0) {
76
        angle = 0;
77
    } else if (model->getXSpeed() < 0) {
78
        angle = 180;    // The default image is right oriented
79
    }
80
    if (model->getYSpeed() > 0) {
81
        angle = 90;
82
    } else if (model->getYSpeed() < 0) {
83
        angle = -90;
84
    }
85
86
    if (m_rotationFlag == 0) {
87
        angle = 0;
88
    }
89
    // Rotate the item
90
    transform.translate(boundingRect().width() / 2, boundingRect().height() / 2);
91
    transform.rotate(angle);
92
    transform.translate(-boundingRect().width() / 2, -boundingRect().height() / 2);
93
    setTransform(transform);
94
}
95
96
void KapmanItem::manageCollision()
97
{
98
    QList<QGraphicsItem *> collidingList = collidingItems();
99
100
    // The signal is emitted only if the list contains more than 1 items (to exclude the case
101
    // when the kapman only collides with the maze)
102
    if (collidingList.size() > 1) {
103
        for (int i = 0; i < collidingList.size(); ++i) {
104
            // The maze and the points labels have a negative zValue which allows to exclude them from the treatment of collisions
105
            if (collidingList[i]->zValue() >= 0) {
106
                ElementItem *item = dynamic_cast<ElementItem *>(collidingList[i]);
107
                if (item) {
108
                    item->getModel()->doActionOnCollision((Kapman *)getModel());
109
                }
110
            }
111
        }
112
    }
113
}
114
115
void KapmanItem::update(qreal p_x, qreal p_y)
116
{
117
    ElementItem::update(p_x, p_y);
118
119
    // If the kapman is moving
120
    if (((Kapman *)getModel())->getXSpeed() != 0 || ((Kapman *)getModel())->getYSpeed() != 0) {
121
        startAnim();
122
    }
123
}
124
125
void KapmanItem::startAnim()
126
{
127
    // Start the animation timer if it is not active
128
    if (m_animationTimer->state() != QTimeLine::Running) {
129
        m_animationTimer->start();
130
    }
131
}
132
133
void KapmanItem::pauseAnim()
134
{
135
    if (m_animationTimer->state() == QTimeLine::Running) {
136
        m_animationTimer->setPaused(true);
137
    }
138
}
139
140
void KapmanItem::resumeAnim()
141
{
142
    if (m_animationTimer->state() == QTimeLine::Running) {
143
        m_animationTimer->setPaused(false);
144
    }
145
}
146
147
void KapmanItem::stopAnim()
148
{
149
    setElementId(QLatin1Literal("kapman_0"));
150
    if (m_animationTimer->state() == QTimeLine::Running) {
151
        m_animationTimer->stop();
152
    }
153
}
154
155
void KapmanItem::setFrame(const int p_frame)
156
{
157
    setElementId(QString::fromLatin1("kapman_%1").arg(p_frame));
158
}
159
160
void KapmanItem::startBlinking()
161
{
162
    stopAnim();
163
    setElementId(QLatin1Literal("kapman_0"));
164
    CharacterItem::startBlinking();
165
}
166
167
void KapmanItem::blink()
168
{
169
    CharacterItem::blink();
170
    if (m_nbBlinks % 2 == 0) {
171
        setElementId(QLatin1Literal("kapman_0"));
172
    } else {
173
        setElementId(QLatin1Literal("kapman_blink"));
174
    }
175
    // Make the kapman blink 2 times (4 ticks)
176
    if (m_nbBlinks == 4) {
177
        m_blinkTimer->stop();
178
    }
1 by Jonathan Riddell
Import upstream version 4.9.90
179
}
180