File indexing completed on 2024-10-13 03:43:14

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004     SPDX-FileCopyrightText: 2007-2008 Alexandre Galinier <alex.galinier@hotmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef GAME_H
0010 #define GAME_H
0011 
0012 #include "granatierglobals.h"
0013 
0014 #include <QObject>
0015 #include <QStringList>
0016 
0017 class GameScene;
0018 class Arena;
0019 class Player;
0020 class Bonus;
0021 class Bomb;
0022 class Block;
0023 class PlayerSettings;
0024 class QTimer;
0025 class QKeyEvent;
0026 
0027 class KGameSound;
0028 
0029 /**
0030  * @brief This class manages the game main loop : it regularly checks the key press events, computes the character moves and updates their coordinates.
0031  */
0032 class Game : public QObject
0033 {
0034 
0035 Q_OBJECT
0036 
0037 private :
0038 
0039     /** Number of FPS */
0040     static const int FPS;
0041 
0042     /** The game different states : RUNNING, PAUSED_LOCKED, PAUSED_UNLOCKED */
0043     enum State
0044     {
0045         RUNNING,            // Game running
0046         PAUSED_LOCKED,      // Game paused and user is not allowed to unpause
0047         PAUSED_UNLOCKED     // Game paused and user is allowed to unpause
0048     };
0049     /** A flag for the State enum */
0050     Q_DECLARE_FLAGS(GameStates, State)
0051 
0052     /** The game state */
0053     State m_state;
0054 
0055     /** The Game main timer */
0056     QTimer* m_timer;
0057 
0058     /** The Round timer */
0059     QTimer* m_roundTimer;
0060 
0061     /** The timer to add a little delay after the round is finished after there's at most only one player alive */
0062     QTimer* m_setRoundFinishedTimer;
0063 
0064     /** The gamecene */
0065     GameScene* m_gameScene;
0066 
0067     /** The player settings */
0068     PlayerSettings* m_playerSettings;
0069 
0070     /** The Arena */
0071     Arena* m_arena;
0072 
0073     /** List with arenas for the random arena mode */
0074     QStringList m_randomArenaModeArenaList;
0075 
0076     /** The Players */
0077     QList<Player*> m_players;
0078 
0079     /** The Bombs */
0080     QList<Bomb*> m_bombs;
0081 
0082     /** The Blocks */
0083     QList<Block*> m_blocks;
0084 
0085     /** The Bonuses */
0086     QList<Bonus*> m_bonus;
0087 
0088     /** The absolute number of bombs, used for the bomb and explosion ID */
0089     int m_bombCount;
0090 
0091     /** The points which are needed to win */
0092     int m_winPoints;
0093 
0094     /** The remaining time for a round */
0095     int m_remainingTime;
0096 
0097     /** Flag if the game is over */
0098     bool m_gameOver;
0099 
0100     /** Name of the playe who won the game */
0101     QString m_strWinner;
0102 
0103     /** Flag if sound is enabled */
0104     bool m_soundEnabled;
0105 
0106     /** Flag to use wilhelm scream for dying  */
0107     bool m_wilhelmScream;
0108 
0109     KGameSound* m_soundPutBomb;
0110     KGameSound* m_soundExplode;
0111     KGameSound* m_soundBonus;
0112     KGameSound* m_soundFalling;
0113     KGameSound* m_soundDie;
0114 
0115 public:
0116 
0117     /**
0118     * Creates a new Game instance.
0119     * @param playerSettings the player settings
0120     */
0121     explicit Game(PlayerSettings* playerSettings);
0122 
0123     /**
0124     * Deletes the Game instance.
0125     */
0126     ~Game() override;
0127 
0128     /**
0129     * Starts the Game.
0130     */
0131     void start();
0132 
0133     /**
0134     * Pauses the Game.
0135     * @param p_locked if true the player will be unable to unset the pause.
0136     */
0137     void pause(bool p_locked = false);
0138 
0139     /**
0140     * Pauses / unpauses the game.
0141     */
0142     void switchPause();
0143 
0144     /**
0145     * Enables / disables the sounds.
0146     * @param p_enabled if true the sounds will be enabled, otherwise they will be disabled
0147     */
0148     void setSoundsEnabled(bool p_enabled);
0149 
0150     /**
0151     * @return the Arena instance
0152     */
0153     Arena* getArena() const;
0154 
0155     /**
0156     * @return the Player models
0157     */
0158     QList<Player*> getPlayers() const;
0159 
0160     /**
0161     * @return the Bonus instance
0162     */
0163     QList<Bonus*> getBonus() const;
0164 
0165     /**
0166     * @return the main timer
0167     */
0168     QTimer* getTimer() const;
0169 
0170     /**
0171     * @return the remaining round time
0172     */
0173     int getRemainingTime() const;
0174 
0175     /**
0176     * @return true if the Game is paused, false otherwise
0177     */
0178     bool isPaused() const;
0179 
0180     /**
0181     * @return flag if the game is over
0182     */
0183     bool getGameOver() const;
0184 
0185     /**
0186     * @return the winner of the game
0187     */
0188     QString getWinner() const;
0189 
0190      /**
0191     * @return the points needed to win the game
0192     */
0193     int getWinPoints() const;
0194 
0195     /**
0196     * Create the hidden Bonuses
0197     */
0198     void createBonus();
0199 
0200     /**
0201     * @param bonus the Bonus to remove
0202     */
0203     void removeBonus(Bonus* bonus);
0204 
0205     /**
0206     * remove Block from list and decide to give bonus
0207     */
0208     void blockDestroyed(const int row, const int col, Block* block);
0209 
0210     /**
0211     * Removes exploded bombs from the bomb list
0212     */
0213     void removeBomb(Bomb* bomb);
0214 
0215     /**
0216     * Sets the games gamescene
0217     * @param p_gameScene the gamescene
0218     */
0219     void setGameScene(GameScene* p_gameScene);
0220 
0221 private:
0222 
0223     /**
0224     * Initializes class
0225     */
0226     void init();
0227     /**
0228     * Cleans class
0229     */
0230     void cleanUp();
0231 
0232     /**
0233     * Initializes the character coordinates.
0234     */
0235     void initCharactersPosition();
0236 
0237 public Q_SLOTS:
0238 
0239     /**
0240     * Manages the key press events.
0241     * @param p_event the key press event
0242     */
0243     void keyPressEvent(QKeyEvent* p_event);
0244 
0245     /**
0246     * Manages the key release events.
0247     * @param p_event the key release event
0248     */
0249     void keyReleaseEvent(QKeyEvent* p_event);
0250 
0251     /**
0252     * Checks if the round has finished and set it finished.
0253     */
0254     void setRoundFinished();
0255 
0256     /**
0257     * Creates a bomb in the Cell with the coordinates x and y or throws the bomb from that possition if the player has the throw bonus
0258     * @param player the player who dropped the bomb
0259     * @param x the x-position where to put the bomb
0260     * @param y the x-position where to put the bomb
0261     * @param newBomb flag if a new bomb can be created or if only throwing is possible
0262     * @param throwDistance the throw distance if the player has the throw bonus and the bomb shall be thrown
0263     */
0264     void createBomb(Player* player, qreal x, qreal y, bool newBomb, int throwDistance);
0265 
0266 private Q_SLOTS:
0267 
0268     /**
0269     * Updates the Game data.
0270     */
0271     void update();
0272 
0273     /**
0274     * Plays the sound of a player falling in a hole.
0275     */
0276     void playerFalling();
0277 
0278     /**
0279     * Manages the loss of a life.
0280     */
0281     void playerDeath();
0282 
0283     /**
0284     * Resurrects the death players
0285     */
0286     void resurrectBonusTaken();
0287 
0288     /**
0289     * Plays the detonation sound
0290     */
0291     void bombDetonated();
0292 
0293     /**
0294     * Decrement the remaining round time
0295     */
0296     void decrementRemainingRoundTime();
0297 
0298 Q_SIGNALS:
0299 
0300     /**
0301     * Emitted when the Game is started.
0302     */
0303     void gameStarted();
0304 
0305     /**
0306     * Emitted when the Game is over.
0307     */
0308     void gameOver();
0309 
0310     /**
0311     * Emitted when the pause state has changed.
0312     * @param p_pause true if the Game is paused, false otherwise
0313     * @param p_fromUser true if the Game has been paused due to an action the player has done, false otherwise
0314     */
0315     void pauseChanged(const bool p_pause, const bool p_fromUser);
0316 
0317     /**
0318     * Emitted when a bomb was created.
0319     */
0320     void bombCreated(Bomb* bomb);
0321 
0322     /**
0323     * Emitted when something to display has changed.
0324     */
0325     void infoChanged(const Granatier::Info::Type p_info);
0326 };
0327 
0328 #endif
0329