improvement(camera): use a new canvas-based view system

This commit is contained in:
Kazhnuz 2019-07-14 22:49:27 +02:00
parent 35433b273d
commit 81dd584e11
2 changed files with 36 additions and 26 deletions

View file

@ -431,13 +431,10 @@ function BaseWorld:draw(dt)
self:drawActors() self:drawActors()
else else
for i=1, camNumber do for i=1, camNumber do
self.cameras:setScissor(i)
self.cameras:attachView(i) self.cameras:attachView(i)
self:drawMap(i) self:drawMap(i)
self:drawActors(i) self:drawActors(i)
self.cameras:detachView(i) self.cameras:detachView(i)
self.cameras:drawHUD(i)
self.cameras:resetScissor( )
end end
end end
end end

View file

@ -149,10 +149,17 @@ function CameraSystem:addView(x, y, target)
table.insert(self.views.list, view) table.insert(self.views.list, view)
self.views.width, self.views.height = self:getViewsDimensions() self.views.width, self.views.height = self:getViewsDimensions()
self:regenerateCanvases()
self:recalculateViewsPositions() self:recalculateViewsPositions()
end end
end end
function CameraSystem:regenerateCanvases()
for i, view in ipairs(self.views.list) do
view.canvas = love.graphics.newCanvas(self.views.width, self.views.height)
end
end
function CameraSystem:getView(id) function CameraSystem:getView(id)
return self.views.list[id] return self.views.list[id]
end end
@ -186,34 +193,45 @@ end
function CameraSystem:attachView(id) function CameraSystem:attachView(id)
if (id ~= nil) then if (id ~= nil) then
local cam = self:getViewCam(id) local view = self:getView(id)
self.current_canvas = love.graphics.getCanvas()
love.graphics.setCanvas(view.canvas)
love.graphics.clear()
if id ~= nil then if id ~= nil then
-- Du à la manière dont fonctionne STI, on est obligé de récupérer les info -- Du à la manière dont fonctionne STI, on est obligé de récupérer les info
-- de position de camera pour afficher la carte par rapport à ces infos -- de position de camera pour afficher la carte par rapport à ces infos
tx, ty = self:getInternalCamCoordinate(id) tx, ty = self:getViewCoordinate(id)
tx, ty = core.screen:getScreenCoordinate(tx, ty) scale = self:getViewScale(id) or 1
scale = self:getViewScale(id) or 2 tx = math.floor(tx) * -1 * scale
local vx, vy = self:getOnScreenViewRelativePosition(id) ty = math.floor(ty) * -1 * scale
tx = math.floor(tx - math.abs(vx)) * -1
ty = math.floor(ty - math.abs(vy)) * -1
local w, h = core.screen:getDimensions() local w, h = core.screen:getDimensions()
end end
print(tx, ty, scale)
love.graphics.push() love.graphics.push()
love.graphics.origin() love.graphics.origin()
love.graphics.translate(math.floor(tx), math.floor(ty)) love.graphics.translate(math.floor(tx), math.floor(ty))
love.graphics.scale(scale, scale) love.graphics.scale(view.cam.scale, view.cam.scale)
end end
end end
function CameraSystem:detachView(id) function CameraSystem:detachView(id)
if (id ~= nil) then if (id ~= nil) then
local cam = self:getViewCam(id) local view = self:getView(id)
love.graphics.pop()
love.graphics.setCanvas(self.current_canvas)
local tx, ty = self:getOnScreenViewCoordinate(id)
local scale = core.screen:getScale()
love.graphics.push()
love.graphics.origin()
love.graphics.translate(math.floor(tx), math.floor(ty))
love.graphics.scale(scale, scale)
love.graphics.draw(view.canvas)
self:drawHUD(id)
love.graphics.pop() love.graphics.pop()
end end
end end
@ -223,11 +241,13 @@ function CameraSystem:getViewCoordinate(id)
local cam = self:getViewCam(id) local cam = self:getViewCam(id)
local viewx, viewy, vieww, viewh local viewx, viewy, vieww, viewh
viewx = view.pos.x - ((self.views.width/2) / cam.scale)
viewy = view.pos.y - ((self.views.height/2) / cam.scale)
vieww = self.views.width / cam.scale vieww = self.views.width / cam.scale
viewh = self.views.height / cam.scale viewh = self.views.height / cam.scale
viewx = view.pos.x - (vieww / 2)
viewy = view.pos.y - (viewh / 2)
return viewx, viewy, vieww, viewh return viewx, viewy, vieww, viewh
end end
@ -285,7 +305,7 @@ end
function CameraSystem:getViewScale(id) function CameraSystem:getViewScale(id)
local cam = self:getViewCam(id) local cam = self:getViewCam(id)
return cam.scale * core.screen:getScale() return cam.scale
end end
function CameraSystem:limitView(id) function CameraSystem:limitView(id)
@ -300,8 +320,6 @@ function CameraSystem:limitView(id)
self.views.list[id].pos.x = utils.math.between(posx, minx, maxx) 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].pos.y = utils.math.between(posy, miny, maxy)
self:computeCamPosition(id)
end end
-- UPDATE and MOVE functions -- UPDATE and MOVE functions
@ -321,7 +339,6 @@ function CameraSystem:moveView(id, x, y)
self.views.list[id].pos.x = x self.views.list[id].pos.x = x
self.views.list[id].pos.y = y self.views.list[id].pos.y = y
self:computeCamPosition(id)
self:limitView(id) self:limitView(id)
end end
@ -412,14 +429,10 @@ function CameraSystem:drawDebugViewBox(id)
end end
function CameraSystem:drawHUD(id) function CameraSystem:drawHUD(id)
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
local view = self:getView(id) local view = self:getView(id)
local string2 = id .. " (" .. viewx .. ":" .. (viewh-viewy) .. ") " local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(id)
love.graphics.translate(viewx, viewy)
view.target:drawHUD(id, vieww, viewh) view.target:drawHUD(id, vieww, viewh)
love.graphics.translate(-viewx, -(viewy))
end end
return CameraSystem return CameraSystem