From 2fc1a92ad189a3e1804e288568c3a982d7723dfc Mon Sep 17 00:00:00 2001 From: neckrat Date: Sun, 26 Oct 2025 01:21:45 +0300 Subject: [PATCH] =?UTF-8?q?quad=20map=20refactor=20=E2=84=961?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ivan Yuriev --- lib/level/procedural.lua | 4 ++-- lib/level/tile.lua | 14 ++------------ lib/level/tileMap.lua | 21 +++++++++++---------- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/lib/level/procedural.lua b/lib/level/procedural.lua index f62ae03..e20a3b6 100644 --- a/lib/level/procedural.lua +++ b/lib/level/procedural.lua @@ -9,8 +9,8 @@ local function new(template, size) for y = 0, size.y - 1 do for x = 0, size.x - 1 do local type = tileMap.map["flower_grass"] - --- @type Tile - local tile = type[math.random(1, #type)]:copyWith({ position = Vec3 { x, y } }) + local tile = require('lib.level.tile').new { quad = type[math.random(1, #type)], atlas = tileMap.atlas } + :copyWith({ position = Vec3 { x, y } }) map[tostring(tile.position)] = tile end end diff --git a/lib/level/tile.lua b/lib/level/tile.lua index 8f494d3..76f5c5e 100644 --- a/lib/level/tile.lua +++ b/lib/level/tile.lua @@ -1,19 +1,9 @@ --- @class TileAtlasData ---- @field x number ---- @field y number ---- @field tileSize number --- @field atlas love.Image ---- @field isClip boolean ---- @field quad love.Quad | nil +--- @field quad love.Quad local tileAtlasData = {} tileAtlasData.__index = tileAtlasData -function tileAtlasData:getQuad() - if self.quad then return self.quad end - self.quad = love.graphics.newQuad(self.x, self.y, self.tileSize, self.tileSize, self.atlas) - return self.quad -end - ----------------------------------------------------------------- --- @class Tile @@ -34,7 +24,7 @@ local function new(atlasData) end function tile:draw() - love.graphics.draw(self.atlasData.atlas, self.atlasData:getQuad(), self.position.x, + love.graphics.draw(self.atlasData.atlas, self.atlasData.quad, self.position.x, self.position.y, nil, 1 / 32, 1 / 32) end diff --git a/lib/level/tileMap.lua b/lib/level/tileMap.lua index 749937f..bdc7f01 100644 --- a/lib/level/tileMap.lua +++ b/lib/level/tileMap.lua @@ -1,7 +1,7 @@ --- @class TileMap --- @field tileSize integer --- @field atlas love.Image ---- @field map Tile[] +--- @field map table local tileMap = {} tileMap.__index = tileMap @@ -12,7 +12,7 @@ local function load(path) --- @type table local manifest = path.manifest - local tiles = { + local _tileMap = { tileSize = manifest.tileSize, atlas = atlas, map = {} @@ -31,16 +31,17 @@ local function load(path) end local cnt = 0 - for y = 0, atlas:getHeight() - 1, tiles.tileSize do - for x = 0, atlas:getWidth() - 1, tiles.tileSize do + for y = 0, atlas:getHeight() - 1, _tileMap.tileSize do + for x = 0, atlas:getWidth() - 1, _tileMap.tileSize do if layout[cnt] then for _, group in ipairs(layout[cnt]) do - print(x, y, tiles.tileSize, atlas, false) - local tile = require('lib.level.tile').new { x = x, y = y, tileSize = tiles.tileSize, atlas = atlas, isClip = false } - if tiles.map[group] then - table.insert(tiles.map[group], tile) + print(x, y, _tileMap.tileSize, atlas, false) + local quad = love.graphics.newQuad(x, y, _tileMap.tileSize, _tileMap.tileSize, atlas) + + if _tileMap.map[group] then + table.insert(_tileMap.map[group], quad) else - tiles.map[group] = { tile } + _tileMap.map[group] = { quad } end end end @@ -48,7 +49,7 @@ local function load(path) end end - return setmetatable(tiles, tileMap) + return setmetatable(_tileMap, tileMap) end return { load = load }