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 EXPLODABLE_H
8 #define EXPLODABLE_H
9 
10 // KDEGames
11 #include <KGameRenderedItem>
12 
13 class BomberBoard;
14 
15 /**
16  * This is a game object that can explode.
17  */
18 class Explodable : public KGameRenderedItem
19 {
20 public:
21     /** The states that a bomb can be in */
22     enum class State {
23         Moving,
24         Exploding,
25         Exploded
26     };
27 
28     /** The width of the explosion relative to the tile */
29     static const qreal EXPLOSION_RELATIVE_SIZE_W;
30     /** The height of the explosion relative to the tile */
31     static const qreal EXPLOSION_RELATIVE_SIZE_H;
32 
33     explicit Explodable(const QString & mainSvg, const QString & explosionSvg,
34                         qreal relativeWidth, qreal relativeHeight,
35                         KGameRenderer * renderer, BomberBoard * board);
36     ~Explodable() override;
37 
38     /**
39      * Updates bomb position and pixmap.
40      * This method is called once per frame.
41      */
42     void update();
43 
44     /**
45      * Sets width and height of bomb. Using the current tile size.
46      * \param tileSize The size of the tile
47      */
48     void resize(const QSize & tileSize);
49 
50     /**
51      * Sets the velocity of the explodable object
52      * \param velocity The velocity of the object
53      */
54     void setVelocity(qreal velocity);
55 
56     /**
57      * Used to get the velocity of the explodable object
58      * \return The velocity
59      */
60     qreal velocity() const;
61 
62     /**
63      * Rechecks the number of frames of bomb animation and sets new pixmaps.
64      * This method is useful when changing game theme.
65      */
66     void resetPixmaps();
67 
68     /**
69      * Used to set the bomb's position
70      * \param xPos Set the x position of the item
71      * \param yPos Set the y position of the item
72      */
73     void setPosition(qreal xPos, qreal yPos);
74 
75     /**
76      * Return the position of the bomb
77      * \return The position of the bomb
78      */
79     QPointF position() const;
80 
81     /**
82      * Set the current frame to a random frame from the frame set
83      */
84     void setRandomFrame();
85 
86     /**
87      * Returns bomb's bounding rect expected in next frame
88      * used by collision test
89      * \return The items next bounding rect
90      */
91     QRectF nextBoundingRect() const;
92 
93     /**
94      * Get the current state of the bomb
95      * \return The current state of the bomb
96      */
97     Explodable::State state() const;
98 
99     /**
100      * Set the current state of the bomb
101      * \param state The state to set the bomb too
102      */
103     void setState(Explodable::State state);
104 
105 protected:
106     qreal m_xPos;
107     qreal m_yPos;
108     QRectF m_nextBoundingRect;
109 
110 private:
111     BomberBoard * m_board;
112 
113     qreal m_velocity;
114 
115     /**
116      * The state the bomb is currently in
117      */
118     State m_state;
119 
120     /** The tile size last time the bomb was resized */
121     QSize m_lastSize;
122 
123     qreal m_relativeHeight;
124     qreal m_relativeWidth;
125     QString m_mainSvg;
126     QString m_explosionSvg;
127 };
128 
129 #endif
130