diff options
| author | Giuseppe Gadola <giusgadola@gmail.com> | 2023-03-01 14:22:38 +0100 |
|---|---|---|
| committer | Giuseppe Gadola <giusgadola@gmail.com> | 2023-03-01 15:08:51 +0100 |
| commit | add39bd3057f1dfa3ad3c5cd5215aba3b1cf182d (patch) | |
| tree | 0951cfca91648db6211e9149ceaa43667e94b41d /lua | |
| parent | docs(help): auto generate docs (diff) | |
feat(pets): added dog
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/pets.lua | 2 | ||||
| -rw-r--r-- | lua/pets/animations.lua | 65 | ||||
| -rw-r--r-- | lua/pets/commands.lua | 56 | ||||
| -rw-r--r-- | lua/pets/pet.lua | 2 | ||||
| -rw-r--r-- | lua/pets/utils.lua | 11 |
5 files changed, 51 insertions, 85 deletions
diff --git a/lua/pets.lua b/lua/pets.lua index a2b3b81..6703a3d 100644 --- a/lua/pets.lua +++ b/lua/pets.lua @@ -9,7 +9,7 @@ M.options = { row = 1, -- the row (height) to display the pet at col = 0, -- the column to display the pet at (set to high numeber to have it stay stil at the right) speed_multiplier = 1, - default_pet = "cat", + default_pet = "dog", default_style = "brown", random = true, death_animation = true, diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua index b413578..37d0f27 100644 --- a/lua/pets/animations.lua +++ b/lua/pets/animations.lua @@ -10,7 +10,7 @@ local lines = {} local listdir = require("pets.utils").listdir local sleeping_animations = { "idle", "sit", "liedown" } -local function get_sleeping_animation() +local function get_sleeping_animation() -- TODO: return sleeping_animations[math.random(#sleeping_animations)] end @@ -18,6 +18,7 @@ end -- @param type,style type and style of the pet -- @param popup the popup where the pet is displayed -- @param user_opts table with user options +-- @param state table with the current plugin state (sleeping, paused, hidden) -- @return a new animation instance function M.Animation.new(sourcedir, type, style, popup, user_opts, state) local instance = setmetatable({}, M.Animation) @@ -45,17 +46,28 @@ function M.Animation.new(sourcedir, type, style, popup, user_opts, state) -- setup frames for _, action in pairs(instance.actions) do - local current_actions = {} + local current_frames = {} for _, file in pairs(listdir(sourcedir .. action)) do local image = require("hologram.image"):new(sourcedir .. action .. "/" .. file) - table.insert(current_actions, image) + table.insert(current_frames, image) end - instance.frames[action] = current_actions + instance.frames[action] = current_frames end + + -- setup next_actions + instance.next_actions = { + idle = { "sit", "liedown", "walk", "run", "pee" }, + liedown = { "sit", "idle" }, + pee = { "idle", "sit" }, + run = { "walk", "idle" }, + sit = { "walk", "liedown", "pee" }, + walk = { "run", "idle" }, + } + return instance end -function M.Animation:start_timer() +function M.Animation:start_timer(interval) if self.timer ~= nil then self:stop_timer() end @@ -68,7 +80,6 @@ function M.Animation:start_timer() end function M.Animation:stop_timer() - print() if self.timer == nil then return end @@ -83,6 +94,7 @@ function M.Animation:start() if self.timer ~= nil then -- reset timer self.timer = nil end + self.repetitions = 0 if self.state.sleeping then self.current_action = get_sleeping_animation() @@ -101,7 +113,6 @@ end -- @function called on every tick from the timer, go to the next frame function M.Animation:next_frame() - self.frame_counter = self.frame_counter + 1 -- pouplate the buffer with spaces to avoid image distortion if self.popup.bufnr == nil or not vim.api.nvim_buf_is_valid(self.popup.bufnr) then return @@ -112,15 +123,20 @@ function M.Animation:next_frame() else self.current_image:delete(0, { free = false }) end - if self.frame_counter > #self.frames[self.current_action] then -- true every 8 frames - M.Animation.set_next_action(self) + if self.frame_counter > #self.frames[self.current_action] then -- what to do when current frames end + self.repetitions = self.repetitions + 1 + -- if the animation lasted at least 8 frames go to the next one else repeat it (useful for 1 or 4 frames animations) + if self.repetitions * #self.frames[self.current_action] >= 8 then + M.Animation.set_next_action(self) + self.repetitions = 0 + end if self.dead then - M.Animation.stop_timer(self) + self:stop() return end - self.frame_counter = 1 + self.frame_counter = 1 -- reset the frame counter end - -- frames contains the images for every action + -- the frames table contains the images for every action local image = self.frames[self.current_action][self.frame_counter] M.Animation.set_next_col(self) local ok = pcall(image.display, image, self.row, self.col, self.popup.bufnr, {}) @@ -138,6 +154,7 @@ function M.Animation:next_frame() end end self.current_image = image + self.frame_counter = self.frame_counter + 1 end -- @function decide which action comes after the following @@ -151,15 +168,6 @@ function M.Animation:set_next_action() self.current_action = "die" return end - local next_actions = { - crouch = { "liedown", "sneak", "sit" }, - idle = { "idle_blink", "walk", "sit" }, - idle_blink = { "idle", "walk", "sit" }, - liedown = { "sneak", "crouch" }, - sit = { "idle", "idle_blink", "crouch", "liedown" }, - sneak = { "crouch", "walk", "liedown" }, - walk = { "idle", "idle_blink" }, - } if self.state.sleeping then -- If the animation isn't currently a sleeping animtion, put the pet in it, otherwise loop the animation if not vim.tbl_contains(sleeping_animations, self.current_action) then @@ -167,7 +175,8 @@ function M.Animation:set_next_action() end else if math.random() < 0.5 then - self.current_action = next_actions[self.current_action][math.random(#next_actions[self.current_action])] + self.current_action = + self.next_actions[self.current_action][math.random(#self.next_actions[self.current_action])] end end end @@ -180,11 +189,9 @@ function M.Animation:set_next_col() else self.col = M.base_col end - elseif self.current_action == "sneak" or self.current_action == "crouch" then + elseif self.current_action == "run" then if self.col < self.popup.win_config.width - 8 then - if self.frame_counter % 2 == 0 then - self.col = self.col + 1 - end + self.col = self.col + 2 else self.col = M.base_col end @@ -195,11 +202,7 @@ function M.Animation:stop() if self.current_image then self.current_image:delete(0, { free = false }) end - if self.timer then - self.timer:stop() - self.timer:close() - self.timer = nil - end + self:stop_timer() end function M.Animation:set_state(new_state) diff --git a/lua/pets/commands.lua b/lua/pets/commands.lua index 33edb14..336a816 100644 --- a/lua/pets/commands.lua +++ b/lua/pets/commands.lua @@ -2,11 +2,7 @@ local pets = require("pets") local utils = require("pets.utils") vim.api.nvim_create_user_command("PetsNew", function(input) - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) - --[[ local pet, style = pets.options.default_pet, pets.options.default_style + local pet, style = pets.options.default_pet, pets.options.default_style -- validate the pets and style options if not utils.validate_type_style(pet, style) then @@ -14,15 +10,15 @@ vim.api.nvim_create_user_command("PetsNew", function(input) end if pets.options.random then - local styles = utils.available_pets["cat"] - pet, style = "cat", styles[math.random(#styles)] + local styles = utils.available_pets["dog"] + pet, style = "dog", styles[math.random(#styles)] end - pets.create_pet(input.args, pet, style) ]] + pets.create_pet(input.args, pet, style) end, { nargs = 1 }) vim.api.nvim_create_user_command("PetsNewCustom", function(input) - --[[ local args = vim.split(input.args, " ", { trimempty = true }) + local args = vim.split(input.args, " ", { trimempty = true }) if #args ~= 3 then utils.warning("Inccorect number of arguments!\nUSAGE: PetsNewCustom {type} {style} {name}") return @@ -31,60 +27,32 @@ vim.api.nvim_create_user_command("PetsNewCustom", function(input) if not utils.validate_type_style(type, style) then return end - pets.create_pet(name, type, style) ]] - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.create_pet(name, type, style) end, { nargs = 1, complete = utils.complete_typestyle }) vim.api.nvim_create_user_command("PetsKillAll", function() - -- pets.kill_all() - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.kill_all() end, {}) vim.api.nvim_create_user_command("PetsKill", function(input) - -- pets.kill_pet(input.args) - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.kill_pet(input.args) end, { nargs = 1, complete = utils.complete_name, }) vim.api.nvim_create_user_command("PetsList", function() - -- pets.list() - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.list() end, {}) vim.api.nvim_create_user_command("PetsPauseToggle", function() - -- pets.toggle_pause() - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.toggle_pause() end, {}) vim.api.nvim_create_user_command("PetsHideToggle", function() - -- pets.toggle_hide() - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.toggle_hide() end, {}) vim.api.nvim_create_user_command("PetsSleepToggle", function() - -- pets.toggle_sleep() - vim.notify( - "Pets is currently searching for new assetes due to licensing issues with the previous ones,\nif you still have them please remove them or buy the asset pack. See #26 on github if you want to suggest anything.", - vim.log.levels.INFO - ) + pets.toggle_sleep() end, {}) diff --git a/lua/pets/pet.lua b/lua/pets/pet.lua index f767ed0..99f0053 100644 --- a/lua/pets/pet.lua +++ b/lua/pets/pet.lua @@ -47,7 +47,7 @@ function M.Pet:kill() self.animation:stop() self.popup:unmount() end - if self.death_animation then + if self.death_animation and self.animation.frames["die"] ~= nil then self.animation.dying = true else self.animation:stop() diff --git a/lua/pets/utils.lua b/lua/pets/utils.lua index dbf1191..68ea6b1 100644 --- a/lua/pets/utils.lua +++ b/lua/pets/utils.lua @@ -3,20 +3,15 @@ M.available_pets = {} -- list all files in a directory -- source: https://stackoverflow.com/a/11130774 -function M.listdir(directory, suppress) - suppress = suppress or false +function M.listdir(directory) local i, t, popen = 0, {}, io.popen local pfile - if suppress then - pfile = popen('/bin/ls -a "' .. directory .. '" 2>/dev/null') - else - pfile = popen('/bin/ls -a "' .. directory .. '"') - end + pfile = popen('/bin/ls -a "' .. directory .. '"') if pfile == nil then error("Error getting assets for specified pet") end for filename in pfile:lines() do - if filename ~= "." and filename ~= ".." then + if filename ~= "." and filename ~= ".." and not vim.endswith(filename, "sh") then i = i + 1 t[i] = filename end |