diff --git a/CHANGELOG.md b/CHANGELOG.md index 12319ae..6abfb82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/gamecore/modules/world/actors/baseactor.lua b/gamecore/modules/world/actors/baseactor.lua index 0274d77..5512449 100644 --- a/gamecore/modules/world/actors/baseactor.lua +++ b/gamecore/modules/world/actors/baseactor.lua @@ -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() diff --git a/gamecore/modules/world/actors/gfx2D.lua b/gamecore/modules/world/actors/gfx2D.lua index e191e13..9f3857e 100644 --- a/gamecore/modules/world/actors/gfx2D.lua +++ b/gamecore/modules/world/actors/gfx2D.lua @@ -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) diff --git a/gamecore/modules/world/baseworld.lua b/gamecore/modules/world/baseworld.lua index ded38f0..9ba876c 100644 --- a/gamecore/modules/world/baseworld.lua +++ b/gamecore/modules/world/baseworld.lua @@ -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