about summary refs log tree commit diff stats
path: root/html/tower/js/game.js
diff options
context:
space:
mode:
Diffstat (limited to 'html/tower/js/game.js')
-rw-r--r--html/tower/js/game.js76
1 files changed, 71 insertions, 5 deletions
diff --git a/html/tower/js/game.js b/html/tower/js/game.js
index 5ab2396..2fdcc84 100644
--- a/html/tower/js/game.js
+++ b/html/tower/js/game.js
@@ -24,21 +24,21 @@ let hoverCell = null;
  * - Game state management
  */
 function gameLoop(timestamp) {
-    // Calculate time since last frame for consistent motion
     const deltaTime = timestamp - lastTimestamp;
     lastTimestamp = timestamp;
     
-    // Clear the canvas for the next frame
     ctx.clearRect(0, 0, canvas.width, canvas.height);
     
-    // Update game state based on current phase
     if (gameState.phase === GamePhase.COMBAT) {
         handleCombatPhase(timestamp, deltaTime);
+        
+        // Check for level completion
+        if (gameState.checkLevelComplete()) {
+            handleLevelComplete();
+        }
     }
     
-    // Render the current game state
     renderGame();
-    // Schedule the next frame
     requestAnimationFrame(gameLoop);
 }
 
@@ -234,4 +234,70 @@ function initializeEventListeners() {
     };
     
     updateStartButton();
+}
+
+/**
+ * Handles the transition between levels
+ * Shows completion message and sets up next level
+ */
+function handleLevelComplete() {
+    // Pause the game briefly
+    gameState.phase = GamePhase.TRANSITION;
+    
+    // Show level complete message with modal
+    const message = `
+        Level ${gameState.level} Complete!
+        Current Money: $${gameState.currency}
+        Level Bonus: +$10
+        
+        Ready for Level ${gameState.level + 1}?
+    `;
+    
+    // Use setTimeout to allow the final frame to render
+    setTimeout(() => {
+        if (confirm(message)) {
+            startNextLevel();
+        }
+    }, 100);
+}
+
+/**
+ * Sets up the next level
+ * Increases difficulty and resets the game state while preserving currency
+ */
+function startNextLevel() {
+    gameState.advanceToNextLevel();
+    
+    // Generate new path
+    generatePath(gameState.grid).then(path => {
+        gameState.path = path;
+        
+        // Increase number of enemies for each level
+        const baseEnemies = 5;
+        const enemiesPerLevel = 3;
+        enemiesRemaining = baseEnemies + (gameState.level - 1) * enemiesPerLevel;
+        
+        // Re-enable tower palette
+        document.querySelectorAll('.tower-option').forEach(option => {
+            option.draggable = true;
+            option.style.cursor = 'grab';
+            option.style.opacity = '1';
+        });
+        
+        // Reset start button
+        const startButton = document.getElementById('startCombat');
+        startButton.disabled = false;
+        startButton.textContent = `Start Level ${gameState.level}`;
+    });
+}
+
+// Update the renderUI function to show current level
+function renderUI(ctx, gameState) {
+    ctx.fillStyle = 'black';
+    ctx.font = '20px Arial';
+    ctx.fillText(`Level: ${gameState.level}`, 10, 30);
+    ctx.fillText(`Currency: $${gameState.currency}`, 10, 60);
+    ctx.fillText(`Phase: ${gameState.phase}`, 10, 90);
+    ctx.fillText(`Destroyed: ${gameState.enemiesDestroyed}`, 10, 120);
+    ctx.fillText(`Escaped: ${gameState.enemiesEscaped}`, 10, 150);
 } 
\ No newline at end of file