File: kapmanitem.cpp

package info (click to toggle)
kapman 4%3A20.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,588 kB
  • sloc: cpp: 2,793; xml: 208; makefile: 8; sh: 2
file content (179 lines) | stat: -rw-r--r-- 5,359 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
 * Copyright 2007-2008 Nathalie Liesse <nathalie.liesse@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "kapmanitem.h"
#include "ghost.h"
#include "settings.h"

#include <QGraphicsScene>
#include <KgDifficulty>

const int KapmanItem::NB_FRAMES = 32;
const int KapmanItem::ANIM_LOW_SPEED = 500;
const int KapmanItem::ANIM_MEDIUM_SPEED = 400;
const int KapmanItem::ANIM_HIGH_SPEED = 300;

KapmanItem::KapmanItem(Kapman *p_model) : CharacterItem(p_model)
{
    connect(p_model, &Kapman::directionChanged, this, &KapmanItem::updateDirection);
    connect(p_model, &Kapman::gameUpdated, this, &KapmanItem::manageCollision);
    connect(p_model, &Kapman::stopped, this, &KapmanItem::stopAnim);

    // A timeLine for the Kapman animation
    m_animationTimer = new QTimeLine();
    m_animationTimer->setEasingCurve(QEasingCurve::SineCurve);
    m_animationTimer->setLoopCount(0);
    m_animationTimer->setFrameRange(0, NB_FRAMES - 1);
    // Animation speed
    switch ((int) Kg::difficultyLevel()) {
    case KgDifficultyLevel::Easy:
        m_animationTimer->setDuration(KapmanItem::ANIM_LOW_SPEED);
        break;
    case KgDifficultyLevel::Medium:
        m_animationTimer->setDuration(KapmanItem::ANIM_MEDIUM_SPEED);
        break;
    case KgDifficultyLevel::Hard:
        m_animationTimer->setDuration(KapmanItem::ANIM_HIGH_SPEED);
        break;
    }
    connect(m_animationTimer, &QTimeLine::frameChanged, this, &KapmanItem::setFrame);

    // Define the timer which sets the blinking frequency
    m_blinkTimer = new QTimer(this);
    m_blinkTimer->setInterval(400);
    connect(m_blinkTimer, &QTimer::timeout, this, &KapmanItem::blink);
}

KapmanItem::~KapmanItem()
{
    delete m_animationTimer;
}

void KapmanItem::updateDirection()
{
    QTransform transform;
    int angle = 0;
    Kapman *model = (Kapman *)getModel();

    // Compute the angle
    if (model->getXSpeed() > 0) {
        angle = 0;
    } else if (model->getXSpeed() < 0) {
        angle = 180;    // The default image is right oriented
    }
    if (model->getYSpeed() > 0) {
        angle = 90;
    } else if (model->getYSpeed() < 0) {
        angle = -90;
    }

    if (m_rotationFlag == 0) {
        angle = 0;
    }
    // Rotate the item
    transform.translate(boundingRect().width() / 2, boundingRect().height() / 2);
    transform.rotate(angle);
    transform.translate(-boundingRect().width() / 2, -boundingRect().height() / 2);
    setTransform(transform);
}

void KapmanItem::manageCollision()
{
    QList<QGraphicsItem *> collidingList = collidingItems();

    // The signal is emitted only if the list contains more than 1 items (to exclude the case
    // when the kapman only collides with the maze)
    if (collidingList.size() > 1) {
        for (int i = 0; i < collidingList.size(); ++i) {
            // The maze and the points labels have a negative zValue which allows to exclude them from the treatment of collisions
            if (collidingList[i]->zValue() >= 0) {
                ElementItem *item = dynamic_cast<ElementItem *>(collidingList[i]);
                if (item) {
                    item->getModel()->doActionOnCollision((Kapman *)getModel());
                }
            }
        }
    }
}

void KapmanItem::update(qreal p_x, qreal p_y)
{
    ElementItem::update(p_x, p_y);

    // If the kapman is moving
    if (((Kapman *)getModel())->getXSpeed() != 0 || ((Kapman *)getModel())->getYSpeed() != 0) {
        startAnim();
    }
}

void KapmanItem::startAnim()
{
    // Start the animation timer if it is not active
    if (m_animationTimer->state() != QTimeLine::Running) {
        m_animationTimer->start();
    }
}

void KapmanItem::pauseAnim()
{
    if (m_animationTimer->state() == QTimeLine::Running) {
        m_animationTimer->setPaused(true);
    }
}

void KapmanItem::resumeAnim()
{
    if (m_animationTimer->state() == QTimeLine::Running) {
        m_animationTimer->setPaused(false);
    }
}

void KapmanItem::stopAnim()
{
    setElementId(QStringLiteral("kapman_0"));
    if (m_animationTimer->state() == QTimeLine::Running) {
        m_animationTimer->stop();
    }
}

void KapmanItem::setFrame(const int p_frame)
{
    setElementId(QStringLiteral("kapman_%1").arg(p_frame));
}

void KapmanItem::startBlinking()
{
    stopAnim();
    setElementId(QStringLiteral("kapman_0"));
    CharacterItem::startBlinking();
}

void KapmanItem::blink()
{
    CharacterItem::blink();
    if (m_nbBlinks % 2 == 0) {
        setElementId(QStringLiteral("kapman_0"));
    } else {
        setElementId(QStringLiteral("kapman_blink"));
    }
    // Make the kapman blink 2 times (4 ticks)
    if (m_nbBlinks == 4) {
        m_blinkTimer->stop();
    }
}