• 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
bombitem.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 "bombitem.h"
20 #include "bomb.h"
21 #include "settings.h"
22 
23 #include <QTimer>
24 #include <QGraphicsView>
25 
26 #include <KGameRenderer>
27 
28 const int nBombPulseTime = 100;
29 
30 BombItem::BombItem(Bomb* p_model, KGameRenderer* renderer) : ElementItem (p_model, renderer)
31 {
32  setSpriteKey("bomb");
33  setZValue(210);
34  connect(p_model, SIGNAL(bombDetonated(Bomb*)), this, SLOT(startDetonation()));
35  connect(p_model, SIGNAL(falling()), this, SLOT(fallingAnimation()));
36  connect(this, SIGNAL(bombItemFinished(BombItem*)), p_model, SLOT(slot_detonationCompleted()));
37 
38  int width = Granatier::CellSize * 0.9;
39  int height = Granatier::CellSize * 0.9;
40  if(((int) Granatier::CellSize - width) % 2 != 0)
41  {
42  width--;
43  }
44  if(((int) Granatier::CellSize - height) % 2 != 0)
45  {
46  height--;
47  }
48  m_itemSizeSet = QSize(width, height);
49  m_itemSizeReal = m_itemSizeSet;
50 
51  m_animationCounter = 0;
52  // Define the timer which sets the puls frequency
53  m_pulseTimer = new QTimer(this);
54  m_pulseTimer->setInterval(nBombPulseTime);
55  m_pulseTimer->start();
56  connect(m_pulseTimer, SIGNAL(timeout()), this, SLOT(pulse()));
57 
58  m_explosionTimer = NULL;
59  m_listExplosionTiming.append(Settings::self()->blastTime1());
60  m_listExplosionTiming.append(Settings::self()->blastTime2());
61  m_listExplosionTiming.append(Settings::self()->blastTime3());
62  m_listExplosionTiming.append(Settings::self()->blastTime4());
63  m_listExplosionTiming.append(Settings::self()->blastTime5());
64 
65  m_fallingAnimationCounter = 0;
66  m_dudBomb = false;
67 }
68 
69 BombItem::~BombItem()
70 {
71  delete m_pulseTimer;
72  delete m_explosionTimer;
73 }
74 
75 QPainterPath BombItem::shape() const
76 {
77  QPainterPath path;
78  // Temporary variable to keep the boundingRect available
79  QRectF rect = boundingRect();
80 
81  // Calculation of the shape
82  QRectF shapeRect = QRectF( rect.x()+rect.width()/8, rect.y()+rect.height()/8, rect.width()*3.0/4.0, rect.height()*3.0/4.0 );
83  path.addEllipse(shapeRect);
84  return path;
85 }
86 
87 void BombItem::pauseAnim()
88 {
89  dynamic_cast <Bomb*> (m_model)->pause();
90  if(m_pulseTimer)
91  {
92  m_pulseTimer->stop();
93  }
94  if(m_explosionTimer)
95  {
96  m_explosionTimer->stop();
97  }
98 }
99 
100 void BombItem::resumeAnim()
101 {
102  if(m_pulseTimer && !m_dudBomb)
103  {
104  m_pulseTimer->start();
105  }
106  if(m_explosionTimer)
107  {
108  m_explosionTimer->start();
109  }
110  dynamic_cast <Bomb*> (m_model)->resume();
111 }
112 
113 void BombItem::update(qreal p_x, qreal p_y)
114 {
115  // Compute the top-right coordinates of the item
116  qreal x = p_x - m_itemSizeSet.width() / 2;
117  qreal y = p_y - m_itemSizeSet.height() / 2;
118  // Updates the view coordinates
119  setPos(x, y);
120  m_x = p_x;
121  m_y = p_y;
122 }
123 
124 void BombItem::startDetonation()
125 {
126  if(m_pulseTimer)
127  {
128  m_pulseTimer->stop();
129  delete m_pulseTimer;
130  m_pulseTimer = 0;
131  }
132  m_animationCounter = 0;
133  QTransform transform;
134  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
135  setRenderSize(m_renderSize);
136  transform.translate(-m_itemSizeReal.width() / 2.0, -m_itemSizeReal.height() / 2.0);
137  setTransform(transform);
138  dynamic_cast <Bomb*> (m_model)->setXSpeed(0);
139  dynamic_cast <Bomb*> (m_model)->setYSpeed(0);
140 
141  // Define the timer which sets the explosion frequency
142  m_explosionTimer = new QTimer(this);
143  m_explosionTimer->setInterval(m_listExplosionTiming.at(0));
144  m_explosionTimer->setSingleShot(true);
145  m_explosionTimer->start();
146  connect(m_explosionTimer, SIGNAL(timeout()), this, SLOT(updateAnimation()));
147 
148  int width = Granatier::CellSize * 1.1;
149  int height = Granatier::CellSize * 1.1;
150  if(((int) Granatier::CellSize - width) % 2 != 0)
151  {
152  width--;
153  }
154  if(((int) Granatier::CellSize - height) % 2 != 0)
155  {
156  height--;
157  }
158  m_itemSizeSet = QSize(width, height);
159  m_itemSizeReal = m_itemSizeSet;
160  setSpriteKey("bomb_blast_core_0");
161  setZValue(300+15); //300+maxBombPower+5
162  updateGraphics(scale());
163  update(m_x, m_y);
164 }
165 
166 void BombItem::pulse()
167 {
168  if(m_fallingAnimationCounter == 0)
169  {
170  m_animationCounter++;
171  if (m_animationCounter % 2 == 0)
172  {
173  m_animationCounter = 0;
174  int viewWidth = m_renderSize.width() * 0.98;
175  int viewHeight = m_renderSize.height() * 0.98;
176  if((m_renderSize.width() - viewWidth) % 2 != 0)
177  {
178  viewWidth--;
179  }
180  if((m_renderSize.height() - viewHeight) % 2 != 0)
181  {
182  viewHeight--;
183  }
184 
185  //calculate the real item size after change of the render size
186  QPointF sceneTopLeft = scene()->views().first()->mapToScene(QPoint(0, 0));
187  QPointF sceneBottomRight = scene()->views().first()->mapToScene(QPoint(viewWidth, viewHeight));
188 
189  qreal sceneWidth = sceneBottomRight.x() - sceneTopLeft.x();
190  qreal sceneHeight = sceneBottomRight.y() - sceneTopLeft.y();
191 
192  // shrink the item
193  QTransform transform;
194  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
195  setRenderSize(QSize(viewWidth, viewHeight));
196  transform.translate(-sceneWidth / 2.0, -sceneHeight / 2.0);
197  setTransform(transform);
198  }
199  else
200  {
201  QTransform transform;
202  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
203  setRenderSize(m_renderSize);
204  transform.translate(-m_itemSizeReal.width() / 2.0, -m_itemSizeReal.height() / 2.0);
205  setTransform(transform);
206  }
207  }
208  else
209  {
210  // shrink the item
211  QTransform transform;
212  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
213  setRenderSize(m_renderSize * (1-m_fallingAnimationCounter*0.02));
214  transform.translate(-m_itemSizeReal.width() * (1-m_fallingAnimationCounter*0.02) / 2.0, -m_itemSizeReal.height() * (1-m_fallingAnimationCounter*0.02) / 2.0);
215  setTransform(transform);
216  m_fallingAnimationCounter++;
217 
218  if(m_fallingAnimationCounter > 50)
219  {
220  m_pulseTimer->stop();
221  m_dudBomb = true;
222  emit bombItemFinished(this);
223  }
224  }
225 }
226 
227 void BombItem::updateAnimation()
228 {
229  m_animationCounter++;
230  if (m_animationCounter > 4)
231  {
232  emit bombItemFinished(this);
233  m_animationCounter = 0;
234  return;
235  }
236  QString strElemetId = QString("bomb_blast_core_%1").arg(m_animationCounter);
237  setSpriteKey(strElemetId);
238  updateGraphics(scale());
239  update(m_x, m_y);
240 
241  emit animationFrameChanged(this, m_animationCounter);
242  m_explosionTimer->setInterval(m_listExplosionTiming.at(m_animationCounter));
243  m_explosionTimer->start();
244 }
245 
246 void BombItem::updateMortar(int nMortarState, int nMortarRampEnd, int nMortarPeak, int nMortarGround)
247 {
248  if(m_pulseTimer)
249  {
250  m_pulseTimer->stop();
251  delete m_pulseTimer;
252  m_pulseTimer = 0;
253  }
254 
255  if(nMortarState <= 0)
256  {
257  setVisible(false);
258  setZValue(-1);
259  }
260  else if(nMortarState <= nMortarGround)
261  {
262  updateMortarAnimation(nMortarState, nMortarRampEnd, nMortarPeak);
263  }
264  else
265  {
266  if(!m_pulseTimer)
267  {
268  m_pulseTimer = new QTimer(this);
269  m_pulseTimer->setInterval(nBombPulseTime);
270  m_animationCounter = 1; // set to one, to start pulsing with a small bomb
271  m_pulseTimer->start();
272  connect(m_pulseTimer, SIGNAL(timeout()), this, SLOT(pulse()));
273  }
274  QTransform transform;
275  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
276  setRenderSize(m_renderSize);
277  transform.translate(-m_itemSizeReal.width() / 2.0, -m_itemSizeReal.height() / 2.0);
278  setTransform(transform);
279  setVisible(true);
280  setZValue(210);
281  }
282 }
283 
284 void BombItem::updateMortarAnimation(int animationCounter, int nMortarRampEnd, int nMortarPeak)
285 {
286  qreal mortarScale = 1;
287  int mortarZValue = 210;
288  QTransform transform;
289 
290  if(animationCounter < nMortarRampEnd)
291  {
292  mortarZValue = 210;
293  }
294  else
295  {
296  mortarZValue = 800;
297  }
298 
299  int frame = animationCounter - nMortarRampEnd;
300  int peak = nMortarPeak - nMortarRampEnd;
301 
302  mortarScale = 1.5 - (frame-peak) * (frame-peak) / (qreal) (peak*peak) * 0.5;
303 
304  transform.translate(m_itemSizeSet.width() / 2.0, m_itemSizeSet.height() / 2.0);
305  setRenderSize(m_renderSize * mortarScale);
306  transform.translate(-m_itemSizeReal.width() * mortarScale / 2.0, -m_itemSizeReal.height() * mortarScale / 2.0);
307  setTransform(transform);
308 
309  setVisible(true);
310  setZValue(mortarZValue);
311 }
312 
313 void BombItem::fallingAnimation()
314 {
315  m_fallingAnimationCounter = 1;
316  // set z-value below the ground
317  setZValue(-2);
318  m_pulseTimer->setInterval(1000 / Granatier::FPS);
319 }
QPainterPath::addEllipse
void addEllipse(const QRectF &boundingRectangle)
QTimer::setInterval
void setInterval(int msec)
QTransform
QSize::width
int width() const
bombitem.h
nMortarGround
const int nMortarGround
Definition: bomb.cpp:29
Granatier::FPS
const int FPS
The Frames Per Second for the game.
Definition: granatierglobals.h:28
QRectF::x
qreal x() const
QRectF::y
qreal y() const
BombItem
This class is the graphical representation of a Bomb.
Definition: bombitem.h:31
QList::at
const T & at(int i) const
nMortarRampEnd
const int nMortarRampEnd
Definition: bomb.cpp:27
QPoint
BombItem::m_y
int m_y
Definition: bombitem.h:57
ElementItem::m_itemSizeSet
QSize m_itemSizeSet
Definition: elementitem.h:44
BombItem::animationFrameChanged
void animationFrameChanged(BombItem *bombItem, int nFrame)
signals next animation frame
QPointF
Settings::self
static Settings * self()
Definition: settings.cpp:17
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
QList::append
void append(const T &value)
BombItem::update
virtual void update(qreal p_x, qreal p_y)
Updates the BombItem coordinates.
Definition: bombitem.cpp:113
BombItem::bombItemFinished
void bombItemFinished(BombItem *bombItem)
signals end of the explosion
QTimer
BombItem::m_dudBomb
bool m_dudBomb
Flag to stop the animation if the bomb is fallin in a hole or TODO: a dud bomb.
Definition: bombitem.h:54
ElementItem::m_model
Element * m_model
The instance of Element the ElementItem will represent.
Definition: elementitem.h:40
Bomb
This class describes the common characteristics and behaviour of the bomb item.
Definition: bomb.h:30
BombItem::shape
QPainterPath shape() const
Overrides the default shape function to make it a small circle This function is used to determinate c...
Definition: bombitem.cpp:75
nMortarPeak
const int nMortarPeak
Definition: bomb.cpp:28
BombItem::BombItem
BombItem(Bomb *p_model, KGameRenderer *renderer)
Creates a new BombItem instance.
Definition: bombitem.cpp:30
ElementItem
This class is the graphical representation of a game Element.
Definition: elementitem.h:30
QString
BombItem::pauseAnim
void pauseAnim()
Pauses the BombItem animation.
Definition: bombitem.cpp:87
QSize
QTimer::stop
void stop()
BombItem::resumeAnim
void resumeAnim()
Resumes the BombItem animation.
Definition: bombitem.cpp:100
QPainterPath
BombItem::m_fallingAnimationCounter
int m_fallingAnimationCounter
Counter for falling animation.
Definition: bombitem.h:51
QRectF::width
qreal width() const
BombItem::~BombItem
~BombItem()
Deletes the BombItem instance.
Definition: bombitem.cpp:69
settings.h
nBombPulseTime
const int nBombPulseTime
Definition: bombitem.cpp:28
QRectF
BombItem::m_listExplosionTiming
QList< int > m_listExplosionTiming
Timing for the explosion.
Definition: bombitem.h:45
QSize::height
int height() const
BombItem::m_explosionTimer
QTimer * m_explosionTimer
Timer used to animate explosion.
Definition: bombitem.h:42
QTimer::start
void start(int msec)
QRectF::height
qreal height() const
ElementItem::m_renderSize
QSize m_renderSize
Definition: elementitem.h:42
BombItem::m_x
int m_x
Definition: bombitem.h:56
BombItem::m_animationCounter
int m_animationCounter
Counter for the animaton frames.
Definition: bombitem.h:48
ElementItem::updateGraphics
virtual void updateGraphics(qreal svgScaleFactor)
Updates the graphics after a resize.
Definition: elementitem.cpp:65
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
ElementItem::m_itemSizeReal
QSize m_itemSizeReal
Definition: elementitem.h:45
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
bomb.h
BombItem::m_pulseTimer
QTimer * m_pulseTimer
Timer used to make the bomb pulse.
Definition: bombitem.h:39
QTimer::setSingleShot
void setSingleShot(bool singleShot)
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