diff --git a/main.js b/main.js index c6e7524..3d0df6c 100644 --- a/main.js +++ b/main.js @@ -108,6 +108,11 @@ function update() { const explosionType = EXPLOSION_TYPES[weapon.explosion.type]; if (!explosionType.update(explosion)) { + for (let player of players) { + if (player.energy > 0) { + player.energy -= explosionType.damage(explosion, player) || 0; + } + } explosion = null; state = 'land-collapse'; } @@ -128,7 +133,11 @@ function update() { stable = false; } } - if (stable) state = 'end-turn'; + if (stable) state = 'destroy-players'; + } + + else if (state === 'destroy-players') { + state = 'end-turn'; } else if (state === 'end-turn') { @@ -143,6 +152,10 @@ function update() { state = 'start-turn'; } + + else { + throw new Error(`Invalid state, ${state}`); + } } function draw() { @@ -154,7 +167,8 @@ function draw() { } function drawPlayer(player) { - const {x, y, a, c} = player; + const {x, y, a, c, energy} = player; + if (energy <= 0) return; const [px, py] = vec(x, y-3, a+180, 3); drawLine(foreground, x, y-3, Math.round(px), Math.round(py), c); drawRect(foreground, x-3, y-2, 6, 3, c); diff --git a/math.js b/math.js index ca98b9b..61133a5 100644 --- a/math.js +++ b/math.js @@ -27,6 +27,10 @@ export function wrap(min, x, max) { return ((x-min + max-min) % (max-min)) + min; } +export function within(x, y, tx, ty, r) { + return Math.abs(x-tx) < r && Math.abs(y-ty) < r; +} + export function parable(t, x, y, a, v, w=0, g=9.8) { const fx = x + t*v * Math.cos(a) - 0.5 * -w * t*t; const fy = y + t*v * Math.sin(a) - 0.5 * -g * t*t; diff --git a/weapons.js b/weapons.js index 0faf3f2..56a5c85 100644 --- a/weapons.js +++ b/weapons.js @@ -1,5 +1,6 @@ import {drawCircle} from './gfx.js'; import {clipTerrain} from './terrain.js'; +import {within} from './math.js'; export function drawExplosion(ctx, x, y, r) { const color = 255 - (16 * (r % 16)); @@ -29,6 +30,11 @@ export const EXPLOSION_TYPES = { drawExplosion(foreground, x, y, cr); clipTerrain(terrain, (ctx) => drawExplosion(ctx, x, y, cr)); }, + damage(explosion, player) { + if (within(explosion.x, explosion.y, player.x, player.y, explosion.r)) { + return 100; + } + } }, dirt: { create(spec, x, y) { @@ -43,5 +49,6 @@ export const EXPLOSION_TYPES = { drawExplosion(foreground, x, y, cr); drawExplosion(terrain, x, y, cr); }, + damage(explosion, player) {} }, }