modules/world: add a way to load objects from tiled

This commit is contained in:
Kazhnuz 2019-04-29 16:40:44 +02:00
parent 66041ff511
commit fd78d083af
2 changed files with 65 additions and 4 deletions

View file

@ -36,11 +36,11 @@ function BaseWorld:new(scene, actorlist, mapfile)
self.scene = scene
self.cameras = CameraSystem(self)
self.actors = {}
self:initPlayers()
self:setActorList(actorlist)
self:setMap(mapfile)
self.actors = {}
end
function BaseWorld:setActorList(actorlist)
@ -61,6 +61,7 @@ function BaseWorld:setMap(mapfile)
self.map = Sti(mapfile)
self.haveBackgroundColor = true
self.backcolor = self.map.backgroundcolor or {128, 128, 128}
self:loadMapObjects()
end
end
@ -71,6 +72,10 @@ function BaseWorld:newActor(name, x, y)
self.obj.index[name](self, x, y)
end
function BaseWorld:newCollision(name, x, y, w, h)
self.obj.collisions[name](self, x, y, w, h)
end
function BaseWorld:registerActor(actor)
table.insert(self.actors, actor)
end
@ -111,6 +116,17 @@ function BaseWorld:getActors()
return self.actors
end
-- INFO FUNCTIONS
-- Give infos about the world
function BaseWorld:isActorIndexed(name)
return (self.obj.index[name] ~= nil)
end
function BaseWorld:isCollisionIndexed(name)
return (self.obj.collisions[name] ~= nil)
end
-- PLAYER MANAGEMENT FUNCTIONS
-- Basic function to handle player actors
@ -150,6 +166,51 @@ end
-- MAP FUNCTIONS
-- All map wrappers
function BaseWorld:loadMapObjects()
self:loadMapCollisions()
self:loadMapPlayers()
self:loadMapActors()
end
function BaseWorld:loadMapCollisions()
for k, objectlayer in pairs(self.map.layers) do
if self:isCollisionIndexed(objectlayer.name) then
print("DEBUG: loading actors in " .. objectlayer.name .. " collision layer")
for k, object in pairs(objectlayer.objects) do
self:newCollision(objectlayer.name, object.x, object.y, object.width, object.height)
end
self.map:removeLayer(objectlayer.name)
end
end
end
function BaseWorld:loadMapActors()
for k, objectlayer in pairs(self.map.layers) do
if self:isActorIndexed(objectlayer.name) then
print("DEBUG: loading actors in " .. objectlayer.name .. " actor layer")
for k, object in pairs(objectlayer.objects) do
self:newActor(objectlayer.name, object.x, object.y)
end
self.map:removeLayer(objectlayer.name)
end
end
end
function BaseWorld:loadMapPlayers()
for k, objectlayer in pairs(self.map.layers) do
if (objectlayer.name == "player") then
print("DEBUG: loading actors in player layer")
local i = 1
for k, object in pairs(objectlayer.objects) do
self:addPlayer(self.obj.Player(self, object.x, object.y), i, true)
-- TODO: don't hardcode camera handling
i = i + 1
end
self.map:removeLayer(objectlayer.name)
end
end
end
function BaseWorld:getDimensions()
if self.haveMap then
return self.map.width * self.map.tilewidth,

View file

@ -34,11 +34,11 @@ function World2D:new(scene, actorlist, mapfile)
self.scene = scene
self.cameras = CameraSystem(self)
self.actors = Bump.newWorld(50)
self:initPlayers()
self:setActorList(actorlist)
self:setMap(mapfile)
self.actors = Bump.newWorld(50)
end
-- ACTORS FUNCTIONS