diff options
-rw-r--r-- | html/tower/js/game.js | 76 | ||||
-rw-r--r-- | html/tower/js/gameState.js | 23 |
2 files changed, 93 insertions, 6 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 diff --git a/html/tower/js/gameState.js b/html/tower/js/gameState.js index 9da8474..41f25e5 100644 --- a/html/tower/js/gameState.js +++ b/html/tower/js/gameState.js @@ -149,12 +149,13 @@ const gameState = { towers: [], enemies: [], currency: 100, - phase: 'placement', + phase: GamePhase.PLACEMENT, isGameOver: false, particles: [], projectiles: [], enemiesDestroyed: 0, enemiesEscaped: 0, + level: 1, // Define the function as part of the initial object awardEnemyDestroyed() { @@ -162,5 +163,25 @@ const gameState = { // Random reward between 1 and 3 const reward = Math.floor(Math.random() * 3) + 1; this.currency += reward; + }, + + // Add method to check for level completion + checkLevelComplete() { + return this.enemies.length === 0 && + enemiesRemaining === 0 && + this.phase === GamePhase.COMBAT; + }, + + // Add method to advance to next level + advanceToNextLevel() { + this.level++; + this.phase = GamePhase.PLACEMENT; + this.towers = []; // Clear existing towers + this.enemies = []; + this.projectiles = []; + this.particles = []; + this.grid = Array(20).fill().map(() => Array(20).fill('empty')); + // Award bonus currency for reaching new level + this.currency += 10; } }; \ No newline at end of file |