-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExplosionBlock.cpp
More file actions
155 lines (126 loc) · 3.64 KB
/
ExplosionBlock.cpp
File metadata and controls
155 lines (126 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Author: R. Schiffers
#include "ExplosionBlock.h"
#include "Player.h"
ExplosionBlock::ExplosionBlock (int blockSize, int X, int Y, QGraphicsScene *scene)
: GameObject (blockSize, X, Y)
{
this->type = "EXPLOSION";
this->scene = scene;
this->blockSize = blockSize;
this->setPixmap (* new QPixmap (":/res/images/Explosion.png"));
this->setScale ((double) blockSize / 100);
this->setPos (X, Y);
setZValue (3);
bombDetection ();
this->destroyTimer = new QTimer ();
connect (destroyTimer, SIGNAL (timeout ()), this, SLOT (destroy ()));
this->destroyTimer->start (300);
}
QString
ExplosionBlock::blockType ()
{
return this->type;
}
bool
ExplosionBlock::collider (QString type)
{
CollisionBlock* colliderBlock = new CollisionBlock (blockSize, x (), y ());
this->scene->addItem (colliderBlock);
QList <QGraphicsItem*> tmp = colliderBlock->collidingItems ();
for (int i = 0; i < tmp.size (); i++)
{
GameObject* tmpBlock = (GameObject*) tmp[i];
if (tmpBlock->blockType () == type)
{
this->scene->removeItem (colliderBlock);
delete colliderBlock;
return true;
}
}
this->scene->removeItem (colliderBlock);
delete colliderBlock;
return false;
}
void
ExplosionBlock::damage ()
{
int itemRandom = getRandomInt (1, 300);
QList <QGraphicsItem*> tmp = getCollision ();
if (collider ("FRAGILE"))
{
for (int i = 0, n = tmp.size (); i < n; i++)
{
GameObject* tmpBLOCK = (GameObject*) tmp[i];
if (tmpBLOCK->blockType () == "FRAGILE")
{
GroundBlock* block = new GroundBlock (blockSize, tmpBLOCK->pos ().x () / blockSize, tmpBLOCK->pos ().y () / blockSize);
this->scene->addItem (block);
if (itemRandom > 200)
{
int typeDef = (itemRandom <= 250) ? 0 : 1;
Item* item = new Item (blockSize, tmpBLOCK->pos ().x () / blockSize, tmpBLOCK->pos ().y () / blockSize, typeDef);
this->scene->addItem (item);
}
this->scene->removeItem (tmpBLOCK);
delete tmpBLOCK;
}
}
}
if (collider ("PLAYER"))
{
for (int i = 0, n = tmp.size (); i < n; i++)
{
GameObject* tmpBLOCK = (GameObject*) tmp[i];
if (tmpBLOCK->blockType () == "PLAYER")
{
Player *tmpPLAYER = (Player*) tmpBLOCK;
tmpPLAYER->decreaseHealth ();
}
}
}
}
void
ExplosionBlock::bombDetection ()
{
QList <QGraphicsItem*> tmp = getCollision ();
if (collider ("BOMB"))
{
for (int i = 0, n = tmp.size (); i < n; i++)
{
GameObject* tmpBLOCK = (GameObject*) tmp[i];
if (tmpBLOCK->blockType () == "BOMB")
{
Bomb* tmpBOMB = (Bomb*) tmpBLOCK;
tmpBOMB->setTimer (0);
}
}
}
}
void
ExplosionBlock::stopTimer ()
{
this->destroyTimer->stop ();
}
QList <QGraphicsItem*>
ExplosionBlock::getCollision ()
{
CollisionBlock *colliderBlock = new CollisionBlock (blockSize, x (), y ());
this->scene->addItem (colliderBlock);
QList <QGraphicsItem*> tmp = colliderBlock->collidingItems ();
this->scene->removeItem (colliderBlock);
delete colliderBlock;
return tmp;
}
int
ExplosionBlock::getRandomInt (int min, int max)
{
int random = qrand () % ((max + 1) - min) + min;
return random;
}
void
ExplosionBlock::destroy ()
{
damage ();
this->scene->removeItem (this);
delete this;
}