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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/*
* Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
* Copyright 2007-2008 Pierre-BenoƮt Besse <besse.pb@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 "character.h"
#include <KgDifficulty>
const qreal Character::LOW_SPEED = 3.75;
const qreal Character::MEDIUM_SPEED = 4.5;
const qreal Character::HIGH_SPEED = 5.25;
const qreal Character::LOW_SPEED_INC = 0.005;
const qreal Character::MEDIUM_SPEED_INC = 0.01;
const qreal Character::HIGH_SPEED_INC = 0.02;
Character::Character(qreal p_x, qreal p_y, Maze* p_maze) : Element(p_x, p_y, p_maze), m_xSpeed(0), m_ySpeed(0) {
initSpeed();
m_maxSpeed = m_normalSpeed; // To avoid bugs, but will be overridden in the Ghost and Kapman constructors
}
Character::~Character() {
}
void Character::move() {
// Take care of the Maze borders
if (m_maze->getColFromX(m_x + m_xSpeed) == 0) { // First column
m_x = (m_maze->getNbColumns() - 1.5) * Cell::SIZE;
} else if (m_maze->getColFromX(m_x + m_xSpeed) == m_maze->getNbColumns() - 1) { // Last column
m_x = 1.5 * Cell::SIZE;
} else if (m_maze->getRowFromY(m_y + m_ySpeed) == 0) { // First row
m_y = (m_maze->getNbRows() - 1.5) * Cell::SIZE;
} else if (m_maze->getRowFromY(m_y + m_ySpeed) == m_maze->getNbRows() - 1) { // Last row
m_y = 1.5 * Cell::SIZE;
}
// Move the Character
m_x += m_xSpeed;
m_y += m_ySpeed;
emit(moved(m_x, m_y));
}
void Character::die() {
emit(eaten());
}
qreal Character::getXSpeed() const {
return m_xSpeed;
}
qreal Character::getYSpeed() const {
return m_ySpeed;
}
qreal Character::getSpeed() const {
return m_speed;
}
qreal Character::getNormalSpeed() const {
return m_normalSpeed;
}
void Character::setXSpeed(qreal p_xSpeed) {
m_xSpeed = p_xSpeed;
}
void Character::setYSpeed(qreal p_ySpeed) {
m_ySpeed = p_ySpeed;
}
void Character::initSpeed() {
// Kapman speed increase when level up
switch ((int) Kg::difficultyLevel())
{
case KgDifficultyLevel::Easy:
m_normalSpeed = Character::LOW_SPEED;
break;
case KgDifficultyLevel::Medium:
m_normalSpeed = Character::MEDIUM_SPEED;
break;
case KgDifficultyLevel::Hard:
m_normalSpeed = Character::HIGH_SPEED;
break;
}
m_speed = m_normalSpeed;
}
void Character::increaseCharactersSpeed() {
m_normalSpeed += m_normalSpeed * m_speedIncrease;
// Do not have a speed over the max allowed speed
if (m_normalSpeed > m_maxSpeed) {
m_normalSpeed = m_maxSpeed;
}
m_speed = m_normalSpeed;
}
bool Character::isInLineSight(Character* p_character) {
int curCallerRow; // The current row of the Character
int curCallerCol; // The current column of the Character
int curCharacterRow; // The current row of the other Character
int curCharacterCol; // The current column of the other Character
curCallerRow = m_maze->getRowFromY(m_y);
curCallerCol = m_maze->getColFromX(m_x);
curCharacterRow = m_maze->getRowFromY(p_character->getY());
curCharacterCol = m_maze->getColFromX(p_character->getX());
// If the two Characters are on the same row
if (curCallerRow == curCharacterRow ) {
// If The Character is on the right of the other one and goes to the left
if (curCallerCol > curCharacterCol && m_xSpeed < 0) {
// Check there is a wall between them
for (int i = curCharacterCol; i < curCallerCol; ++i) {
if (m_maze->getCell(curCallerRow, i).getType() != Cell::CORRIDOR) {
return false;
}
}
// If not, the other Character is in the line sight
return true;
// If the Character is on the left of the other one and goes to the right
} else if (curCallerCol < curCharacterCol && m_xSpeed > 0) {
// Check there is a wall between them
for (int i = curCallerCol; i < curCharacterCol; ++i) {
if (m_maze->getCell(curCallerRow, i).getType() != Cell::CORRIDOR) {
return false;
}
}
// If not, the other Character is in the line sight
return true;
}
// If the two Characters are on the same column
} else if (curCallerCol == curCharacterCol) {
// If The Character is on the bottom of the other one and goes up
if (curCallerRow > curCharacterRow && m_ySpeed < 0) {
// Check there is a wall between them
for (int i = curCharacterRow; i < curCallerRow; ++i) {
if (m_maze->getCell(i, curCallerCol).getType() != Cell::CORRIDOR) {
return false;
}
}
// If not, the other Character is in the line sight
return true;
// If the Character is on the top of the other one and goes down
} else if (curCallerRow < curCharacterRow && m_ySpeed > 0) {
// Check there is a wall between them
for (int i = curCallerRow; i < curCharacterRow; ++i) {
if (m_maze->getCell(i, curCallerCol).getType() != Cell::CORRIDOR) {
return false;
}
}
// If not, the other Character is in the line sight
return true;
}
}
// If the two Characters are not on the same row or column, they are not in the line of sight
return false;
}
Cell Character::getNextCell() {
Cell nextCell;
// Get the current cell coordinates from the character coordinates
int curCellRow = m_maze->getRowFromY(m_y);
int curCellCol = m_maze->getColFromX(m_x);
// Get the next cell function of the character direction
if (m_xSpeed > 0) {
nextCell = m_maze->getCell(curCellRow, curCellCol + 1);
} else if (m_xSpeed < 0) {
nextCell = m_maze->getCell(curCellRow, curCellCol - 1);
} else if (m_ySpeed > 0) {
nextCell = m_maze->getCell(curCellRow + 1, curCellCol);
} else if (m_ySpeed < 0) {
nextCell = m_maze->getCell(curCellRow - 1, curCellCol);
}
return nextCell;
}
bool Character::onCenter() {
// Get the current cell center coordinates
qreal centerX = (m_maze->getColFromX(m_x) + 0.5) * Cell::SIZE;
qreal centerY = (m_maze->getRowFromY(m_y) + 0.5) * Cell::SIZE;
bool willGoPast = false;
// Will the character go past the center of the cell it's on ?
// If goes right
if (m_xSpeed > 0) {
willGoPast = (m_x <= centerX && m_x + m_xSpeed >= centerX);
}
// If goes left
else if (m_xSpeed < 0) {
willGoPast = (m_x >= centerX && m_x + m_xSpeed <= centerX);
}
// If goes down
else if (m_ySpeed > 0) {
willGoPast = (m_y <= centerY && m_y + m_ySpeed >= centerY);
}
// If goes up
else if (m_ySpeed < 0) {
willGoPast = (m_y >= centerY && m_y + m_ySpeed <= centerY);
}
// If does not moe
else {
willGoPast = (m_x == centerX && m_y == centerY);
}
return willGoPast;
}
void Character::moveOnCenter() {
setX((m_maze->getColFromX(m_x) + 0.5) * Cell::SIZE);
setY((m_maze->getRowFromY(m_y) + 0.5) * Cell::SIZE);
}
|