From a046eb5a9c6b6e9d385fb3a210ae80f3a5ccd717 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Mon, 29 Apr 2019 11:21:41 +0200 Subject: [PATCH] modules/world: add camera system to the world system --- gamecore/modules/world/baseworld.lua | 37 +++++++++++++++++++++++----- gamecore/modules/world/world2D.lua | 3 +++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index b6ad080..1eb95b9 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -26,7 +26,8 @@ local cwd = (...):gsub('%.baseworld$', '') .. "." local BaseWorld = Object:extend() -local Sti = require(cwd .. "libs.sti") +local Sti = require(cwd .. "libs.sti") +local CameraSystem = require(cwd .. "camera") -- INIT FUNCTIONS -- All functions to init the world and the map @@ -34,6 +35,8 @@ local Sti = require(cwd .. "libs.sti") function BaseWorld:new(scene, actorlist, mapfile) self.scene = scene + self.cameras = CameraSystem(self) + self:initPlayers() self:setActorList(actorlist) self:setMap(mapfile) @@ -192,20 +195,42 @@ end function BaseWorld:draw(dt) self:drawBackgroundColor() - self:drawMap() - self:drawActors() + local camNumber = self.cameras:getViewNumber() + + if (camNumber == 0) then + self:drawMap() + self:drawActors() + else + for i=1, camNumber do + self:drawMap(i) + self:drawActors(i) + end + end end -function BaseWorld:drawActors() +function BaseWorld:drawActors(id) + self.cameras:attachView(id) local actors = self:getActors() for i,v in ipairs(actors) do v:draw() end + self.cameras:detachView(id) end -function BaseWorld:drawMap() +function BaseWorld:drawMap(id) + local tx, ty, scale = 0, 0, 1 + if id ~= nil then + -- 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 + tx, ty = self.cameras:getViewCoordinate(id) + scale = self.cameras:getViewScale(id) or 1 + + tx = math.floor(tx) + ty = math.floor(ty) + end + if self.haveMap then - self.map:draw() + self.map:draw(-tx, -ty, scale, scale) end end diff --git a/gamecore/modules/world/world2D.lua b/gamecore/modules/world/world2D.lua index 42850ea..3526280 100644 --- a/gamecore/modules/world/world2D.lua +++ b/gamecore/modules/world/world2D.lua @@ -28,10 +28,13 @@ local World2D = BaseWorld:extend() local Sti = require(cwd .. "libs.sti") local Bump = require(cwd .. "libs.bump") +local CameraSystem = require(cwd .. "camera") function World2D:new(scene, actorlist, mapfile) self.scene = scene + self.cameras = CameraSystem(self) + self:initPlayers() self:setActorList(actorlist) self:setMap(mapfile)