modules/world: add a way to load objects from tiled
This commit is contained in:
parent
66041ff511
commit
fd78d083af
2 changed files with 65 additions and 4 deletions
|
@ -35,12 +35,12 @@ local CameraSystem = require(cwd .. "camera")
|
||||||
function BaseWorld:new(scene, actorlist, mapfile)
|
function BaseWorld:new(scene, actorlist, mapfile)
|
||||||
self.scene = scene
|
self.scene = scene
|
||||||
|
|
||||||
self.cameras = CameraSystem(self)
|
self.cameras = CameraSystem(self)
|
||||||
|
self.actors = {}
|
||||||
|
|
||||||
self:initPlayers()
|
self:initPlayers()
|
||||||
self:setActorList(actorlist)
|
self:setActorList(actorlist)
|
||||||
self:setMap(mapfile)
|
self:setMap(mapfile)
|
||||||
self.actors = {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseWorld:setActorList(actorlist)
|
function BaseWorld:setActorList(actorlist)
|
||||||
|
@ -61,6 +61,7 @@ function BaseWorld:setMap(mapfile)
|
||||||
self.map = Sti(mapfile)
|
self.map = Sti(mapfile)
|
||||||
self.haveBackgroundColor = true
|
self.haveBackgroundColor = true
|
||||||
self.backcolor = self.map.backgroundcolor or {128, 128, 128}
|
self.backcolor = self.map.backgroundcolor or {128, 128, 128}
|
||||||
|
self:loadMapObjects()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -71,6 +72,10 @@ function BaseWorld:newActor(name, x, y)
|
||||||
self.obj.index[name](self, x, y)
|
self.obj.index[name](self, x, y)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function BaseWorld:newCollision(name, x, y, w, h)
|
||||||
|
self.obj.collisions[name](self, x, y, w, h)
|
||||||
|
end
|
||||||
|
|
||||||
function BaseWorld:registerActor(actor)
|
function BaseWorld:registerActor(actor)
|
||||||
table.insert(self.actors, actor)
|
table.insert(self.actors, actor)
|
||||||
end
|
end
|
||||||
|
@ -111,6 +116,17 @@ function BaseWorld:getActors()
|
||||||
return self.actors
|
return self.actors
|
||||||
end
|
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
|
-- PLAYER MANAGEMENT FUNCTIONS
|
||||||
-- Basic function to handle player actors
|
-- Basic function to handle player actors
|
||||||
|
|
||||||
|
@ -150,6 +166,51 @@ end
|
||||||
-- MAP FUNCTIONS
|
-- MAP FUNCTIONS
|
||||||
-- All map wrappers
|
-- 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()
|
function BaseWorld:getDimensions()
|
||||||
if self.haveMap then
|
if self.haveMap then
|
||||||
return self.map.width * self.map.tilewidth,
|
return self.map.width * self.map.tilewidth,
|
||||||
|
|
|
@ -33,12 +33,12 @@ 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.cameras = CameraSystem(self)
|
||||||
|
self.actors = Bump.newWorld(50)
|
||||||
|
|
||||||
self:initPlayers()
|
self:initPlayers()
|
||||||
self:setActorList(actorlist)
|
self:setActorList(actorlist)
|
||||||
self:setMap(mapfile)
|
self:setMap(mapfile)
|
||||||
self.actors = Bump.newWorld(50)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- ACTORS FUNCTIONS
|
-- ACTORS FUNCTIONS
|
||||||
|
|
Loading…
Reference in a new issue