modules/world: add camera system to the world system

This commit is contained in:
Kazhnuz 2019-04-29 11:21:41 +02:00
parent ffac18d6a5
commit a046eb5a9c
2 changed files with 34 additions and 6 deletions

View file

@ -27,6 +27,7 @@ local cwd = (...):gsub('%.baseworld$', '') .. "."
local BaseWorld = Object:extend() local BaseWorld = Object:extend()
local Sti = require(cwd .. "libs.sti") local Sti = require(cwd .. "libs.sti")
local CameraSystem = require(cwd .. "camera")
-- INIT FUNCTIONS -- INIT FUNCTIONS
-- All functions to init the world and the map -- 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) function BaseWorld:new(scene, actorlist, mapfile)
self.scene = scene self.scene = scene
self.cameras = CameraSystem(self)
self:initPlayers() self:initPlayers()
self:setActorList(actorlist) self:setActorList(actorlist)
self:setMap(mapfile) self:setMap(mapfile)
@ -192,20 +195,42 @@ end
function BaseWorld:draw(dt) function BaseWorld:draw(dt)
self:drawBackgroundColor() self:drawBackgroundColor()
local camNumber = self.cameras:getViewNumber()
if (camNumber == 0) then
self:drawMap() self:drawMap()
self:drawActors() self:drawActors()
else
for i=1, camNumber do
self:drawMap(i)
self:drawActors(i)
end
end
end end
function BaseWorld:drawActors() function BaseWorld:drawActors(id)
self.cameras:attachView(id)
local actors = self:getActors() local actors = self:getActors()
for i,v in ipairs(actors) do for i,v in ipairs(actors) do
v:draw() v:draw()
end end
self.cameras:detachView(id)
end
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 end
function BaseWorld:drawMap()
if self.haveMap then if self.haveMap then
self.map:draw() self.map:draw(-tx, -ty, scale, scale)
end end
end end

View file

@ -28,10 +28,13 @@ local World2D = BaseWorld:extend()
local Sti = require(cwd .. "libs.sti") local Sti = require(cwd .. "libs.sti")
local Bump = require(cwd .. "libs.bump") local Bump = require(cwd .. "libs.bump")
local CameraSystem = require(cwd .. "camera")
function World2D:new(scene, actorlist, mapfile) function World2D:new(scene, actorlist, mapfile)
self.scene = scene self.scene = scene
self.cameras = CameraSystem(self)
self:initPlayers() self:initPlayers()
self:setActorList(actorlist) self:setActorList(actorlist)
self:setMap(mapfile) self:setMap(mapfile)