From 5946172728cc9478f7099406c362208ed56e2202 Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Mon, 15 Dec 2025 12:20:47 +0100 Subject: [PATCH] Keep whale swimming within visible area MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Added proper boundary margins (60px) for whale size and bobbing - Clamps whale position to bounds when hitting edges - Uses Math.abs() for reliable direction reversal - Properly flips whale sprite when bouncing - Bounds: X: 60-740, Y: 120-550 - Whale can no longer escape off screen 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/scenes/HuntingScene.js | 39 ++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/scenes/HuntingScene.js b/src/scenes/HuntingScene.js index 4a00297..2945ea9 100644 --- a/src/scenes/HuntingScene.js +++ b/src/scenes/HuntingScene.js @@ -329,8 +329,10 @@ export default class HuntingScene extends Phaser.Scene { } // Swimming movement - const swimSpeedX = whale.getData('swimSpeedX'); - const swimSpeedY = whale.getData('swimSpeedY'); + let swimSpeedX = whale.getData('swimSpeedX'); + let swimSpeedY = whale.getData('swimSpeedY'); + + // Update position with swimming whale.x += swimSpeedX; whale.y += swimSpeedY; @@ -339,19 +341,36 @@ export default class HuntingScene extends Phaser.Scene { bobTime += 0.02; whale.setData('bobTime', bobTime); - const bobOffsetX = Math.sin(bobTime) * 15; // Reduced from 30 since we're swimming - const bobOffsetY = Math.cos(bobTime * 0.7) * 10; // Reduced from 20 + const bobOffsetX = Math.sin(bobTime) * 15; + const bobOffsetY = Math.cos(bobTime * 0.7) * 10; whale.x += bobOffsetX; whale.y += bobOffsetY; - // Bounce off screen edges - if (whale.x < 50 || whale.x > 750) { - whale.setData('swimSpeedX', -swimSpeedX); - whale.setScale(whale.scaleX * -1, whale.scaleY); // Flip whale + // Keep whale within visible bounds + const margin = 60; // Account for whale size and bobbing + const minX = margin; + const maxX = 800 - margin; + const minY = 120; + const maxY = 550; + + // Bounce off screen edges and clamp position + if (whale.x < minX) { + whale.x = minX; + whale.setData('swimSpeedX', Math.abs(swimSpeedX)); // Swim right + whale.setScale(-Math.abs(whale.scaleX), whale.scaleY); // Face right + } else if (whale.x > maxX) { + whale.x = maxX; + whale.setData('swimSpeedX', -Math.abs(swimSpeedX)); // Swim left + whale.setScale(Math.abs(whale.scaleX), whale.scaleY); // Face left } - if (whale.y < 100 || whale.y > 550) { - whale.setData('swimSpeedY', -swimSpeedY); + + if (whale.y < minY) { + whale.y = minY; + whale.setData('swimSpeedY', Math.abs(swimSpeedY)); // Swim down + } else if (whale.y > maxY) { + whale.y = maxY; + whale.setData('swimSpeedY', -Math.abs(swimSpeedY)); // Swim up } // Update opacity based on diving