scenes/levels: reorganize and cleanup world.lua
This commit is contained in:
parent
314cfc241d
commit
26e9d7c4d5
|
@ -1,15 +1,17 @@
|
|||
local Obj = require "scenes.levels.entities"
|
||||
|
||||
-- INIT FUNCTIONS
|
||||
-- All functions to init the world and the map
|
||||
|
||||
function Level:initWorld()
|
||||
self.map = Sti("assets/maps/" .. self.mapfile .. ".lua")
|
||||
self.obj = Obj
|
||||
self.entities = Bump.newWorld(50)
|
||||
self.backcolor = self.map.backgroundcolor or {0, 0, 0}
|
||||
self.blocks = {} -- On vide la liste des blocks
|
||||
|
||||
self.activeObjects = 0
|
||||
|
||||
self:loadCollisions()-- On charge les collisions
|
||||
self:loadCollisions()
|
||||
self:loadEntities()
|
||||
self:getStartPosition()
|
||||
end
|
||||
|
@ -26,6 +28,10 @@ function Level:loadEntities()
|
|||
end
|
||||
end
|
||||
|
||||
function Level:isEntityLayer(layername)
|
||||
return (self.obj.index[layername] ~= nil)
|
||||
end
|
||||
|
||||
function Level:addEntityByNameOnGrid(name, x, y, w, h)
|
||||
local objWidth, objHeight = 16, 16
|
||||
local cellHor = math.ceil(w / objWidth)
|
||||
|
@ -35,18 +41,23 @@ function Level:addEntityByNameOnGrid(name, x, y, w, h)
|
|||
self:addEntityByName(name, x + (i-1)*objWidth, y + (j-1)*objHeight)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function Level:addEntityByName(name, x, y)
|
||||
self.obj.index[name](self, x, y)
|
||||
end
|
||||
|
||||
function Level:isEntityLayer(layername)
|
||||
return (self.obj.index[layername] ~= nil)
|
||||
function Level:loadCollisions()
|
||||
for k, objectlayer in pairs(self.map.layers) do
|
||||
if self:isCollisionLayer(objectlayer.name) then
|
||||
for k, object in pairs(objectlayer.objects) do
|
||||
self:addCollision(object.x,object.y,object.width,object.height, objectlayer.name)
|
||||
end
|
||||
self.map:removeLayer(objectlayer.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function Level:getStartPosition()
|
||||
for k, objectlayer in pairs(self.map.layers) do
|
||||
if objectlayer.name == "playerstart" then
|
||||
|
@ -60,32 +71,21 @@ function Level:getStartPosition()
|
|||
return self.startx, self.starty
|
||||
end
|
||||
|
||||
function Level:loadCollisions()
|
||||
for k, objectlayer in pairs(self.map.layers) do
|
||||
if self:isCollisionLayer(objectlayer.name) then
|
||||
for k, object in pairs(objectlayer.objects) do
|
||||
self:addCollision(object.x,object.y,object.width,object.height, objectlayer.name)
|
||||
end
|
||||
self.map:removeLayer(objectlayer.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function Level:addCollision(x, y, w, h, name) -- Rajouter un block solide dans le monde physique
|
||||
--block.collType = name
|
||||
if w == 0 then
|
||||
w = 1
|
||||
end
|
||||
local block = self.obj.Collision(self, x, y, w, h, name)--{x=x,y=y,w=w,h=h,solid=1}
|
||||
self.blocks[#self.blocks+1] = block -- On ajoute le block à la liste des blocks
|
||||
|
||||
--self:addPhysicalObject(block, x,y,w,h) -- On le rajoute au monde physique
|
||||
end
|
||||
|
||||
function Level:isCollisionLayer(layername)
|
||||
return (self.obj.collisions[layername] == true)
|
||||
end
|
||||
|
||||
function Level:addCollision(x, y, w, h, name)
|
||||
-- Rajouter un block solide dans le monde physique
|
||||
if w == 0 then
|
||||
w = 1
|
||||
end
|
||||
self.obj.Collision(self, x, y, w, h, name)
|
||||
end
|
||||
|
||||
-- ENTITY MANAGEMENT FUNCTIONS
|
||||
-- All BUMP2D wrappers
|
||||
|
||||
function Level:addEntity(entity)
|
||||
return self.entities:add(entity, entity.x, entity.y, entity.w, entity.h)
|
||||
end
|
||||
|
@ -106,6 +106,13 @@ function Level:countEntities()
|
|||
return self.entities:countItems()
|
||||
end
|
||||
|
||||
function Level:getEntities()
|
||||
return self.entities:getItems()
|
||||
end
|
||||
|
||||
-- UPDATE FUNCTIONS
|
||||
-- All update functions
|
||||
|
||||
function Level:updateWorld(dt)
|
||||
self:updateEntities(dt)
|
||||
self:updateMap(dt)
|
||||
|
@ -123,6 +130,13 @@ function Level:updateEntities(dt)
|
|||
self.activeObjects = len
|
||||
end
|
||||
|
||||
function Level:updateMap(dt)
|
||||
self.map:update(dt)
|
||||
end
|
||||
|
||||
-- DRAW FUNCTIONS
|
||||
-- All function to draw the map, world and entities
|
||||
|
||||
function Level:drawWorld()
|
||||
self:drawBackgroundColor()
|
||||
self.camera:attach()
|
||||
|
@ -141,14 +155,6 @@ function Level:drawEntities()
|
|||
end
|
||||
end
|
||||
|
||||
function Level:updateMap(dt)
|
||||
self.map:update(dt)
|
||||
end
|
||||
|
||||
function Level:getEntities()
|
||||
return self.entities:getItems()
|
||||
end
|
||||
|
||||
function Level:drawMap()
|
||||
local tx = self.camera.x - 424 / 2
|
||||
local ty = self.camera.y - 240 / 2
|
||||
|
|
Loading…
Reference in New Issue