module/world: make sure that the view doesn't go outside the world

This commit is contained in:
Kazhnuz 2019-05-01 10:27:07 +02:00
parent 1c167ba566
commit a2d38a1ef4

View file

@ -235,6 +235,30 @@ function CameraSystem:getViewScale(id)
return cam.scale return cam.scale
end 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 -- UPDATE and MOVE functions
-- Move and update the camera system -- Move and update the camera system
@ -249,6 +273,7 @@ function CameraSystem:moveView(id, x, y)
self.views.list[id].pos.y = y self.views.list[id].pos.y = y
self:computeCamPosition(id) self:computeCamPosition(id)
self:limitView(id)
end end
function CameraSystem:computeCamPosition(id) function CameraSystem:computeCamPosition(id)