File indexing completed on 2024-05-05 04:02:03

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 John-Paul Stanford <jp@stanwood.org.uk>
0003     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef BOMBER_WIDGET_H
0009 #define BOMBER_WIDGET_H
0010 
0011 // Qt
0012 #include <QGraphicsView>
0013 #include <QMouseEvent>
0014 
0015 // KDEGames
0016 #include <KGameSound>
0017 
0018 // Bomber
0019 #include "board.h"
0020 
0021 class KGameThemeProvider;
0022 
0023 /**
0024  * This is the main game widget class. It manages things like the lives, source counts and game states.
0025  */
0026 class BomberGameWidget : public QGraphicsView
0027 {
0028     Q_OBJECT
0029 
0030 public:
0031     /**
0032      * The different states that the game can be in
0033      */
0034     enum class State { BeforeFirstGame,
0035                        Running,
0036                        BetweenLevels,
0037                        Paused,
0038                        Suspended,
0039                        GameOver };
0040 
0041     explicit BomberGameWidget(KGameThemeProvider * provider, QWidget * parent = nullptr);
0042     ~BomberGameWidget() override;
0043 
0044     /**
0045      * Used to find out the current level
0046      * \return The current level
0047      */
0048     unsigned int level() const;
0049 
0050     /**
0051      * Used to find the current score
0052      * \return the current score
0053      */
0054     unsigned int score() const;
0055 
0056     /**
0057      * Used to return the game sate
0058      * \return The game state
0059      */
0060     BomberGameWidget::State state() const;
0061 
0062 public Q_SLOTS:
0063     /**
0064      * Used to end the game
0065      */
0066     void closeGame();
0067 
0068     /**
0069      * Used to start a new game
0070      */
0071     void newGame();
0072 
0073     /**
0074      * Used to pause the game
0075      * \param paused True to pause the game, False to resume the game
0076      */
0077     void setPaused(bool paused);
0078 
0079     /**
0080      * This is called when the settings change to save the settings
0081      */
0082     void settingsChanged();
0083 
0084     /**
0085      * Enabled or disabled the sounds
0086      * \param enable True to enable the sounds, false to disable
0087      */
0088     void setSoundsEnabled(bool enable);
0089 
0090     /**
0091      * Used to toggle the suspended game state. If val is true and the state is currently running,
0092      * them set the sate to suspended. If value is false and the state is suspended, set the sate to running.
0093      * \param value True to set state to suspended, False to set state to running.
0094      */
0095     void setSuspended(bool value);
0096 
0097     /**
0098      * This is called when the drop bomb key is press or the mouse in the game area.
0099      * If the game state is running, If it is between levels, then a new level is created and
0100      * if it's BeforeFirstGame then a new game is started.
0101      */
0102     void onDropBomb();
0103 
0104 Q_SIGNALS:
0105     /**
0106      * This is emitted when the game is over
0107      */
0108     void gameOver();
0109 
0110     /**
0111      * This is emitted when the level changes.
0112      * \param level The new level
0113      */
0114     void levelChanged(unsigned int level);
0115 
0116     /**
0117      * This is emitted when the source changes
0118      * \param score The new score
0119      */
0120     void scoreChanged(unsigned int score);
0121 
0122     /**
0123      * This is emitted when the lives change
0124      * \param lives The new lives
0125      */
0126     void livesChanged(unsigned int lives);
0127 
0128     /**
0129      * This is emitted when the time changes
0130      * \param time The new time value
0131      */
0132     void timeChanged(unsigned int time);
0133 
0134     /**
0135      * This is emitted when the game state changes
0136      * \param state The new game state
0137      */
0138     void stateChanged(BomberGameWidget::State state);
0139 
0140 private Q_SLOTS:
0141     void tick();
0142 
0143     /**
0144      * This is called when a bomb hits a building
0145      */
0146     void onBombHit();
0147 
0148     /**
0149      * This slot is called when a level has been cleared to change the state and get
0150      * ready for the next level.
0151      */
0152     void onLevelCleared();
0153 
0154     /**
0155      * This slot is called when the plane crashes
0156      */
0157     void onPlaneCrashed();
0158 
0159 private:
0160     void resizeEvent(QResizeEvent * event) override;
0161     void mouseReleaseEvent(QMouseEvent * event) override;
0162 
0163     /**
0164      * Create the overlay used to display info to the user. The info will
0165      * change depending on the game state.
0166      */
0167     void generateOverlay();
0168     void redraw();
0169 
0170     void closeLevel();
0171     void newLevel();
0172 
0173     void playBombSound();
0174     void playCrashSound();
0175 
0176     State m_state;
0177     unsigned int m_level;
0178     unsigned int m_score;
0179     unsigned int m_lives;
0180     unsigned int m_time;
0181 
0182     /**
0183      * Used to store the remaining score before a new life is given
0184      */
0185     int m_scoreLeftBeforeNewLife;
0186 
0187     KGameRenderer m_renderer;
0188     QTimer * m_clock;
0189     BomberBoard * m_board;
0190 
0191     QGraphicsPixmapItem * m_overlay;
0192 
0193     KGameSound m_soundBomb;
0194     KGameSound m_soundCrash;
0195 };
0196 
0197 #endif