-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbomb.cpp
170 lines (147 loc) · 5.46 KB
/
bomb.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "bomb.h"
#include "playfield.h"
#include <algorithm>
#include <vector>
std::vector<Bomb*> bombs; //In dieser Liste werden alle Bomben auf dem Spielfeld abgespeichert
int bomb_count=0; //anzahl Bomben die erstellt wurden um eine einmalige ID zu erhalten
/*
* Konstuktor: Erstellt eine Bombe an der Position des aktuellen Spielers
*/
Bomb::Bomb(Playfield *Field, Player *Gameplayer, QGraphicsScene *Scene )
{
id = bomb_count++; //einmalige ID abspeichern
scene = Scene;
playfield = Field;
player = Gameplayer;
setBomb(); //Bombe platzieren
bombs.push_back(this); //in Liste abspeichern
}
/*
* Plaziert eine Bombe an der aktuellen Spielerposition
*/
void Bomb::setBomb()
{
setRect(player->Get_PlayerPos_X(),player->Get_PlayerPos_Y(),BOMB_SIZE_X,BOMB_SIZE_Y);
setBrush(QBrush(Qt::black)); //Schwarze Bombe
}
/*
* Zeichnet die Bombe auf die Szene
*/
void Bomb::draw()
{
scene->addItem(this);
}
/*
* Wird zyklisch aufgerufen für eine Bombe. Hier ist das Verhalten definiert, wie die Lunte abbrennt
* und die Bombe explodiert, Blöcke zerstört, etc...
*/
void Bomb::burningFuse()
{
ticks++; //Timer für die Bombe incrementieren
if(current_explosionsradius == 0) //Bombe ist noch nicht explodiert
{
//Blombe Grau/Schwarz blinken lassen
if((ticks/2)%2)
setBrush(QBrush(Qt::black));
else
setBrush(QBrush(Qt::gray));
//Warte bis Bombe explodiert
if(ticks>TIME_FUSE)
{
current_explosionsradius=1;
}
}
else
{
if(current_explosionsradius <= player->Get_Bombintensity()) //maximaler Ausbreitungsradius ist noch nicht erreicht
{
if(ticks>TIME_EXPANDING_EXPLOSION) //Wartezeit für nächste Explosionsstufe ist erreicht
{
if(current_explosionsradius==1) //Erste Explosionsstufe
{
//Das Feld wo die Bombe drauf ist expodiert
playfield->getBlock(BOMB_MIDDLE_X(rect().x()), BOMB_MIDDLE_Y(rect().y()),CURRENT)->exploding();
//"Bombe" verschwinden lassen
setBrush(QBrush(Qt::transparent));
setPen(QPen(Qt::transparent));
current_explosionsradius++;
}
else //nicht erste Explosionsstufe
{
if(!wall_down && !wall_left && !wall_up && !wall_right) //frühzeitig abberech
current_explosionsradius = player->Get_Bombintensity();
else
{
for(int i=1; i<current_explosionsradius; i++) //Explosion in jede Ricung verbreiten
{
if(wall_down) wall_down = playfield->getBlock(BOMB_MIDDLE_X(rect().x() ), BOMB_MIDDLE_Y(rect().y()+i*BLOCK_SIZE_Y),CURRENT)->exploding();
if(wall_left) wall_left = playfield->getBlock(BOMB_MIDDLE_X(rect().x()+i*BLOCK_SIZE_X), BOMB_MIDDLE_Y(rect().y() ),CURRENT)->exploding();
if(wall_up) wall_up = playfield->getBlock(BOMB_MIDDLE_X(rect().x() ), BOMB_MIDDLE_Y(rect().y()-i*BLOCK_SIZE_Y),CURRENT)->exploding();
if(wall_right)wall_right = playfield->getBlock(BOMB_MIDDLE_X(rect().x()-i*BLOCK_SIZE_X), BOMB_MIDDLE_Y(rect().y() ),CURRENT)->exploding();
}
current_explosionsradius++;
}
}
ticks=0;
}
}
else
{
if(ticks>TIME_EXPANDING_EXPLOSION)
{
//Explosion entfernen
for(int i=current_explosionsradius; i>=0 ;i--)
{
Block * block;
block = playfield->getBlock(BOMB_MIDDLE_X(rect().x() ), BOMB_MIDDLE_Y(rect().y()+i*BLOCK_SIZE_Y),CURRENT);
if(block != NULL) block->reset_Blockbehavoir();
block = playfield->getBlock(BOMB_MIDDLE_X(rect().x()+i*BLOCK_SIZE_X), BOMB_MIDDLE_Y(rect().y() ),CURRENT);
if(block != NULL) block->reset_Blockbehavoir();
block = playfield->getBlock(BOMB_MIDDLE_X(rect().x() ), BOMB_MIDDLE_Y(rect().y()-i*BLOCK_SIZE_Y),CURRENT);
if(block != NULL) block->reset_Blockbehavoir();
block = playfield->getBlock(BOMB_MIDDLE_X(rect().x()-i*BLOCK_SIZE_X), BOMB_MIDDLE_Y(rect().y() ),CURRENT);
if(block != NULL) block->reset_Blockbehavoir();
}
removeBomb(id); //Bombe aus Liste löschen
}
}
}
}
//***************************************************
//Static Funktionen
//***************************************************
int Bomb::getBombCount(Player * player)
{
int count = 0;
for(unsigned int i=0; i<bombs.size() ; i++)
{
Bomb * bomb = bombs.at(i);
if(bomb->player == player)
count++;
}
return count;
}
void Bomb::tick()
{
if(bombs.size()>0 )
{
for(unsigned int i=0; i<bombs.size() ; i++)
{
Bomb * bomb = bombs.at(i);
bomb->burningFuse();
}
}
}
void Bomb::removeBomb(int id)
{
for(unsigned int i=0; i<bombs.size() ; i++)
{
Bomb * bomb = bombs.at(i);
if(id == bomb->id)
{
scene->removeItem(this);
bombs.erase(bombs.begin() + i);
break;
}
}
}