From f1edd118ecbcd839822643595bc36cfe3b22ac43 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 2 May 2019 16:19:28 +0200 Subject: [PATCH] modules/world: simplify limitView() by using utils.math.between() --- gamecore/modules/world/camera.lua | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/gamecore/modules/world/camera.lua b/gamecore/modules/world/camera.lua index 5e11f99..7df4622 100644 --- a/gamecore/modules/world/camera.lua +++ b/gamecore/modules/world/camera.lua @@ -236,25 +236,17 @@ function CameraSystem:getViewScale(id) end function CameraSystem:limitView(id) - local viewx, viewy, vieww, viewh = self:getViewCoordinate(id) + local viewx, viewy, vieww, viewh = self:getViewCoordinate(id) + local worldw, worldh = self.world:getDimensions() + local posx = self.views.list[id].pos.x + local posy = self.views.list[id].pos.y + local minx = self.views.width / 2 + local miny = self.views.height / 2 + local maxx = worldw - minx + local maxy = worldh - miny - 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.views.list[id].pos.x = utils.math.between(posx, minx, maxx) + self.views.list[id].pos.y = utils.math.between(posy, miny, maxy) self:computeCamPosition(id) end