-
Notifications
You must be signed in to change notification settings - Fork 1
/
bomb.js
224 lines (215 loc) · 11.4 KB
/
bomb.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
class Bomb {
constructor(owner, iGrid, jGrid, power, bombID) {
this.owner = owner;
this.iGrid = iGrid;
this.jGrid = jGrid;
this.power = power;
this.exploding = false;
this.bombID = bombID;
}
placeRandomPowerup(){
let powers = [
"speed",
"bombpower",
"extrabomb"
];
// 50% chance to spawn powerup, 50% chance free space
if(0.5 < Math.random()){
return powers[Math.floor(Math.random()*3)];
} else {
return 'free';
}
}
explode() {
// Kill bomber standing on bomb
if (typeof m.bomberLocations[this.iGrid][this.jGrid] === 'object') {
m.bomberLocations[this.iGrid][this.jGrid].die();
}
m.bombMap[this.iGrid][this.jGrid] = this.bombID;
let rockCollide = {up: false, down: false, left: false, right: false};
for (let i = 1; i < this.power+1; i++){
// Explode above, checking for walls & bombs
if (!rockCollide.up) {
if (this.iGrid-i >= 0) {
// Kill bomber in blast radius above
if (typeof m.bomberLocations[this.iGrid-i][this.jGrid] === 'object') {
m.bomberLocations[this.iGrid-i][this.jGrid].die();
}
if (m.bombMap[this.iGrid-i][this.jGrid] === 'free') {
m.bombMap[this.iGrid-i][this.jGrid] = this.bombID;
} else if (m.bombMap[this.iGrid-i][this.jGrid] === 'rock') {
// Negative bombID to signal possible powerup spawn under rock
m.bombMap[this.iGrid-i][this.jGrid] = -this.bombID;
rockCollide.up = true;
} else if (typeof m.bombMap[this.iGrid-i][this.jGrid] === 'object') {
// Explode bombs in blast radius above
m.bombMap[this.iGrid-i][this.jGrid].exploding = true;
m.bombMap[this.iGrid-i][this.jGrid].explode();
rockCollide.up = true;
} else if (typeof m.bombMap[this.iGrid-i][this.jGrid] === 'number') {
// Overwrites previous bombs blast
m.bombMap[this.iGrid-i][this.jGrid] = this.bombID;
} else if (m.bombMap[this.iGrid-i][this.jGrid] === 'bombpower' || m.bombMap[this.iGrid-i][this.jGrid] === 'extrabomb' || m.bombMap[this.iGrid-i][this.jGrid] === 'speed' ){
// Destroys any powerups in space
m.bombMap[this.iGrid-i][this.jGrid] = this.bombID;
}else {
rockCollide.up = true;
}
} else {
rockCollide.up = true
}
}
// Explode below, checking for walls & bombs
if (!rockCollide.down) {
// Kill bomber in blast radius below
if (typeof m.bomberLocations[this.iGrid+i][this.jGrid] === 'object') {
m.bomberLocations[this.iGrid+i][this.jGrid].die();
}
if (this.iGrid+i < 16) {
if (m.bombMap[this.iGrid+i][this.jGrid] === 'free') {
m.bombMap[this.iGrid+i][this.jGrid] = this.bombID;
} else if (m.bombMap[this.iGrid+i][this.jGrid] === 'rock') {
// Negative bombID to signal possible powerup spawn under rock
m.bombMap[this.iGrid+i][this.jGrid] = -this.bombID;
rockCollide.down = true;
}else if (typeof m.bombMap[this.iGrid+i][this.jGrid] === 'object') {
// Explode bombs in blast radius below
m.bombMap[this.iGrid+i][this.jGrid].exploding = true;
m.bombMap[this.iGrid+i][this.jGrid].explode();
rockCollide.down = true;
} else if (typeof m.bombMap[this.iGrid+i][this.jGrid] === 'number') {
// Overwrites previous bombs blast
m.bombMap[this.iGrid+i][this.jGrid] = this.bombID;
}else if (m.bombMap[this.iGrid+i][this.jGrid] === 'bombpower' || m.bombMap[this.iGrid+i][this.jGrid] === 'extrabomb' || m.bombMap[this.iGrid+i][this.jGrid] === 'speed' ){
// Destroys any powerups in space
m.bombMap[this.iGrid+i][this.jGrid] = this.bombID;
}else {
rockCollide.down = true;
}
} else {
rockCollide.down = true
}
}
// Explode right, checking for walls & bombs
if (!rockCollide.right) {
// Kill bomber in blast radius right
if (typeof m.bomberLocations[this.iGrid][this.jGrid+i] === 'object') {
m.bomberLocations[this.iGrid][this.jGrid+i].die();
}
if (this.jGrid+i < 16) {
if (m.bombMap[this.iGrid][this.jGrid+i] === 'free') {
m.bombMap[this.iGrid][this.jGrid+i] = this.bombID;
} else if (m.bombMap[this.iGrid][this.jGrid+i] === 'rock') {
// Negative bombID to signal possible powerup spawn under rock
m.bombMap[this.iGrid][this.jGrid+i] = -this.bombID;
rockCollide.right = true;
} else if (typeof m.bombMap[this.iGrid][this.jGrid+i] === 'object') {
// Explode bombs in blast radius right
m.bombMap[this.iGrid][this.jGrid+i].exploding = true;
m.bombMap[this.iGrid][this.jGrid+i].explode();
rockCollide.right = true;
} else if (typeof m.bombMap[this.iGrid][this.jGrid+i] === 'number'){
// Overwrites previous bombs blast
m.bombMap[this.iGrid][this.jGrid+i] = this.bombID;
}else if (m.bombMap[this.iGrid][this.jGrid+i] === 'bombpower' || m.bombMap[this.iGrid][this.jGrid+i] === 'extrabomb' || m.bombMap[this.iGrid][this.jGrid+i] === 'speed' ){
// Destroys any powerups in space
m.bombMap[this.iGrid][this.jGrid+i] = this.bombID;
}else {
rockCollide.right = true;
}
} else {
rockCollide.right = true
}
}
// Explode left, checking for walls & bombs
if (!rockCollide.left) {
// Kill bomber in blast radius left
if (typeof m.bomberLocations[this.iGrid][this.jGrid-i] === 'object') {
m.bomberLocations[this.iGrid][this.jGrid-i].die();
}
if (this.jGrid-i >= 0) {
if (m.bombMap[this.iGrid][this.jGrid-i] === 'free') {
m.bombMap[this.iGrid][this.jGrid-i] = this.bombID;
} else if (m.bombMap[this.iGrid][this.jGrid-i] === 'rock') {
// Negative bombID to signal possible powerup spawn under rock
m.bombMap[this.iGrid][this.jGrid-i] = -this.bombID;
rockCollide.left = true;
} else if (typeof m.bombMap[this.iGrid][this.jGrid-i] === 'object') {
// Explode bombs in blast radius left
m.bombMap[this.iGrid][this.jGrid-i].exploding = true;
m.bombMap[this.iGrid][this.jGrid-i].explode();
rockCollide.left = true;
} else if (typeof m.bombMap[this.iGrid][this.jGrid-i] === 'number') {
// Overwrites previous bombs blast
m.bombMap[this.iGrid][this.jGrid-i] = this.bombID;
}else if (m.bombMap[this.iGrid][this.jGrid-i] === 'bombpower' || m.bombMap[this.iGrid][this.jGrid-i] === 'extrabomb' || m.bombMap[this.iGrid][this.jGrid-i] === 'speed' ){
// Destroys any powerups in space
m.bombMap[this.iGrid][this.jGrid-i] = this.bombID;
} else {
rockCollide.left = true;
}
} else {
rockCollide.left = true
}
}
}
this.owner.bombAmmo +=1;
// Sets spaces back into free after bomb blast
setTimeout(() => {
// Checks to see if the space has been overwritten by new bomb blast
if (m.bombMap[this.iGrid][this.jGrid] === this.bombID) {
m.bombMap[this.iGrid][this.jGrid] = 'free';
}
for (let i = 1; i < this.power+1; i++){
// Clear above
if (this.iGrid-i >= 0) {
// Checks to see if the space has been overwritten by new bomb blast
if (m.bombMap[this.iGrid-i][this.jGrid] === this.bombID) {
m.bombMap[this.iGrid-i][this.jGrid] = 'free';
} else if (m.bombMap[this.iGrid-i][this.jGrid] === -this.bombID){
// Places random powerup if space previously contained a rock
m.bombMap[this.iGrid-i][this.jGrid] = this.placeRandomPowerup();
}
}
// Clear below
if (this.iGrid+i < 16) {
if (m.bombMap[this.iGrid+i][this.jGrid] === this.bombID) {
m.bombMap[this.iGrid+i][this.jGrid] = 'free';
} else if (m.bombMap[this.iGrid+i][this.jGrid] === -this.bombID){
m.bombMap[this.iGrid+i][this.jGrid] = this.placeRandomPowerup();
}
}
// Clear right
if (this.jGrid+i < 16) {
if (m.bombMap[this.iGrid][this.jGrid+i] === this.bombID) {
m.bombMap[this.iGrid][this.jGrid+i] = 'free';
} else if (m.bombMap[this.iGrid][this.jGrid+i] === -this.bombID){
m.bombMap[this.iGrid][this.jGrid+i] = this.placeRandomPowerup();
}
}
// Clear left
if (this.jGrid-i >= 0) {
if (m.bombMap[this.iGrid][this.jGrid-i] === this.bombID) {
m.bombMap[this.iGrid][this.jGrid-i] = 'free';
} else if (m.bombMap[this.iGrid][this.jGrid-i] === -this.bombID){
m.bombMap[this.iGrid][this.jGrid-i] = this.placeRandomPowerup();
}
}
}
// Deletes the bomb
delete this;
}, 300)
}
timerExplode () {
setTimeout(() => {
// Explodes the bomb if it hasn't yet been triggered by another
if (!this.exploding && !gameReset) {
this.explode();
this.exploding = true;
}
}, 3000)
}
gridPlacer () {
m.bombMap[this.iGrid][this.jGrid] = this;
}
}