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

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 John-Paul Stanford <jp@stanwood.org.uk>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 // own
0008 #include "explodable.h"
0009 
0010 // Qt
0011 #include <QRandomGenerator>
0012 
0013 // Bomber
0014 #include "board.h"
0015 
0016 /**
0017  * How big is the explosion in relation to the tiles height.
0018  * 1.0 means it's the same size as the tile.
0019  */
0020 const qreal Explodable::EXPLOSION_RELATIVE_SIZE_H = 1.0;
0021 /**
0022  * How big is the explosion in relation to the tiles width.
0023  * 1.0 means it's the same size as the tile.
0024  */
0025 const qreal Explodable::EXPLOSION_RELATIVE_SIZE_W = 1.0;
0026 
0027 Explodable::Explodable(const QString & mainSvg, const QString & explosionSvg,
0028                        qreal relativeWidth, qreal relativeHeight, KGameRenderer * renderer,
0029                        BomberBoard * board)
0030     : KGameRenderedItem(renderer, mainSvg)
0031     , m_board(board)
0032     , m_mainSvg(mainSvg)
0033     , m_explosionSvg(explosionSvg)
0034 {
0035     setRenderSize(QSize(32, 64));
0036     m_relativeWidth = relativeWidth;
0037     m_relativeHeight = relativeHeight;
0038     resetPixmaps();
0039     m_state = State::Moving;
0040     m_nextBoundingRect.setSize(QSizeF(m_relativeWidth, m_relativeHeight));
0041     setPos(m_board->mapPosition(QPointF(m_xPos, m_yPos)));
0042 }
0043 
0044 Explodable::~Explodable()
0045 {
0046 }
0047 
0048 void Explodable::setPosition(qreal xPos, qreal yPos)
0049 {
0050     m_xPos = xPos, m_yPos = yPos;
0051     m_nextBoundingRect.moveTo(m_xPos, m_yPos);
0052 }
0053 
0054 void Explodable::update()
0055 {
0056     setFrame(frame() + 1);
0057     setPos(m_board->mapPosition(QPointF(m_xPos, m_yPos)));
0058 }
0059 
0060 void Explodable::resize(const QSize & tileSize)
0061 {
0062     m_lastSize = tileSize;
0063     if (m_state == State::Moving) {
0064         setRenderSize(QSize(
0065             m_relativeWidth * tileSize.width(),
0066             m_relativeHeight * tileSize.height()));
0067     } else {
0068         setRenderSize(QSize(
0069             EXPLOSION_RELATIVE_SIZE_W * tileSize.width(),
0070             EXPLOSION_RELATIVE_SIZE_H * tileSize.height()));
0071     }
0072     setPos(m_board->mapPosition(QPointF(m_xPos, m_yPos)));
0073 }
0074 
0075 void Explodable::setVelocity(qreal vX)
0076 {
0077     m_velocity = vX;
0078 }
0079 
0080 void Explodable::setRandomFrame()
0081 {
0082     setFrame(QRandomGenerator::global()->generate());
0083 }
0084 
0085 /**
0086  * Returns bomb's bounding rect expected in next frame
0087  * used by collision test
0088  */
0089 QRectF Explodable::nextBoundingRect() const
0090 {
0091     return m_nextBoundingRect;
0092 }
0093 
0094 void Explodable::setState(Explodable::State state)
0095 {
0096     m_state = state;
0097     setRandomFrame();
0098     if (m_state == State::Moving) {
0099         m_nextBoundingRect.setSize(QSizeF(m_relativeWidth, m_relativeHeight));
0100         setSpriteKey(m_mainSvg);
0101     } else {
0102         m_nextBoundingRect.setSize(QSizeF(EXPLOSION_RELATIVE_SIZE_W, EXPLOSION_RELATIVE_SIZE_H));
0103         setSpriteKey(m_explosionSvg);
0104     }
0105     resize(m_lastSize);
0106 }
0107 
0108 QPointF Explodable::position() const
0109 {
0110     return QPointF(m_xPos, m_yPos);
0111 }
0112 
0113 void Explodable::resetPixmaps()
0114 {
0115     setFrame(0);
0116 }
0117 
0118 Explodable::State Explodable::state() const
0119 {
0120     return m_state;
0121 }
0122 
0123 qreal Explodable::velocity() const
0124 {
0125     return m_velocity;
0126 }