1 /*
2     SPDX-FileCopyrightText: 2009 Mathias Kraus <k.hias@gmx.de>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "bonusitem.h"
8 #include "bonus.h"
9 
10 #include <QTimer>
11 
12 #include <KGameRenderer>
13 
BonusItem(Bonus * p_model,KGameRenderer * renderer)14 BonusItem::BonusItem(Bonus* p_model, KGameRenderer* renderer) : ElementItem (p_model, renderer)
15 {
16     setSpriteKey(p_model->getImageId());
17     m_undestroyableExplosionID = 0;
18     m_destructionTimer = nullptr;
19     m_destructionCounter = 0;
20 }
21 
~BonusItem()22 BonusItem::~BonusItem()
23 {
24     delete m_destructionTimer;
25 }
26 
setUndestroyable(int nExplosionID)27 void BonusItem::setUndestroyable(int nExplosionID)
28 {
29     m_undestroyableExplosionID = nExplosionID;
30 }
31 
initDestruction(int nExplosionID)32 void BonusItem::initDestruction(int nExplosionID)
33 {
34     if(m_undestroyableExplosionID == nExplosionID || dynamic_cast <Bonus*> (m_model)->isTaken())
35     {
36         return;
37     }
38 
39     dynamic_cast <Bonus*> (m_model)->setDestroyed();
40 
41     m_destructionCounter = 0;
42     m_destructionTimer = new QTimer(this);
43     m_destructionTimer->setInterval(300);
44     m_destructionTimer->start();
45     connect(m_destructionTimer, &QTimer::timeout, this, &BonusItem::destructionAnimation);
46 }
47 
destructionAnimation()48 void BonusItem::destructionAnimation()
49 {
50     m_destructionCounter++;
51     m_destructionTimer->setInterval(50);
52     setOpacity((5-m_destructionCounter)/5.0);
53     if(m_destructionCounter >= 5)
54     {
55         m_destructionTimer->stop();
56         delete m_destructionTimer;
57         m_destructionTimer = nullptr;
58         Q_EMIT bonusItemDestroyed(this);
59     }
60 }
61