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

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
character.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Mathias Kraus <k.hias@gmx.de>
3  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
4  * Copyright 2007-2008 Pierre-BenoĆ®t Besse <besse.pb@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "character.h"
21 #include "arena.h"
22 
23 Character::Character(qreal p_x, qreal p_y, Arena* p_arena) : Element(p_x, p_y, p_arena), m_xSpeed(0), m_ySpeed(0)
24 {
25  initSpeed();
26  m_maxSpeed = m_normalSpeed; // To avoid bugs, but will be overridden in the Player constructors
27 }
28 
29 Character::~Character()
30 {
31 }
32 
33 void Character::move()
34 {
35  // Take care of the Arena borders
36  if (m_arena->getColFromX(m_x + m_xSpeed) == 0) // First column
37  {
38  m_x = (m_arena->getNbColumns() - 1.5) * Granatier::CellSize;
39  }
40  else if (m_arena->getColFromX(m_x + m_xSpeed) == m_arena->getNbColumns() - 1) // Last column
41  {
42  m_x = 1.5 * Granatier::CellSize;
43  }
44  else if (m_arena->getRowFromY(m_y + m_ySpeed) == 0) // First row
45  {
46  m_y = (m_arena->getNbRows() - 1.5) * Granatier::CellSize;
47  }
48  else if (m_arena->getRowFromY(m_y + m_ySpeed) == m_arena->getNbRows() - 1) // Last row
49  {
50  m_y = 1.5 * Granatier::CellSize;
51  }
52  // Move the Character
53  m_x += m_xSpeed;
54  m_y += m_ySpeed;
55  emit(moved(m_x, m_y));
56 }
57 
58 void Character::die()
59 {
60  emit(dead());
61 }
62 
63 qreal Character::getXSpeed() const
64 {
65  return m_xSpeed;
66 }
67 
68 qreal Character::getYSpeed() const
69 {
70  return m_ySpeed;
71 }
72 
73 qreal Character::getSpeed() const
74 {
75  return m_speed;
76 }
77 
78 qreal Character::getNormalSpeed() const
79 {
80  return m_normalSpeed;
81 }
82 
83 void Character::setXSpeed(qreal p_xSpeed)
84 {
85  m_xSpeed = p_xSpeed;
86 }
87 
88 void Character::setYSpeed(qreal p_ySpeed)
89 {
90  m_ySpeed = p_ySpeed;
91 }
92 
93 void Character::initSpeed()
94 {
95  // Player speed
96  m_normalSpeed = 1;
97  m_speed = m_normalSpeed;
98 }
99 
100 bool Character::isInLineSight(Character* p_character) const
101 {
102  int curCallerRow; // The current row of the Character
103  int curCallerCol; // The current column of the Character
104  int curCharacterRow; // The current row of the other Character
105  int curCharacterCol; // The current column of the other Character
106 
107  curCallerRow = m_arena->getRowFromY(m_y);
108  curCallerCol = m_arena->getColFromX(m_x);
109  curCharacterRow = m_arena->getRowFromY(p_character->getY());
110  curCharacterCol = m_arena->getColFromX(p_character->getX());
111 
112  // If the two Characters are on the same row
113  if (curCallerRow == curCharacterRow )
114  {
115  // If The Character is on the right of the other one and goes to the left
116  if (curCallerCol > curCharacterCol && m_xSpeed < 0)
117  {
118  // Check there is a wall between them
119  for (int i = curCharacterCol; i < curCallerCol; ++i)
120  {
121  if (m_arena->getCell(curCallerRow, i).getType() != Granatier::Cell::GROUND)
122  {
123  return false;
124  }
125  }
126  // If not, the other Character is in the line sight
127  return true;
128  // If the Character is on the left of the other one and goes to the right
129  }
130  else if (curCallerCol < curCharacterCol && m_xSpeed > 0)
131  {
132  // Check there is a wall between them
133  for (int i = curCallerCol; i < curCharacterCol; ++i)
134  {
135  if (m_arena->getCell(curCallerRow, i).getType() != Granatier::Cell::GROUND)
136  {
137  return false;
138  }
139  }
140  // If not, the other Character is in the line sight
141  return true;
142  }
143  // If the two Characters are on the same column
144  }
145  else if (curCallerCol == curCharacterCol)
146  {
147  // If The Character is on the bottom of the other one and goes up
148  if (curCallerRow > curCharacterRow && m_ySpeed < 0)
149  {
150  // Check there is a wall between them
151  for (int i = curCharacterRow; i < curCallerRow; ++i)
152  {
153  if (m_arena->getCell(i, curCallerCol).getType() != Granatier::Cell::GROUND)
154  {
155  return false;
156  }
157  }
158  // If not, the other Character is in the line sight
159  return true;
160  // If the Character is on the top of the other one and goes down
161  }
162  else if (curCallerRow < curCharacterRow && m_ySpeed > 0)
163  {
164  // Check there is a wall between them
165  for (int i = curCallerRow; i < curCharacterRow; ++i)
166  {
167  if (m_arena->getCell(i, curCallerCol).getType() != Granatier::Cell::GROUND)
168  {
169  return false;
170  }
171  }
172  // If not, the other Character is in the line sight
173  return true;
174  }
175  }
176  // If the two Characters are not on the same row or column, they are not in the line of sight
177  return false;
178 }
character.h
Granatier::Cell::GROUND
Definition: granatierglobals.h:74
Element::m_x
qreal m_x
The Element current x-coordinate.
Definition: element.h:48
Character::getXSpeed
qreal getXSpeed() const
Gets the Character x-speed value.
Definition: character.cpp:63
Element::m_y
qreal m_y
The Element current y-coordinate.
Definition: element.h:51
Element::getX
qreal getX() const
Gets the Element x-coordinate.
Definition: element.cpp:46
Arena::getNbColumns
int getNbColumns() const
Gets the number of columns of the Arena.
Definition: arena.cpp:164
Character
This class describes the common characteristics and behaviour of the game characters (Players)...
Definition: character.h:31
Character::Character
Character(qreal p_x, qreal p_y, Arena *p_arena)
Creates a new Character instance.
Definition: character.cpp:23
Character::setXSpeed
void setXSpeed(qreal p_xSpeed)
Set the Character x-speed value.
Definition: character.cpp:83
Character::setYSpeed
void setYSpeed(qreal p_ySpeed)
Set the Character y-speed value.
Definition: character.cpp:88
Element::moved
void moved(qreal p_x, qreal p_y)
Emitted on Element move.
Granatier::CellSize
const qreal CellSize
The Cell size.
Definition: granatierglobals.h:31
Character::getYSpeed
qreal getYSpeed() const
Gets the Character y-speed value.
Definition: character.cpp:68
Character::isInLineSight
bool isInLineSight(Character *p_character) const
Checks the Character is in the line of sight of the given other Character.
Definition: character.cpp:100
Element::m_arena
Arena * m_arena
The Arena the Element is on.
Definition: element.h:54
Character::move
void move()
Moves the Character function of its current coordinates and speed.
Definition: character.cpp:33
Arena
This class represents the Arena of the game.
Definition: arena.h:36
Character::initSpeed
void initSpeed()
Initializes the Character speed considering the difficulty level.
Definition: character.cpp:93
Character::dead
void dead()
Emitted when the character is dead.
Element
This class describes the common characteristics and behaviour of any game Element (character or item)...
Definition: element.h:32
Character::getNormalSpeed
qreal getNormalSpeed() const
Gets the Character normal speed.
Definition: character.cpp:78
Character::m_ySpeed
qreal m_ySpeed
The Character y-speed.
Definition: character.h:54
Element::getY
qreal getY() const
Gets the Element y-coordinate.
Definition: element.cpp:51
Character::m_xSpeed
qreal m_xSpeed
The Character x-speed.
Definition: character.h:51
Character::getSpeed
qreal getSpeed() const
Gets the Character speed.
Definition: character.cpp:73
Character::~Character
~Character()
Deletes the Character instance.
Definition: character.cpp:29
Character::die
void die()
Manages the character death (essentially blinking).
Definition: character.cpp:58
Character::m_maxSpeed
qreal m_maxSpeed
The maximum character speed.
Definition: character.h:63
Arena::getColFromX
int getColFromX(const qreal p_x) const
Gets the column index corresponding to the given x-coordinate.
Definition: arena.cpp:154
Character::m_normalSpeed
qreal m_normalSpeed
Reference to the speed of the character when in "normal" behaviour.
Definition: character.h:60
arena.h
Arena::getNbRows
int getNbRows() const
Gets the number of rows of the Arena.
Definition: arena.cpp:169
Character::m_speed
qreal m_speed
The character speed.
Definition: character.h:57
Cell::getType
Granatier::Cell::Type getType() const
Gets the Cell type.
Definition: cell.cpp:52
Arena::getCell
Cell getCell(const int p_row, const int p_column) const
Gets the Cell at the given coordinates.
Definition: arena.cpp:120
Arena::getRowFromY
int getRowFromY(const qreal p_y) const
Gets the row index corresponding to the given y-coordinate.
Definition: arena.cpp:144
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • 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