41 lines
1.4 KiB
Lua
41 lines
1.4 KiB
Lua
local utils = require "lib.utils.utils"
|
||
--- @class TileGrid : Grid
|
||
local map = setmetatable({}, require "lib.level.grid.base")
|
||
map.__index = map
|
||
|
||
--- create new map
|
||
--- @param type "procedural"|"handmaded"
|
||
--- @param template Procedural|Handmaded
|
||
--- @param size? Vec3
|
||
local function new(type, template, size)
|
||
local tMap = require('lib.level.' .. type).new(template, size)
|
||
local grid = setmetatable({ __grid = tMap }, map)
|
||
grid:refreshBatch()
|
||
return grid
|
||
end
|
||
|
||
function map:refreshBatch()
|
||
-- Находим атлас первого попавшегося тайла (предполагаем, что он один для всех)
|
||
local _, firstTile = next(self.__grid)
|
||
if not firstTile then return end
|
||
|
||
local atlas = firstTile.atlasData.atlas
|
||
local count = 0
|
||
for _ in pairs(self.__grid) do count = count + 1 end
|
||
|
||
self.batch = love.graphics.newSpriteBatch(atlas, count)
|
||
for _, tile in pairs(self.__grid) do
|
||
-- 1/32 это масштаб, так как размер тайла в мире 1x1 метр, а в атласе 32x32 пикселя
|
||
self.batch:add(tile.atlasData.quad, tile.position.x, tile.position.y, 0, 1 / 32, 1 / 32)
|
||
end
|
||
end
|
||
|
||
function map:draw()
|
||
if not self.batch then return end
|
||
Tree.level.render:enqueue(Tree.level.render.LAYERS.FLOOR, 0, function()
|
||
love.graphics.draw(self.batch)
|
||
end)
|
||
end
|
||
|
||
return { new = new }
|