@maco-online/paint-tanks-rules 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/ai-host.d.ts +15 -0
- package/dist/ai-host.js +1 -0
- package/dist/balance-config.d.ts +50 -0
- package/dist/balance-config.js +41 -0
- package/dist/boss-ai.d.ts +9 -0
- package/dist/boss-ai.js +213 -0
- package/dist/bosses.d.ts +34 -0
- package/dist/bosses.js +47 -0
- package/dist/constants.d.ts +133 -0
- package/dist/constants.js +163 -0
- package/dist/enemy-ai.d.ts +11 -0
- package/dist/enemy-ai.js +361 -0
- package/dist/game-factory.d.ts +16 -0
- package/dist/game-factory.js +78 -0
- package/dist/game-state.d.ts +256 -0
- package/dist/game-state.js +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +27 -0
- package/dist/labels.d.ts +12 -0
- package/dist/labels.js +129 -0
- package/dist/levels.d.ts +45 -0
- package/dist/levels.js +209 -0
- package/dist/movement.d.ts +25 -0
- package/dist/movement.js +158 -0
- package/dist/online/match.d.ts +28 -0
- package/dist/online/match.js +176 -0
- package/dist/online/prediction.d.ts +41 -0
- package/dist/online/prediction.js +97 -0
- package/dist/online/protocol.d.ts +124 -0
- package/dist/online/protocol.js +54 -0
- package/dist/online/room-game.d.ts +5 -0
- package/dist/online/room-game.js +20 -0
- package/dist/online/view.d.ts +43 -0
- package/dist/online/view.js +347 -0
- package/dist/pathfinding.d.ts +8 -0
- package/dist/pathfinding.js +45 -0
- package/dist/power-ups.d.ts +28 -0
- package/dist/power-ups.js +66 -0
- package/dist/random.d.ts +1 -0
- package/dist/random.js +1 -0
- package/dist/sim-effects.d.ts +30 -0
- package/dist/sim-effects.js +1 -0
- package/dist/simulation.d.ts +74 -0
- package/dist/simulation.js +768 -0
- package/dist/sound-names.d.ts +4 -0
- package/dist/sound-names.js +1 -0
- package/dist/spawning.d.ts +3 -0
- package/dist/spawning.js +56 -0
- package/dist/tank-rules.d.ts +20 -0
- package/dist/tank-rules.js +39 -0
- package/dist/visibility.d.ts +20 -0
- package/dist/visibility.js +39 -0
- package/package.json +43 -3
- package/README.md +0 -3
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import { bossAI, bossSpecOf, spawnBoss } from "./boss-ai.js";
|
|
2
|
+
import { COL, DX, DY, N, PAINT, PET, SPAWNS, T, tankSpec, W, WIN_POINTS, WIN_STARS } from "./constants.js";
|
|
3
|
+
import { enemyAI } from "./enemy-ai.js";
|
|
4
|
+
import { CLAIM_MAX_TICKS, CLAIM_SLACK, claimStepAllowed, furthestAllowedPart, move, overlaps, tanksOf, turn } from "./movement.js";
|
|
5
|
+
import { distanceField } from "./pathfinding.js";
|
|
6
|
+
import { applyPowerUp, isPowerUp, POWER_COLORS, POWER_LIFE, POWER_PICK_LABELS, POWER_SOUNDS, rollPowerUp } from "./power-ups.js";
|
|
7
|
+
import { spawnEnemy } from "./spawning.js";
|
|
8
|
+
import { canFire, enemyDamage, fireCooldown, shotRadius, shotSpeed, STEALTH_CLOAK } from "./tank-rules.js";
|
|
9
|
+
import { playerFieldSources } from "./visibility.js";
|
|
10
|
+
/** The longest a fallen player waits for another tank to leave their starting position. */
|
|
11
|
+
const RESPAWN_MAX_WAIT = 120;
|
|
12
|
+
/** Less than this, in pixels, of a refused claim is not worth adopting; the server steps the tank itself instead. */
|
|
13
|
+
const MIN_PARTIAL_CLAIM = 0.25;
|
|
14
|
+
/** Angle between the middle shot of a fan and each side shot: 20 degrees. */
|
|
15
|
+
const FAN_SPREAD = Math.PI / 9;
|
|
16
|
+
/** Pixels a frame that stars and fruit drift toward a magnet tank. */
|
|
17
|
+
const MAGNET_PULL = 1.5;
|
|
18
|
+
/** Whether bullet `b` touches the tank `t`. */
|
|
19
|
+
function touches(b, t) {
|
|
20
|
+
return b.x > t.x - b.r && b.x < t.x + t.s + b.r && b.y > t.y - b.r && b.y < t.y + t.s + b.r;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* The rules of one level: players, enemies, bullets, pick-ups and the end of the level. It runs the same in the
|
|
24
|
+
* browser for local play and on the online server, so it touches no DOM, canvas or audio; everything to show or
|
|
25
|
+
* play goes to `effects`.
|
|
26
|
+
*/
|
|
27
|
+
export class PaintTanksSimulation {
|
|
28
|
+
game;
|
|
29
|
+
effects;
|
|
30
|
+
outcome = null;
|
|
31
|
+
/** For each online player, the input number of the last claim taken into account and the movement banked. */
|
|
32
|
+
claims = new WeakMap();
|
|
33
|
+
/** Ticks each fallen player has waited for another tank to leave their starting position. */
|
|
34
|
+
respawnWait = new Map();
|
|
35
|
+
constructor(game, effects) {
|
|
36
|
+
this.game = game;
|
|
37
|
+
this.effects = effects;
|
|
38
|
+
}
|
|
39
|
+
sound(name) {
|
|
40
|
+
this.effects.sound(name);
|
|
41
|
+
}
|
|
42
|
+
burst(x, y, color, count, type = 'dot') {
|
|
43
|
+
this.effects.burst(x, y, color, count, type);
|
|
44
|
+
}
|
|
45
|
+
/** Enemies the players still have to beat, the boss included. */
|
|
46
|
+
remaining() {
|
|
47
|
+
const g = this.game;
|
|
48
|
+
return g.toSpawn + g.enemies.filter(e => !e.minion).length + (g.D.boss && !g.bossDone ? 1 : 0);
|
|
49
|
+
}
|
|
50
|
+
/** Advances one frame (1/60 s); `inputs[i]` drives player `i`, a missing one holds still. */
|
|
51
|
+
step(inputs) {
|
|
52
|
+
const g = this.game;
|
|
53
|
+
if (g.state !== 'play') {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
g.t++;
|
|
57
|
+
const D = g.D;
|
|
58
|
+
if (g.fieldDirty) {
|
|
59
|
+
g.field = distanceField(g.map, [PET]);
|
|
60
|
+
g.fieldDirty = false;
|
|
61
|
+
}
|
|
62
|
+
if (g.t % 30 === 1) {
|
|
63
|
+
const { cells: src, keep } = playerFieldSources(g);
|
|
64
|
+
if (!keep) {
|
|
65
|
+
g.pfield = src.length ? distanceField(g.map, src) : null;
|
|
66
|
+
g.bfield = src.length && g.enemies.some(e => e.kind === 'bomb') ? distanceField(g.map, src, true) : null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (g.freeze > 0) {
|
|
70
|
+
g.freeze--;
|
|
71
|
+
}
|
|
72
|
+
for (const p of g.players) {
|
|
73
|
+
this.stepPlayer(p, inputs[p.idx] ?? { dir: -1, fire: false });
|
|
74
|
+
}
|
|
75
|
+
if (g.toSpawn > 0 && --g.spawnT <= 0) {
|
|
76
|
+
g.spawnT = 20;
|
|
77
|
+
if (g.enemies.filter(e => !e.minion).length < D.max) {
|
|
78
|
+
for (let k = 0; k < 3; k++) {
|
|
79
|
+
const i = (g.spawnIdx + k) % 3;
|
|
80
|
+
const [c, r] = SPAWNS[i];
|
|
81
|
+
const box = { x: c * T + 3, y: r * T + 3, s: 26 };
|
|
82
|
+
if (!tanksOf(g).some(o => overlaps(box, box.x, box.y, o))) {
|
|
83
|
+
spawnEnemy(g, i);
|
|
84
|
+
g.toSpawn--;
|
|
85
|
+
g.spawnIdx = i + 1;
|
|
86
|
+
g.spawnT = D.gap;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (D.boss && !g.boss && !g.bossDone && g.toSpawn === 0 && g.enemies.length <= D.bossEarly) {
|
|
93
|
+
if (g.bossT == null) {
|
|
94
|
+
g.bossT = 120;
|
|
95
|
+
g.banner = { key: 'bannerTeddyComing', boss: bossSpecOf(g.lv).id, life: 120 };
|
|
96
|
+
}
|
|
97
|
+
else if (--g.bossT <= 0) {
|
|
98
|
+
spawnBoss(this);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (D.rush && !D.boss && !g.rush && g.toSpawn === 0 && g.enemies.length > 0 && g.enemies.length <= 2) {
|
|
102
|
+
g.rush = true;
|
|
103
|
+
g.enemies.forEach(e => (e.angry = true));
|
|
104
|
+
g.banner = { key: 'bannerFinalRush', life: 110 };
|
|
105
|
+
}
|
|
106
|
+
for (const e of g.enemies) {
|
|
107
|
+
enemyAI(this, e);
|
|
108
|
+
}
|
|
109
|
+
if (g.boss) {
|
|
110
|
+
bossAI(this, g.boss);
|
|
111
|
+
}
|
|
112
|
+
for (const b of g.bullets) {
|
|
113
|
+
if (!b.dead) {
|
|
114
|
+
this.bulletStep(b);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
g.bullets = g.bullets.filter(b => !b.dead);
|
|
118
|
+
g.enemies = g.enemies.filter(e => !e.dead);
|
|
119
|
+
for (const k of g.picks) {
|
|
120
|
+
if (--k.life <= 0) {
|
|
121
|
+
k.dead = true;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
g.picks = g.picks.filter(k => !k.dead);
|
|
125
|
+
if (g.petFlash > 0) {
|
|
126
|
+
g.petFlash--;
|
|
127
|
+
}
|
|
128
|
+
if (g.shake > 0) {
|
|
129
|
+
g.shake--;
|
|
130
|
+
}
|
|
131
|
+
if (g.banner && --g.banner.life <= 0) {
|
|
132
|
+
g.banner = null;
|
|
133
|
+
}
|
|
134
|
+
if (!g.result) {
|
|
135
|
+
const clear = g.toSpawn === 0 && !g.enemies.length && (!D.boss || g.bossDone);
|
|
136
|
+
g.result = g.petHp <= 0 ? 'pet' : g.players.every(p => !p.alive) ? 'tanks' : clear ? 'win' : null;
|
|
137
|
+
if (g.result) {
|
|
138
|
+
g.endT = g.result === 'win' ? 90 : 60;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
else if (--g.endT <= 0) {
|
|
142
|
+
this.conclude();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Online movement: the browser drives its own tank and claims where it ended up. Each tick on the server earns the
|
|
147
|
+
* tank one tick of movement, banked up to `CLAIM_MAX_TICKS`, and a claim may spend as many ticks as the browser
|
|
148
|
+
* inputs it covers. Inputs that arrive bunched together after a late one are therefore adopted, while over time a
|
|
149
|
+
* tank never moves faster than its speed, whatever input numbers a browser sends. A claim within the budget that
|
|
150
|
+
* ends in a tank or wall is adopted as far as it is clear; any other claim that breaks the rules is replaced by one
|
|
151
|
+
* ordinary step in the held direction. Either way the browser is corrected.
|
|
152
|
+
*/
|
|
153
|
+
applyClaim(p, inp) {
|
|
154
|
+
const g = this.game;
|
|
155
|
+
const n = inp.n ?? 0;
|
|
156
|
+
// The direction only changes while a key is held; a tank keeps facing where it last drove.
|
|
157
|
+
if (inp.dir >= 0 && inp.dir <= 3) {
|
|
158
|
+
p.dir = inp.dir;
|
|
159
|
+
}
|
|
160
|
+
const last = this.claims.get(p) ?? { n: -1, credit: CLAIM_MAX_TICKS };
|
|
161
|
+
last.credit = Math.min(CLAIM_MAX_TICKS, last.credit + 1);
|
|
162
|
+
this.claims.set(p, last);
|
|
163
|
+
if (n === last.n) {
|
|
164
|
+
// No new input since the last tick: the tank stays where its last claim put it.
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// A smaller number than before is a new connection counting from the start again.
|
|
168
|
+
const covered = last.n >= 0 && n > last.n ? n - last.n : 1;
|
|
169
|
+
const ticks = Math.max(1, Math.min(covered, Math.floor(last.credit)));
|
|
170
|
+
last.n = n;
|
|
171
|
+
const x = inp.x;
|
|
172
|
+
const y = inp.y;
|
|
173
|
+
const budget = p.speed * ticks + CLAIM_SLACK;
|
|
174
|
+
if (claimStepAllowed(g, p, x, y, budget)) {
|
|
175
|
+
last.credit -= ticks;
|
|
176
|
+
if (x !== p.x || y !== p.y) {
|
|
177
|
+
p.anim += p.speed;
|
|
178
|
+
}
|
|
179
|
+
p.x = x;
|
|
180
|
+
p.y = y;
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
// The browser drove a little into something, typically a computer tank it saw a moment in the past: go as far
|
|
184
|
+
// toward the claim as the rules allow, so the browser is corrected by a few pixels instead of a whole step.
|
|
185
|
+
const part = furthestAllowedPart(g, p, x, y, budget);
|
|
186
|
+
if (part > 0) {
|
|
187
|
+
const nx = p.x + (x - p.x) * part;
|
|
188
|
+
const ny = p.y + (y - p.y) * part;
|
|
189
|
+
if (Math.abs(nx - p.x) + Math.abs(ny - p.y) >= MIN_PARTIAL_CLAIM) {
|
|
190
|
+
last.credit = Math.max(0, last.credit - Math.max(1, ticks * part));
|
|
191
|
+
p.anim += p.speed;
|
|
192
|
+
p.x = nx;
|
|
193
|
+
p.y = ny;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
last.credit = Math.max(0, last.credit - 1);
|
|
198
|
+
if (inp.dir >= 0 && inp.dir <= 3) {
|
|
199
|
+
turn(g, p, inp.dir, false);
|
|
200
|
+
if (move(g, p, p.speed)) {
|
|
201
|
+
p.anim += p.speed;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/** Takes player `p` out of the level, as when they leave an online room for good. */
|
|
206
|
+
retire(p) {
|
|
207
|
+
p.alive = false;
|
|
208
|
+
p.lives = 0;
|
|
209
|
+
}
|
|
210
|
+
stepPlayer(p, inp) {
|
|
211
|
+
const g = this.game;
|
|
212
|
+
if (!p.alive) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
if (p.respawn > 0) {
|
|
216
|
+
// Do not spawn on another tank, but keep the match moving if that tank never drives away.
|
|
217
|
+
const wait = this.respawnWait.get(p) ?? 0;
|
|
218
|
+
const blocked = p.respawn === 1 && tanksOf(g).some(o => o !== p && overlaps(p, p.sx, p.sy, o));
|
|
219
|
+
if (blocked && wait < RESPAWN_MAX_WAIT) {
|
|
220
|
+
this.respawnWait.set(p, wait + 1);
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
p.respawn--;
|
|
224
|
+
}
|
|
225
|
+
if (p.respawn === 0) {
|
|
226
|
+
this.respawnWait.delete(p);
|
|
227
|
+
p.x = p.sx;
|
|
228
|
+
p.y = p.sy;
|
|
229
|
+
p.dir = 0;
|
|
230
|
+
p.shield = g.D.shield;
|
|
231
|
+
// Claims sent before the respawn reached the browser still point at where the tank was hit.
|
|
232
|
+
this.claims.delete(p);
|
|
233
|
+
}
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (p.shield > 0) {
|
|
237
|
+
p.shield--;
|
|
238
|
+
}
|
|
239
|
+
if (p.cloak > 0) {
|
|
240
|
+
p.cloak--;
|
|
241
|
+
}
|
|
242
|
+
if (p.cd > 0) {
|
|
243
|
+
p.cd--;
|
|
244
|
+
}
|
|
245
|
+
this.stepPassives(p);
|
|
246
|
+
if (Number.isFinite(inp.x) && Number.isFinite(inp.y)) {
|
|
247
|
+
this.applyClaim(p, inp);
|
|
248
|
+
}
|
|
249
|
+
else if (inp.dir >= 0) {
|
|
250
|
+
turn(g, p, inp.dir, false);
|
|
251
|
+
if (move(g, p, p.speed)) {
|
|
252
|
+
p.anim += p.speed;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (inp.fire && canFire(p, g.bullets.filter(b => b.owner === p.idx + 1).length)) {
|
|
256
|
+
this.fire(p);
|
|
257
|
+
p.cd = fireCooldown(p);
|
|
258
|
+
p.quiet = 0;
|
|
259
|
+
if (p.spec.stealth && p.cloak <= STEALTH_CLOAK) {
|
|
260
|
+
p.cloak = 0;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
for (const k of g.picks) {
|
|
264
|
+
if (!k.dead && Math.abs(k.x - p.x - 13) < 18 && Math.abs(k.y - p.y - 13) < 18) {
|
|
265
|
+
k.dead = true;
|
|
266
|
+
if (isPowerUp(k.type)) {
|
|
267
|
+
applyPowerUp(g, p, k.type);
|
|
268
|
+
this.sound(POWER_SOUNDS[k.type]);
|
|
269
|
+
this.effects.float(k.x, k.y - 8, { kind: 'label', key: POWER_PICK_LABELS[k.type] }, POWER_COLORS[k.type]);
|
|
270
|
+
this.burst(k.x, k.y, POWER_COLORS[k.type], 12, 'star');
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
this.sound(k.type);
|
|
274
|
+
if (k.type === 'up') {
|
|
275
|
+
p.up = Math.min(4, (p.up || 1) + 1);
|
|
276
|
+
p.shield = Math.max(p.shield, 60);
|
|
277
|
+
g.score += 200 * g.mult;
|
|
278
|
+
this.effects.float(k.x, k.y - 8, { kind: 'raw', text: `Lv ${p.up}` }, COL.blue);
|
|
279
|
+
this.burst(k.x, k.y, COL.blue, 12, 'star');
|
|
280
|
+
}
|
|
281
|
+
else {
|
|
282
|
+
const v = k.type === 'star' ? 1 : 3;
|
|
283
|
+
g.stars += v;
|
|
284
|
+
g.score += 50 * v * g.mult;
|
|
285
|
+
this.effects.float(k.x, k.y - 8, { kind: 'raw', text: `+${v}★` }, COL.yellow);
|
|
286
|
+
this.burst(k.x, k.y, COL.yellow, 10, 'star');
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** Cooldowns and abilities a tank's spec gives it, run once a frame while it is in play. */
|
|
292
|
+
stepPassives(p) {
|
|
293
|
+
const g = this.game;
|
|
294
|
+
const spec = p.spec;
|
|
295
|
+
for (const k of ['guard', 'shieldT', 'healT']) {
|
|
296
|
+
if (p[k]) {
|
|
297
|
+
p[k]--;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
p.quiet = (p.quiet ?? 0) + 1;
|
|
301
|
+
if (spec.stealth && p.quiet >= spec.stealth) {
|
|
302
|
+
p.cloak = Math.max(p.cloak, STEALTH_CLOAK);
|
|
303
|
+
}
|
|
304
|
+
if (spec.magnet) {
|
|
305
|
+
const cx = p.x + p.s / 2;
|
|
306
|
+
const cy = p.y + p.s / 2;
|
|
307
|
+
for (const k of g.picks) {
|
|
308
|
+
const d = Math.hypot(cx - k.x, cy - k.y);
|
|
309
|
+
if ((k.type === 'star' || k.type === 'fruit') && d < spec.magnet && d > 0) {
|
|
310
|
+
const step = Math.min(d, MAGNET_PULL);
|
|
311
|
+
k.x += ((cx - k.x) / d) * step;
|
|
312
|
+
k.y += ((cy - k.y) / d) * step;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
breakTile(r, c, v) {
|
|
318
|
+
const g = this.game;
|
|
319
|
+
this.sound(v === 1 ? 'jelly' : v === 7 ? 'gift' : 'candy');
|
|
320
|
+
g.map[r][c] = 0;
|
|
321
|
+
g.fieldDirty = true;
|
|
322
|
+
this.burst(c * T + 16, r * T + 16, v === 1 ? 'rgba(207,32,47,.9)' : v === 2 ? COL.yellow : v === 7 ? COL.blue : COL.soft, 12, v === 2 || v === 7 ? 'star' : 'bubble');
|
|
323
|
+
}
|
|
324
|
+
fire(p) {
|
|
325
|
+
const g = this.game;
|
|
326
|
+
const spec = p.spec;
|
|
327
|
+
const cx = p.x + p.s / 2;
|
|
328
|
+
const cy = p.y + p.s / 2;
|
|
329
|
+
const d = p.dir;
|
|
330
|
+
const fx = DX[d];
|
|
331
|
+
const fy = DY[d];
|
|
332
|
+
const speed = shotSpeed(p);
|
|
333
|
+
const shot = (dx, dy, ox, oy) => g.bullets.push({
|
|
334
|
+
x: cx + ox,
|
|
335
|
+
y: cy + oy,
|
|
336
|
+
vx: dx * speed,
|
|
337
|
+
vy: dy * speed,
|
|
338
|
+
owner: p.idx + 1,
|
|
339
|
+
kind: p.kind,
|
|
340
|
+
r: shotRadius(p),
|
|
341
|
+
big: !!spec.big,
|
|
342
|
+
power: p.up >= 4,
|
|
343
|
+
...(spec.range ? { range: spec.range } : {}),
|
|
344
|
+
...(spec.bounce ? { bounce: spec.bounce } : {}),
|
|
345
|
+
...(spec.pierce ? { pierce: spec.pierce, hits: [] } : {})
|
|
346
|
+
});
|
|
347
|
+
const pattern = spec.pattern ?? 'single';
|
|
348
|
+
if (pattern === 'twin') {
|
|
349
|
+
for (const o of [-6, 6]) {
|
|
350
|
+
shot(fx, fy, fx * 16 + (d % 2 ? 0 : o), fy * 16 + (d % 2 ? o : 0));
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
else if (pattern === 'foreAft') {
|
|
354
|
+
shot(fx, fy, fx * 16, fy * 16);
|
|
355
|
+
shot(-fx, -fy, -fx * 16, -fy * 16);
|
|
356
|
+
}
|
|
357
|
+
else if (pattern === 'fan3') {
|
|
358
|
+
for (const a of [0, -FAN_SPREAD, FAN_SPREAD]) {
|
|
359
|
+
shot(fx * Math.cos(a) - fy * Math.sin(a), fx * Math.sin(a) + fy * Math.cos(a), fx * 16, fy * 16);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
else {
|
|
363
|
+
shot(fx, fy, fx * 16, fy * 16);
|
|
364
|
+
}
|
|
365
|
+
this.burst(cx + DX[d] * 16, cy + DY[d] * 16, p.color, 4);
|
|
366
|
+
this.sound(`shot-${spec.style}`);
|
|
367
|
+
}
|
|
368
|
+
efire(e, vx, vy, shot = {}) {
|
|
369
|
+
const cx = e.x + e.s / 2;
|
|
370
|
+
const cy = e.y + e.s / 2;
|
|
371
|
+
const m = Math.hypot(vx, vy) || 1;
|
|
372
|
+
const off = e.s * 0.55;
|
|
373
|
+
this.game.bullets.push({ x: cx + (vx / m) * off, y: cy + (vy / m) * off, vx, vy, owner: 0, r: e.boss ? 5 : 4, ...shot });
|
|
374
|
+
this.sound('eshot');
|
|
375
|
+
}
|
|
376
|
+
splat(b, x, y) {
|
|
377
|
+
if (b.owner && b.kind) {
|
|
378
|
+
this.effects.splash(b.kind, x, y, PAINT[b.owner], 1);
|
|
379
|
+
}
|
|
380
|
+
else {
|
|
381
|
+
this.burst(x, y, b.owner ? PAINT[b.owner] : COL.muted, 8);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
bulletStep(b) {
|
|
385
|
+
const g = this.game;
|
|
386
|
+
const n = Math.ceil(Math.hypot(b.vx, b.vy) / 2);
|
|
387
|
+
const spec = b.kind ? tankSpec(b.kind) : undefined;
|
|
388
|
+
for (let i = 0; i < n && !b.dead; i++) {
|
|
389
|
+
const px = b.x;
|
|
390
|
+
const py = b.y;
|
|
391
|
+
b.x += b.vx / n;
|
|
392
|
+
b.y += b.vy / n;
|
|
393
|
+
if (b.range !== undefined) {
|
|
394
|
+
b.range -= Math.hypot(b.vx, b.vy) / n;
|
|
395
|
+
if (b.range <= 0) {
|
|
396
|
+
this.splat(b, b.x, b.y);
|
|
397
|
+
b.dead = true;
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (b.x < 0 || b.y < 0 || b.x >= W || b.y >= W) {
|
|
402
|
+
if (this.bounce(b, px, py, b.x < 0 || b.x >= W, b.y < 0 || b.y >= W)) {
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
this.splat(b, px, py);
|
|
406
|
+
b.dead = true;
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
const c = Math.floor(b.x / T);
|
|
410
|
+
const r = Math.floor(b.y / T);
|
|
411
|
+
const v = g.map[r][c];
|
|
412
|
+
if (v === 0 || v === 6) {
|
|
413
|
+
// Bullets fly over the floor, and over a blanket, which only hides tanks.
|
|
414
|
+
}
|
|
415
|
+
else if (v === 7) {
|
|
416
|
+
g.hp[r][c] -= b.big || b.power ? 2 : 1;
|
|
417
|
+
if (g.hp[r][c] <= 0) {
|
|
418
|
+
this.breakTile(r, c, 7);
|
|
419
|
+
if (b.owner) {
|
|
420
|
+
g.picks.push({ x: c * T + 16, y: r * T + 16, type: Math.random() < 0.6 ? 'star' : 'fruit', life: 600 });
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
this.burst(b.x, b.y, COL.blue, 6);
|
|
425
|
+
this.sound('crack');
|
|
426
|
+
}
|
|
427
|
+
this.splat(b, b.x, b.y);
|
|
428
|
+
b.dead = true;
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
else if (v === 1) {
|
|
432
|
+
this.breakTile(r, c, 1);
|
|
433
|
+
this.splat(b, b.x, b.y);
|
|
434
|
+
b.dead = true;
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
else if (v === 2) {
|
|
438
|
+
g.hp[r][c] -= b.big || b.power ? 2 : 1;
|
|
439
|
+
if (g.hp[r][c] <= 0) {
|
|
440
|
+
this.breakTile(r, c, 2);
|
|
441
|
+
this.splat(b, b.x, b.y);
|
|
442
|
+
}
|
|
443
|
+
else {
|
|
444
|
+
this.burst(b.x, b.y, COL.yellow, 6);
|
|
445
|
+
this.sound('crack');
|
|
446
|
+
}
|
|
447
|
+
b.dead = true;
|
|
448
|
+
break;
|
|
449
|
+
}
|
|
450
|
+
else if (v === 3) {
|
|
451
|
+
if (b.power || spec?.breaksBlock) {
|
|
452
|
+
this.breakTile(r, c, 3);
|
|
453
|
+
this.splat(b, b.x, b.y);
|
|
454
|
+
}
|
|
455
|
+
else if (this.bounce(b, px, py, Math.floor(px / T) !== c, Math.floor(py / T) !== r)) {
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
else {
|
|
459
|
+
this.burst(px, py, COL.soft, 6);
|
|
460
|
+
this.splat(b, px, py);
|
|
461
|
+
if (b.owner) {
|
|
462
|
+
this.sound('block');
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
b.dead = true;
|
|
466
|
+
break;
|
|
467
|
+
}
|
|
468
|
+
else if (v === 5) {
|
|
469
|
+
const medic = g.players[b.owner - 1];
|
|
470
|
+
if (!b.owner && g.petHp > 0 && !b.bounced) {
|
|
471
|
+
g.petHp--;
|
|
472
|
+
g.petFlash = 40;
|
|
473
|
+
this.sound('pet');
|
|
474
|
+
this.effects.float(c * T + 16, r * T, { kind: 'raw', text: '-1♥' }, COL.red);
|
|
475
|
+
}
|
|
476
|
+
else if (spec?.support && medic && !medic.healT && g.petHp > 0 && g.petHp < g.D.pet) {
|
|
477
|
+
g.petHp++;
|
|
478
|
+
medic.healT = spec.support.healEvery;
|
|
479
|
+
this.sound('heal');
|
|
480
|
+
this.effects.float(c * T + 16, r * T, { kind: 'raw', text: '+1♥' }, COL.green);
|
|
481
|
+
}
|
|
482
|
+
this.burst(b.x, b.y, b.owner ? PAINT[b.owner] : COL.muted, 8);
|
|
483
|
+
b.dead = true;
|
|
484
|
+
break;
|
|
485
|
+
}
|
|
486
|
+
if (b.owner) {
|
|
487
|
+
for (const e of [...g.enemies, ...(g.boss ? [g.boss] : [])]) {
|
|
488
|
+
if (!e.dead && !e.spawning && !b.hits?.includes(e) && touches(b, e)) {
|
|
489
|
+
this.hitEnemy(e, b);
|
|
490
|
+
if (b.pierce && !e.boss) {
|
|
491
|
+
b.pierce--;
|
|
492
|
+
b.hits.push(e);
|
|
493
|
+
}
|
|
494
|
+
else {
|
|
495
|
+
b.dead = true;
|
|
496
|
+
}
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
// A teammate's tank stops the shot, unharmed, rather than letting it fly through.
|
|
501
|
+
const mate = b.dead ? undefined : g.players.find(p => p.idx + 1 !== b.owner && p.alive && !p.respawn && touches(b, p));
|
|
502
|
+
if (mate) {
|
|
503
|
+
this.splat(b, b.x, b.y);
|
|
504
|
+
b.dead = true;
|
|
505
|
+
const medic = g.players[b.owner - 1];
|
|
506
|
+
if (spec?.support && medic && !medic.shieldT && mate.shield < spec.support.shield) {
|
|
507
|
+
mate.shield = Math.max(mate.shield, spec.support.shield);
|
|
508
|
+
medic.shieldT = spec.support.shieldEvery;
|
|
509
|
+
this.sound('up');
|
|
510
|
+
this.burst(mate.x + mate.s / 2, mate.y + mate.s / 2, '#fff', 10, 'star');
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
else {
|
|
515
|
+
for (const p of g.players) {
|
|
516
|
+
if (p.alive && !p.respawn && touches(b, p)) {
|
|
517
|
+
if (p.shield > 0) {
|
|
518
|
+
this.burst(b.x, b.y, '#fff', 6, 'bubble');
|
|
519
|
+
this.sound('shield');
|
|
520
|
+
}
|
|
521
|
+
else if (this.guards(p, b)) {
|
|
522
|
+
p.guard = p.spec.guard;
|
|
523
|
+
this.burst(b.x, b.y, COL.green, 8, 'bubble');
|
|
524
|
+
this.sound('shield');
|
|
525
|
+
}
|
|
526
|
+
else {
|
|
527
|
+
this.hitPlayer(p);
|
|
528
|
+
}
|
|
529
|
+
b.dead = true;
|
|
530
|
+
break;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
if (b.dead) {
|
|
535
|
+
break;
|
|
536
|
+
}
|
|
537
|
+
for (const o of g.bullets) {
|
|
538
|
+
if (o !== b && !o.dead && !o.owner !== !b.owner && Math.hypot(o.x - b.x, o.y - b.y) < b.r + o.r + 2) {
|
|
539
|
+
o.dead = b.dead = true;
|
|
540
|
+
this.burst(b.x, b.y, '#ffffff', 8, 'bubble');
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Turns shot `b` back from what it just ran into, when it has a bounce left: it returns to (`px`, `py`) and
|
|
548
|
+
* reverses along each axis it crossed.
|
|
549
|
+
*/
|
|
550
|
+
bounce(b, px, py, flipX, flipY) {
|
|
551
|
+
if (!b.bounce) {
|
|
552
|
+
return false;
|
|
553
|
+
}
|
|
554
|
+
b.bounce--;
|
|
555
|
+
b.bounced = true;
|
|
556
|
+
b.vx *= flipX || !flipY ? -1 : 1;
|
|
557
|
+
b.vy *= flipY || !flipX ? -1 : 1;
|
|
558
|
+
b.x = px;
|
|
559
|
+
b.y = py;
|
|
560
|
+
return true;
|
|
561
|
+
}
|
|
562
|
+
/** Whether a guarding tank's front stops enemy shot `b`: one flying at it from ahead while the guard is ready. */
|
|
563
|
+
guards(p, b) {
|
|
564
|
+
if (!p.spec.guard || p.guard) {
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
const fx = DX[p.dir];
|
|
568
|
+
const fy = DY[p.dir];
|
|
569
|
+
const ahead = (b.x - p.x - p.s / 2) * fx + (b.y - p.y - p.s / 2) * fy > 0;
|
|
570
|
+
return ahead && b.vx * fx + b.vy * fy < 0;
|
|
571
|
+
}
|
|
572
|
+
/** Paint splashing from a hit on `hit`: every other ordinary enemy within `radius` loses 1. */
|
|
573
|
+
splashAround(hit, radius, b) {
|
|
574
|
+
const cx = hit.x + hit.s / 2;
|
|
575
|
+
const cy = hit.y + hit.s / 2;
|
|
576
|
+
this.burst(cx, cy, PAINT[b.owner] ?? COL.muted, 10, 'ring');
|
|
577
|
+
for (const o of this.game.enemies) {
|
|
578
|
+
if (o !== hit && !o.dead && !o.spawning && Math.hypot(o.x + o.s / 2 - cx, o.y + o.s / 2 - cy) < radius + o.s / 2) {
|
|
579
|
+
o.hp -= 1;
|
|
580
|
+
o.flash = 8;
|
|
581
|
+
if (o.hp <= 0) {
|
|
582
|
+
this.killEnemy(o, false, b);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
hitEnemy(e, b) {
|
|
588
|
+
if (e.boss && (e.shielded || e.inv > 0)) {
|
|
589
|
+
this.burst(b.x, b.y, '#ffffff', 8, 'bubble');
|
|
590
|
+
this.sound('shield');
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
const spec = b.kind ? tankSpec(b.kind) : undefined;
|
|
594
|
+
e.hp -= enemyDamage(spec, b, e);
|
|
595
|
+
e.flash = 8;
|
|
596
|
+
this.splat(b, b.x, b.y);
|
|
597
|
+
if (!e.boss && spec?.slow) {
|
|
598
|
+
e.slow = spec.slow.frames;
|
|
599
|
+
e.slowF = spec.slow.factor;
|
|
600
|
+
}
|
|
601
|
+
if (spec?.splash) {
|
|
602
|
+
this.splashAround(e, spec.splash, b);
|
|
603
|
+
}
|
|
604
|
+
if (e.hp > 0) {
|
|
605
|
+
this.sound('block');
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
this.killEnemy(e, false, b);
|
|
609
|
+
}
|
|
610
|
+
killEnemy(e, self, by) {
|
|
611
|
+
const g = this.game;
|
|
612
|
+
if (e.dead) {
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
e.dead = true;
|
|
616
|
+
// Water puts a bomb tank out before it can blow.
|
|
617
|
+
const defused = !e.boss && e.kind === 'bomb' && !!by?.kind && !!tankSpec(by.kind).defuse;
|
|
618
|
+
this.sound(e.boss ? 'bossDown' : e.kind === 'bomb' && !defused ? '' : 'pop');
|
|
619
|
+
const cx = e.x + e.s / 2;
|
|
620
|
+
const cy = e.y + e.s / 2;
|
|
621
|
+
if (by && by.owner && by.kind) {
|
|
622
|
+
this.effects.splash(by.kind, cx, cy, PAINT[by.owner], e.boss ? 3 : 2);
|
|
623
|
+
}
|
|
624
|
+
for (const col of [COL.blue, COL.yellow, COL.green, COL.red]) {
|
|
625
|
+
this.burst(cx, cy, col, e.boss ? 14 : 5, 'bubble');
|
|
626
|
+
}
|
|
627
|
+
this.burst(cx, cy, '#fff', 8);
|
|
628
|
+
if (e.boss) {
|
|
629
|
+
g.boss = null;
|
|
630
|
+
g.bossDone = true;
|
|
631
|
+
g.stars += 5;
|
|
632
|
+
const pts = (1000 + 20 * g.lv) * g.mult;
|
|
633
|
+
g.score += pts;
|
|
634
|
+
this.effects.float(cx, cy, { kind: 'points', value: pts }, COL.yellow);
|
|
635
|
+
g.banner = { key: 'bannerAllBubbles', life: 100 };
|
|
636
|
+
g.bullets.forEach((x) => {
|
|
637
|
+
if (!x.owner) {
|
|
638
|
+
x.dead = true;
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
// The boss's minions fall with it, so beating the boss clears the level.
|
|
642
|
+
for (const m of g.enemies) {
|
|
643
|
+
if (m.minion && !m.dead) {
|
|
644
|
+
m.dead = true;
|
|
645
|
+
this.burst(m.x + m.s / 2, m.y + m.s / 2, COL.muted, 6, 'bubble');
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
if (!e.minion) {
|
|
651
|
+
g.kills++;
|
|
652
|
+
}
|
|
653
|
+
if (!self) {
|
|
654
|
+
const pts = e.pts * g.mult;
|
|
655
|
+
g.score += pts;
|
|
656
|
+
this.effects.float(cx, cy - 6, { kind: 'points', value: pts }, '#0a0b0d');
|
|
657
|
+
}
|
|
658
|
+
if (e.carrier && !self) {
|
|
659
|
+
g.picks.push({ x: cx, y: cy, type: 'up', life: 900 });
|
|
660
|
+
}
|
|
661
|
+
else if (!self && Math.random() < g.D.drop) {
|
|
662
|
+
g.picks.push({ x: cx, y: cy, type: Math.random() < 0.7 ? 'star' : 'fruit', life: 600 });
|
|
663
|
+
}
|
|
664
|
+
const power = e.carrier || e.minion || self ? null : rollPowerUp(g);
|
|
665
|
+
if (power) {
|
|
666
|
+
const crowded = g.picks.some(k => !k.dead && k.x === cx && k.y === cy);
|
|
667
|
+
g.picks.push({ x: cx + (crowded ? (cx > W / 2 ? -14 : 14) : 0), y: cy, type: power, life: POWER_LIFE });
|
|
668
|
+
}
|
|
669
|
+
if (e.kind === 'bomb' && !defused) {
|
|
670
|
+
this.boom(cx, cy);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
boom(cx, cy) {
|
|
675
|
+
const g = this.game;
|
|
676
|
+
const R = 48;
|
|
677
|
+
g.shake = 12;
|
|
678
|
+
this.sound('boom');
|
|
679
|
+
this.burst(cx, cy, COL.red, 18, 'bubble');
|
|
680
|
+
this.burst(cx, cy, COL.yellow, 14, 'star');
|
|
681
|
+
this.burst(cx, cy, '#fff', 10);
|
|
682
|
+
this.effects.float(cx, cy - 12, { kind: 'label', key: 'boom' }, COL.red);
|
|
683
|
+
for (let r = 0; r < N; r++) {
|
|
684
|
+
for (let c = 0; c < N; c++) {
|
|
685
|
+
const v = g.map[r][c];
|
|
686
|
+
if ((v === 1 || v === 2 || v === 7) && Math.hypot(c * T + 16 - cx, r * T + 16 - cy) < R) {
|
|
687
|
+
this.breakTile(r, c, v);
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
for (const p of g.players) {
|
|
692
|
+
if (p.alive && !p.respawn && Math.hypot(p.x + 13 - cx, p.y + 13 - cy) < R + 6) {
|
|
693
|
+
this.hitPlayer(p);
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
if (g.petHp > 0 && Math.hypot(PET[0] * T + 16 - cx, PET[1] * T + 16 - cy) < R) {
|
|
697
|
+
g.petHp--;
|
|
698
|
+
g.petFlash = 40;
|
|
699
|
+
this.sound('pet');
|
|
700
|
+
this.effects.float(PET[0] * T + 16, PET[1] * T, { kind: 'raw', text: '-1♥' }, COL.red);
|
|
701
|
+
}
|
|
702
|
+
for (const o of g.enemies) {
|
|
703
|
+
if (!o.dead && !o.spawning && Math.hypot(o.x + 13 - cx, o.y + 13 - cy) < R) {
|
|
704
|
+
o.hp -= 2;
|
|
705
|
+
o.flash = 8;
|
|
706
|
+
if (o.hp <= 0) {
|
|
707
|
+
this.killEnemy(o, false);
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
const b = g.boss;
|
|
712
|
+
if (b && !b.shielded && !b.inv && Math.hypot(b.x + 30 - cx, b.y + 30 - cy) < R + 20) {
|
|
713
|
+
b.hp -= 3;
|
|
714
|
+
b.flash = 8;
|
|
715
|
+
if (b.hp <= 0) {
|
|
716
|
+
this.killEnemy(b, false);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
hitPlayer(p) {
|
|
721
|
+
if (p.respawn || p.shield > 0 || !p.alive) {
|
|
722
|
+
return;
|
|
723
|
+
}
|
|
724
|
+
p.lives--;
|
|
725
|
+
p.cloak = 0;
|
|
726
|
+
p.quiet = 0;
|
|
727
|
+
p.up = Math.max(1, (p.up || 1) - 1);
|
|
728
|
+
this.sound('hurt');
|
|
729
|
+
this.burst(p.x + 13, p.y + 13, COL.muted, 14, 'bubble');
|
|
730
|
+
this.effects.float(p.x + 13, p.y, { kind: 'raw', text: '-1♥' }, COL.red);
|
|
731
|
+
if (p.lives <= 0) {
|
|
732
|
+
p.alive = false;
|
|
733
|
+
}
|
|
734
|
+
else {
|
|
735
|
+
p.respawn = 90;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
/** Ends the level once its closing pause is over: adds the clear bonus and records the outcome. */
|
|
739
|
+
conclude() {
|
|
740
|
+
const g = this.game;
|
|
741
|
+
const D = g.D;
|
|
742
|
+
g.state = 'over';
|
|
743
|
+
const win = g.result === 'win';
|
|
744
|
+
const carryUp = g.players.map(p => (p.alive ? p.up : 1));
|
|
745
|
+
if (win) {
|
|
746
|
+
g.score +=
|
|
747
|
+
(WIN_POINTS + g.players.reduce((a, p) => a + (p.alive ? p.lives : 0), 0) * 300 + g.petHp * 200) * g.mult;
|
|
748
|
+
}
|
|
749
|
+
g.score = Math.round(g.score);
|
|
750
|
+
const kp = Math.min(1, g.kills / g.total);
|
|
751
|
+
const bp = g.bossDone ? 1 : g.boss ? 1 - g.boss.hp / g.boss.maxHp : 0;
|
|
752
|
+
const progress = win ? 1 : D.boss ? kp * 0.6 + bp * 0.4 : kp;
|
|
753
|
+
let lossMessage = null;
|
|
754
|
+
if (!win) {
|
|
755
|
+
const rem = this.remaining();
|
|
756
|
+
if (D.boss && g.boss && g.boss.hp / g.boss.maxHp <= 0.25) {
|
|
757
|
+
lossMessage = { key: 'msgTeddyLow', params: { hp: g.boss.hp } };
|
|
758
|
+
}
|
|
759
|
+
else if (!D.boss && rem <= 3) {
|
|
760
|
+
lossMessage = { key: 'msgFewLeft', params: { count: rem }, plural: rem };
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
lossMessage = { key: g.result === 'pet' ? 'msgPuppyMuddy' : 'msgNoTanks' };
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
this.outcome = { win, lv: g.lv, score: g.score, stars: g.stars + (win ? WIN_STARS : 0), progress, lossMessage, carryUp };
|
|
767
|
+
}
|
|
768
|
+
}
|