Add pickQuad method to spriteAtlas for random quad selection
This commit is contained in:
parent
aec916cd14
commit
1d42bac89a
@ -8,8 +8,7 @@ local function new(template, size)
|
|||||||
local tileMap = require("lib.utils.sprite_atlas").load(Tree.assets.files.tiles.grass)
|
local tileMap = require("lib.utils.sprite_atlas").load(Tree.assets.files.tiles.grass)
|
||||||
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 tile = require('lib.level.tile').new { quad = tileMap:pickQuad("flower_grass"), atlas = tileMap.atlas }
|
||||||
local tile = require('lib.level.tile').new { quad = type[math.random(1, #type)], atlas = tileMap.atlas }
|
|
||||||
:copyWith({ position = Vec3 { x, y } })
|
:copyWith({ position = Vec3 { x, y } })
|
||||||
map[tostring(tile.position)] = tile
|
map[tostring(tile.position)] = tile
|
||||||
end
|
end
|
||||||
|
|||||||
@ -52,4 +52,18 @@ local function load(path)
|
|||||||
return setmetatable(_spriteAtlas, spriteAtlas)
|
return setmetatable(_spriteAtlas, spriteAtlas)
|
||||||
end
|
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 }
|
return { load = load }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user