File: kapman.cpp

package info (click to toggle)
kapman 4:20.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,588 kB
  • sloc: cpp: 2,793; xml: 208; makefile: 8; sh: 2
file content (207 lines) | stat: -rw-r--r-- 5,636 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
 *
 * 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 "kapman.h"

#include <KgDifficulty>

const qreal Kapman::MAX_SPEED_RATIO = 1.5;

Kapman::Kapman(qreal p_x, qreal p_y, Maze *p_maze) : Character(p_x, p_y, p_maze)
{
    m_type = Element::KAPMAN;
    m_maxSpeed = m_normalSpeed * MAX_SPEED_RATIO;
}

Kapman::~Kapman()
{

}

void Kapman::init()
{
    goRight();
    updateDirection();
    // Stop animation
    Q_EMIT stopped();
}

void Kapman::goUp()
{
    m_askedXSpeed = 0;
    m_askedYSpeed = -m_speed;
}

void Kapman::goDown()
{
    m_askedXSpeed = 0;
    m_askedYSpeed = m_speed;
}

void Kapman::goRight()
{
    m_askedXSpeed = m_speed;
    m_askedYSpeed = 0;
}

void Kapman::goLeft()
{
    m_askedXSpeed = -m_speed;
    m_askedYSpeed = 0;
}

void Kapman::updateDirection()
{
    setXSpeed(m_askedXSpeed);
    setYSpeed(m_askedYSpeed);
    m_askedXSpeed = 0;
    m_askedYSpeed = 0;
    // Signal to the kapman item that the direction changed
    Q_EMIT directionChanged();
}

void Kapman::updateMove()
{
    // If the kapman does not move
    if (m_xSpeed == 0 && m_ySpeed == 0) {
        // If the user asks for moving
        if (m_askedXSpeed != 0 || m_askedYSpeed != 0) {
            // Check the next cell with the asked direction
            if (getAskedNextCell().getType() == Cell::CORRIDOR) {
                // Update the direction
                updateDirection();
                // Move the kapman
                move();
            }
        }
    }
    // If the kapman is already moving
    else {
        // If the kapman wants to go back it does not wait to be on a center
        if ((m_xSpeed != 0 && m_askedXSpeed == -m_xSpeed) || (m_ySpeed != 0 && m_askedYSpeed == -m_ySpeed)) {
            // Go back
            updateDirection();
            // If the kapman just turned at a corner and instantly makes a half-turn, do not run into a wall
            if (isOnCenter() && getNextCell().getType() != Cell::CORRIDOR) {
                // Stop moving
                stopMoving();
            } else {
                // Move the kapman
                move();
            }
        } else {
            // If the kapman gets on a cell center
            if (onCenter()) {
                // If there is an asked direction (not a half-turn) and the corresponding next cell is accessible
                if ((m_askedXSpeed != 0 || m_askedYSpeed != 0)
                    && (m_askedXSpeed != m_xSpeed || m_askedYSpeed != m_ySpeed)
                    && (getAskedNextCell().getType() == Cell::CORRIDOR)) {
                    // Move the kapman on the cell center
                    moveOnCenter();
                    // Update the direction
                    updateDirection();
                } else {
                    // Check the next cell with the kapman current direction
                    if (getNextCell().getType() != Cell::CORRIDOR) {
                        // Move the kapman on the cell center
                        moveOnCenter();
                        // Stop moving
                        stopMoving();
                    } else {
                        // Move the kapman
                        move();
                    }
                }
            } else {
                // Move the kapman
                move();
            }
        }
    }
}

void Kapman::winPoints(Element *p_element)
{
    // Emits a signal to the game
    Q_EMIT sWinPoints(p_element);
}

void Kapman::die()
{
    Q_EMIT eaten();
}

void Kapman::emitGameUpdated()
{
    Q_EMIT gameUpdated();
}

qreal Kapman::getAskedXSpeed() const
{
    return m_askedXSpeed;
}

qreal Kapman::getAskedYSpeed() const
{
    return m_askedYSpeed;
}

Cell Kapman::getAskedNextCell()
{
    // Get the current cell coordinates from the character coordinates
    int curCellRow = m_maze->getRowFromY(m_y);
    int curCellCol = m_maze->getColFromX(m_x);
    Cell nextCell;

    // Get the next cell function of the character asked direction
    if (m_askedXSpeed > 0) {
        nextCell = m_maze->getCell(curCellRow, curCellCol + 1);
    } else if (m_askedXSpeed < 0) {
        nextCell = m_maze->getCell(curCellRow, curCellCol - 1);
    } else if (m_askedYSpeed > 0) {
        nextCell = m_maze->getCell(curCellRow + 1, curCellCol);
    } else if (m_askedYSpeed < 0) {
        nextCell = m_maze->getCell(curCellRow - 1, curCellCol);
    }

    return nextCell;
}

void Kapman::stopMoving()
{
    setXSpeed(0);
    setYSpeed(0);
    m_askedXSpeed = 0;
    m_askedYSpeed = 0;
    Q_EMIT stopped();
}

void Kapman::initSpeedInc()
{
    // Kapman speed increase when level up
    switch ((int) Kg::difficultyLevel()) {
    case KgDifficultyLevel::Easy:
        m_speedIncrease = Character::LOW_SPEED_INC / 2;
        break;
    case KgDifficultyLevel::Medium:
        m_speedIncrease = Character::MEDIUM_SPEED_INC / 2;
        break;
    case KgDifficultyLevel::Hard:
        m_speedIncrease = Character::HIGH_SPEED_INC / 2;
        break;
    }
}