File: game.h

package info (click to toggle)
kapman 4:20.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,588 kB
  • sloc: cpp: 2,793; xml: 208; makefile: 8; sh: 2
file content (377 lines) | stat: -rw-r--r-- 8,635 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
 * Copyright 2007-2008 Alexandre Galinier <alex.galinier@hotmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef GAME_H
#define GAME_H

#include "maze.h"
#include "kapman.h"
#include "ghost.h"
#include "bonus.h"

#include <QPointF>
#include <QTimer>
#include <QKeyEvent>
#include <KgSound>

/**
 * @brief This class manages the game main loop : it regularly checks the key press events, computes the character moves and updates their coordinates.
 */
class Game : public QObject
{

    Q_OBJECT

public :
    /** Ratio which modify the timers function of the difficulty */
    static qreal s_durationRatio;

    /** Timer duration for prey state in medium difficulty */
    static int s_preyStateDuration;

    /** Timer duration for bonus apparition in medium difficulty */
    static int s_bonusDuration;

private :

    /** Number of FPS */
    static const int FPS;

    /** The game different states : RUNNING, PAUSED_LOCKED, PAUSED_UNLOCKED */
    enum State {
        RUNNING,            // Game running
        PAUSED_LOCKED,      // Game paused and user is not allowed to unpause
        PAUSED_UNLOCKED     // Game paused and user is allowed to unpause
    };
    /** A flag for the State enum */
    Q_DECLARE_FLAGS(GameStates, State)

    /** The game state */
    State m_state;

    /** The Game main timer */
    QTimer *m_timer;

    /** The Bonus timer to make it disappear if it is not eaten after a given time */
    QTimer *m_bonusTimer;

    /** Timer to manage the prey state of the ghosts */
    QTimer *m_preyTimer;

    /** The Maze */
    Maze *m_maze;

    /** The main Character */
    Kapman *m_kapman;

    /** The Ghosts */
    QList<Ghost *> m_ghosts;

    /** The Bonus instance */
    Bonus *m_bonus;

    /** A flag to know if the player has cheated during the game */
    bool m_isCheater;

    /** The remaining number of lives */
    int m_lives;

    /** The won points */
    long m_points;

    /** The current game level */
    int m_level;

    /** The number of eaten ghosts since the beginning of the current level */
    int m_nbEatenGhosts;

    /** Flag if sound is enabled */
    bool m_soundEnabled;

    KgSound m_soundGameOver;
    KgSound m_soundGhost;
    KgSound m_soundGainLife;
    KgSound m_soundEnergizer;
    KgSound m_soundBonus;
    KgSound m_soundPill;
    KgSound m_soundLevelUp;

public:

    /**
     * Creates a new Game instance.
     */
    Game();

    /**
     * Deletes the Game instance.
     */
    ~Game() override;

    /**
     * Starts the Game.
     */
    void start();

    /**
     * Pauses the Game.
     * @param p_locked if true the player will be unable to unset the pause.
     */
    void pause(bool p_locked = false);

    /**
     * Pauses / unpauses the game.
     * @param p_locked if true the player will be unable to unset the pause.
     */
    void switchPause(bool p_locked = false);

    /**
     * @return the Maze instance
     */
    Maze *getMaze() const;

    /**
     * @return the Kapman model
     */
    Kapman *getKapman() const;

    /**
     * @return the Ghost models
     */
    QList<Ghost *> getGhosts() const;

    /**
     * @return the Bonus instance
     */
    Bonus *getBonus() const;

    /**
     * @return the main timer
     */
    QTimer *getTimer() const;

    /**
     * @return true if the Game is paused, false otherwise
     */
    bool isPaused() const;

    /**
     * @return true if the player has cheated during the game, false otherwise
     */
    bool isCheater() const;

    /**
     * @return the score
     */
    int getScore() const;

    /**
     * @return the number of remaining lives
     */
    int getLives() const;

    /**
     * @return the current level
     */
    int getLevel() const;

    /**
     * Sets the level to the given number.
     * @param p_level the new level
     */
    void setLevel(int p_level);

    /**
     * Create the new Bonus
     * @param p_position the Bonus position
     */
    void createBonus(QPointF p_position);

    /**
     * Create the new Kapman
     * @param p_position the Kapman position
     */
    void createKapman(QPointF p_position);

    /**
     * Create the new Ghost
     * @param p_position the Ghost position
     * @param p_imageId the image of the Ghost
     */
    void createGhost(QPointF p_position, const QString &p_imageId);

    /**
     * Initializes a Maze
     * @param p_nbRows the number of rows
     * @param p_nbColumns the number of columns
     */
    void initMaze(const int p_nbRows, const int p_nbColumns);

    /**
     * Initializes a Ghost
     */
    void initGhost();

    /**
     * Initializes a Kapman
     */
    void initKapman();

    /**
     * Enables / disables the sounds.
     * @param p_enabled if true the sounds will be enabled, otherwise they will be disabled
     */
    void setSoundsEnabled(bool p_enabled);

private:

    /**
     * Initializes the character coordinates.
     */
    void initCharactersPosition();

    /**
     * Calculates and update the ghosts speed depending on the ghosts speed
     * The value is in Ghost::s_speed
     */
    void setTimersDuration();

public Q_SLOTS:

    /**
     * Manages the key press events.
     * @param p_event the key press event
     */
    void keyPressEvent(QKeyEvent *p_event);

    /**
     * Resumes the Game after the Kapman death.
     */
    void resumeAfterKapmanDeath();

private Q_SLOTS:

    /**
     * Updates the Game data.
     */
    void update();

    /**
     * Manages the loss of a life.
     */
    void kapmanDeath();

    /**
     * Manages the death of a Ghost.
     */
    void ghostDeath(Ghost *p_ghost);

    /**
     * Increases the score considering the eaten Element.
     * @param p_element the eaten Element
     */
    void winPoints(Element *p_element);

    /**
     * Starts the next level.
     */
    void nextLevel();

    /**
     * Hides the Bonus.
     */
    void hideBonus();

    /**
     * Ends the Ghosts prey state.
     */
    void endPreyState();

Q_SIGNALS:

    /**
     * Emitted when the Game is started.
     */
    void gameStarted();

    /**
     * Emitted when the Game is over.
     * @param p_unused this parameter must always be true !
     */
    void gameOver(const bool p_unused);

    /**
     * Emitted when a level begins, if level up or if a life has been lost.
     * @param p_newLevel true if a new level is beginning, false otherwise
     */
    void levelStarted(const bool p_newLevel);

    /**
     * Emitted when the pause state has changed.
     * @param p_pause true if the Game is paused, false otherwise
     * @param p_fromUser true if the Game has been paused due to an action the player has done, false otherwise
     */
    void pauseChanged(const bool p_pause, const bool p_fromUser);

    /**
     * Emitted when an Element has been eaten.
     * @param p_x the Element x-coordinate
     * @param p_y the Element y-coordinate
     */
    void elementEaten(const qreal p_x, const qreal p_y);

    /**
     * Emitted when the Bonus has to be displayed.
     */
    void bonusOn();

    /**
     * Emitted when the Bonus has to disappear.
     */
    void bonusOff();

    /**
     * Emitted when the level have changed.
     * @param p_level the new level data
     */
    void levelChanged(unsigned int p_level);

    /**
     * Emitted when the score have changed.
     * @param p_score the new score data
     */
    void scoreChanged(unsigned int p_score);

    /**
     * Emitted when the lives have changed.
     * @param p_lives the new lives data
     */
    void livesChanged(unsigned int p_lives);

    /**
     * Emitted when a ghost or a bonus is eaten. It tells to the scene to
     * display the number of won points
     * @param p_wonPoints the value to display
     * @param p_xPos the x position of the label
     * @param p_yPos the y position of the label
     */
    void pointsToDisplay(long p_wonPoints, qreal p_xPos, qreal p_yPos);
};

#endif