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 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

View File

@ -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

View File

@ -1,7 +1,7 @@
--- @class TileMap
--- @field tileSize integer
--- @field atlas love.Image
--- @field map Tile[]
--- @field map table<string, love.Quad>
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 }