quad map refactor №1

Co-authored-by: Ivan Yuriev <ivanyr44@gmail.com>
This commit is contained in:
neckrat 2025-10-26 01:21:45 +03:00
parent 12b6646642
commit 2fc1a92ad1
3 changed files with 15 additions and 24 deletions

View File

@ -9,8 +9,8 @@ local function new(template, size)
for y = 0, size.y - 1 do for y = 0, size.y - 1 do
for x = 0, size.x - 1 do for x = 0, size.x - 1 do
local type = tileMap.map["flower_grass"] local type = tileMap.map["flower_grass"]
--- @type Tile local tile = require('lib.level.tile').new { quad = type[math.random(1, #type)], atlas = tileMap.atlas }
local tile = type[math.random(1, #type)]:copyWith({ position = Vec3 { x, y } }) :copyWith({ position = Vec3 { x, y } })
map[tostring(tile.position)] = tile map[tostring(tile.position)] = tile
end end
end end

View File

@ -1,19 +1,9 @@
--- @class TileAtlasData --- @class TileAtlasData
--- @field x number
--- @field y number
--- @field tileSize number
--- @field atlas love.Image --- @field atlas love.Image
--- @field isClip boolean --- @field quad love.Quad
--- @field quad love.Quad | nil
local tileAtlasData = {} local tileAtlasData = {}
tileAtlasData.__index = 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 --- @class Tile
@ -34,7 +24,7 @@ local function new(atlasData)
end end
function tile:draw() 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, self.position.y,
nil, 1 / 32, 1 / 32) nil, 1 / 32, 1 / 32)
end end

View File

@ -1,7 +1,7 @@
--- @class TileMap --- @class TileMap
--- @field tileSize integer --- @field tileSize integer
--- @field atlas love.Image --- @field atlas love.Image
--- @field map Tile[] --- @field map table<string, love.Quad>
local tileMap = {} local tileMap = {}
tileMap.__index = tileMap tileMap.__index = tileMap
@ -12,7 +12,7 @@ local function load(path)
--- @type table --- @type table
local manifest = path.manifest local manifest = path.manifest
local tiles = { local _tileMap = {
tileSize = manifest.tileSize, tileSize = manifest.tileSize,
atlas = atlas, atlas = atlas,
map = {} map = {}
@ -31,16 +31,17 @@ local function load(path)
end end
local cnt = 0 local cnt = 0
for y = 0, atlas:getHeight() - 1, tiles.tileSize do for y = 0, atlas:getHeight() - 1, _tileMap.tileSize do
for x = 0, atlas:getWidth() - 1, tiles.tileSize do for x = 0, atlas:getWidth() - 1, _tileMap.tileSize do
if layout[cnt] then if layout[cnt] then
for _, group in ipairs(layout[cnt]) do for _, group in ipairs(layout[cnt]) do
print(x, y, tiles.tileSize, atlas, false) print(x, y, _tileMap.tileSize, atlas, false)
local tile = require('lib.level.tile').new { x = x, y = y, tileSize = tiles.tileSize, atlas = atlas, isClip = false } local quad = love.graphics.newQuad(x, y, _tileMap.tileSize, _tileMap.tileSize, atlas)
if tiles.map[group] then
table.insert(tiles.map[group], tile) if _tileMap.map[group] then
table.insert(_tileMap.map[group], quad)
else else
tiles.map[group] = { tile } _tileMap.map[group] = { quad }
end end
end end
end end
@ -48,7 +49,7 @@ local function load(path)
end end
end end
return setmetatable(tiles, tileMap) return setmetatable(_tileMap, tileMap)
end end
return { load = load } return { load = load }