diff options
| author | the lemons <citrons@mondecitronne.com> | 2022-08-27 01:23:20 -0500 |
|---|---|---|
| committer | the lemons <citrons@mondecitronne.com> | 2022-08-27 01:23:20 -0500 |
| commit | 8bb18a89060ecd581a06238ce3fa5d2170ee3a15 (patch) | |
| tree | 25baba9d2c267b986adfe7af661b3cd99a32b346 /Sprite.lua | |
| parent | 001272dbcbaa1110541e0666af3cea1c97bc199f (diff) | |
implement component system
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 |
