-
Notifications
You must be signed in to change notification settings - Fork 24
/
SuperBunker.js
executable file
·406 lines (271 loc) · 9.86 KB
/
SuperBunker.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import { game } from '../core/Game.js';
import { utils } from '../core/utils.js';
import { TYPES, FPS, getTypes } from '../core/global.js';
import { playSound, playSoundWithDelay, sounds } from '../core/sound.js';
import { common } from '../core/common.js';
import { checkProduction, collisionCheckMidPoint, nearbyTest } from '../core/logic.js';
import { zones } from '../core/zones.js';
import { sprites } from '../core/sprites.js';
const SuperBunker = (options = {}) => {
let css, dom, data, height, nearby, radarItem, exports;
function updateFireModulus() {
// firing speed increases with # of infantry
data.fireModulus = 8 - data.energy;
}
function capture(isEnemy) {
let isFriendlyCapture = (isEnemy === game.players.local.data.isEnemy);
data.isEnemy = isEnemy;
setFriendly(isFriendlyCapture);
if (isFriendlyCapture) {
game.objects.notifications.add('You captured and armed a super bunker ⛳');
playSoundWithDelay(sounds.friendlyClaim, exports, 500);
} else {
game.objects.notifications.add('The enemy captured and armed a super bunker 🚩');
playSoundWithDelay(sounds.enemyClaim, exports, 500);
}
// check if enemy convoy production should stop or start
checkProduction();
}
function setFriendly(isFriendly) {
utils.css.addOrRemove(radarItem.dom.o, isFriendly, css.friendly);
utils.css.addOrRemove(radarItem.dom.o, !isFriendly, css.enemy);
zones.changeOwnership(exports);
}
function setFiring(state) {
if (state && data.energy) {
data.firing = state;
} else {
data.firing = false;
}
}
function updateHealth(attacker) {
// notify if just disarmed by tank gunfire
// note: the super bunker has not become friendly to the tank; it's still "dangerous", but unarmed and won't fire at incoming units.
if (data.energy) return;
const isFriendly = (attacker.data.isEnemy === game.players.local.data.isEnemy);
// we have a tank, after all
if (isFriendly) {
game.objects.notifications.addNoRepeat('You disarmed a super bunker ⛳');
} else {
game.objects.notifications.addNoRepeat('The enemy disarmed a super bunker 🚩');
}
// disarmed super bunkers are dangerous to both sides.
setFriendly(false);
}
function hit(points, target) {
// only tank flamethrowers - or, gunfire - counts against super bunkers.
if (target && (target.data.type === TYPES.flame || target.data.type === TYPES.gunfire) && target.data.parentType === TYPES.tank) {
data.energy = Math.max(0, data.energy - points);
updateFireModulus();
sprites.updateEnergy(exports);
}
}
function die() {
if (data.dead) return;
// un-manned, but dangerous to helicopters on both sides.
data.hostile = true;
// gunfire from both sides should now hit this element.
data.energy = 0;
updateFireModulus();
// this object, in fact, never actually dies because it only becomes neutral/hostile and can still be hit.
data.dead = false;
data.hostile = true;
setFriendly(false);
sprites.updateEnergy(exports);
// check if enemy convoy production should stop or start
checkProduction();
common.onDie(exports);
}
function fire() {
let fireOptions;
if (!data.firing || !data.energy || data.frameCount % data.fireModulus !== 0) return;
fireOptions = {
parent: exports,
parentType: data.type,
isEnemy: data.isEnemy,
collisionItems: nearby.items,
x: data.x + (data.width + 1),
y: data.y + data.gunYOffset, // position of bunker gun
vX: 2,
vY: 0
};
game.addObject(TYPES.gunfire, fireOptions);
// other side
fireOptions.x = (data.x - 1);
// and reverse direction
fireOptions.vX *= -1;
game.addObject(TYPES.gunfire, fireOptions);
if (sounds.genericGunFire) {
playSound(sounds.genericGunFire, exports);
}
}
function animate() {
sprites.moveWithScrollOffset(exports);
data.frameCount++;
// start, or stop firing?
nearbyTest(nearby, exports);
fire();
// note: super bunkers never die, but leaving this in anyway.
return (!dom.o);
}
function refreshNearbyItems() {
// set on init, updated with `zones.changeOwnership()` as targets change sides
nearby.items = getTypes('infantry:all, engineer, missileLauncher, helicopter', { group: 'enemy', exports })
}
function initDOM() {
dom.o = sprites.create({
className: css.className,
id: data.id,
isEnemy: (data.isEnemy ? css.enemy : false)
});
sprites.setTransformXY(exports, dom.o, `${data.x}px`, `${data.y}px`);
}
function initSuperBunker() {
refreshNearbyItems();
initDOM();
radarItem = game.objects.radar.addItem(exports, dom.o.className);
}
function destroy() {
radarItem?.die();
sprites.removeNodes(dom);
}
height = 28;
css = common.inheritCSS({
className: TYPES.superBunker,
friendly: 'friendly'
});
data = common.inheritData({
type: TYPES.superBunker,
bottomAligned: true,
frameCount: 0,
energy: (options.energy || 5),
energyMax: 5, // note: +/- depending on friendly vs. enemy infantry
energyLineScale: 0.95,
centerEnergyLine: true,
isEnemy: !!options.isEnemy,
width: 66,
halfWidth: 33,
doorWidth: 6,
height,
firing: false,
gunYOffset: 20.5,
// fire speed relative to # of infantry arming it
fireModulus: 8 - (options.energy || 0),
fundsModulus: FPS * 10,
hostile: false,
midPoint: null,
y: game.objects.view.data.world.height - height
}, options);
if (data.energy === 0) {
// initially neutral/hostile only if 0 energy
data.hostile = true;
}
// coordinates of the doorway
data.midPoint = {
x: data.x + data.halfWidth - (data.doorWidth / 2),
y: data.y,
// hackish: make the collision point the center, not the actual width
width: 1,
height: data.height
};
dom = {
o: null
};
exports = {
animate,
capture,
data,
destroy,
die,
dom,
hit,
init: initSuperBunker,
updateHealth,
refreshNearbyItems
};
nearby = {
options: {
source: exports,
targets: undefined,
useLookAhead: true,
hit(target) {
let isFriendly = (target.data.isEnemy === data.isEnemy);
const isTargetFriendlyToPlayer = (target.data.isEnemy === game.players.local.data.isEnemy);
if (!isFriendly && data.energy > 0) {
// nearby enemy, and defenses activated? let 'em have it.
setFiring(true);
}
// only infantry (excluding engineers by role=1) are involved, beyond this point
if (target.data.type !== TYPES.infantry || target.data.role) return;
// super bunkers can hold up to five men. only interact if not full (and friendly), OR an opposing, non-friendly infantry.
if (data.energy < data.energyMax || !isFriendly) {
// infantry at door? contribute to capture, or arm base, depending.
if (collisionCheckMidPoint(target, exports)) {
// claim infantry, change "alignment" depending on friendliness.
if (data.energy === 0) {
// claimed by infantry, switching sides from neutral/hostile.
data.hostile = false;
// ensure that if we were dead, we aren't any more.
data.dead = false;
// super bunker can be enemy, hostile or friendly. for now, we only care about enemy / friendly.
capture(target.data.isEnemy);
// update, now that capture has happened.
isFriendly = (target.data.isEnemy === data.isEnemy);
}
// add or subtract energy, depending on alignment.
// passing infantry on same team?
if (isFriendly) {
// friendly passer-by, relative to the super bunker.
data.energy++;
setFriendly(isTargetFriendlyToPlayer);
// switch over the first time energy goes up
if (data.energy > 0) {
// "one of ours?"
if (isTargetFriendlyToPlayer) {
game.objects.notifications.add('You reinforced a super bunker 💪');
} else {
game.objects.notifications.add('The enemy reinforced a super bunker 💪');
}
}
} else {
// enemy infantry hit.
// "one of ours?"
if (isTargetFriendlyToPlayer) {
if (data.energy > 1) game.objects.notifications.add('You weakened a super bunker ⚔️');
} else {
if (data.energy > 1) game.objects.notifications.add('The enemy weakened a super bunker ⚔️');
}
data.energy--;
}
// limit to +/- range.
data.energy = Math.min(data.energyMax, data.energy);
// small detail: firing speed relative to # of infantry
updateFireModulus();
if (data.energy === 0) {
// un-manned, but dangerous to helicopters on both sides.
data.hostile = true;
if (isTargetFriendlyToPlayer) {
game.objects.notifications.add('Your infantry neutralized a super bunker ⛳');
} else {
game.objects.notifications.add('Enemy infantry neutralized a super bunker ⚔️');
}
setFriendly(false);
}
// "claim" the infantry, kill if enemy and man the bunker if friendly.
target.die({ silent: true });
playSound(sounds.doorClose, exports);
sprites.updateEnergy(exports);
}
}
},
miss() {
setFiring(false);
}
},
// who gets fired at?
items: null,
targets: []
};
return exports;
};
export { SuperBunker };