fixed character z-ordering while moving

This commit is contained in:
Ivan Yuriev 2025-09-07 14:27:20 +03:00
parent e8bb7306ac
commit 5cead3c282
2 changed files with 13 additions and 3 deletions

View File

@ -1,6 +1,8 @@
local utils = require "lib.utils.utils" local utils = require "lib.utils.utils"
local pQueue = require "lib.utils.priority_queue"
--- @class CharacterGrid : Grid --- @class CharacterGrid : Grid
--- @field __grid {string: Id|nil} --- @field __grid {string: Id|nil}
--- @field yOrderQueue PriorityQueue<Character> очередь отрисовки сверху вниз
local grid = setmetatable({}, require "lib.level.grid.base") local grid = setmetatable({}, require "lib.level.grid.base")
grid.__index = grid grid.__index = grid
@ -21,13 +23,21 @@ function grid:add(id)
end end
end end
--- @param a Character
--- @param b Character
local function drawCmp(a, b)
return a.logic.mapLogic.displayedPosition.y < b.logic.mapLogic.displayedPosition.y
end
--- fills the grid with the actual data --- fills the grid with the actual data
--- ---
--- should be called as early as possible during every tick --- should be called as early as possible during every tick
function grid:reload() function grid:reload()
self:reset() self:reset()
self.yOrderQueue = pQueue.new(drawCmp)
utils.each(Tree.level.characters, function(c) utils.each(Tree.level.characters, function(c)
self:add(c.id) self:add(c.id)
self.yOrderQueue:insert(c)
end) end)
end end

View File

@ -39,9 +39,9 @@ end
function level:draw() function level:draw()
self.tileGrid:draw() self.tileGrid:draw()
utils.each(self.characters, function(el) while not self.characterGrid.yOrderQueue:is_empty() do -- по сути это сортировка кучей за n log n, но линейное
el:draw() self.characterGrid.yOrderQueue:pop():draw()
end) end
end end
return { return {