File indexing completed on 2024-02-18 04:03:23

0001 /*
0002     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
0003     SPDX-FileCopyrightText: 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "bombexplosionitem.h"
0009 #include "bomb.h"
0010 
0011 #include <QGraphicsView>
0012 #include <KGameRenderer>
0013 
0014 BombExplosionItem::BombExplosionItem(Bomb* p_model, Granatier::Direction::Type direction, int bombPower, KGameRenderer* renderer, qreal svgScaleFactor) : KGameRenderedItem(renderer, QStringLiteral(""))
0015 {
0016     m_direction = direction;
0017     m_explosionID = p_model->explosionID();
0018 
0019     m_svgScaleFactor = svgScaleFactor;
0020 
0021     int width = 1;
0022     int height = 1;
0023     switch(m_direction)
0024     {
0025         case Granatier::Direction::NORTH:
0026             setSpriteKey(QStringLiteral("bomb_blast_north_0"));
0027             width = static_cast<int>(Granatier::CellSize * 0.5);
0028             height = static_cast<int>(Granatier::CellSize * 1.5);
0029             break;
0030         case Granatier::Direction::EAST:
0031             setSpriteKey(QStringLiteral("bomb_blast_east_0"));
0032             width = static_cast<int>(Granatier::CellSize * 1.5);
0033             height = static_cast<int>(Granatier::CellSize * 0.5);
0034             break;
0035         case Granatier::Direction::SOUTH:
0036             setSpriteKey(QStringLiteral("bomb_blast_south_0"));
0037             width = static_cast<int>(Granatier::CellSize * 0.5);
0038             height = static_cast<int>(Granatier::CellSize * 1.5);
0039             break;
0040         case Granatier::Direction::WEST:
0041             setSpriteKey(QStringLiteral("bomb_blast_west_0"));
0042             width = static_cast<int>(Granatier::CellSize * 1.5);
0043             height = static_cast<int>(Granatier::CellSize * 0.5);
0044             break;
0045     }
0046 
0047     if((static_cast<int>(Granatier::CellSize) - width) % 2 != 0)
0048     {
0049         width--;
0050     }
0051     if((static_cast<int>(Granatier::CellSize) - height) % 2 != 0)
0052     {
0053         height--;
0054     }
0055     m_itemSizeSet = QSize(width, height);
0056 
0057     QSize svgSize = m_itemSizeSet / m_svgScaleFactor;
0058     setRenderSize(svgSize);
0059     setScale(m_svgScaleFactor);
0060 
0061     setZValue(303 + bombPower);
0062 }
0063 
0064 BombExplosionItem::~BombExplosionItem()
0065 = default;
0066 
0067 QPainterPath BombExplosionItem::shape() const
0068 {
0069     QPainterPath path;
0070     // Temporary variable to keep the boundingRect available
0071     QRectF rect = boundingRect();
0072 
0073     // Calculation of the shape
0074     QRectF shapeRect = QRectF(rect.x(), rect.y(), rect.width(), rect.height());
0075     path.addEllipse(shapeRect);
0076     return path;
0077 }
0078 
0079 int BombExplosionItem::explosionID()
0080 {
0081     return m_explosionID;
0082 }
0083 
0084 void BombExplosionItem::setPosition(qreal p_x, qreal p_y)
0085 {
0086     qreal x;
0087     qreal y;
0088 
0089     switch(m_direction)
0090     {
0091         case Granatier::Direction::NORTH:
0092             x = p_x - m_itemSizeSet.width() / 2;
0093             y = p_y - Granatier::CellSize/2;
0094             setPos(x, y);
0095             break;
0096         case Granatier::Direction::EAST:
0097             x = p_x - Granatier::CellSize;
0098             y = p_y - m_itemSizeSet.height() / 2;
0099             setPos(x, y);
0100             break;
0101         case Granatier::Direction::SOUTH:
0102             x = p_x - m_itemSizeSet.width() / 2;
0103             y = p_y - Granatier::CellSize;
0104             setPos(x, y);
0105             break;
0106         case Granatier::Direction::WEST:
0107             x = p_x - Granatier::CellSize/2;
0108             y = p_y - m_itemSizeSet.height() / 2;
0109             setPos(x, y);
0110             break;
0111     }
0112 }
0113 
0114 void BombExplosionItem::updateAnimationn(int nFrame)
0115 {
0116     switch(m_direction)
0117     {
0118         case Granatier::Direction::NORTH:
0119             setSpriteKey(QStringLiteral("bomb_blast_north_%1").arg(nFrame));
0120             break;
0121         case Granatier::Direction::EAST:
0122             setSpriteKey(QStringLiteral("bomb_blast_east_%1").arg(nFrame));
0123             break;
0124         case Granatier::Direction::SOUTH:
0125             setSpriteKey(QStringLiteral("bomb_blast_south_%1").arg(nFrame));
0126             break;
0127         case Granatier::Direction::WEST:
0128             setSpriteKey(QStringLiteral("bomb_blast_west_%1").arg(nFrame));
0129             break;
0130     }
0131 }
0132 
0133 void BombExplosionItem::updateGraphics(qreal svgScaleFactor)
0134 {
0135     if(scene()->views().isEmpty())
0136     {
0137         return;
0138     }
0139 
0140     //calculate the size of the item on the view
0141     QPoint viewTopLeft = scene()->views().first()->mapFromScene(0, 0);
0142     QPoint viewBottomRight = scene()->views().first()->mapFromScene(m_itemSizeSet.width(), m_itemSizeSet.height());
0143 
0144     int viewWidth = viewBottomRight.x() - viewTopLeft.x();
0145     int viewHeight = viewBottomRight.y() - viewTopLeft.y();
0146 
0147     //for better alignment, if the item size is different from the cell size, make the difference between the cell size and item size always even
0148     if(m_itemSizeSet.width() != Granatier::CellSize || m_itemSizeSet.height() != Granatier::CellSize)
0149     {
0150         viewBottomRight = scene()->views().first()->mapFromScene(Granatier::CellSize, Granatier::CellSize);
0151         int viewCellWidth = viewBottomRight.x() - viewTopLeft.x();
0152         int viewCellHeight = viewBottomRight.y() - viewTopLeft.y();
0153         if((viewCellWidth - viewWidth) % 2 != 0)
0154         {
0155             viewWidth--;
0156         }
0157         if((viewCellHeight - viewHeight) % 2 != 0)
0158         {
0159             viewHeight--;
0160         }
0161 
0162         //calculate the real item size after change of the render size
0163         QPointF sceneTopLeft = scene()->views().first()->mapToScene(0, 0);
0164         QPointF sceneBottomRight = scene()->views().first()->mapToScene(viewWidth, viewHeight);
0165 
0166         int sceneWidth = static_cast<int>(sceneBottomRight.x() - sceneTopLeft.x());
0167         int sceneHeight = static_cast<int>(sceneBottomRight.y() - sceneTopLeft.y());
0168         m_itemSizeReal = QSize(sceneWidth, sceneHeight);
0169     }
0170 
0171     setRenderSize(QSize(viewWidth, viewHeight));
0172     setScale(svgScaleFactor);
0173     m_svgScaleFactor = svgScaleFactor;
0174 
0175     QTransform transform;
0176     transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
0177     setRenderSize(renderSize());
0178     transform.translate(-m_itemSizeReal.width() / 2.0, -m_itemSizeReal.height() / 2.0);
0179     setTransform(transform);
0180 }
0181 
0182 #include "moc_bombexplosionitem.cpp"