1/*
2 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18
19#include "kapman.h"
20
21#include <KgDifficulty>
22
23const qreal Kapman::MAX_SPEED_RATIO = 1.5;
24
25Kapman::Kapman(qreal p_x, qreal p_y, Maze* p_maze) : Character(p_x, p_y, p_maze) {
26 m_type = Element::KAPMAN;
27 m_maxSpeed = m_normalSpeed * MAX_SPEED_RATIO;
28}
29
30Kapman::~Kapman() {
31
32}
33
34void Kapman::init() {
35 goRight();
36 updateDirection();
37 // Stop animation
38 emit(stopped());
39}
40
41void Kapman::goUp() {
42 m_askedXSpeed = 0;
43 m_askedYSpeed = -m_speed;
44}
45
46void Kapman::goDown() {
47 m_askedXSpeed = 0;
48 m_askedYSpeed = m_speed;
49}
50
51void Kapman::goRight() {
52 m_askedXSpeed = m_speed;
53 m_askedYSpeed = 0;
54}
55
56void Kapman::goLeft() {
57 m_askedXSpeed = -m_speed;
58 m_askedYSpeed = 0;
59}
60
61void Kapman::updateDirection() {
62 setXSpeed(m_askedXSpeed);
63 setYSpeed(m_askedYSpeed);
64 m_askedXSpeed = 0;
65 m_askedYSpeed = 0;
66 // Signal to the kapman item that the direction changed
67 emit(directionChanged());
68}
69
70void Kapman::updateMove() {
71 // If the kapman does not move
72 if (m_xSpeed == 0 && m_ySpeed == 0) {
73 // If the user asks for moving
74 if (m_askedXSpeed != 0 || m_askedYSpeed != 0) {
75 // Check the next cell with the asked direction
76 if (getAskedNextCell().getType() == Cell::CORRIDOR) {
77 // Update the direction
78 updateDirection();
79 // Move the kapman
80 move();
81 }
82 }
83 }
84 // If the kapman is already moving
85 else {
86 // If the kapman wants to go back it does not wait to be on a center
87 if ( (m_xSpeed!=0 && m_askedXSpeed==-m_xSpeed) || (m_ySpeed!=0 && m_askedYSpeed==-m_ySpeed) ) {
88 // Go back
89 updateDirection();
90 // Move the kapman
91 move();
92 } else {
93 // If the kapman gets on a cell center
94 if (onCenter()) {
95 // If there is an asked direction (but not a half-turn)
96 if ((m_askedXSpeed != 0 || m_askedYSpeed != 0) && (m_askedXSpeed != m_xSpeed || m_askedYSpeed != m_ySpeed)) {
97 // Check the next cell with the kapman asked direction
98 if (getAskedNextCell().getType() == Cell::CORRIDOR) {
99 // Move the kapman on the cell center
100 moveOnCenter();
101 // Update the direction
102 updateDirection();
103 } else {
104 // Check the next cell with the kapman current direction
105 if (getNextCell().getType() != Cell::CORRIDOR) {
106 // Move the kapman on the cell center
107 moveOnCenter();
108 // Stop moving
109 stopMoving();
110 } else {
111 // Move the kapman
112 move();
113 }
114 }
115 } else {
116 // Check the next cell with the kapman current direction
117 if (getNextCell().getType() != Cell::CORRIDOR) {
118 // Move the kapman on the cell center
119 moveOnCenter();
120 // Stop moving
121 stopMoving();
122 } else {
123 // Move the kapman
124 move();
125 }
126 }
127 } else {
128 // Move the kapman
129 move();
130 }
131 }
132 }
133}
134
135void Kapman::winPoints(Element* p_element) {
136 // Emits a signal to the game
137 emit(sWinPoints(p_element));
138}
139
140void Kapman::die() {
141 emit(eaten());
142}
143
144void Kapman::emitGameUpdated() {
145 emit(gameUpdated());
146}
147
148qreal Kapman::getAskedXSpeed() const {
149 return m_askedXSpeed;
150}
151
152qreal Kapman::getAskedYSpeed() const {
153 return m_askedYSpeed;
154}
155
156Cell Kapman::getAskedNextCell() {
157 // Get the current cell coordinates from the character coordinates
158 int curCellRow = m_maze->getRowFromY(m_y);
159 int curCellCol = m_maze->getColFromX(m_x);
160 Cell nextCell;
161
162 // Get the next cell function of the character asked direction
163 if (m_askedXSpeed > 0) {
164 nextCell = m_maze->getCell(curCellRow, curCellCol + 1);
165 } else if (m_askedXSpeed < 0) {
166 nextCell = m_maze->getCell(curCellRow, curCellCol - 1);
167 } else if (m_askedYSpeed > 0) {
168 nextCell = m_maze->getCell(curCellRow + 1, curCellCol);
169 } else if (m_askedYSpeed < 0) {
170 nextCell = m_maze->getCell(curCellRow - 1, curCellCol);
171 }
172
173 return nextCell;
174}
175
176void Kapman::stopMoving() {
177 setXSpeed(0);
178 setYSpeed(0);
179 m_askedXSpeed = 0;
180 m_askedYSpeed = 0;
181 emit(stopped());
182}
183
184void Kapman::initSpeedInc() {
185 // Kapman speed increase when level up
186 switch ((int) Kg::difficultyLevel())
187 {
188 case KgDifficultyLevel::Easy:
189 m_speedIncrease = Character::LOW_SPEED_INC / 2;
190 break;
191 case KgDifficultyLevel::Medium:
192 m_speedIncrease = Character::MEDIUM_SPEED_INC / 2;
193 break;
194 case KgDifficultyLevel::Hard:
195 m_speedIncrease = Character::HIGH_SPEED_INC / 2;
196 break;
197 }
198}
199
200