diff options
| author | elioat <elioat@tilde.institute> | 2025-08-28 20:34:14 -0400 |
|---|---|---|
| committer | elioat <elioat@tilde.institute> | 2025-08-28 20:34:14 -0400 |
| commit | f624f3826fc7d1d297b07709d26b5238e1da0646 (patch) | |
| tree | 5e7c74dc3118cf55611675666d220e142f1944af /html/smoosh | |
| parent | 5703c544f06bacae71cd63a56aa98cf0895abf52 (diff) | |
| download | tour-f624f3826fc7d1d297b07709d26b5238e1da0646.tar.gz | |
*
Diffstat (limited to 'html/smoosh')
| -rw-r--r-- | html/smoosh/smoosh.js | 173 |
1 files changed, 154 insertions, 19 deletions
diff --git a/html/smoosh/smoosh.js b/html/smoosh/smoosh.js index 66df35f..1e7ed14 100644 --- a/html/smoosh/smoosh.js +++ b/html/smoosh/smoosh.js @@ -31,6 +31,10 @@ class GameEngine { this.FPS = 60; this.FRAME_TIME = 1000 / this.FPS; + // Frame rate smoothing + this.frameTimes = []; + this.maxFrameSamples = 10; + this.init(); this.inputManager.setupRestartHandler(this); } @@ -104,7 +108,16 @@ class GameEngine { const deltaTime = currentTime - this.lastFrameTime; this.lastFrameTime = currentTime; - this.accumulator += deltaTime; + // Frame rate smoothing + this.frameTimes.push(deltaTime); + if (this.frameTimes.length > this.maxFrameSamples) { + this.frameTimes.shift(); + } + + // Use smoothed delta time for more consistent movement + const smoothedDeltaTime = this.frameTimes.reduce((a, b) => a + b, 0) / this.frameTimes.length; + + this.accumulator += smoothedDeltaTime; while (this.accumulator >= this.FRAME_TIME) { this.update(this.FRAME_TIME); @@ -122,7 +135,7 @@ class GameEngine { class GameState { constructor() { - this.LEVEL_WIDTH_MULTIPLIER = 300; + this.LEVEL_WIDTH_MULTIPLIER = 10; // Increased for testing smooth scrolling this.viewport = { width: 1200, height: 800 }; this.level = 1; this.state = 'playing'; // playing, game_over, level_complete @@ -134,20 +147,21 @@ class GameState { update(deltaTime) { this.player.update(deltaTime, this); - this.camera.update(this.player, this.viewport, this.LEVEL_WIDTH_MULTIPLIER); + this.camera.update(this.player, this.viewport, this.LEVEL_WIDTH_MULTIPLIER, deltaTime); } reset() { this.level = 1; this.state = 'playing'; this.player.reset(); + this.player.resetPosition(this.viewport.height); this.platforms = []; } nextLevel() { this.level++; this.state = 'level_complete'; - this.player.resetPosition(); + this.player.resetPosition(this.viewport.height); setTimeout(() => { this.state = 'playing'; @@ -170,7 +184,7 @@ class Player { reset() { this.x = 20; - this.y = 800 - 40; + this.y = 800 - 40; // Will be updated in resetPosition this.velocityX = 0; this.velocityY = 0; this.size = 20; @@ -183,9 +197,9 @@ class Player { this.isDead = false; } - resetPosition() { + resetPosition(viewportHeight = 800) { this.x = 20; - this.y = 800 - 40; + this.y = viewportHeight - 40; this.velocityX = 0; this.velocityY = 0; this.jumpsLeft = 2; @@ -225,8 +239,14 @@ class Player { applyPhysics(deltaTime) { this.velocityY += 0.5; // Gravity + + // Sub-pixel movement for smoother scrolling this.x += this.velocityX; this.y += this.velocityY; + + // Round to prevent sub-pixel jitter in rendering + this.x = Math.round(this.x * 100) / 100; + this.y = Math.round(this.y * 100) / 100; } updateGrowth(totalLevelWidth) { @@ -259,10 +279,12 @@ class Player { checkBoundaries(gameState) { const totalLevelWidth = gameState.viewport.width * gameState.LEVEL_WIDTH_MULTIPLIER; + // Allow player to move within the full level width, not just viewport if (this.x < 0) this.x = 0; if (this.x + this.size > totalLevelWidth) this.x = totalLevelWidth - this.size; - if (this.y > gameState.viewport.height + 100) { + // Vertical boundaries - player can fall below viewport + if (this.y > gameState.viewport.height + 200) { gameState.gameOver(); } } @@ -287,20 +309,67 @@ class Camera { this.y = 0; this.targetX = 0; this.targetY = 0; - this.smoothing = 0.1; + this.velocityX = 0; + this.velocityY = 0; + this.smoothing = 0.08; // Reduced for gentler movement + this.acceleration = 0.5; // Increased for better player following + this.deceleration = 0.85; // Increased damping for smoother stops } - update(player, viewport, levelWidthMultiplier) { - this.targetX = player.x - viewport.width / 2; + update(player, viewport, levelWidthMultiplier, deltaTime) { + // Convert deltaTime from milliseconds to seconds for proper physics + const deltaTimeSeconds = deltaTime / 1000; + + // Calculate target position with minimal prediction + const predictionOffset = player.velocityX * 0.03; // Reduced prediction to prevent overshooting + this.targetX = player.x - viewport.width / 2 + predictionOffset; this.targetY = 0; // Keep camera at top - this.x = this.lerp(this.x, this.targetX, this.smoothing); - this.y = this.lerp(this.y, this.targetY, this.smoothing); + // Boost camera speed if player is moving fast and camera is falling behind + const distanceToTarget = Math.abs(this.targetX - this.x); + if (distanceToTarget > 100 && Math.abs(player.velocityX) > 5) { + this.velocityX += (this.targetX - this.x) * 0.1 * deltaTimeSeconds; // Catch-up boost + } + + // Smooth camera movement with velocity-based interpolation + const dx = this.targetX - this.x; + const dy = this.targetY - this.y; + + // Apply acceleration/deceleration for smoother movement + const deadzone = 2; // Reduced deadzone for more responsive following + + if (Math.abs(dx) > deadzone) { + this.velocityX += dx * this.acceleration * deltaTimeSeconds; + } else { + this.velocityX *= this.deceleration; + } - // Clamp to level bounds + if (Math.abs(dy) > deadzone) { + this.velocityY += dy * this.acceleration * deltaTimeSeconds; + } else { + this.velocityY *= this.deceleration; + } + + // Apply velocity with damping and speed limits + const maxVelocity = 400; // Increased max velocity to keep up with player + this.velocityX = Math.max(-maxVelocity, Math.min(maxVelocity, this.velocityX)); + this.velocityY = Math.max(-maxVelocity, Math.min(maxVelocity, this.velocityY)); + + this.x += this.velocityX * deltaTimeSeconds; + this.y += this.velocityY * deltaTimeSeconds; + + // Clamp to level bounds - allow camera to move across full level width const totalLevelWidth = viewport.width * levelWidthMultiplier; this.x = Math.max(0, Math.min(this.x, totalLevelWidth - viewport.width)); this.y = Math.max(0, Math.min(this.y, 0)); + + // Stop movement at boundaries to prevent jitter + if (this.x <= 0 || this.x >= totalLevelWidth - viewport.width) { + this.velocityX = 0; + } + if (this.y >= 0) { + this.velocityY = 0; + } } lerp(start, end, factor) { @@ -309,8 +378,8 @@ class Camera { worldToScreen(worldX, worldY) { return { - x: worldX - this.x, - y: worldY - this.y + x: Math.round(worldX - this.x), // Round to prevent sub-pixel jitter + y: Math.round(worldY - this.y) }; } } @@ -1051,6 +1120,12 @@ class Renderer { renderParticles(particleSystem, gameState) { particleSystem.particles.forEach(particle => { + // Viewport culling for particles + const screenPos = gameState.camera.worldToScreen(particle.x, particle.y); + if (!this.isOnScreen(screenPos, { width: particle.size, height: particle.size }, gameState.viewport)) { + return; // Skip off-screen particles + } + const alpha = particle.life / particle.maxLife; // Different colors for different particle types @@ -1065,8 +1140,6 @@ class Renderer { const rgb = this.hexToRgb(color); this.ctx.fillStyle = `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${alpha})`; - // Transform particle from world coordinates to screen coordinates - const screenPos = gameState.camera.worldToScreen(particle.x, particle.y); this.ctx.fillRect(screenPos.x, screenPos.y, particle.size, particle.size); }); } @@ -1080,6 +1153,22 @@ class Renderer { } : { r: 139, g: 69, b: 19 }; } + isOnScreen(screenPos, object, viewport) { + const margin = 50; // Render objects slightly outside viewport + return screenPos.x + object.width > -margin && + screenPos.x < viewport.width + margin && + screenPos.y + object.height > -margin && + screenPos.y < viewport.height + margin; + } + + getPlatformColor(platform) { + if (platform.type === 'goal') return this.colors.GOAL; + if (platform.type === 'indestructible') return this.colors.PLATFORM.INDESTRUCTIBLE; + if (platform.type === 'wall_indestructible') return this.colors.WALL.INDESTRUCTIBLE; + if (platform.type === 'wall_destructible') return this.colors.WALL.DESTRUCTIBLE; + return this.colors.PLATFORM.DESTRUCTIBLE; + } + renderUI(gameState) { this.ctx.fillStyle = this.colors.TEXT; this.ctx.font = '20px Arial'; @@ -1090,13 +1179,59 @@ class Renderer { this.ctx.fillText(`Progress: ${Math.floor(gameState.player.levelProgress)}%`, 10, 120); this.ctx.fillText(`Objects: ${gameState.platforms.filter(p => !p.isDestroyed).length}`, 10, 150); + // Debug info for scrolling + const totalLevelWidth = gameState.viewport.width * gameState.LEVEL_WIDTH_MULTIPLIER; + this.ctx.fillText(`Player X: ${Math.floor(gameState.player.x)}`, 10, 210); + this.ctx.fillText(`Camera X: ${Math.floor(gameState.camera.x)}`, 10, 240); + this.ctx.fillText(`Level Width: ${Math.floor(totalLevelWidth)}`, 10, 270); + this.ctx.fillText(`Goal X: ${Math.floor(gameState.goal.x)}`, 10, 300); + // Show difficulty info if (gameState.level > 1) { this.ctx.fillStyle = '#FF6B35'; - this.ctx.fillText(`Difficulty: ${this.getDifficultyText(gameState.level)}`, 10, 180); + this.ctx.fillText(`Difficulty: ${this.getDifficultyText(gameState.level)}`, 10, 330); + } + } + + renderGameState(gameState) { + // Render game state overlay (game over, level complete, etc.) + if (gameState.state === 'game_over') { + this.renderGameOver(gameState); + } else if (gameState.state === 'level_complete') { + this.renderLevelComplete(gameState); } } + renderGameOver(gameState) { + // Semi-transparent overlay + this.ctx.fillStyle = this.colors.GAME_OVER.OVERLAY; + this.ctx.fillRect(0, 0, gameState.viewport.width, gameState.viewport.height); + + // Game over text + this.ctx.fillStyle = this.colors.GAME_OVER.TEXT; + this.ctx.font = '48px Arial'; + this.ctx.textAlign = 'center'; + this.ctx.fillText('GAME OVER', gameState.viewport.width / 2, gameState.viewport.height / 2 - 50); + + this.ctx.font = '24px Arial'; + this.ctx.fillText('Press R to restart', gameState.viewport.width / 2, gameState.viewport.height / 2 + 20); + } + + renderLevelComplete(gameState) { + // Semi-transparent overlay + this.ctx.fillStyle = this.colors.GAME_OVER.OVERLAY; + this.ctx.fillRect(0, 0, gameState.viewport.width, gameState.viewport.height); + + // Level complete text + this.ctx.fillStyle = this.colors.GAME_OVER.TEXT; + this.ctx.font = '36px Arial'; + this.ctx.textAlign = 'center'; + this.ctx.fillText(`Level ${gameState.level - 1} Complete!`, gameState.viewport.width / 2, gameState.viewport.height / 2 - 30); + + this.ctx.font = '20px Arial'; + this.ctx.fillText('Loading next level...', gameState.viewport.width / 2, gameState.viewport.height / 2 + 20); + } + getDifficultyText(level) { const phase = this.getDifficultyPhase(level); const phaseProgress = this.getPhaseProgress(level, phase); |