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 // own
8 #include "explodable.h"
9 
10 // Qt
11 #include <QRandomGenerator>
12 
13 // Bomber
14 #include "board.h"
15 
16 /**
17  * How big is the explosion in relation to the tiles height.
18  * 1.0 means it's the same size as the tile.
19  */
20 const qreal Explodable::EXPLOSION_RELATIVE_SIZE_H = 1.0;
21 /**
22  * How big is the explosion in relation to the tiles width.
23  * 1.0 means it's the same size as the tile.
24  */
25 const qreal Explodable::EXPLOSION_RELATIVE_SIZE_W = 1.0;
26 
Explodable(const QString & mainSvg,const QString & explosionSvg,qreal relativeWidth,qreal relativeHeight,KGameRenderer * renderer,BomberBoard * board)27 Explodable::Explodable(const QString & mainSvg, const QString & explosionSvg,
28                        qreal relativeWidth, qreal relativeHeight, KGameRenderer * renderer,
29                        BomberBoard * board)
30     : KGameRenderedItem(renderer, mainSvg)
31     , m_board(board)
32     , m_mainSvg(mainSvg)
33     , m_explosionSvg(explosionSvg)
34 {
35     setRenderSize(QSize(32, 64));
36     m_relativeWidth = relativeWidth;
37     m_relativeHeight = relativeHeight;
38     resetPixmaps();
39     m_state = State::Moving;
40     m_nextBoundingRect.setSize(QSizeF(m_relativeWidth, m_relativeHeight));
41     setPos(m_board->mapPosition(QPointF(m_xPos, m_yPos)));
42 }
43 
~Explodable()44 Explodable::~Explodable()
45 {
46 }
47 
setPosition(qreal xPos,qreal yPos)48 void Explodable::setPosition(qreal xPos, qreal yPos)
49 {
50     m_xPos = xPos, m_yPos = yPos;
51     m_nextBoundingRect.moveTo(m_xPos, m_yPos);
52 }
53 
update()54 void Explodable::update()
55 {
56     setFrame(frame() + 1);
57     setPos(m_board->mapPosition(QPointF(m_xPos, m_yPos)));
58 }
59 
resize(const QSize & tileSize)60 void Explodable::resize(const QSize & tileSize)
61 {
62     m_lastSize = tileSize;
63     if (m_state == State::Moving) {
64         setRenderSize(QSize(
65             m_relativeWidth * tileSize.width(),
66             m_relativeHeight * tileSize.height()));
67     } else {
68         setRenderSize(QSize(
69             EXPLOSION_RELATIVE_SIZE_W * tileSize.width(),
70             EXPLOSION_RELATIVE_SIZE_H * tileSize.height()));
71     }
72     setPos(m_board->mapPosition(QPointF(m_xPos, m_yPos)));
73 }
74 
setVelocity(qreal vX)75 void Explodable::setVelocity(qreal vX)
76 {
77     m_velocity = vX;
78 }
79 
setRandomFrame()80 void Explodable::setRandomFrame()
81 {
82     setFrame(QRandomGenerator::global()->generate());
83 }
84 
85 /**
86  * Returns bomb's bounding rect expected in next frame
87  * used by collision test
88  */
nextBoundingRect() const89 QRectF Explodable::nextBoundingRect() const
90 {
91     return m_nextBoundingRect;
92 }
93 
setState(Explodable::State state)94 void Explodable::setState(Explodable::State state)
95 {
96     m_state = state;
97     setRandomFrame();
98     if (m_state == State::Moving) {
99         m_nextBoundingRect.setSize(QSizeF(m_relativeWidth, m_relativeHeight));
100         setSpriteKey(m_mainSvg);
101     } else {
102         m_nextBoundingRect.setSize(QSizeF(EXPLOSION_RELATIVE_SIZE_W, EXPLOSION_RELATIVE_SIZE_H));
103         setSpriteKey(m_explosionSvg);
104     }
105     resize(m_lastSize);
106 }
107 
position() const108 QPointF Explodable::position() const
109 {
110     return QPointF(m_xPos, m_yPos);
111 }
112 
resetPixmaps()113 void Explodable::resetPixmaps()
114 {
115     setFrame(0);
116 }
117 
state() const118 Explodable::State Explodable::state() const
119 {
120     return m_state;
121 }
122 
velocity() const123 qreal Explodable::velocity() const
124 {
125     return m_velocity;
126 }
127