From 9a2398df8d54c25ff0d710d335176d427466ed64 Mon Sep 17 00:00:00 2001 From: Neckrat Date: Fri, 22 Aug 2025 01:21:15 +0300 Subject: [PATCH] tile struct initial --- assets/tiles/grass/manifest2.lua | 199 ------------------------------- lib/level/procedural.lua | 2 +- lib/level/tile.lua | 75 +++++------- lib/level/tileMap.lua | 52 ++++++++ 4 files changed, 80 insertions(+), 248 deletions(-) delete mode 100644 assets/tiles/grass/manifest2.lua create mode 100644 lib/level/tileMap.lua diff --git a/assets/tiles/grass/manifest2.lua b/assets/tiles/grass/manifest2.lua deleted file mode 100644 index dcd9090..0000000 --- a/assets/tiles/grass/manifest2.lua +++ /dev/null @@ -1,199 +0,0 @@ -return { - flower_grass = { - flower_grass1 = { - h = 32, - w = 32, - x = 96, - y = 0 - } --[[table: 0x7f4aca9c8c90]], - flower_grass2 = { - h = 32, - w = 32, - x = 96, - y = 32 - } --[[table: 0x7f4aca9c8850]], - flower_grass3 = { - h = 32, - w = 32, - x = 96, - y = 64 - } --[[table: 0x7f4aca9c7240]], - flower_grass4 = { - h = 32, - w = 32, - x = 96, - y = 96 - } --[[table: 0x7f4aca9c7358]], - flower_grass5 = { - h = 32, - w = 32, - x = 128, - y = 0 - } --[[table: 0x7f4aca9c7438]], - flower_grass6 = { - h = 32, - w = 32, - x = 128, - y = 32 - } --[[table: 0x7f4aca9bb9e8]], - flower_grass7 = { - h = 32, - w = 32, - x = 128, - y = 64 - } --[[table: 0x7f4aca9bba60]], - flower_grass8 = { - h = 32, - w = 32, - x = 128, - y = 96 - } --[[table: 0x7f4aca9bbb40]], - flower_grass9 = { - h = 32, - w = 32, - x = 160, - y = 0 - } --[[table: 0x7f4aca9c9088]], - flower_grass10 = { - h = 32, - w = 32, - x = 160, - y = 32 - } --[[table: 0x7f4aca9c92c0]], - flower_grass11 = { - h = 32, - w = 32, - x = 160, - y = 64 - } --[[table: 0x7f4aca9bb8f0]], - flower_grass12 = { - h = 32, - w = 32, - x = 160, - y = 96 - } --[[table: 0x7f4aca9c5500]], - flower_grass13 = { - h = 32, - w = 32, - x = 192, - y = 0 - } --[[table: 0x7f4aca9c55e0]], - flower_grass14 = { - h = 32, - w = 32, - x = 192, - y = 32 - } --[[table: 0x7f4aca9c56c0]], - flower_grass15 = { - h = 32, - w = 32, - x = 192, - y = 64 - } --[[table: 0x7f4aca9c57a0]], - flower_grass16 = { - h = 32, - w = 32, - x = 192, - y = 96 - } --[[table: 0x7f4aca9c7f80]] - } --[[table: 0x7f4aca9c8be0]], - grass = { - grass1 = { - h = 32, - w = 32, - x = 0, - y = 0 - } --[[table: 0x7f4aca9ca818]], - grass2 = { - h = 32, - w = 32, - x = 0, - y = 32 - } --[[table: 0x7f4aca9caa18]], - grass3 = { - h = 32, - w = 32, - x = 0, - y = 64 - } --[[table: 0x7f4aca9c7a20]], - grass4 = { - h = 32, - w = 32, - x = 0, - y = 96 - } --[[table: 0x7f4aca9c7da8]], - grass5 = { - h = 32, - w = 32, - x = 32, - y = 0 - } --[[table: 0x7f4aca9c7780]], - grass6 = { - h = 32, - w = 32, - x = 32, - y = 32 - } --[[table: 0x7f4aca9c5060]], - grass7 = { - h = 32, - w = 32, - x = 32, - y = 64 - } --[[table: 0x7f4aca9cf078]], - grass8 = { - h = 32, - w = 32, - x = 32, - y = 96 - } --[[table: 0x7f4aca9c96e8]], - grass9 = { - h = 32, - w = 32, - x = 64, - y = 0 - } --[[table: 0x7f4aca9c7ad8]], - grass10 = { - h = 32, - w = 32, - x = 64, - y = 32 - } --[[table: 0x7f4aca9c5318]], - grass11 = { - h = 32, - w = 32, - x = 64, - y = 64 - } --[[table: 0x7f4aca9ca900]], - grass12 = { - h = 32, - w = 32, - x = 64, - y = 96 - } --[[table: 0x7f4aca9cab58]], - grass13 = { - h = 32, - w = 32, - x = 96, - y = 0 - } --[[table: 0x7f4aca9c5e60]], - grass14 = { - h = 32, - w = 32, - x = 96, - y = 32 - } --[[table: 0x7f4aca9caba0]], - grass15 = { - h = 32, - w = 32, - x = 96, - y = 64 - } --[[table: 0x7f4aca9c6e90]], - grass16 = { - h = 32, - w = 32, - x = 96, - y = 96 - } --[[table: 0x7f4aca9c8b30]] - } --[[table: 0x7f4aca9c5240]], - tileSize = 32 -} --[[table: 0x7f4aca9c93e0]] diff --git a/lib/level/procedural.lua b/lib/level/procedural.lua index 7c8f69f..2c147e4 100644 --- a/lib/level/procedural.lua +++ b/lib/level/procedural.lua @@ -5,7 +5,7 @@ local function new(template, size) local map = {} -- паттерн-матчинг для самых маленьких if template == "flower_plains" then - local tileMap = require("lib.level.tile").load(Tree.assets.files.tiles.grass) + local tileMap = require("lib.level.tileMap").load(Tree.assets.files.tiles.grass) map.atlas = tileMap.atlas for y = 0, size.y - 1 do map[y] = {} diff --git a/lib/level/tile.lua b/lib/level/tile.lua index ad72fb6..eaceafc 100644 --- a/lib/level/tile.lua +++ b/lib/level/tile.lua @@ -1,51 +1,30 @@ ---- @class TileMap ---- @field tileSize integer ---- @field atlas love.Image -local tileMap = {} -tileMap.__index = tileMap +--- @class Tile +--- @field isClip boolean +--- @field quad love.Quad +local tile = {} +tile.__index = tile ---- @param path table -local function load(path) - --- @type love.Image - local atlas = path.atlas - --- @type table - local manifest = path.manifest - - --- @type TileMap - local tiles = { - tileSize = manifest.tileSize, - atlas = atlas - } - manifest.tileSize = nil - - local layout = {} - for group, ids in pairs(manifest) do - for _, id in ipairs(ids) do - if layout[id] then - table.insert(layout[id], group) - else - layout[id] = { group } - end - end - end - - local cnt = 0 - for y = 0, atlas:getHeight() - 1, tiles.tileSize do - for x = 0, atlas:getWidth() - 1, tiles.tileSize do - if layout[cnt] then - for _, group in ipairs(layout[cnt]) do - if tiles[group] then - table.insert(tiles[group], love.graphics.newQuad(x, y, tiles.tileSize, tiles.tileSize, atlas)) - else - tiles[group] = { love.graphics.newQuad(x, y, tiles.tileSize, tiles.tileSize, atlas) } - end - end - end - cnt = cnt + 1 - end - end - - return setmetatable(tiles, tileMap) +--- TODO: сделать как love.graphics.draw несколько сигнатур у функции +--- @param x number +--- @param y number +--- @param tileSize number +--- @param atlas love.Image +--- @param isClip boolean +local function new(x, y, tileSize, atlas, isClip) + local quad = love.graphics.newQuad(x, y, tileSize, tileSize, atlas) + return setmetatable({ + isClip = isClip, + quad = quad + }) end -return { load = load } +--- @param quad love.Quad +--- @param isClip boolean +local function fromQuad(quad, isClip) + return setmetatable({ + isClip = isClip, + quad = quad + }) +end + +return { new = new } diff --git a/lib/level/tileMap.lua b/lib/level/tileMap.lua new file mode 100644 index 0000000..a9da8b5 --- /dev/null +++ b/lib/level/tileMap.lua @@ -0,0 +1,52 @@ +--- @class TileMap +--- @field tileSize integer +--- @field atlas love.Image +--- @field map Tile[] +local tileMap = {} +tileMap.__index = tileMap + +--- @param path table +local function load(path) + --- @type love.Image + local atlas = path.atlas + --- @type table + local manifest = path.manifest + + local tiles = { + tileSize = manifest.tileSize, + atlas = atlas + } + manifest.tileSize = nil + + local layout = {} + for group, ids in pairs(manifest) do + for _, id in ipairs(ids) do + if layout[id] then + table.insert(layout[id], group) + else + layout[id] = { group } + end + end + end + + local cnt = 0 + for y = 0, atlas:getHeight() - 1, tiles.tileSize do + for x = 0, atlas:getWidth() - 1, tiles.tileSize do + if layout[cnt] then + for _, group in ipairs(layout[cnt]) do + local tile = require('lib.level.tile').new(x, y, tiles.tileSize, atlas, false) + if tiles.map[group] then + table.insert(tiles.map[group], tile) + else + tiles.map[group] = { tile } + end + end + end + cnt = cnt + 1 + end + end + + return setmetatable(tiles, tileMap) +end + +return { load = load }