diff options
Diffstat (limited to 'Sprite.lua')
-rw-r--r-- | Sprite.lua | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Sprite.lua b/Sprite.lua new file mode 100644 index 0000000..dac5ce8 --- /dev/null +++ b/Sprite.lua @@ -0,0 +1,48 @@ +local component = require 'component' +local Transform = require 'Transform' +local Drawable = require 'Drawable' + +local Sprite = component({Drawable}, {Transform}) + +local function load_image(path) + return love.graphics.newImage(path, {dpiscale = 2}) +end + +local loaded = setmetatable({}, {__mode = 'v'}) +local function get_states(name) + if loaded[name] then return loaded[name] end + loaded[name] = {} + + local path = "sprite/"..name + if love.filesystem.getInfo(path..".png", 'file') then + loaded[name][name] = load_image(path..".png") + elseif love.filesystem.getInfo(path, 'directory') then + local files = love.filesystem.getDirectoryItems(path) + for _, file in ipairs(files) do + local state = file:match '^(.*)%.png$' + if state then + loaded[name][state] = load_image(path.."/"..file) + end + end + end + return loaded[name] +end + +function Sprite:init(inst) + assert(inst.name) + inst.state = inst.state or inst.name + inst.states = get_states(inst.name) +end + +function Sprite:draw() + self.obj[Transform]:use() + local i = self.i.states[self.i.state] + local w, h = i:getDimensions() + love.graphics.draw(i, 0, 0, 0, 1, 1, w / 2, h / 2) +end + +function Sprite:bee() + print "apioform" +end + +return Sprite |