diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-11 17:50:51 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-02-11 17:50:51 +0100 |
| commit | 755074947f0dcd62b4fb2ca0bd8df6b861b61208 (patch) | |
| tree | 51bd1008d87036078a797319746ef3e24f09a0dd /lua | |
| parent | feat(pets): don't allow duplicate names (diff) | |
feat(animations): set next position based on current action
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/pets/animations.lua | 22 | ||||
| -rw-r--r-- | lua/pets/pet.lua | 9 |
2 files changed, 29 insertions, 2 deletions
diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index e2f6225..35ff5b8 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -14,7 +14,7 @@ end local listdir = require("pets.utils").listdir -function M.Animation.new(sourcedir, type, style, row, col) +function M.Animation.new(sourcedir, type, style, row, col, popup_width) local instance = setmetatable({}, M.Animation) instance.type = type instance.style = style @@ -22,6 +22,7 @@ function M.Animation.new(sourcedir, type, style, row, col) instance.frame_counter = 1 instance.actions = listdir(sourcedir) instance.frames = {} + instance.popup_width = popup_width instance.row, instance.col = row, col for _, action in pairs(instance.actions) do local current_actions = {} @@ -63,10 +64,29 @@ function M.Animation:next_frame() self.frame_counter = 1 end local image = self.frames[self.current_action][self.frame_counter] + M.Animation.set_next_col(self) image:display(self.row, self.col, self.bufnr, {}) self.current_image = image end +function M.Animation:set_next_col() + if self.current_action == "walk" then + if self.col < self.popup_width - 5 then + self.col = self.col + 1 + else + self.col = 0 + end + elseif self.current_action == "sneak" then + if self.col < self.popup_width - 8 then + if self.frame_counter % 2 == 0 then + self.col = self.col + 1 + end + else + self.col = 0 + end + end +end + function M.Animation:stop() if self.current_image then self.current_image:delete(0, { free = false }) diff --git a/lua/pets/pet.lua b/lua/pets/pet.lua index e5dca4d..424f138 100644 --- a/lua/pets/pet.lua +++ b/lua/pets/pet.lua @@ -31,8 +31,15 @@ function M.Pet.new(name, type, style, row, col) local wd = debug.getinfo(1).source:sub(2):match("(.*nvim/)") .. "media/" instance.sourcedir = wd .. type .. "/" .. style .. "/" - instance.animation = require("pets.animations").Animation.new(instance.sourcedir, type, style, row, col) instance.popup = require("nui.popup")(popup_opts) + instance.animation = require("pets.animations").Animation.new( + instance.sourcedir, + type, + style, + row, + col, + instance.popup.win_config.width + ) return instance end |