modules/world: add a way to link the camera to an actor

This commit is contained in:
Kazhnuz 2019-04-29 15:03:28 +02:00
parent cc5de7d5ff
commit 882f98bf40
3 changed files with 55 additions and 2 deletions

View file

@ -161,6 +161,14 @@ end
-- DRAW FUNCTIONS -- DRAW FUNCTIONS
-- Draw the actors. -- Draw the actors.
function Actor2D:getCenter()
return (self.x + (self.w / 2)), (self.y + (self.h / 2))
end
function Actor2D:getViewCenter()
return self:getCenter()
end
function Actor2D:draw() function Actor2D:draw()
-- here will be update actions -- here will be update actions
end end

View file

@ -118,12 +118,17 @@ function BaseWorld:initPlayers()
self.players = {} self.players = {}
end end
function BaseWorld:addPlayer(actor, sourceid) function BaseWorld:addPlayer(actor, sourceid, haveCam)
local player = {} local player = {}
player.actor = actor player.actor = actor
player.sourceid = sourceid or 1 player.sourceid = sourceid or 1
table.insert(self.players, player) table.insert(self.players, player)
if (haveCam) then
local xx, yy = player.actor:getViewCenter()
self.cameras:addView(xx, yy, player.actor)
end
end end
function BaseWorld:sendInputToPlayers(actor) function BaseWorld:sendInputToPlayers(actor)
@ -174,6 +179,7 @@ function BaseWorld:update(dt)
self:updateMap(dt) self:updateMap(dt)
self:sendInputToPlayers(dt) self:sendInputToPlayers(dt)
self:updateActors(dt) self:updateActors(dt)
self.cameras:update(dt)
end end
function BaseWorld:updateActors(dt) function BaseWorld:updateActors(dt)

View file

@ -143,7 +143,7 @@ end
-- WRAPPER and UTILS -- WRAPPER and UTILS
-- Access data from the views -- Access data from the views
function CameraSystem:addView(x, y) function CameraSystem:addView(x, y, target)
if (#self.views.list < SCREEN_LIMIT) then if (#self.views.list < SCREEN_LIMIT) then
local id = #self.views.list + 1 local id = #self.views.list + 1
local view = {} local view = {}
@ -156,6 +156,7 @@ function CameraSystem:addView(x, y)
view.cam = View(view.pos.x, view.pos.y, 1, 0, true) view.cam = View(view.pos.x, view.pos.y, 1, 0, true)
-- TODO: add a target system in order to make a camera able -- TODO: add a target system in order to make a camera able
-- to target a specific object -- to target a specific object
view.target = 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()
@ -234,6 +235,44 @@ function CameraSystem:getViewScale(id)
return cam.scale return cam.scale
end end
-- UPDATE and MOVE functions
-- Move and update the camera system
function CameraSystem:update(dt)
for i,v in ipairs(self.views.list) do
self:followActor(i)
end
end
function CameraSystem:moveView(id, x, y)
self.views.list[id].pos.x = x
self.views.list[id].pos.y = y
self:computeCamPosition(id)
end
function CameraSystem:computeCamPosition(id)
local decalx = self.views.list[id].pos.onScreen.x
local decaly = self.views.list[id].pos.onScreen.y
local realx = self.views.list[id].pos.x
local realy = self.views.list[id].pos.y
self.views.list[id].cam.x = realx - decalx
self.views.list[id].cam.y = realy - decaly
end
function CameraSystem:followActor(id)
local view = self:getView(id)
if view.target ~= nil then
local x, y = view.target:getViewCenter()
x = math.floor(x)
y = math.floor(y)
self:moveView(id, x, y)
end
end
function CameraSystem:drawDebugViewBox() function CameraSystem:drawDebugViewBox()
for i=1, #self.views.list do for i=1, #self.views.list do
local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(i) local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(i)