local utils = require "lib.utils.utils" local pQueue = require "lib.utils.priority_queue" --- @class CharacterGrid : Grid --- @field __grid {string: Id|nil} --- @field yOrderQueue PriorityQueue очередь отрисовки сверху вниз local grid = setmetatable({}, require "lib.level.grid.base") grid.__index = grid --- Adds a character id to the grid --- @param id Id function grid:add(id) local character = Tree.level.characters[id] if not character then return end local mapB = character:has(Tree.behaviors.map) if not mapB then return end local centerX, centerY = math.floor(mapB.displayedPosition.x + 0.5), math.floor(mapB.displayedPosition.y + 0.5) local sizeX, sizeY = mapB.size.x, mapB.size.y for y = centerY, centerY + sizeY - 1 do for x = centerX, centerX + sizeX - 1 do self.__grid[tostring(Vec3 { x, y })] = character.id end end end --- @param a Character --- @param b Character local function drawCmp(a, b) --- @TODO: это захардкожено, надо разделить поведения return (a:has(Tree.behaviors.map) and a:has(Tree.behaviors.map).displayedPosition.y or a:has(Tree.behaviors.light).position.y) < (b:has(Tree.behaviors.map) and b:has(Tree.behaviors.map).displayedPosition.y or b:has(Tree.behaviors.light).position.y) end --- fills the grid with the actual data --- --- should be called as early as possible during every tick function grid:reload() self:reset() self.yOrderQueue = pQueue.new(drawCmp) utils.each(Tree.level.characters, function(c) self:add(c.id) self.yOrderQueue:insert(c) end) end --- Generates an empty grid --- @return CharacterGrid local function new() return setmetatable({ __grid = {} }, grid) end return { new = new }