From 882f98bf40910fe2adaeb772c1f78f8791d456d8 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 29 Apr 2019 15:03:28 +0200 Subject: [PATCH] modules/world: add a way to link the camera to an actor --- gamecore/modules/world/actors/actor2D.lua | 8 +++++ gamecore/modules/world/baseworld.lua | 8 ++++- gamecore/modules/world/camera.lua | 41 ++++++++++++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/gamecore/modules/world/actors/actor2D.lua b/gamecore/modules/world/actors/actor2D.lua index 2faf328..d7c722d 100644 --- a/gamecore/modules/world/actors/actor2D.lua +++ b/gamecore/modules/world/actors/actor2D.lua @@ -161,6 +161,14 @@ end -- DRAW FUNCTIONS -- 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() -- here will be update actions end diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index 5a0aee8..df0dfcc 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -118,12 +118,17 @@ function BaseWorld:initPlayers() self.players = {} end -function BaseWorld:addPlayer(actor, sourceid) +function BaseWorld:addPlayer(actor, sourceid, haveCam) local player = {} player.actor = actor player.sourceid = sourceid or 1 table.insert(self.players, player) + + if (haveCam) then + local xx, yy = player.actor:getViewCenter() + self.cameras:addView(xx, yy, player.actor) + end end function BaseWorld:sendInputToPlayers(actor) @@ -174,6 +179,7 @@ function BaseWorld:update(dt) self:updateMap(dt) self:sendInputToPlayers(dt) self:updateActors(dt) + self.cameras:update(dt) end function BaseWorld:updateActors(dt) diff --git a/gamecore/modules/world/camera.lua b/gamecore/modules/world/camera.lua index 263e4db..359a56a 100644 --- a/gamecore/modules/world/camera.lua +++ b/gamecore/modules/world/camera.lua @@ -143,7 +143,7 @@ end -- WRAPPER and UTILS -- Access data from the views -function CameraSystem:addView(x, y) +function CameraSystem:addView(x, y, target) if (#self.views.list < SCREEN_LIMIT) then local id = #self.views.list + 1 local view = {} @@ -156,6 +156,7 @@ function CameraSystem:addView(x, y) 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 + view.target = target table.insert(self.views.list, view) self.views.width, self.views.height = self:getViewsDimensions() @@ -234,6 +235,44 @@ function CameraSystem:getViewScale(id) return cam.scale 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() for i=1, #self.views.list do local viewx, viewy, vieww, viewh = self:getOnScreenViewCoordinate(i)