This system doesn't properly expose/account for the user wanting to scale the tiles. You'd have to know what size it is already to scale things. Not good. Will figure out how to properly adjust that later.
40 lines
1.0 KiB
Lua
40 lines
1.0 KiB
Lua
local load_image = function(file_path)
|
|
local file = io.open(file_path, "rb")
|
|
local raw_data = file:read("*all")
|
|
file:close()
|
|
|
|
local love_file_data = love.filesystem.newFileData(raw_data, file_path)
|
|
local love_image = love.graphics.newImage(love_file_data)
|
|
return love_image
|
|
end
|
|
|
|
local tilesheet = {}
|
|
|
|
local tile_metatable = {
|
|
__index = {
|
|
draw = function(self, ...)
|
|
love.graphics.draw(self.image, self.quad, ...)
|
|
end,
|
|
}
|
|
}
|
|
|
|
local tilesheet.load = function(data_or_path)
|
|
if type(data_or_path) == "table" then
|
|
local tile_image = load_image(data_or_path.image_path)
|
|
local tiles = {}
|
|
|
|
for name, data in pairs(data_or_path.tiles) do
|
|
tiles[name] = setmetatable({
|
|
image = tile_image,
|
|
quad = love.graphics.newQuad( (data.x - 1) * data_or_path.tile_size, (data.y - 1) * data_or_path.tile_size, data_or_path.tile_size, data_or_path.tile_size, tile_image ),
|
|
}, tile_metatable)
|
|
end
|
|
|
|
return tiles
|
|
end
|
|
end
|
|
|
|
return setmetatable(tilesheet, {
|
|
__call = function(self, ...) return tilesheet.load(...) end
|
|
})
|