diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-10 13:24:55 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-10 16:05:59 +0100 |
| commit | 47dad9ad589b7df71a90bbb69f4839366913cba4 (patch) | |
| tree | 8733c80d2d02983febbb67a664d3ab9386843414 /lua | |
| parent | refactor(popups): popups are now an attribute of the pet class (diff) | |
refactor(animations): frames are now image objects
The frames table is contains a table for every action the pet can perform,
the action table contains all the image objects for the specified action.
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/pets/animations.lua | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index 8e10c31..0629727 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -12,32 +12,40 @@ for _ = 0, 15 do table.insert(lines, string) end +local listdir = require("pets.utils").listdir + function M.Animation.new(sourcedir, type, style) local instance = setmetatable({}, M.Animation) instance.type = type instance.style = style instance.sourcedir = sourcedir instance.frame_counter = 1 + instance.actions = listdir(sourcedir) instance.frames = {} + for _, action in pairs(instance.actions) do + local current_actions = {} + for _, file in pairs(listdir(sourcedir .. action)) do + local image = require("hologram.image"):new(sourcedir .. action .. "/" .. file) + table.insert(current_actions, image) + end + instance.frames[action] = current_actions + end return instance end function M.Animation:start(bufnr) - local files = require("pets.utils").listdir(self.sourcedir .. "walk/") - for _, file in pairs(files) do - table.insert(self.frames, self.sourcedir .. "walk/" .. file) - end - if self.timer ~= nil then + if self.timer ~= nil then -- reset timer self.timer = nil end self.timer = vim.loop.new_timer() self.bufnr = bufnr + self.current_action = "idle" - self.timer:start(100, 1000 / 8, function() + self.timer:start(0, 1000 / 8, function() -- run timer at 8fps vim.schedule(function() M.Animation.next_frame(self) end) - end) -- TODO: try timeout = 0 + end) end function M.Animation:next_frame() @@ -48,10 +56,10 @@ function M.Animation:next_frame() else self.current_image:delete(0, { free = false }) end - if self.frame_counter > #self.frames then + if self.frame_counter > #self.frames[self.current_action] then self.frame_counter = 1 end - local image = require("hologram.image"):new(self.frames[self.frame_counter]) + local image = self.frames[self.current_action][self.frame_counter] image:display(1, self.frame_counter, self.bufnr, {}) self.current_image = image end |