1 /*
2     SPDX-FileCopyrightText: 2007-2008 John-Paul Stanford <jp@stanwood.org.uk>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #ifndef BOARD_H
8 #define BOARD_H
9 
10 // Qt
11 #include <QGraphicsScene>
12 #include <QList>
13 #include <QQueue>
14 #include <QSize>
15 
16 // KDEGames
17 #include <KGameRenderer>
18 
19 class Plane;
20 class Building;
21 class Bomb;
22 
23 /**
24  * This class used to represent the game board. This makes sure all the game objects
25  * get moved and redrawn every second. It also checks for any collisions
26  */
27 class BomberBoard : public QGraphicsScene
28 {
29     Q_OBJECT
30 
31 public:
32     /**
33      * The constructor used to create the board.
34      * \param renderer The renderer used to render game objects
35      * \param view The graphics view object which this board is bound to
36      * \param parent The widget which the board is inserted into
37      */
38     explicit BomberBoard(KGameRenderer * renderer, QGraphicsView * view, QObject * parent = nullptr);
39 
40     ~BomberBoard() override;
41 
42     /**
43      * This is called when the game board is resized
44      * \param size The new tile size used on the game board
45      */
46     void resize(QSize & size);
47 
48     /**
49      * This will redraw the game board
50      */
51     void redraw();
52 
53     /**
54      * This is called to start a new level
55      * \param level The level number been started
56      */
57     void newLevel(unsigned int level);
58 
59     /**
60      * This is called to pause the game.
61      * \param val True if paused, otherwise false
62      */
63     void setPaused(bool val);
64 
65     /**
66      * This will convert the tile location to actual cords on the board
67      * \param pos The cords relative to the tile
68      * \return The cords relative to the widget
69      */
70     QPoint mapPosition(const QPointF & pos) const;
71 
72     /**
73      * This will convert the widget location to tile locations
74      * \param pos The cords relative to the widget
75      * \return The cords relative to the tile
76      */
77     QPointF unmapPosition(const QPoint & pos) const;
78 
79     /**
80      * Used to set the plane state to flying and move it to the start position
81      */
82     void resetPlane();
83 
84     /**
85      * This will attempt to drop a bomb if their is not already a bomb dropping
86      */
87     void dropBomb();
88 
89 Q_SIGNALS:
90     /**
91      * This is emitted when a plane crashes into a building
92      */
93     void onPlaneCrash();
94 
95     void playBombSound();
96     void playCrashSound();
97 
98     /**
99      * This signal is emitted when a bomb hits a building and before it's exploding
100      * animation starts
101      */
102     void onBombHit();
103 
104     /**
105      * This is emitted when the level has been cleared of all buildings
106      */
107     void levelCleared();
108 
109 public Q_SLOTS:
110     /**
111      * This is called when the settings change to save the settings
112      */
113     void settingsChanged();
114 
115 private Q_SLOTS:
116     /**
117      * This is called once a second to update and draw all the game objects
118      */
119     void tick();
120 
121     /**
122      * This is called when a plane has finished exploding
123      */
124     void planeExploded();
125 
126     /**
127      * This is called when a bomb has finished exploding
128      */
129     void bombExploded();
130 
131 private:
132     /**
133      * This is called when a bomb hits a building
134      * \param bomb The bomb that hit
135      * \param moveBombToX The x position to move the explosion too
136      * \param moveBombToY The y position to move the explosion too
137      */
138     void bombHit(Bomb * bomb, qreal moveBombToX, qreal moveBombToY);
139 
140     /**
141      * This is used to remove all the current game objects, usually called before
142      * stating a new level
143      */
144     void clear();
145 
146     /**
147      * This is used to check for any collisions of the plane or bombs
148      */
149     void checkCollisions();
150 
151     /**
152      * This is called when a plane crashes into a building
153      */
154     void crashed();
155 
156     /**
157      * This is the renderer used to render game objects
158      */
159     KGameRenderer * m_renderer;
160 
161     /**
162      * This is the size of a tiling block
163      */
164     QSize m_tileSize;
165     QTimer * m_clock;
166 
167     /**
168      * If their is a bomb currently dropping then it is pointed to by this
169      */
170     Bomb * m_bomb;
171 
172     /**
173      * This points to the plane object used in the level
174      */
175     Plane * m_plane;
176 
177     /**
178      * This contains all the buildings in the current level
179      */
180     QList<Building *> m_buildings;
181 
182     /**
183      * This contains all the bombs that are currently exploding
184      */
185     QQueue<Bomb *> m_explodingBombs;
186 
187     /**
188      * This is the number of blocks that make up the buildings
189      */
190     unsigned int m_buildingBlocks;
191 
192     /**
193      * This is the graphics view object which this board is bound.
194      */
195     QGraphicsView * m_view;
196 };
197 
198 #endif
199