From a8c188b24e355a6a91d8d40b6250613a5790ca87 Mon Sep 17 00:00:00 2001 From: PeaAshMeter Date: Sun, 2 Nov 2025 05:48:09 +0300 Subject: [PATCH] Add pickQuad method to spriteAtlas for random quad selection --- lib/level/procedural.lua | 3 +-- lib/utils/sprite_atlas.lua | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/level/procedural.lua b/lib/level/procedural.lua index 0722ca1..80e80f5 100644 --- a/lib/level/procedural.lua +++ b/lib/level/procedural.lua @@ -8,8 +8,7 @@ local function new(template, size) local tileMap = require("lib.utils.sprite_atlas").load(Tree.assets.files.tiles.grass) for y = 0, size.y - 1 do for x = 0, size.x - 1 do - local type = tileMap.map["flower_grass"] - local tile = require('lib.level.tile').new { quad = type[math.random(1, #type)], atlas = tileMap.atlas } + local tile = require('lib.level.tile').new { quad = tileMap:pickQuad("flower_grass"), atlas = tileMap.atlas } :copyWith({ position = Vec3 { x, y } }) map[tostring(tile.position)] = tile end diff --git a/lib/utils/sprite_atlas.lua b/lib/utils/sprite_atlas.lua index c51ae5c..25da58a 100644 --- a/lib/utils/sprite_atlas.lua +++ b/lib/utils/sprite_atlas.lua @@ -52,4 +52,18 @@ local function load(path) return setmetatable(_spriteAtlas, spriteAtlas) end +--- Returns a random quad of the group +--- +--- @param tag string +--- @return love.Quad +function spriteAtlas:pickQuad(tag) + local group = self.map[tag] + if group then + return group[math.random(1, #group)] + end + + print("[SpriteAtlas]: no tile with a tag '" .. tag .. "'") + return love.graphics.newQuad(0, 0, self.tileSize, self.tileSize, self.atlas) --fallback +end + return { load = load }