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:
@@ -329,8 +329,10 @@ export default class HuntingScene extends Phaser.Scene {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Swimming movement
|
// Swimming movement
|
||||||
const swimSpeedX = whale.getData('swimSpeedX');
|
let swimSpeedX = whale.getData('swimSpeedX');
|
||||||
const swimSpeedY = whale.getData('swimSpeedY');
|
let swimSpeedY = whale.getData('swimSpeedY');
|
||||||
|
|
||||||
|
// Update position with swimming
|
||||||
whale.x += swimSpeedX;
|
whale.x += swimSpeedX;
|
||||||
whale.y += swimSpeedY;
|
whale.y += swimSpeedY;
|
||||||
|
|
||||||
@@ -339,19 +341,36 @@ export default class HuntingScene extends Phaser.Scene {
|
|||||||
bobTime += 0.02;
|
bobTime += 0.02;
|
||||||
whale.setData('bobTime', bobTime);
|
whale.setData('bobTime', bobTime);
|
||||||
|
|
||||||
const bobOffsetX = Math.sin(bobTime) * 15; // Reduced from 30 since we're swimming
|
const bobOffsetX = Math.sin(bobTime) * 15;
|
||||||
const bobOffsetY = Math.cos(bobTime * 0.7) * 10; // Reduced from 20
|
const bobOffsetY = Math.cos(bobTime * 0.7) * 10;
|
||||||
|
|
||||||
whale.x += bobOffsetX;
|
whale.x += bobOffsetX;
|
||||||
whale.y += bobOffsetY;
|
whale.y += bobOffsetY;
|
||||||
|
|
||||||
// Bounce off screen edges
|
// Keep whale within visible bounds
|
||||||
if (whale.x < 50 || whale.x > 750) {
|
const margin = 60; // Account for whale size and bobbing
|
||||||
whale.setData('swimSpeedX', -swimSpeedX);
|
const minX = margin;
|
||||||
whale.setScale(whale.scaleX * -1, whale.scaleY); // Flip whale
|
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
|
// Update opacity based on diving
|
||||||
|
|||||||
Reference in New Issue
Block a user