diff --git a/src/game.js b/src/game.js index 4a24ba7..c6decc9 100644 --- a/src/game.js +++ b/src/game.js @@ -28,6 +28,11 @@ export class Game { this.debugMode = false; this.lastProximityWarning = 0; + // Move tracking for replay + this.playerMoves = []; + this.gameStartTime = null; + this.currentMazeLayout = null; + this.setupControls(); this.bindEvents(); } @@ -142,6 +147,9 @@ export class Game { if (this.maze.canMoveTo(newX, newY)) { this.player.moveTo(newX, newY); + // Track player move for replay + this.trackMove(newX, newY); + // Check if player stepped on a visual danger square if (this.maze.isDangerousVisual(newX, newY)) { this.handleVisualDanger(); @@ -159,6 +167,17 @@ export class Game { } } + trackMove(x, y) { + // Track player move with timestamp + const timestamp = Date.now(); + this.playerMoves.push({ + x: x, + y: y, + timestamp: timestamp, + relativeTime: timestamp - (this.gameStartTime || timestamp) + }); + } + handleVisualDanger() { // Visual danger squares end the game this.gameOver(); @@ -179,8 +198,7 @@ export class Game { } setTimeout(() => { - alert('Game Over! You stepped on a dangerous square.\nReturning to start...'); - this.resetGame(); + this.showReplay('Game Over! You stepped on a dangerous square.'); }, 500); } @@ -194,14 +212,153 @@ export class Game { } setTimeout(() => { - alert('🎉 Congratulations! You survived the danger field!\nGenerating new challenge...'); - this.resetGame(); + this.showReplay('🎉 Congratulations! You survived the danger field!'); }, 500); } + showReplay(message) { + // Create and show replay modal + this.createReplayModal(message); + this.startReplayAnimation(); + } + + createReplayModal(message) { + // Remove existing modal if any + const existingModal = document.getElementById('replayModal'); + if (existingModal) { + existingModal.remove(); + } + + // Create modal HTML + const modal = document.createElement('div'); + modal.id = 'replayModal'; + modal.innerHTML = ` +
Moves: ${this.playerMoves.length}
+Time: ${Math.round((Date.now() - this.gameStartTime) / 1000)}s
+