refactor(levels): use World2D map loading functions.
This commit is contained in:
parent
91ce8ba9c0
commit
652771de5a
|
@ -0,0 +1,11 @@
|
|||
local Entity = require "scenes.levels.entities.parent"
|
||||
|
||||
local Platform = Entity:extend()
|
||||
|
||||
function Platform:new(world, x, y, w, h)
|
||||
Platform.super.new(self, world, "platform", x, y, w, h)
|
||||
|
||||
self:setDebugColor(.3,.3,.3)
|
||||
end
|
||||
|
||||
return Platform
|
|
@ -0,0 +1,11 @@
|
|||
local Entity = require "scenes.levels.entities.parent"
|
||||
|
||||
local Wall = Entity:extend()
|
||||
|
||||
function Wall:new(world, x, y, w, h)
|
||||
Wall.super.new(self, world, "wall", x, y, w, h)
|
||||
|
||||
self:setDebugColor(0,0,0)
|
||||
end
|
||||
|
||||
return Wall
|
|
@ -0,0 +1,11 @@
|
|||
local Entity = require "scenes.levels.entities.parent"
|
||||
|
||||
local Water = Entity:extend()
|
||||
|
||||
function Water:new(world, x, y, w, h)
|
||||
Water.super.new(self, world, "water", x, y, w, h)
|
||||
|
||||
self:setDebugColor(.1,.1,.9)
|
||||
end
|
||||
|
||||
return Water
|
|
@ -1,7 +1,6 @@
|
|||
local Obj = {}
|
||||
|
||||
-- On charge toutes les différentes types d'entitées
|
||||
Obj.Collision = require "scenes.levels.entities.collision"
|
||||
Obj.Block = require "scenes.levels.entities.block"
|
||||
Obj.Bullet = require "scenes.levels.entities.bullet"
|
||||
Obj.Coin = require "scenes.levels.entities.loot.coin"
|
||||
|
@ -24,9 +23,11 @@ Obj.index["coin10"] = Obj.Coin10
|
|||
Obj.index["block"] = Obj.Block
|
||||
Obj.index["ennemy"] = Obj.Ennemy
|
||||
|
||||
local collisions = "scenes.levels.entities.collision."
|
||||
|
||||
Obj.collisions = {}
|
||||
Obj.collisions["wall"] = true
|
||||
Obj.collisions["water"] = true
|
||||
Obj.collisions["platform"] = true
|
||||
Obj.collisions["wall"] = require(collisions .. "wall")
|
||||
Obj.collisions["water"] = require(collisions .. "water")
|
||||
Obj.collisions["platform"] = require(collisions .. "plateform")
|
||||
|
||||
return Obj
|
||||
|
|
|
@ -20,50 +20,8 @@ function World:new(scene, mapfile)
|
|||
end
|
||||
|
||||
function World:load()
|
||||
self:loadCollisions()
|
||||
self:loadEntities()
|
||||
end
|
||||
|
||||
function World:loadEntities()
|
||||
for k, objectlayer in pairs(self.map.layers) do
|
||||
if (self:isEntityLayer(objectlayer.name)) then
|
||||
for k, object in pairs(objectlayer.objects) do
|
||||
self:addEntityByNameOnGrid( objectlayer.name, object.x, object.y,
|
||||
object.width, object.height )
|
||||
end
|
||||
self.map:removeLayer(objectlayer.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function World:isEntityLayer(layername)
|
||||
return (self.obj.index[layername] ~= nil)
|
||||
end
|
||||
|
||||
function World:addEntityByNameOnGrid(name, x, y, w, h)
|
||||
local objWidth, objHeight = 16, 16
|
||||
local cellHor = math.ceil(w / objWidth)
|
||||
local cellVert = math.ceil(h / objHeight)
|
||||
for i=1, cellHor do
|
||||
for j=1, cellVert do
|
||||
self:addEntityByName(name, x + (i-1)*objWidth, y + (j-1)*objHeight)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function World:addEntityByName(name, x, y)
|
||||
self.obj.index[name](self, x, y)
|
||||
end
|
||||
|
||||
function World: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
|
||||
self:loadMapCollisions()
|
||||
self:loadMapActors()
|
||||
end
|
||||
|
||||
function World:getStartPosition()
|
||||
|
@ -80,18 +38,6 @@ function World:getStartPosition()
|
|||
return startx, starty
|
||||
end
|
||||
|
||||
function World:isCollisionLayer(layername)
|
||||
return (self.obj.collisions[layername] == true)
|
||||
end
|
||||
|
||||
function World: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
|
||||
|
||||
-- MAP FUNCTIONS
|
||||
-- All map wrappers
|
||||
|
||||
|
|
Loading…
Reference in New Issue