modules/world: initial functions to compute camera size and positionning

This commit is contained in:
Kazhnuz 2019-04-29 13:58:13 +02:00
parent 3c498f2d16
commit bc0b1bb87b

View file

@ -48,6 +48,44 @@ function CameraSystem:initViews()
self.views.list = {}
self.views.basewidth, self.views.baseheight = core.screen:getDimensions()
self.views.width, self.views.height = self:getViewsDimensions()
self.views.posList = {}
self.views.posList.dual = {}
self.views.posList.multi = {}
if (self.verticalSplit) then
self.views.posList.dual[1] = {}
self.views.posList.dual[1].x = 0
self.views.posList.dual[1].y = -(self.views.baseheight/4)
self.views.posList.dual[2] = {}
self.views.posList.dual[2].x = 0
self.views.posList.dual[2].y = (self.views.baseheight/4)
else
self.views.posList.dual[1] = {}
self.views.posList.dual[1].x = (self.views.basewidth/4)
self.views.posList.dual[1].y = 0
self.views.posList.dual[2] = {}
self.views.posList.dual[2].x = -(self.views.basewidth/4)
self.views.posList.dual[2].y = 0
end
self.views.posList.multi[1] = {}
self.views.posList.multi[1].x = -(self.views.basewidth /4)
self.views.posList.multi[1].y = -(self.views.baseheight/4)
self.views.posList.multi[2] = {}
self.views.posList.multi[2].x = (self.views.basewidth /4)
self.views.posList.multi[2].y = -(self.views.baseheight/4)
self.views.posList.multi[3] = {}
self.views.posList.multi[3].x = -(self.views.basewidth /4)
self.views.posList.multi[3].y = (self.views.baseheight/4)
self.views.posList.multi[4] = {}
self.views.posList.multi[4].x = (self.views.basewidth /4)
self.views.posList.multi[4].y = (self.views.baseheight/4)
end
-- INFO FUNCTIONS
@ -61,36 +99,67 @@ function CameraSystem:haveView()
return (self:getViewNumber() == 0)
end
function CameraSystem:getViewsDimensions()
function CameraSystem:getViewsDimensions(viewNumber)
local basewidth, baseheight = self.views.basewidth, self.views.baseheight
local viewnumber = self:getViewNumber()
local viewnumber = viewNumber or self:getViewNumber()
if (viewnumber <= 1) then
return basewidth, baseheight
elseif (viewnumber == 2) then
if (self.verticalSplit) then
return (basewidth/2), (baseheight)
return (basewidth), (baseheight/2)
else
return (basewidth), (baseheight/2)
return (basewidth/2), (baseheight)
end
else
return (basewidth/2), (baseheight/2)
end
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
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
end
end
end
function CameraSystem:getViewPositions(id)
local viewNumber = #self.views.list
if (viewNumber == 2) and (id == 1) or (id == 2) then
return self.views.posList.dual[id].x, self.views.posList.dual[id].y
elseif (viewNumber > 2) and ((id >= 1) or (id <= 4)) then
return self.views.posList.multi[id].x, self.views.posList.multi[id].y
end
end
-- WRAPPER and UTILS
-- Access data from the views
function CameraSystem:addView(x, y)
if (#self.views.list < SCREEN_LIMIT) then
local id = #self.views.list + 1
local view = {}
view.cam = View(x, y, 1, 0, true)
view.pos = {}
view.pos.x = x or 0
view.pos.y = y or 0
view.pos.onScreen = {}
view.cam = View(view.pos.x, view.pos.y, 1, 0, true)
-- TODO: add a target system in order to make a camera able
-- to target a specific object
table.insert(self.views.list, view)
self.views.width, self.views.height = self:getViewsDimensions()
self:recalculateViewsPositions()
end
end
@ -132,10 +201,47 @@ function CameraSystem:getViewCoordinate(id)
return camx, camy, camh, camw
end
function CameraSystem:getOnScreenViewCoordinate(id)
local view = self:getView(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)
vieww = self.views.width
viewh = self.views.height
return viewx, viewy, vieww, viewh
end
function CameraSystem:getOnScreenViewCenter(id)
local view = self:getView(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
return viewx, viewy
end
function CameraSystem:getViewScale(id)
local cam = self:getViewCam(id)
return cam.scale
end
function CameraSystem:drawDebugViewBox()
for i=1, #self.views.list do
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(i)
utils.graphics.box(viewx, viewy, vieww, viewh)
local xx, yy = self:getOnScreenViewCenter(i)
love.graphics.line(xx-3, yy, xx+3, yy)
love.graphics.line(xx, yy-3, xx, yy+3)
print(viewx, viewy, vieww, viewh)
end
end
return CameraSystem