Keep whale swimming within visible area

- 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 <noreply@anthropic.com>
This commit is contained in:
Thomas Richter
2025-12-15 12:20:47 +01:00
parent 033da743b3
commit 5946172728

View File

@@ -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