scenes/levels: split camera system

This commit is contained in:
Kazhnuz 2019-03-03 15:14:08 +01:00
parent ff02216890
commit 3ce78cacc5
4 changed files with 70 additions and 56 deletions

View File

@ -1,40 +1,44 @@
function Level:initCamera(x, y)
self.camera = Camera(x, y, 1, 0, true)
local CameraSystem = Object:extend()
function CameraSystem:new(scene, x, y)
self.scene = scene
self.world = scene
self.view = Camera(x, y, 1, 0, true)
--local width, height = love.graphics.getDimensions()
local width, height, flags = love.window.getMode( )
self.screenWidth = width
self.screenHeight = height
self.cameraWidth = 424
self.cameraHeight = 240
self.viewWidth = 424
self.viewHeight = 240
self.resolution = self.screenWidth / self.cameraWidth
self:limitCamera()
self.resolution = self.screenWidth / self.viewWidth
self:limit()
end
function Level:floorCameraCoord()
self.camera.x, self.camera.y = utils.math.floorCoord(self.camera.x, self.camera.y)
function CameraSystem:floorCoord()
self.view.x, self.view.y = utils.math.floorCoord(self.view.x, self.view.y)
end
function Level:updateCamera(dt)
self:cameraFollowPlayer(self.playermanager.activePlayer)
self:limitCamera()
self:floorCameraCoord()
function CameraSystem:update(dt)
self:followPlayer(self.scene.playermanager.activePlayer)
self:limit()
self:floorCoord()
end
function Level:cameraFollowPlayer(id)
local player = self.playermanager:getPlayerByID(id)
function CameraSystem:followPlayer(id)
local player = self.scene.playermanager:getPlayerByID(id)
if (player ~= nil) then
local playx, playy = utils.math.floorCoord(player.center.x,
player.center.y)
local camx, camy = self.camera.x + (self.cameraWidth/2),
self.camera.y + (self.cameraHeight/2)
local camx, camy = self.view.x + (self.viewWidth/2),
self.view.y + (self.viewHeight/2)
playx, playy = self:cameraCoords(playx, playy)
playx = playx - (self.cameraWidth/2)
playy = playy - (self.cameraHeight/2)
playx = playx - (self.viewWidth/2)
playy = playy - (self.viewHeight/2)
if (math.abs(playx) > 8) then
camx = camx + (playx - (8*utils.math.sign(playx)))
@ -46,27 +50,27 @@ function Level:cameraFollowPlayer(id)
camy = camy + (playy + 64)
end
self.camera.x, self.camera.y = camx - (self.cameraWidth/2),
camy - (self.cameraHeight/2)
self.view.x, self.view.y = camx - (self.viewWidth/2),
camy - (self.viewHeight/2)
end
end
function Level:debugDrawCameraPlayerZone()
function CameraSystem:debugDrawCameraPlayerZone()
--
end
function Level:moveCamera(x, y)
self.camera.x = x
self.camera.y = y
function CameraSystem:move(x, y)
self.view.x = x
self.view.y = y
self:limitCamera()
self:limit()
end
function Level:limitCamera()
local camx, camy = self.camera.x, self.camera.y
function CameraSystem:limit()
local camx, camy = self.view.x, self.view.y
local camMinX, camMinY = (self.screenWidth/2), (self.screenHeight/2)
local camMaxX, camMaxY = self:getDimensions()
local camMaxX, camMaxY = self.world:getDimensions()
if (self.resolution == 1) then
camMaxX, camMaxY = camMaxX - camMinX, camMaxY - camMinY
@ -79,27 +83,27 @@ function Level:limitCamera()
camy = math.max(camy, camMinY)
camy = math.min(camy, camMaxY)
self.camera.x, self.camera.y = camx, camy
self.view.x, self.view.y = camx, camy
end
function Level:getCameraCoord()
function CameraSystem:getCoord()
local camx, camy, camh, camw
camx = self.camera.x - (self.cameraWidth/2)
camy = self.camera.y - (self.cameraHeight/2)
camx = self.view.x - (self.viewWidth/2)
camy = self.view.y - (self.viewHeight/2)
camw = self.cameraWidth
camh = self.cameraHeight
camw = self.viewWidth
camh = self.viewHeight
return camx, camy, camw, camh
end
function Level:getCameraScale()
return self.camera.scale
function CameraSystem:getScale()
return self.view.scale
end
function Level:getScreenCoord()
function CameraSystem:getScreenCoord()
local camx, camy, camh, camw
camx = self.camera.x - (self.screenWidth/2)
camy = self.camera.y - (self.screenHeight/2)
camx = self.view.x - (self.screenWidth/2)
camy = self.view.y - (self.screenHeight/2)
camw = self.screenWidth
camh = self.screenHeight
@ -107,24 +111,24 @@ function Level:getScreenCoord()
end
function Level:cameraWorldCoord(x, y, ox, oy, w, h)
function CameraSystem:worldCoord(x, y, ox, oy, w, h)
ox, oy = ox or 0, oy or 0
w,h = w or love.graphics.getWidth(), h or love.graphics.getHeight()
return self.camera:worldCoords(x, y, ox, oy, w, h)
return self.view:worldCoords(x, y, ox, oy, w, h)
end
function Level:cameraCoords(x, y)
return self.camera:cameraCoords(x, y, ox, oy, w, h)
function CameraSystem:cameraCoords(x, y)
return self.view:cameraCoords(x, y, ox, oy, w, h)
end
function Level:getVisibleEntities()
function CameraSystem:getVisibleEntities()
local camx, camy, camw, camh = self:getScreenCoord()
local visibleThings, len = self:queryRect(camx-64, camy-64, camw+128, camh+128)
local visibleThings, len = self.world:queryRect(camx-64, camy-64, camw+128, camh+128)
return visibleThings, len
end
function Level:isInsideView(x, y, w, h, border)
local camx, camy, camw, camh = self:getCameraCoord()
function CameraSystem:isInsideView(x, y, w, h, border)
local camx, camy, camw, camh = self:getCoord()
local border = border or 0
if (x + w + border >= camx) and (x < camx + camw + border)
and (y + h + border >= camy) and (y < camy + camh + border) then
@ -133,3 +137,13 @@ function Level:isInsideView(x, y, w, h, border)
return false
end
end
function CameraSystem:attach()
self.view:attach()
end
function CameraSystem:detach()
self.view:detach()
end
return CameraSystem

View File

@ -1,7 +1,7 @@
Level = Object:extend() -- On créer la classe des entitées, c'est la classe de base
require "scenes.levels.controller.world"
require "scenes.levels.controller.camera"
local Camera = require "scenes.levels.controller.camera"
require "scenes.levels.controller.debug"
PlayerManager = require "scenes.levels.controller.players"
@ -62,7 +62,7 @@ function Level:launchMission()
self.playermanager:addPlayer(1)
self.playermanager:spawnPlayer(1)
self:initCamera(self.playermanager.startx, self.playermanager.starty)
self.camera = Camera(self, self.playermanager.startx, self.playermanager.starty)
self.score = 0
self.gold = 0
@ -76,7 +76,7 @@ function Level:update(dt)
self.playermanager:update(dt)
self:updateWorld(dt)
assets:update(dt)
self:updateCamera(dt)
self.camera:update(dt)
end
end
@ -88,7 +88,7 @@ function Level:draw(dt)
-- Ona attache puis détache la caméra pour dessiner le monde, afin que celui
-- reste "fixe" tandis que le jouer bouge.
self:floorCameraCoord()
self.camera:floorCoord()
self:drawWorld()
if (self.pause == false) then

View File

@ -136,7 +136,7 @@ end
function Level:updateEntities(dt)
--l,t,w,h = l or 0, t or 0, w or self.width, h or self.height
local visibleThings, len = self:getVisibleEntities()
local visibleThings, len = self.camera:getVisibleEntities()
--table.sort(visibleThings, sortByUpdateOrder)
for i=1, len do
@ -163,7 +163,7 @@ end
function Level:drawEntities()
--l,t,w,h = l or 0, t or 0, w or self.width, h or self.height
local visibleThings, len = self:getVisibleEntities()
local visibleThings, len = self.camera:getVisibleEntities()
--table.sort(visibleThings, sortByUpdateOrder)
for i=1, len do
@ -172,8 +172,8 @@ function Level:drawEntities()
end
function Level:drawMap()
local tx, ty = self:getCameraCoord()
local scale = self:getCameraScale()
local tx, ty = self.camera:getCoord()
local scale = self.camera:getScale()
local tx = tx
local ty = ty

View File

@ -10,7 +10,7 @@ end
function Entity:initPhysics(level, collType, x, y, w, h)
self.level = level
self.world = level
self.camera = level
self.camera = level.camera
self.obj = self.world.obj
self.collType = collType