From 985e7447db6c27ae6b7b40fe87b62d70e36cdd32 Mon Sep 17 00:00:00 2001 From: StrangeD0s Date: Sat, 13 Apr 2024 19:55:29 +0200 Subject: [PATCH] Update camera.js --- js/camera.js | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/js/camera.js b/js/camera.js index 4693365..2bb5338 100644 --- a/js/camera.js +++ b/js/camera.js @@ -10,20 +10,24 @@ function renderCamera() { const camera = { get x() { - if (player.x < scaledCanvas.width) return 0 + if (player.x < scaledCanvas.width) return 0 // This is when the camera hits the left map boundary if (player.x > cameraWidth - scaledCanvas.width) - return -(cameraWidth - scaledCanvas.width * 2) - else return -(player.x - scaledCanvas.width) + return ( + cameraWidth - scaledCanvas.width * 2 + ) // This centers the player horizontally + else return player.x - scaledCanvas.width // This is when the camera hits the right map boundary }, get y() { - if (player.y < scaledCanvas.height) return 0 + if (player.y < scaledCanvas.height) return 0 // This is when the camera hits top map boundary if (player.y > cameraHeight - scaledCanvas.height) - return -(cameraHeight - scaledCanvas.height * 2) - else return -(player.y - scaledCanvas.height) + return ( + cameraHeight - scaledCanvas.height * 2 + ) // This centers the player vertically + else return player.y - scaledCanvas.height // This is when the camera hits the bottom map boundary }, } - ctx.translate(camera.x, camera.y) + ctx.translate(-camera.x, -camera.y) } //-------------------------------------------------------------------------