chore(camera): simplify view system

Remove view.cam and view.pos
This commit is contained in:
Kazhnuz 2019-07-14 22:59:58 +02:00
parent 7ec715850c
commit 7376d75aeb

View file

@ -92,13 +92,13 @@ end
function CameraSystem:recalculateViewsPositions()
if #self.views.list == 1 then
self.views.list[1].pos.onScreen.x = 0
self.views.list[1].pos.onScreen.y = 0
self.views.list[1].onScreen.x = 0
self.views.list[1].onScreen.y = 0
else
for i,v in ipairs(self.views.list) do
local x, y = self:getViewPositions(i)
self.views.list[i].pos.onScreen.x = x
self.views.list[i].pos.onScreen.y = y
self.views.list[i].onScreen.x = x
self.views.list[i].onScreen.y = y
end
end
end
@ -135,14 +135,12 @@ function CameraSystem:addView(x, y, target)
local view = {}
view.pos = {}
view.pos.x = x or 0
view.pos.y = y or 0
view.pos.onScreen = {}
view.x = x or 0
view.y = y or 0
view.scale = 1
view.onScreen = {}
view.cam = {}
view.cam.scale = 1
view.cam.x = x
view.cam.y = y
-- TODO: add a target system in order to make a camera able
-- to target a specific object
view.target = target
@ -164,12 +162,6 @@ function CameraSystem:getView(id)
return self.views.list[id]
end
function CameraSystem:getViewCam(id)
local view = self:getView(id)
return view.cam
end
function CameraSystem:attachView(id)
if (id ~= nil) then
local view = self:getView(id)
@ -192,7 +184,7 @@ function CameraSystem:attachView(id)
love.graphics.push()
love.graphics.origin()
love.graphics.translate(math.floor(tx), math.floor(ty))
love.graphics.scale(view.cam.scale, view.cam.scale)
love.graphics.scale(view.scale, view.scale)
end
end
@ -217,15 +209,14 @@ end
function CameraSystem:getViewCoordinate(id)
local view = self:getView(id)
local cam = self:getViewCam(id)
local viewx, viewy, vieww, viewh
vieww = self.views.width / cam.scale
viewh = self.views.height / cam.scale
vieww = self.views.width / view.scale
viewh = self.views.height / view.scale
viewx = view.pos.x - (vieww / 2)
viewy = view.pos.y - (viewh / 2)
viewx = view.x - (vieww / 2)
viewy = view.y - (viewh / 2)
return viewx, viewy, vieww, viewh
end
@ -235,8 +226,8 @@ function CameraSystem:getOnScreenViewCoordinate(id)
local viewx, viewy, vieww, viewh
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
viewx = (basex) + view.pos.onScreen.x - (self.views.width / 2)
viewy = (basey) + view.pos.onScreen.y - (self.views.height / 2)
viewx = (basex) + view.onScreen.x - (self.views.width / 2)
viewy = (basey) + view.onScreen.y - (self.views.height / 2)
viewx, viewy = core.screen:getScreenCoordinate(viewx, viewy)
@ -250,8 +241,8 @@ function CameraSystem:getOnScreenViewRelativePosition(id)
local viewx, viewy
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
viewx = view.pos.onScreen.x
viewy = view.pos.onScreen.y
viewx = view.onScreen.x
viewy = view.onScreen.y
return core.screen:getScreenCoordinate(viewx, viewy)
end
@ -261,30 +252,30 @@ function CameraSystem:getOnScreenViewCenter(id)
local viewx, viewy
local basex, basey = (self.views.basewidth / 2), (self.views.baseheight / 2)
viewx = (basex) + view.pos.onScreen.x
viewy = (basey) + view.pos.onScreen.y
viewx = (basex) + view.onScreen.x
viewy = (basey) + view.onScreen.y
return core.screen:getScreenCoordinate(viewx, viewy)
end
function CameraSystem:getViewScale(id)
local cam = self:getViewCam(id)
local view = self:getView(id)
return cam.scale
return view.scale
end
function CameraSystem:limitView(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 posx = self.views.list[id].x
local posy = self.views.list[id].y
local minx = self.views.width / 2
local miny = self.views.height / 2
local maxx = worldw - minx
local maxy = worldh - miny
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.views.list[id].x = utils.math.between(posx, minx, maxx)
self.views.list[id].y = utils.math.between(posy, miny, maxy)
end
-- UPDATE and MOVE functions
@ -301,8 +292,8 @@ function CameraSystem:update(dt)
end
function CameraSystem:moveView(id, x, y)
self.views.list[id].pos.x = x
self.views.list[id].pos.y = y
self.views.list[id].x = x
self.views.list[id].y = y
self:limitView(id)
end
@ -357,7 +348,7 @@ function CameraSystem:followAllActors(id)
local scalex = (maxX-minX)/ww
local scaley = (maxY-minY)/hh
scale = math.max(scale, scalex, scaley)
view.cam.scale = 1/scale
view.scale = 1/scale
self.world:resizeMap(ww * 3, hh * 3)
end