feat(world): draw actors according to their depth

It allow us to draw some sprites always over some others.

Fixes #8
This commit is contained in:
Kazhnuz 2019-06-16 10:24:30 +02:00
parent 54b01b0d94
commit f7040d1854
4 changed files with 13 additions and 0 deletions

View file

@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **world:** Add `Actor.creationID` to keep the order in which actors have been created
- **world:** Add `Actor.depth` to keep the order in which actors are drawn
### Changed
- **world:** automatize world integration in a scene

View file

@ -33,6 +33,7 @@ local Timer = require(cwd .. "utils.timer")
function BaseActor:new(world, type, isSolid)
self.type = type or ""
self.isSolid = isSolid or false
self.depth = 0
self:setManagers(world)
self:initKeys()

View file

@ -34,6 +34,7 @@ function GFX:new(world, x, y, spritename)
local duration = self.sprite.clone:getAnimationDuration()
self:addTimer("destroy", duration)
self.depth = -100
end
function GFX:timerResponse(name)

View file

@ -382,6 +382,15 @@ function BaseWorld:drawActors(id)
else
actors = self:getVisibleActors(id)
end
table.sort(actors, function(a,b)
if (a.depth == b.depth) then
return a.creationID < b.creationID
else
return a.depth > b.depth
end
end)
for i,v in ipairs(actors) do
v:draw()
end