46 lines
1.1 KiB
Lua
46 lines
1.1 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,
|
|
}
|
|
}
|
|
|
|
tilesheet.load = function(data_or_path)
|
|
local tile_image, tile_size
|
|
local tiles = {}
|
|
|
|
if type(data_or_path) == "table" then
|
|
tile_image = load_image(data_or_path.image_path)
|
|
tile_size = data_or_path.tile_size
|
|
|
|
for name, tile_data in pairs(data_or_path.tiles) do
|
|
tiles[name] = setmetatable({
|
|
image = tile_image,
|
|
quad = love.graphics.newQuad( (tile_data.x - 1) * tile_size, (tile_data.y - 1) * tile_size, tile_size, tile_size, tile_image ),
|
|
}, tile_metatable)
|
|
end
|
|
|
|
else
|
|
tile_image = load_image(data_or_path)
|
|
end
|
|
|
|
return tiles, tile_image
|
|
end
|
|
|
|
return setmetatable(tilesheet, {
|
|
__call = function(self, ...) return tilesheet.load(...) end
|
|
})
|