diff options
Diffstat (limited to 'html/rogue/js/camera.js')
-rw-r--r-- | html/rogue/js/camera.js | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/html/rogue/js/camera.js b/html/rogue/js/camera.js index a5aae47..c389b6e 100644 --- a/html/rogue/js/camera.js +++ b/html/rogue/js/camera.js @@ -10,10 +10,25 @@ const Camera = { smoothFollow(target) { const targetPixel = HexGrid.toPixel(target); - const targetX = targetPixel.x - state.canvas.width / 2; - const targetY = targetPixel.y - state.canvas.height / 2; + const screenX = targetPixel.x - this.x; + const screenY = targetPixel.y - this.y; - this.x += (targetX - this.x) * Config.camera.FOLLOW_SPEED; - this.y += (targetY - this.y) * Config.camera.FOLLOW_SPEED; + const centerX = state.canvas.width / 2; + const centerY = state.canvas.height / 2; + + // Calculate distance from center of screen + const deltaX = screenX - centerX; + const deltaY = screenY - centerY; + + // Only move camera if player is outside deadzone + if (Math.abs(deltaX) > Config.camera.DEADZONE_X) { + const adjustX = deltaX - (deltaX > 0 ? Config.camera.DEADZONE_X : -Config.camera.DEADZONE_X); + this.x += adjustX * Config.camera.FOLLOW_SPEED; + } + + if (Math.abs(deltaY) > Config.camera.DEADZONE_Y) { + const adjustY = deltaY - (deltaY > 0 ? Config.camera.DEADZONE_Y : -Config.camera.DEADZONE_Y); + this.y += adjustY * Config.camera.FOLLOW_SPEED; + } } }; \ No newline at end of file |