2 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
3 * Copyright 2007-2008 Alexandre Galinier <alex.galinier@hotmail.com>
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.
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.
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/>.
30 #include <KGameDifficulty>
31 #include <Phonon/MediaObject>
34 * @brief This class manages the game main loop : it regularly checks the key press events, computes the character moves and updates their coordinates.
36 class Game
: public QObject
{
41 /** Ratio which modify the timers function of the difficulty */
42 static qreal s_durationRatio
;
44 /** Timer duration for prey state in medium difficulty */
45 static int s_preyStateDuration
;
47 /** Timer duration for bonus apparition in medium difficulty */
48 static int s_bonusDuration
;
55 /** The game different states : RUNNING, PAUSED_LOCKED, PAUSED_UNLOCKED */
57 RUNNING
, // Game running
58 PAUSED_LOCKED
, // Game paused and user is not allowed to unpause
59 PAUSED_UNLOCKED
// Game paused and user is allowed to unpause
61 /** A flag for the State enum */
62 Q_DECLARE_FLAGS(GameStates
, State
)
67 /** The Game main timer */
70 /** The Bonus timer to make it disappear if it is not eaten after a given time */
73 /** Timer to manage the prey state of the ghosts */
79 /** The main Character */
83 QList
<Ghost
*> m_ghosts
;
85 /** The Bonus instance */
88 /** A flag to know if the player has cheated during the game */
91 /** The remaining number of lives */
97 /** The current game level */
100 /** The number of eaten ghosts since the beginning of the current level */
103 /** A first MediaObject to play sounds */
104 Phonon::MediaObject
* m_media1
;
106 /** A second MediaObject to play sounds */
107 Phonon::MediaObject
* m_media2
;
111 /** The different types of information about the game */
112 enum Information
{ NoInfo
= 0,
113 ScoreInfo
= 1, // Score
114 LivesInfo
= 2, // Number of remaining lives
115 LevelInfo
= 4, // Current level
116 AllInfo
= ScoreInfo
| LivesInfo
| LevelInfo
};
117 /** A flag for the Information enum */
118 Q_DECLARE_FLAGS(InformationTypes
, Information
)
121 * Creates a new Game instance.
122 * @param p_difficulty the KGameDifficulty level of the Game
124 Game(KGameDifficulty::standardLevel p_difficulty
= KGameDifficulty::Medium
);
127 * Deletes the Game instance.
138 * @param p_locked if true the player will be unable to unset the pause.
140 void pause(bool p_locked
= false);
143 * Pauses / unpauses the game.
144 * @param p_locked if true the player will be unable to unset the pause.
146 void switchPause(bool p_locked
= false);
149 * @return the Maze instance
151 Maze
* getMaze() const;
154 * @return the Kapman model
156 Kapman
* getKapman() const;
159 * @return the Ghost models
161 QList
<Ghost
*> getGhosts () const;
164 * @return the Bonus instance
169 * @return the main timer
171 QTimer
* getTimer() const;
174 * @return true if the Game is paused, false otherwise
176 bool isPaused() const;
179 * @return true if the player has cheated during the game, false otherwise
181 bool isCheater() const;
186 int getScore () const;
189 * @return the number of remaining lives
191 int getLives() const;
194 * @return the current level
196 int getLevel() const;
199 * Sets the level to the given number.
200 * @param p_level the new level
202 void setLevel(int p_level
);
205 * Create the new Bonus
206 * @param p_position the Bonus position
208 void createBonus(QPointF p_position
);
211 * Create the new Kapman
212 * @param p_position the Kapman position
214 void createKapman(QPointF p_position
);
217 * Create the new Ghost
218 * @param p_position the Ghost position
219 * @param p_imageId the image of the Ghost
221 void createGhost(QPointF p_position
, const QString
& p_imageId
);
225 * @param p_nbRows the number of rows
226 * @param p_nbColumns the number of columns
228 void initMaze(const int p_nbRows
, const int p_nbColumns
);
231 * Initializes a Ghost
236 * Initializes a Kapman
241 * Enables / disables the sounds.
242 * @param p_enabled if true the sounds will be enabled, otherwise they will be disabled
244 void setSoundsEnabled(bool p_enabled
);
249 * Initializes the character coordinates.
251 void initCharactersPosition();
254 * Calculates and update the ghosts speed depending on the ghosts speed
255 * The value is in Ghost::s_speed
257 void setTimersDuration();
260 * Plays the given sound.
261 * @param p_sound the path to the sound to play
263 void playSound(const QString
& p_sound
);
268 * Manages the key press events.
269 * @param p_event the key press event
271 void keyPressEvent(QKeyEvent
* p_event
);
274 * Resumes the Game after the Kapman death.
276 void resumeAfterKapmanDeath();
281 * Updates the Game data.
286 * Manages the loss of a life.
291 * Manages the death of a Ghost.
293 void ghostDeath(Ghost
* p_ghost
);
296 * Increases the score considering the eaten Element.
297 * @param p_element the eaten Element
299 void winPoints(Element
* p_element
);
302 * Starts the next level.
312 * Ends the Ghosts prey state.
319 * Emitted when the Game is started.
324 * Emitted when the Game is over.
325 * @param p_unused this parameter must always be true !
327 void gameOver(const bool p_unused
);
330 * Emitted when a level begins, if level up or if a life has been lost.
331 * @param p_newLevel true if a new level is beginning, false otherwise
333 void levelStarted(const bool p_newLevel
);
336 * Emitted when the pause state has changed.
337 * @param p_pause true if the Game is paused, false otherwise
338 * @param p_fromUser true if the Game has been paused due to an action the player has done, false otherwise
340 void pauseChanged(const bool p_pause
, const bool p_fromUser
);
343 * Emitted when an Element has been eaten.
344 * @param p_x the Element x-coordinate
345 * @param p_y the Element y-coordinate
347 void elementEaten(const qreal p_x
, const qreal p_y
);
350 * Emitted when the Bonus has to be displayed.
355 * Emitted when the Bonus has to disappear.
360 * Emitted when the Game data (score, level, lives) have changed.
361 * @param p_infoType the type of data that have changed
363 void dataChanged(Game::InformationTypes p_infoType
);
366 * Emitted when a ghost or a bonus is eaten. It tells to the scene to
367 * display the number of won points
368 * @param p_wonPoints the value to display
369 * @param p_xPos the x position of the label
370 * @param p_yPos the y position of the label
372 void pointsToDisplay(long p_wonPoints
, qreal p_xPos
, qreal p_yPos
);