From a2d38a1ef4f5e91025e371bcee9525c1a2e901a3 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Wed, 1 May 2019 10:27:07 +0200 Subject: [PATCH] module/world: make sure that the view doesn't go outside the world --- gamecore/modules/world/camera.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/gamecore/modules/world/camera.lua b/gamecore/modules/world/camera.lua index 4adb8cb..88eb2ae 100644 --- a/gamecore/modules/world/camera.lua +++ b/gamecore/modules/world/camera.lua @@ -235,6 +235,30 @@ function CameraSystem:getViewScale(id) return cam.scale end +function CameraSystem:limitView(id) + local viewx, viewy, vieww, viewh = self:getViewCoordinate(id) + + if (viewx < 0) then + self.views.list[id].pos.x = (self.views.width / 2) + end + + if (viewy < 0) then + self.views.list[id].pos.y = (self.views.height / 2) + end + + local worldw, worldh = self.world:getDimensions() + + if ((viewx + vieww) > worldw) then + self.views.list[id].pos.x = worldw - (self.views.width / 2) + end + + if ((viewy + viewh) > worldh) then + self.views.list[id].pos.y = worldh - (self.views.height / 2) + end + + self:computeCamPosition(id) +end + -- UPDATE and MOVE functions -- Move and update the camera system @@ -249,6 +273,7 @@ function CameraSystem:moveView(id, x, y) self.views.list[id].pos.y = y self:computeCamPosition(id) + self:limitView(id) end function CameraSystem:computeCamPosition(id)