• 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
kapman.cpp
Go to the documentation of this file.
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 
23 const qreal Kapman::MAX_SPEED_RATIO = 1.5;
24 
25 Kapman::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 
30 Kapman::~Kapman() {
31 
32 }
33 
34 void Kapman::init() {
35  goRight();
36  updateDirection();
37  // Stop animation
38  emit(stopped());
39 }
40 
41 void Kapman::goUp() {
42  m_askedXSpeed = 0;
43  m_askedYSpeed = -m_speed;
44 }
45 
46 void Kapman::goDown() {
47  m_askedXSpeed = 0;
48  m_askedYSpeed = m_speed;
49 }
50 
51 void Kapman::goRight() {
52  m_askedXSpeed = m_speed;
53  m_askedYSpeed = 0;
54 }
55 
56 void Kapman::goLeft() {
57  m_askedXSpeed = -m_speed;
58  m_askedYSpeed = 0;
59 }
60 
61 void 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 
70 void 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 
135 void Kapman::winPoints(Element* p_element) {
136  // Emits a signal to the game
137  emit(sWinPoints(p_element));
138 }
139 
140 void Kapman::die() {
141  emit(eaten());
142 }
143 
144 void Kapman::emitGameUpdated() {
145  emit(gameUpdated());
146 }
147 
148 qreal Kapman::getAskedXSpeed() const {
149  return m_askedXSpeed;
150 }
151 
152 qreal Kapman::getAskedYSpeed() const {
153  return m_askedYSpeed;
154 }
155 
156 Cell 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 
176 void Kapman::stopMoving() {
177  setXSpeed(0);
178  setYSpeed(0);
179  m_askedXSpeed = 0;
180  m_askedYSpeed = 0;
181  emit(stopped());
182 }
183 
184 void 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 
Element::m_x
qreal m_x
The Element current x-coordinate.
Definition: element.h:57
Kapman::goDown
void goDown()
Makes the Kapman ask to go down.
Definition: kapman.cpp:46
Cell
This class represents a Cell of the Maze.
Definition: cell.h:28
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
Character::eaten
void eaten()
Emitted when the character is eaten.
Kapman::goRight
void goRight()
Makes the Kapman ask to go to the right.
Definition: kapman.cpp:51
Kapman::goUp
void goUp()
Makes the Kapman ask to go up.
Definition: kapman.cpp:41
Kapman::winPoints
void winPoints(Element *p_element)
Manages the points won.
Definition: kapman.cpp:135
Character
This class describes the common characteristics and behaviour of the game characters (Kapman and the ...
Definition: character.h:27
Element::m_type
Type m_type
The Element type.
Definition: element.h:48
Kapman::~Kapman
~Kapman()
Deletes the Kapman instance.
Definition: kapman.cpp:30
Kapman::init
void init()
Initializes the Kapman.
Definition: kapman.cpp:34
Kapman::directionChanged
void directionChanged()
Emitted when the direction changed.
Character::setXSpeed
void setXSpeed(qreal p_xSpeed)
Set the Character x-speed value.
Definition: character.cpp:75
Kapman::getAskedYSpeed
qreal getAskedYSpeed() const
Definition: kapman.cpp:152
Character::setYSpeed
void setYSpeed(qreal p_ySpeed)
Set the Character y-speed value.
Definition: character.cpp:79
Character::getNextCell
Cell getNextCell()
Gets the next Cell the Character is going to reach.
Definition: character.cpp:171
Character::LOW_SPEED_INC
static const qreal LOW_SPEED_INC
Speed increase on easy level (percentage)
Definition: character.h:43
Kapman::stopped
void stopped()
Emitted when the kapman stops moving.
kapman.h
Character::move
void move()
Moves the Character function of its current coordinates and speed.
Definition: character.cpp:38
Cell::CORRIDOR
Definition: cell.h:38
Kapman::Kapman
Kapman(qreal p_x, qreal p_y, Maze *p_maze)
Creates a new Kapman instance.
Definition: kapman.cpp:25
Character::onCenter
bool onCenter()
Checks the Character gets on a Cell center during its next movement.
Definition: character.cpp:191
Element
This class describes the common characteristics and behaviour of any game Element (character or item)...
Definition: element.h:30
Kapman::initSpeedInc
void initSpeedInc()
Initializes the Kapman speed from the Character speed.
Definition: kapman.cpp:184
Character::m_ySpeed
qreal m_ySpeed
The Character y-speed.
Definition: character.h:57
Character::m_xSpeed
qreal m_xSpeed
The Character x-speed.
Definition: character.h:54
Kapman::die
void die()
Implements the Character function.
Definition: kapman.cpp:140
Kapman::gameUpdated
void gameUpdated()
Signals to Kapmanitem that the game has been updated.
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
Kapman::sWinPoints
void sWinPoints(Element *p_element)
Signals to the game that the kapman win points.
Character::m_normalSpeed
qreal m_normalSpeed
Reference to the speed of the character when in "normal" behaviour.
Definition: character.h:63
Kapman::updateMove
void updateMove()
Updates the Kapman move.
Definition: kapman.cpp:70
Kapman::goLeft
void goLeft()
Makes the Kapman ask to go to the left.
Definition: kapman.cpp:56
Element::m_maze
Maze * m_maze
The Maze the Element is on.
Definition: element.h:63
Character::moveOnCenter
void moveOnCenter()
Moves the character on the center of its current Cell.
Definition: character.cpp:222
Element::getType
Element::Type getType() const
Gets the type of the Element.
Definition: element.cpp:54
Element::KAPMAN
Definition: element.h:38
Maze::getRowFromY
int getRowFromY(const qreal p_y) const
Gets the row index corresponding to the given y-coordinate.
Definition: maze.cpp:199
Maze::getColFromX
int getColFromX(const qreal p_x) const
Gets the column index corresponding to the given x-coordinate.
Definition: maze.cpp:203
Character::m_speed
qreal m_speed
The character speed.
Definition: character.h:60
Kapman::emitGameUpdated
void emitGameUpdated()
Emits a signal to Kapmanitem in order to manage collisions.
Definition: kapman.cpp:144
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
Kapman::getAskedXSpeed
qreal getAskedXSpeed() const
Definition: kapman.cpp:148
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