• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

granatier

  • sources
  • kde-4.14
  • kdegames
  • granatier
  • src
bombexplosionitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Mathias Kraus <k.hias@gmx.de>
3  * Copyright 2007-2008 Thomas Gallinari <tg8187@yahoo.fr>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "bombexplosionitem.h"
20 #include "bomb.h"
21 
22 #include <QGraphicsView>
23 #include <KGameRenderer>
24 
25 BombExplosionItem::BombExplosionItem(Bomb* p_model, Granatier::Direction::Type direction, int bombPower, KGameRenderer* renderer, qreal svgScaleFactor) : KGameRenderedItem(renderer, "")
26 {
27  m_direction = direction;
28  m_explosionID = p_model->explosionID();
29 
30  m_svgScaleFactor = svgScaleFactor;
31 
32  QString strElemetId;
33  int width = 1;
34  int height = 1;
35  switch(m_direction)
36  {
37  case Granatier::Direction::NORTH:
38  setSpriteKey("bomb_blast_north_0");
39  width = Granatier::CellSize * 0.5;
40  height = Granatier::CellSize * 1.5;
41  break;
42  case Granatier::Direction::EAST:
43  setSpriteKey("bomb_blast_east_0");
44  width = Granatier::CellSize * 1.5;
45  height = Granatier::CellSize * 0.5;
46  break;
47  case Granatier::Direction::SOUTH:
48  setSpriteKey("bomb_blast_south_0");
49  width = Granatier::CellSize * 0.5;
50  height = Granatier::CellSize * 1.5;
51  break;
52  case Granatier::Direction::WEST:
53  setSpriteKey("bomb_blast_west_0");
54  width = Granatier::CellSize * 1.5;
55  height = Granatier::CellSize * 0.5;
56  break;
57  }
58 
59  if(((int) Granatier::CellSize - width) % 2 != 0)
60  {
61  width--;
62  }
63  if(((int) Granatier::CellSize - height) % 2 != 0)
64  {
65  height--;
66  }
67  m_itemSizeSet = QSize(width, height);
68 
69  QSize svgSize = m_itemSizeSet / m_svgScaleFactor;
70  setRenderSize(svgSize);
71  setScale(m_svgScaleFactor);
72 
73  setZValue(303 + bombPower);
74 }
75 
76 BombExplosionItem::~BombExplosionItem()
77 {
78 }
79 
80 QPainterPath BombExplosionItem::shape() const
81 {
82  QPainterPath path;
83  // Temporary variable to keep the boundingRect available
84  QRectF rect = boundingRect();
85 
86  // Calculation of the shape
87  QRectF shapeRect = QRectF(rect.x(), rect.y(), rect.width(), rect.height());
88  path.addEllipse(shapeRect);
89  return path;
90 }
91 
92 int BombExplosionItem::explosionID()
93 {
94  return m_explosionID;
95 }
96 
97 void BombExplosionItem::setPosition(qreal p_x, qreal p_y)
98 {
99  qreal x;
100  qreal y;
101 
102  switch(m_direction)
103  {
104  case Granatier::Direction::NORTH:
105  x = p_x - m_itemSizeSet.width() / 2;
106  y = p_y - Granatier::CellSize/2;
107  setPos(x, y);
108  break;
109  case Granatier::Direction::EAST:
110  x = p_x - Granatier::CellSize;
111  y = p_y - m_itemSizeSet.height() / 2;
112  setPos(x, y);
113  break;
114  case Granatier::Direction::SOUTH:
115  x = p_x - m_itemSizeSet.width() / 2;
116  y = p_y - Granatier::CellSize;
117  setPos(x, y);
118  break;
119  case Granatier::Direction::WEST:
120  x = p_x - Granatier::CellSize/2;
121  y = p_y - m_itemSizeSet.height() / 2;
122  setPos(x, y);
123  break;
124  }
125 }
126 
127 void BombExplosionItem::updateAnimationn(int nFrame)
128 {
129  QString strElemetId;
130  switch(m_direction)
131  {
132  case Granatier::Direction::NORTH:
133  setSpriteKey(QString("bomb_blast_north_%1").arg(nFrame));
134  break;
135  case Granatier::Direction::EAST:
136  setSpriteKey(QString("bomb_blast_east_%1").arg(nFrame));
137  break;
138  case Granatier::Direction::SOUTH:
139  setSpriteKey(QString("bomb_blast_south_%1").arg(nFrame));
140  break;
141  case Granatier::Direction::WEST:
142  setSpriteKey(QString("bomb_blast_west_%1").arg(nFrame));
143  break;
144  }
145 }
146 
147 void BombExplosionItem::updateGraphics(qreal svgScaleFactor)
148 {
149  if(scene()->views().isEmpty())
150  {
151  return;
152  }
153 
154  //calculate the size of the item on the view
155  QPoint viewTopLeft = scene()->views().first()->mapFromScene(0, 0);
156  QPoint viewBottomRight = scene()->views().first()->mapFromScene(m_itemSizeSet.width(), m_itemSizeSet.height());
157 
158  int viewWidth = viewBottomRight.x() - viewTopLeft.x();
159  int viewHeight = viewBottomRight.y() - viewTopLeft.y();
160 
161  //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
162  if(m_itemSizeSet.width() != Granatier::CellSize || m_itemSizeSet.height() != Granatier::CellSize)
163  {
164  viewBottomRight = scene()->views().first()->mapFromScene(Granatier::CellSize, Granatier::CellSize);
165  int viewCellWidth = viewBottomRight.x() - viewTopLeft.x();
166  int viewCellHeight = viewBottomRight.y() - viewTopLeft.y();
167  if((viewCellWidth - viewWidth) % 2 != 0)
168  {
169  viewWidth--;
170  }
171  if((viewCellHeight - viewHeight) % 2 != 0)
172  {
173  viewHeight--;
174  }
175 
176  //calculate the real item size after change of the render size
177  QPointF sceneTopLeft = scene()->views().first()->mapToScene(0, 0);
178  QPointF sceneBottomRight = scene()->views().first()->mapToScene(viewWidth, viewHeight);
179 
180  qreal sceneWidth = sceneBottomRight.x() - sceneTopLeft.x();
181  qreal sceneHeight = sceneBottomRight.y() - sceneTopLeft.y();
182  m_itemSizeReal = QSize(sceneWidth, sceneHeight);
183  }
184 
185  setRenderSize(QSize(viewWidth, viewHeight));
186  setScale(svgScaleFactor);
187  m_svgScaleFactor = svgScaleFactor;
188 
189  QTransform transform;
190  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
191  setRenderSize(renderSize());
192  transform.translate(-m_itemSizeReal.width() / 2.0, -m_itemSizeReal.height() / 2.0);
193  setTransform(transform);
194 }
QPainterPath::addEllipse
void addEllipse(const QRectF &boundingRectangle)
QTransform
BombExplosionItem::~BombExplosionItem
~BombExplosionItem()
Deletes the BombExplosionItem instance.
Definition: bombexplosionitem.cpp:76
QSize::width
int width() const
QRectF::x
qreal x() const
QRectF::y
qreal y() const
QPoint
Granatier::Direction::Type
Type
Definition: granatierglobals.h:124
BombExplosionItem::BombExplosionItem
BombExplosionItem(Bomb *p_model, Granatier::Direction::Type direction, int bombPower, KGameRenderer *renderer, qreal svgScaleFactor)
Creates a new BombExplosionItem instance.
Definition: bombexplosionitem.cpp:25
bombexplosionitem.h
QPoint::x
int x() const
QPoint::y
int y() const
QPointF
BombExplosionItem::updateGraphics
virtual void updateGraphics(qreal svgScaleFactor)
Updates the graphics after a resize.
Definition: bombexplosionitem.cpp:147
QTransform::translate
QTransform & translate(qreal dx, qreal dy)
Granatier::CellSize
const qreal CellSize
The Cell size.
Definition: granatierglobals.h:31
QPointF::x
qreal x() const
QPointF::y
qreal y() const
BombExplosionItem::m_svgScaleFactor
qreal m_svgScaleFactor
The scale factor from the svg for the pixmap.
Definition: bombexplosionitem.h:52
Granatier::Direction::WEST
Definition: granatierglobals.h:129
BombExplosionItem::shape
QPainterPath shape() const
Overrides the default shape function to make it a small circle This function is used to determinate c...
Definition: bombexplosionitem.cpp:80
Bomb
This class describes the common characteristics and behaviour of the bomb item.
Definition: bomb.h:30
QString
BombExplosionItem::m_itemSizeSet
QSize m_itemSizeSet
Definition: bombexplosionitem.h:54
Granatier::Direction::NORTH
Definition: granatierglobals.h:126
QSize
Bomb::explosionID
int explosionID()
Definition: bomb.cpp:488
BombExplosionItem::m_explosionID
int m_explosionID
The ID of the Bomb that causes the explosion.
Definition: bombexplosionitem.h:49
QPainterPath
QRectF::width
qreal width() const
QRectF
BombExplosionItem::setPosition
virtual void setPosition(qreal p_x, qreal p_y)
Updates the BombExplosionItem coordinates.
Definition: bombexplosionitem.cpp:97
QSize::height
int height() const
BombExplosionItem::m_direction
Granatier::Direction::Type m_direction
The direction of the blast.
Definition: bombexplosionitem.h:43
KGameRenderedItem
BombExplosionItem::explosionID
int explosionID()
Definition: bombexplosionitem.cpp:92
Granatier::Direction::EAST
Definition: granatierglobals.h:128
BombExplosionItem::updateAnimationn
void updateAnimationn(int nFrame)
updates the animation
Definition: bombexplosionitem.cpp:127
QRectF::height
qreal height() const
BombExplosionItem::m_itemSizeReal
QSize m_itemSizeReal
Definition: bombexplosionitem.h:55
bomb.h
Granatier::Direction::SOUTH
Definition: granatierglobals.h:127
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

granatier

Skip menu "granatier"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal