From d0206ef337f31c94f2da57ea0b43e3e0be76ed1b Mon Sep 17 00:00:00 2001 From: He4eT Date: Fri, 6 Jun 2025 20:16:27 +0200 Subject: [PATCH] game: destroy enemies by letter --- game.js | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/game.js b/game.js index 9c0c454..b8c93e9 100644 --- a/game.js +++ b/game.js @@ -78,6 +78,7 @@ let player = { /** * @typedef {{ + * letter: string, * type: 'zombie', * dangerZone: number, * positions: Point[], @@ -173,19 +174,33 @@ function handleMorse() { player.sprite = btn(BTN_B) ? 65 : 64 if (btnp(BTN_B, 100, 100)) { - if (enemies.length > 0) { - effects.unshift({ - type: 'laser', - from: player.position, - to: enemies[0].positions[0], - frames: [1, 2, 3, 4, 7, 7, 7, 6, 5, 4, 3, 2, 1], - }) - enemies.shift() - trace(enemies.length) - } + destroyEnemiesByLetter('e') } } +function destroyEnemiesByLetter(letter) { + const destructionEffects = [ + ['laser', [1, 2, 3, 4, 7, 7, 7, 6, 5, 4, 3, 2, 1]], + ['nuke', [7, 6, 5, 4, 3, 2]], + ['verticalLine', [4, 5, 6, 7, 7, 6, 5, 4]], + ['horizontalLine', [4, 5, 6, 7, 7, 6, 5, 4]], + ] + + enemies + .filter((enemy) => enemy.letter === letter) + .forEach((enemy) => { + const [type, frames] = destructionEffects[rnd(0, 3)] + effects.unshift({ + type, + frames, + from: player.position, + to: enemy.positions[0], + }) + }) + + enemies = enemies.filter((enemy) => enemy.letter !== letter) +} + /* Enemies */ function spawn() { @@ -199,6 +214,7 @@ function spawn() { }, ], type: 'zombie', + letter: 'e', dangerZone: 8, }, ] @@ -206,7 +222,6 @@ function spawn() { enemies.forEach((enemy) => { effects.unshift({ type: 'detection', - from: {}, to: enemy.positions[0], frames: Array(5).fill(4), }) @@ -263,13 +278,12 @@ function drawFX() { effects .map((effect) => ({ ...effect, - from: arenaToScreen(effect.from), - to: arenaToScreen(effect.to), + from: arenaToScreen(effect.from ?? {}), + to: arenaToScreen(effect.to ?? {}), })) .forEach((effect) => ({ laser: ({ from, to, frames }) => { - /* [1, 2, 3, 4, 7, 7, 7, 6, 5, 4, 3, 2, 1] */ const color = frames.shift() line(from.x, from.y, to.x, to.y, color) circ(from.x, from.y, frames.length / 3, color) @@ -277,22 +291,18 @@ function drawFX() { circb(to.x, to.y, frames.length, color + 3) }, nuke: ({ to, frames }) => { - /* [6, 5, 4, 3, 2] */ const color = frames.shift() circ(to.x, to.y, Math.pow(frames.length, 5), color) }, verticalLine: ({ to, frames }) => { - /* [4, 5, 6, 7, 7, 6, 5, 4] */ const color = frames.shift() rect(0, to.y - frames.length, SCREEN_W, frames.length * 2, color) }, horizontalLine: ({ to, frames }) => { - /* [4, 5, 6, 7, 7, 6, 5, 4] */ const color = frames.shift() rect(to.x - frames.length, 0, frames.length * 2, SCREEN_W, color) }, detection: ({ to, frames }) => { - /* Array(5).fill(4)] */ const color = frames.shift() const w = arena.spriteHalfSize const d = frames.length + 2 * w