diff --git a/src/game-v4.js b/src/game-v4.js index 4d8d771..2adb47e 100644 --- a/src/game-v4.js +++ b/src/game-v4.js @@ -3,7 +3,7 @@ import './style.css'; import airshipSheetUrl from './assets/airships.svg?url'; import explosionSheetUrl from './assets/explosions.svg?url'; import { airshipLog, registerDebugSnapshot } from './diagnostics.js'; -import { normalizePlayerDamage, processQueuedPlayerHits } from './game-logic.js'; +import { normalizePlayerDamage, reducePlayerVolleyHits } from './game-logic.js'; const WORLD_SIZE = 2400; const PLAYER_MAX_SPEED = 180; @@ -571,30 +571,17 @@ class GameScene extends Phaser.Scene { gameState: this.gameState, }; - const acceptedHits = []; - const ignoredHits = []; + const result = reducePlayerVolleyHits({ + hull: this.playerHull, + hits, + seenVolleyIds: this.damagedPlayerVolleys, + maxHull: PLAYER_MAX_HULL, + fallbackDamage: PIRATE_SHOT_DAMAGE, + }); - for (const hit of hits) { - const volleyId = hit.volleyId; - if (volleyId != null && this.damagedPlayerVolleys.has(volleyId)) { - ignoredHits.push(hit); - continue; - } - - if (volleyId != null) { - this.damagedPlayerVolleys.add(volleyId); - if (this.damagedPlayerVolleys.size > 128) { - const oldestVolleyId = this.damagedPlayerVolleys.values().next().value; - this.damagedPlayerVolleys.delete(oldestVolleyId); - } - } - - acceptedHits.push(hit); - } - - const totalDamage = acceptedHits.reduce((sum, hit) => sum + normalizePlayerDamage(hit.damage, PIRATE_SHOT_DAMAGE), 0); - this.playerHull = Math.max(0, this.playerHull - totalDamage); - const gameOver = this.playerHull <= 0; + this.damagedPlayerVolleys = result.seenVolleyIds; + this.playerHull = result.hull; + const { acceptedHits, ignoredHits, totalDamage, gameOver } = result; if (acceptedHits.length > 0) { const impact = acceptedHits[0];