File indexing completed on 2024-05-12 05:46:43

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