diff options
author | elioat <elioat@tilde.institute> | 2024-12-06 08:31:03 -0500 |
---|---|---|
committer | elioat <elioat@tilde.institute> | 2024-12-06 08:31:03 -0500 |
commit | 67203663c65c1862a213ce7b03fb7f825d46a59d (patch) | |
tree | 3d48c9e5d77abfc814f0707e3c49121d87eb0b1d /html/mountain | |
parent | 99d2a6c0fca85a67f8579dd2efa26bfb89d54ff0 (diff) | |
download | tour-67203663c65c1862a213ce7b03fb7f825d46a59d.tar.gz |
*
Diffstat (limited to 'html/mountain')
-rw-r--r-- | html/mountain/game.js | 120 |
1 files changed, 85 insertions, 35 deletions
diff --git a/html/mountain/game.js b/html/mountain/game.js index acabef1..be75810 100644 --- a/html/mountain/game.js +++ b/html/mountain/game.js @@ -37,7 +37,7 @@ const MIN_PLATFORM_WIDTH = 100; const MAX_PLATFORM_WIDTH = 300; const GRAVITY = 0.5; const JUMP_FORCE = 12; -const MOVE_SPEED = 5; +const MOVE_SPEED = 7; // Add these constants for level generation const MIN_PARTITION_SIZE = MAX_PLATFORM_WIDTH + 20; // Reduced from 50 to allow more splits @@ -175,7 +175,7 @@ let level = 1; let platforms = []; let player = { x: PLAYER_SIZE, - y: window.innerHeight - PLAYER_SIZE, + y: window.innerHeight - PLAYER_SIZE * 2, velocityX: 0, velocityY: 0, isJumping: false, @@ -192,7 +192,7 @@ let exit = { // Add reset player function function resetPlayer() { player.x = PLAYER_SIZE; - player.y = window.innerHeight - PLAYER_SIZE; + player.y = window.innerHeight - PLAYER_SIZE * 2; player.velocityX = 0; player.velocityY = 0; player.isJumping = false; @@ -204,33 +204,32 @@ function resetPlayer() { generateLevel(); } -// Update the generateLevel function to use these new functions +// Restore the original generateLevel function function generateLevel() { platforms = []; - // Add start platform - const startPlatform = { + // Add starting platform + platforms.push({ x: 0, y: window.innerHeight - PLATFORM_HEIGHT, width: MIN_PLATFORM_WIDTH, - height: PLATFORM_HEIGHT - }; - platforms.push(startPlatform); - + height: PLATFORM_HEIGHT, + type: PLATFORM_TYPE.NORMAL + }); + // Add end platform - const endPlatform = { + platforms.push({ x: window.innerWidth - MIN_PLATFORM_WIDTH, y: PLAYER_SIZE * 3, width: MIN_PLATFORM_WIDTH, - height: PLATFORM_HEIGHT - }; - platforms.push(endPlatform); + height: PLATFORM_HEIGHT, + type: PLATFORM_TYPE.NORMAL + }); - // Create multiple root partitions for better coverage - const verticalSections = 3; - const horizontalSections = 2; + const horizontalSections = 3; + const verticalSections = 2; const sectionWidth = window.innerWidth / horizontalSections; - const sectionHeight = (window.innerHeight - PLATFORM_HEIGHT * 6) / verticalSections; + const sectionHeight = (window.innerHeight - PLATFORM_HEIGHT * 4) / verticalSections; function subdivide(node, depth) { if (depth === 0) { @@ -344,24 +343,28 @@ function updatePlayer() { } } - // Keep player in bounds (modified for gravity direction) + // Check for deadly border collisions (modified for gap) + if (player.y <= DEADLY_BORDER_HEIGHT) { + // Only die if touching the actual drawn deadly border + if (player.x < canvas.width - exit.size - 300) { + player.isDead = true; + gameState = GAME_STATE.GAME_OVER; + } + } else if (player.y + PLAYER_SIZE >= canvas.height - DEADLY_BORDER_HEIGHT) { + player.isDead = true; + gameState = GAME_STATE.GAME_OVER; + } + + // Keep player in bounds (modified to account for deadly borders) if (player.x < 0) player.x = 0; if (player.x + PLAYER_SIZE > canvas.width) player.x = canvas.width - PLAYER_SIZE; if (player.y < 0) { player.y = 0; player.velocityY = 0; - if (player.gravityMultiplier < 0) { - player.isJumping = false; - player.jumpsLeft = 2; - } } if (player.y + PLAYER_SIZE > canvas.height) { player.y = canvas.height - PLAYER_SIZE; player.velocityY = 0; - if (player.gravityMultiplier > 0) { - player.isJumping = false; - player.jumpsLeft = 2; - } } // Check if player reached exit @@ -376,11 +379,28 @@ function updatePlayer() { } } -function draw() { +// Add these variables at the top with other game state +let frameCount = 0; +let lastFpsUpdate = 0; +let currentFps = 0; + +// Add this constant at the top with other constants +const DEADLY_BORDER_HEIGHT = 7; + +// Update the draw function to include the deadly borders +function draw(currentTime) { // Clear canvas with light grey ctx.fillStyle = '#E0E0E0'; ctx.fillRect(0, 0, canvas.width, canvas.height); + // Draw deadly borders with gap for exit + ctx.fillStyle = '#FF0000'; + // Top border (with gap) + ctx.fillRect(0, 0, canvas.width - exit.size - 300, DEADLY_BORDER_HEIGHT); // Left section stops before exit + + // Bottom border (full width) + ctx.fillRect(0, canvas.height - DEADLY_BORDER_HEIGHT, canvas.width, DEADLY_BORDER_HEIGHT); + // Draw platforms (almost black for normal platforms, red for deadly) for (let platform of platforms) { ctx.fillStyle = platform.type === PLATFORM_TYPE.DEADLY ? @@ -399,9 +419,10 @@ function draw() { ctx.fillRect(player.x, player.y, PLAYER_SIZE, PLAYER_SIZE); } - // Draw level number + // Draw level number only ctx.fillStyle = '#000000'; ctx.font = '20px Arial'; + ctx.textAlign = 'left'; ctx.fillText(`Level: ${level}`, 10, 30); // Draw game over screen @@ -421,13 +442,42 @@ function draw() { } } -// Game loop -function gameLoop() { - updatePlayer(); - draw(); +// Add these constants at the top with other game constants +const FPS = 60; +const FRAME_TIME = 1000 / FPS; + +// Replace the simple game loop with this new version +let lastFrameTime = 0; +let accumulator = 0; + +function gameLoop(currentTime) { + if (lastFrameTime === 0) { + lastFrameTime = currentTime; + lastFpsUpdate = currentTime; + } + + // Calculate delta time + const deltaTime = currentTime - lastFrameTime; + lastFrameTime = currentTime; + + // Accumulate time to process + accumulator += deltaTime; + + // Update physics at a fixed time step + while (accumulator >= FRAME_TIME) { + updatePlayer(); + accumulator -= FRAME_TIME; + } + + // Render at whatever frame rate the browser can handle + draw(currentTime); requestAnimationFrame(gameLoop); } -// Start game +// Initialize the first level before starting the game loop generateLevel(); -gameLoop(); + +// Start the game loop +lastFrameTime = 0; +accumulator = 0; +requestAnimationFrame(gameLoop); |