• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

kapman

  • sources
  • kde-4.14
  • kdegames
  • kapman
ghost.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3  * Copyright 2007-2008 Alexandre Galinier <alex.galinier@hotmail.com>
4  *
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
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
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.
14  *
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 "ghost.h"
20 #include "time.h"
21 
22 #include <QPointF>
23 #include <KgDifficulty>
24 #include <cstdlib>
25 
26 const qreal Ghost::MAX_SPEED_RATIO = 2.0;
27 const int Ghost::POINTS = 200;
28 
29 Ghost::Ghost(qreal p_x, qreal p_y, const QString & p_imageId, Maze* p_maze) : Character(p_x, p_y, p_maze) {
30  // Initialize the ghost attributes
31  m_imageId = p_imageId;
32  m_points = Ghost::POINTS;
33  m_type = Element::GHOST;
34  m_state = Ghost::HUNTER;
35  m_maxSpeed = m_normalSpeed * MAX_SPEED_RATIO;
36  // Initialize the random-number generator
37  srand(time(NULL));
38  // Makes the ghost move as soon as the game is created
39  goLeft();
40 }
41 
42 Ghost::~Ghost() {
43 
44 }
45 
46 void Ghost::goUp() {
47  m_xSpeed = 0;
48  m_ySpeed = -m_speed;
49 }
50 
51 void Ghost::goDown() {
52  m_xSpeed = 0;
53  m_ySpeed = m_speed;
54 }
55 
56 void Ghost::goRight() {
57  m_xSpeed = m_speed;
58  m_ySpeed = 0;
59 }
60 
61 void Ghost::goLeft() {
62  m_xSpeed = -m_speed;
63  m_ySpeed = 0;
64 }
65 
66 void Ghost::updateMove() {
67  // Get the current cell coordinates from the character coordinates
68  int curCellRow = m_maze->getRowFromY(m_y);
69  int curCellCol = m_maze->getColFromX(m_x);
70  // Flag to know when the ghost has no choice but go back
71  bool halfTurnRequired = true;
72  // Contains the different directions a ghost can choose when on a cell center
73  QList<QPointF> directionsList;
74  int nb = 0;
75 
76  // If the ghost is not "eaten"
77  if (m_state != Ghost::EATEN) {
78  // If the ghost gets on a Cell center
79  if (onCenter()) {
80  // We retrieve all the directions the ghost can choose (save the turnning back)
81  if (m_maze->getCell(curCellRow, curCellCol + 1).getType() == Cell::CORRIDOR ||
82  (m_maze->getCell(curCellRow, curCellCol).getType() == Cell::GHOSTCAMP &&
83  m_maze->getCell(curCellRow, curCellCol + 1).getType() == Cell::GHOSTCAMP)) {
84  if (m_xSpeed >= 0) {
85  directionsList.append(QPointF(m_speed, 0.0));
86  halfTurnRequired = false;
87  }
88  }
89  if (m_maze->getCell(curCellRow + 1, curCellCol).getType() == Cell::CORRIDOR ||
90  (m_maze->getCell(curCellRow, curCellCol).getType() == Cell::GHOSTCAMP &&
91  m_maze->getCell(curCellRow + 1, curCellCol).getType() == Cell::GHOSTCAMP)) {
92  if (m_ySpeed >= 0) {
93  directionsList.append(QPointF(0.0, m_speed));
94  halfTurnRequired = false;
95  }
96  }
97  if (m_maze->getCell(curCellRow - 1, curCellCol).getType() == Cell::CORRIDOR ||
98  (m_maze->getCell(curCellRow, curCellCol).getType() == Cell::GHOSTCAMP &&
99  m_maze->getCell(curCellRow - 1, curCellCol).getType() == Cell::GHOSTCAMP)) {
100  if (m_ySpeed <= 0) {
101  directionsList.append(QPointF(0.0, -m_speed));
102  halfTurnRequired = false;
103  }
104  }
105  if (m_maze->getCell(curCellRow, curCellCol - 1).getType() == Cell::CORRIDOR ||
106  (m_maze->getCell(curCellRow, curCellCol).getType() == Cell::GHOSTCAMP &&
107  m_maze->getCell(curCellRow, curCellCol - 1).getType() == Cell::GHOSTCAMP)) {
108  if (m_xSpeed <= 0) {
109  directionsList.append(QPointF(-m_speed, 0.0));
110  halfTurnRequired = false;
111  }
112  }
113  // Random number generation to choose one of the directions
114  nb = int(double(rand()) / (double(RAND_MAX) + 1) * directionsList.size());
115  // If there is no directions in the list, the character goes backward
116  if (directionsList.size() == 0) {
117  m_xSpeed = -m_xSpeed;
118  m_ySpeed = -m_ySpeed;
119  } else if ((m_xSpeed != 0 && m_xSpeed != directionsList[nb].x()) ||
120  (m_ySpeed != 0 && m_ySpeed != directionsList[nb].y())) { // If the chosen direction isn't forward
121  // We move the ghost on the center of the cell and update the directions
122  moveOnCenter();
123  m_xSpeed = directionsList[nb].x();
124  m_ySpeed = directionsList[nb].y();
125  }
126  }
127  // We move the ghost
128  move();
129  } else { // If the ghost has been eaten
130  if (onCenter()) {
131  // If the ghost has not reached the camp yet
132  if (m_pathToCamp.size() != 0) {
133  // Go to the next cell to the camp
134  updateMove(m_pathToCamp.first().y(), m_pathToCamp.first().x());
135  // Remove the cell the ghost has reached from the path
136  m_pathToCamp.removeFirst();
137  } else {
138  // If the ghost is not at home (that means it has just been eaten)
139  if (curCellRow != m_maze->getResurrectionCell().y() || curCellCol != m_maze->getResurrectionCell().x()) {
140  // Compute the path to go back to the camp
141  m_pathToCamp = m_maze->getPathToGhostCamp(curCellRow, curCellCol);
142  if (!m_pathToCamp.isEmpty()) {
143  updateMove(m_pathToCamp.first().y(), m_pathToCamp.first().x());
144  } else {
145  // Set the ghost at home
146  m_x = m_maze->getResurrectionCell().x() * Cell::SIZE + Cell::SIZE / 2;
147  m_y = m_maze->getResurrectionCell().y() * Cell::SIZE + Cell::SIZE / 2;
148  setState(Ghost::HUNTER);
149  }
150  } else { // The ghost has reached the ghost camp
151  setState(Ghost::HUNTER);
152  }
153  }
154  }
155  move();
156  }
157 }
158 
159 void Ghost::updateMove(int p_row, int p_col) {
160  // Get the current cell coordinates from the ghost coordinates
161  int curGhostRow = m_maze->getRowFromY(m_y);
162  int curGhostCol = m_maze->getColFromX(m_x);
163 
164  if (onCenter()) {
165  if (curGhostRow == p_row) {
166  if (p_col > curGhostCol) {
167  m_xSpeed = m_speed;
168  m_ySpeed = 0;
169  } else {
170  m_xSpeed = -m_speed;
171  m_ySpeed = 0;
172  }
173  } else {
174  if (p_row > curGhostRow) {
175  m_xSpeed = 0;
176  m_ySpeed = m_speed;
177  } else {
178  m_xSpeed = 0;
179  m_ySpeed = -m_speed;
180  }
181  }
182  }
183  // We move the ghost
184  move();
185 }
186 
187 QString Ghost::getImageId() const {
188  return m_imageId;
189 }
190 
191 Ghost::State Ghost::getState() const {
192  return m_state;
193 }
194 
195 void Ghost::setState(Ghost::State p_state) {
196  // Change the state
197  m_state = p_state;
198  switch (m_state) {
199  case Ghost::PREY:
200  m_speed = m_normalSpeed / 2;
201  break;
202  case HUNTER:
203  case EATEN:
204  m_speed = m_normalSpeed;
205  break;
206  }
207  emit(stateChanged());
208 }
209 
210 void Ghost::doActionOnCollision(Kapman*) {
211  switch (m_state) {
212  case Ghost::HUNTER:
213  emit(lifeLost());
214  break;
215  case Ghost::PREY:
216  emit(ghostEaten(this));
217  break;
218  case Ghost::EATEN:
219  // Do nothing
220  break;
221  }
222 }
223 
224 void Ghost::initSpeedInc() {
225  // Ghosts speed increase when level up
226  switch ((int) Kg::difficultyLevel())
227  {
228  case KgDifficultyLevel::Easy:
229  m_speedIncrease = Character::LOW_SPEED_INC;
230  break;
231  case KgDifficultyLevel::Medium:
232  m_speedIncrease = Character::MEDIUM_SPEED_INC;
233  break;
234  case KgDifficultyLevel::Hard:
235  m_speedIncrease = Character::HIGH_SPEED_INC;
236  break;
237  }
238 }
239 
Ghost::lifeLost
void lifeLost()
Emitted when the Kapman has lost a life.
Maze::getPathToGhostCamp
QList< QPoint > getPathToGhostCamp(const int p_row, const int p_column) const
Gets the path, as a list of Cell coordinates, to go to the Ghost camp from the Cell whose coordinates...
Definition: maze.cpp:80
Element::m_x
qreal m_x
The Element current x-coordinate.
Definition: element.h:57
Maze::getCell
Cell getCell(const int p_row, const int p_column) const
Gets the Cell at the given coordinates.
Definition: maze.cpp:180
Character::m_speedIncrease
qreal m_speedIncrease
The value the character's speed is incremented by when level up.
Definition: character.h:66
Element::m_y
qreal m_y
The Element current y-coordinate.
Definition: element.h:60
Ghost::EATEN
Definition: ghost.h:39
Ghost::updateMove
void updateMove()
Updates the Ghost move.
Definition: ghost.cpp:66
Element::m_points
int m_points
Points won when the Element is eaten.
Definition: element.h:69
Ghost::POINTS
static const int POINTS
The value of an Ghost.
Definition: ghost.h:43
Cell::getType
Type getType() const
Gets the Cell type.
Definition: cell.cpp:31
Character
This class describes the common characteristics and behaviour of the game characters (Kapman and the ...
Definition: character.h:27
QList::removeFirst
void removeFirst()
Element::m_type
Type m_type
The Element type.
Definition: element.h:48
Element::GHOST
Definition: element.h:39
Ghost::PREY
Definition: ghost.h:38
QPoint::x
int x() const
QPoint::y
int y() const
QList::size
int size() const
QPointF
Cell::SIZE
static const qreal SIZE
The Cell side size.
Definition: cell.h:33
Character::LOW_SPEED_INC
static const qreal LOW_SPEED_INC
Speed increase on easy level (percentage)
Definition: character.h:43
Ghost::getImageId
QString getImageId() const
Gets the path to the Ghost image.
Definition: ghost.cpp:187
Ghost::setState
void setState(Ghost::State p_state)
Sets the Ghost state to the given value.
Definition: ghost.cpp:195
QList::append
void append(const T &value)
QList::isEmpty
bool isEmpty() const
Ghost::Ghost
Ghost(qreal p_x, qreal p_y, const QString &p_imageId, Maze *p_maze)
Creates a new Ghost instance.
Definition: ghost.cpp:29
Ghost::HUNTER
Definition: ghost.h:37
Character::move
void move()
Moves the Character function of its current coordinates and speed.
Definition: character.cpp:38
Cell::CORRIDOR
Definition: cell.h:38
Ghost::stateChanged
void stateChanged()
Emitted when the Ghost has changed his state.
Ghost::ghostEaten
void ghostEaten(Ghost *p_ghost)
Emitted when the Ghost has been eaten.
QList::first
T & first()
QString
QList
Character::onCenter
bool onCenter()
Checks the Character gets on a Cell center during its next movement.
Definition: character.cpp:191
Character::m_ySpeed
qreal m_ySpeed
The Character y-speed.
Definition: character.h:57
Cell::GHOSTCAMP
Definition: cell.h:39
Character::m_xSpeed
qreal m_xSpeed
The Character x-speed.
Definition: character.h:54
Ghost::State
State
The ghost possible states.
Definition: ghost.h:36
ghost.h
Character::MEDIUM_SPEED_INC
static const qreal MEDIUM_SPEED_INC
Speed increase on medium level (percentage)
Definition: character.h:46
Character::m_maxSpeed
qreal m_maxSpeed
The maximum character speed.
Definition: character.h:69
Character::m_normalSpeed
qreal m_normalSpeed
Reference to the speed of the character when in "normal" behaviour.
Definition: character.h:63
Ghost::initSpeedInc
void initSpeedInc()
Initializes the Ghost speed from the Character speed.
Definition: ghost.cpp:224
Element::m_maze
Maze * m_maze
The Maze the Element is on.
Definition: element.h:63
Ghost::getState
State getState() const
Gets the current state of the Ghost.
Definition: ghost.cpp:191
Ghost::~Ghost
~Ghost()
Deletes the Ghost instance.
Definition: ghost.cpp:42
Character::moveOnCenter
void moveOnCenter()
Moves the character on the center of its current Cell.
Definition: character.cpp:222
Maze::getRowFromY
int getRowFromY(const qreal p_y) const
Gets the row index corresponding to the given y-coordinate.
Definition: maze.cpp:199
Ghost::doActionOnCollision
void doActionOnCollision(Kapman *p_kapman)
Manages the collison with the Kapman.
Definition: ghost.cpp:210
Maze::getColFromX
int getColFromX(const qreal p_x) const
Gets the column index corresponding to the given x-coordinate.
Definition: maze.cpp:203
Kapman
This class represents the main character of the game.
Definition: kapman.h:26
Character::m_speed
qreal m_speed
The character speed.
Definition: character.h:60
Maze
This class represents the Maze of the game.
Definition: maze.h:31
Character::HIGH_SPEED_INC
static const qreal HIGH_SPEED_INC
Speed increase on hard level (percentage)
Definition: character.h:49
Maze::getResurrectionCell
QPoint getResurrectionCell() const
Gets the cell on witch the ghosts resurects.
Definition: maze.cpp:223
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:15 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kapman

Skip menu "kapman"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal