game: extract effectHandlers

This commit is contained in:
He4eT 2025-06-06 23:20:15 +02:00
commit 66a70082cf

78
game.js
View file

@ -358,45 +358,45 @@ function drawArena() {
} }
const effectHandlers = { const effectHandlers = {
laser: ({ from, to, frames }) => { laser: ({ from, to, frames }) => {
const color = frames.shift() const color = frames.shift()
line(from.x, from.y, to.x, to.y, color) line(from.x, from.y, to.x, to.y, color)
circ(from.x, from.y, frames.length / 3, color) circ(from.x, from.y, frames.length / 3, color)
circ(to.x, to.y, frames.length / 2, color) circ(to.x, to.y, frames.length / 2, color)
circb(to.x, to.y, frames.length, color + 3) circb(to.x, to.y, frames.length, color + 3)
}, },
nuke: ({ to, frames }) => { nuke: ({ to, frames }) => {
const color = frames.shift() const color = frames.shift()
circ(to.x, to.y, Math.pow(frames.length, 5), color) circ(to.x, to.y, Math.pow(frames.length, 5), color)
}, },
verticalLine: ({ to, frames }) => { verticalLine: ({ to, frames }) => {
const color = frames.shift() const color = frames.shift()
rect(0, to.y - frames.length, SCREEN_W, frames.length * 2, color) rect(0, to.y - frames.length, SCREEN_W, frames.length * 2, color)
}, },
horizontalLine: ({ to, frames }) => { horizontalLine: ({ to, frames }) => {
const color = frames.shift() const color = frames.shift()
rect(to.x - frames.length, 0, frames.length * 2, SCREEN_W, color) rect(to.x - frames.length, 0, frames.length * 2, SCREEN_W, color)
}, },
detection: ({ to, frames }) => { detection: ({ to, frames }) => {
const color = frames.shift() const color = frames.shift()
const w = arena.spriteHalfSize const w = arena.spriteHalfSize
const d = frames.length + 2 * w const d = frames.length + 2 * w
const corners = [ const corners = [
[+1, +1], [+1, +1],
[+1, -1], [+1, -1],
[-1, +1], [-1, +1],
[-1, -1], [-1, -1],
] ]
corners.forEach(([dx, dy]) => { corners.forEach(([dx, dy]) => {
const x = to.x + dx * d const x = to.x + dx * d
const y = to.y + dy * d const y = to.y + dy * d
line(x, y, x - dx * w, y, color) line(x, y, x - dx * w, y, color)
line(x, y, x, y - dy * w, color) line(x, y, x, y - dy * w, color)
}) })
}, },
} }
function drawFX() { function drawFX() {
effects effects
@ -405,9 +405,7 @@ function drawFX() {
from: arenaToScreen(effect.from ?? {}), from: arenaToScreen(effect.from ?? {}),
to: arenaToScreen(effect.to ?? {}), to: arenaToScreen(effect.to ?? {}),
})) }))
.forEach((effect) => .forEach((effect) => effectHandlers[effect.type](effect))
effectHandlers[effect.type](effect),
)
effects = effects.filter(({ frames }) => frames.length > 0) effects = effects.filter(({ frames }) => frames.length > 0)
} }