improvement: rework layer drawing code

This commit is contained in:
Kazhnuz 2021-03-23 11:26:15 +01:00
parent 9d589dba8f
commit a5f362ceaa
3 changed files with 60 additions and 14 deletions

View file

@ -398,12 +398,15 @@ function BaseWorld:draw(dt)
local camNumber = self.cameras:getViewNumber()
if (camNumber == 0) then
self:drawMap()
self:drawLowerLayers()
self:drawActors()
self:drawUpperLayers()
else
for i=1, camNumber do
self.cameras:attachView(i)
self:drawMap(i)
self:drawLowerLayers(i)
self:drawActors(i)
self:drawUpperLayers(i)
self.cameras:detachView(i)
end
end
@ -417,6 +420,18 @@ function BaseWorld:drawActors(id)
end
end
function BaseWorld:drawUpperLayers()
if (self.map ~= nil) then
self.map:drawUpperLayers()
end
end
function BaseWorld:drawLowerLayers()
if (self.map ~= nil) then
self.map:drawLowerLayers()
end
end
function BaseWorld:drawMap(id)
if (self.map ~= nil) then
self.map:draw()

View file

@ -85,6 +85,10 @@ function ParentMap:getDimensions()
return core.screen:getDimensions()
end
function ParentMap:drawLowerLayers()
self:draw()
end
function ParentMap:getBox()
local x1, y1, x2, y2 = self:getPadding()
local w, h = self:getDimensions()

View file

@ -10,6 +10,26 @@ function StiMap:new(world, mapfile)
StiMap.super.new(self, world)
self:setBackgroundColorFromTable(self.sti.backgroundcolor)
self.mapname = self:getMapName(mapfile)
self.objectlayer = 0
end
function StiMap:loadObjects()
self:loadCollisions()
self:loadPlayers()
self:loadActors()
self.objectlayer = self:getObjectLayer()
end
function StiMap:getObjectLayer()
local objectlayer = 0
for i, layer in ipairs(self.sti.layers) do
if (layer.name == "objects") then
objectlayer = i
end
self.nbrLayer = i
end
return objectlayer
end
function StiMap:getMapName(mapfile)
@ -224,20 +244,27 @@ end
-- DRAW FUNCTIONS
-- Draw the map
function StiMap:draw(i)
local haveDrawnObjects = false
for _, layer in ipairs(self.sti.layers) do
if layer.visible and layer.opacity > 0 and (layer.type == "tilelayer") then
self.sti:drawLayer(layer)
else
if (layer.name == "objects") then
haveDrawnObjects = true
self.world:drawActors(i)
end
function StiMap:drawUpperLayers()
if (self.objectlayer > 0) then
for i = self.objectlayer, self.nbrLayer, 1 do
self:drawLayer(i)
end
end
if (not haveDrawnObjects) then
self.world:drawActors(i)
end
function StiMap:drawLowerLayers()
for i = 1, self.objectlayer, 1 do
self:drawLayer(i)
end
end
function StiMap:drawLayer(id)
local layer = self.sti.layers[id]
if (layer ~= nil) then
if layer.visible and layer.opacity > 0 and (layer.type == "tilelayer") then
print("upper " .. id)
self.sti:drawLayer(layer)
end
end
end