From 7bd730816a3882d57f7db46352f0ee00ce22310f Mon Sep 17 00:00:00 2001 From: Admin Date: Thu, 17 Sep 2026 21:59:43 +0000 Subject: [PATCH] Fix hit feedback and native scene reset --- src/game-v4.js | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/game-v4.js b/src/game-v4.js index f108a08..2023202 100644 --- a/src/game-v4.js +++ b/src/game-v4.js @@ -15,6 +15,8 @@ const SHOT_SPEED = 520; const SHOT_LIFETIME = 900; const PLAYER_MAX_HULL = 100; const PIRATE_MAX_HULL = 120; +const PIRATE_SHOT_DAMAGE = 6; +const PLAYER_HIT_COOLDOWN_MS = 300; const MAX_PIRATES = 6; const AUTO_RESET_MS = 2200; @@ -33,10 +35,14 @@ const headingRad = (ship) => degToRad(ship.angle - 90); class GameScene extends Phaser.Scene { constructor() { super('game'); + } + + init() { this.playerSpeed = 0; this.playerHull = PLAYER_MAX_HULL; this.gameState = 'playing'; this.lastPlayerShot = 0; + this.lastPlayerHitAt = -Infinity; this.wave = 1; this.defeated = 0; } @@ -119,6 +125,9 @@ class GameScene extends Phaser.Scene { }; this.scale.on('resize', this.layoutHud, this); + this.events.once(Phaser.Scenes.Events.SHUTDOWN, () => { + this.scale.off('resize', this.layoutHud, this); + }); this.layoutHud({ width: this.scale.width, height: this.scale.height }); this.spawnWave(); } @@ -305,7 +314,7 @@ class GameScene extends Phaser.Scene { const lastShot = pirate.getData('lastShot') || 0; if (distance < 420 && broadsideError < 38 && this.time.now - lastShot > 1650) { const side = relative >= 0 ? 1 : -1; - this.fireBroadside(pirate, side, this.pirateShots, 'pirate-shot', 12, 0.9); + this.fireBroadside(pirate, side, this.pirateShots, 'pirate-shot', PIRATE_SHOT_DAMAGE, 0.9); pirate.setData('lastShot', this.time.now); } } @@ -343,7 +352,7 @@ class GameScene extends Phaser.Scene { const damage = shot.getData('damage') || 10; const { x, y } = shot; shot.destroy(); - this.spawnExplosion(x, y, 0.7); + this.spawnExplosion(x, y, 0.32); const hull = Math.max(0, (pirate.getData('hull') || PIRATE_MAX_HULL) - damage); pirate.setData('hull', hull); @@ -356,7 +365,7 @@ class GameScene extends Phaser.Scene { if (this.pirates.countActive(true) === 0) { this.wave += 1; this.playerHull = Math.min(PLAYER_MAX_HULL, this.playerHull + 15); - this.status.setText(`WAVE CLEARED\n+15 HULL`); + this.status.setText('WAVE CLEARED\n+15 HULL'); this.time.delayedCall(1100, () => { if (this.gameState === 'playing' && this.pirates.countActive(true) === 0) this.spawnWave(); }); @@ -369,11 +378,22 @@ class GameScene extends Phaser.Scene { hitPlayer(shot) { if (!shot?.active || this.gameState !== 'playing') return; - const damage = shot.getData('damage') || 10; + + const damage = shot.getData('damage') || PIRATE_SHOT_DAMAGE; const { x, y } = shot; shot.destroy(); - this.spawnExplosion(x, y, 0.7); + + if (this.time.now - this.lastPlayerHitAt < PLAYER_HIT_COOLDOWN_MS) return; + this.lastPlayerHitAt = this.time.now; + this.playerHull = Math.max(0, this.playerHull - damage); + + if (this.playerHull <= 0) { + this.finishGame(); + return; + } + + this.spawnExplosion(x, y, 0.28); this.flashShip(this.player, 0xffcccc); } @@ -485,10 +505,12 @@ class GameScene extends Phaser.Scene { } this.spawnExplosion(this.player.x, this.player.y, 1.35); - this.player.setTint(0x555555); - this.status.setText(`SHIP DISABLED\nRestarting...`); + this.player.setVisible(false); + this.status.setText('SHIP DESTROYED\nRestarting in 2s...'); - this.time.delayedCall(AUTO_RESET_MS, () => this.scene.restart()); + this.time.delayedCall(AUTO_RESET_MS, () => { + this.scene.restart(); + }); } layoutHud(gameSize) { @@ -517,4 +539,4 @@ new Phaser.Game({ autoCenter: Phaser.Scale.CENTER_BOTH, }, scene: GameScene, -}); +}); \ No newline at end of file