aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiuseppe Gadola <giusgadola@gmail.com>2023-03-06 20:44:08 +0100
committerGiuseppe Gadola <giusgadola@gmail.com>2023-03-07 11:40:15 +0100
commit1bef06d5fc3eebb3e825d810b4fc267f6c57014d (patch)
tree0271ff0248d9483dc8e8407e6f60d220f55239d7
parentfeat(pets): new pet: slime (diff)
refactor(animations): repeat current action with next_actions[1]
-rw-r--r--lua/pets/animations.lua25
-rw-r--r--lua/pets/pets/dog.lua16
-rw-r--r--lua/pets/pets/slime.lua17
3 files changed, 28 insertions, 30 deletions
diff --git a/lua/pets/animations.lua b/lua/pets/animations.lua
index eeba79f..e4462a2 100644
--- a/lua/pets/animations.lua
+++ b/lua/pets/animations.lua
@@ -53,6 +53,7 @@ function M.Animation.new(sourcedir, type, style, popup, user_opts, state)
instance.next_actions = pet.next_actions
instance.idle_actions = pet.idle_actions
instance.movements = pet.movements
+ instance.first_action = pet.first_action
return instance
end
@@ -89,17 +90,7 @@ function M.Animation:start()
if self.state.idle then
self.current_action = self.idle_actions[math.random(#self.idle_actions)]
else
- -- get available first action
- local first_action
- for k in pairs(self.frames) do
- if vim.startswith(k, "idle") then
- first_action = k
- break
- elseif first_action == nil then
- first_action = k
- end
- end
- self.current_action = self.current_action or first_action
+ self.current_action = self.current_action or self.first_action
end
if not self.state.paused and not self.state.hidden then
@@ -132,8 +123,8 @@ function M.Animation:next_frame()
end
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
+ -- if the animation has less than 2 frames loop it until it lasted 8 frames
+ if #self.frames[self.current_action] > 2 or self.repetitions * #self.frames[self.current_action] >= 8 then
M.Animation.set_next_action(self)
self.repetitions = 0
end
@@ -164,7 +155,7 @@ function M.Animation:next_frame()
self.frame_counter = self.frame_counter + 1
end
--- @function decide which action comes after the following
+-- @function decide which action comes after the current_action
function M.Animation:set_next_action()
if self.dying then
if self.current_action == "die" then
@@ -181,9 +172,11 @@ function M.Animation:set_next_action()
self.current_action = self.idle_actions[math.random(#self.idle_actions)]
end
else
- if math.random() < 0.5 then
+ if math.random() < 0.5 then -- 50% chance to keep doing the current action
self.current_action =
self.next_actions[self.current_action][math.random(#self.next_actions[self.current_action])]
+ else
+ self.current_action = self.next_actions[self.current_action][1]
end
end
end
@@ -204,7 +197,7 @@ function M.Animation:set_next_col()
end
elseif vim.tbl_contains(self.movements.right.slow, self.current_action) then
if self.col < self.popup.win_config.width - 8 then
- if #self.frames[self.current_action] < 2 then -- if there is only one frame in the current action
+ if #self.frames[self.current_action] <= 2 then -- if there is only one frame in the current action
if self.repetitions % 2 == 0 then -- then use repetitions as a counter
self.col = self.col + 1
end
diff --git a/lua/pets/pets/dog.lua b/lua/pets/pets/dog.lua
index a5890c2..26053d4 100644
--- a/lua/pets/pets/dog.lua
+++ b/lua/pets/pets/dog.lua
@@ -1,13 +1,13 @@
return {
next_actions = {
- idle = { "sit", "liedown", "walk", "run", "pee", "walk_left" },
- liedown = { "sit", "idle" },
- pee = { "idle", "sit", "walk" },
- run = { "walk", "idle", "run_left" },
- sit = { "walk", "liedown", "pee" },
- walk = { "run", "idle" },
- run_left = { "walk_left", "run" },
- walk_left = { "run_left", "idle" },
+ idle = { "idle", "sit", "liedown", "walk", "run", "pee", "walk_left" },
+ liedown = { "liedown", "sit", "idle" },
+ pee = { "pee", "idle", "sit", "walk" },
+ run = { "run", "walk", "idle", "run_left" },
+ sit = { "sit", "walk", "liedown", "pee" },
+ walk = { "walk", "run", "idle" },
+ run_left = { "run_left", "walk_left", "run" },
+ walk_left = { "walk_left", "run_left", "idle" },
},
idle_actions = { "idle", "sit", "liedown" },
movements = {
diff --git a/lua/pets/pets/slime.lua b/lua/pets/pets/slime.lua
index d972035..55b4dcf 100644
--- a/lua/pets/pets/slime.lua
+++ b/lua/pets/pets/slime.lua
@@ -1,17 +1,22 @@
return {
next_actions = {
- idle_blink = { "idle_wobble", "walk", "walk_left", "run", "run_left" },
- idle_wobble = { "idle_blink", "walk", "walk_left", "run", "run_left" },
- walk = { "idle_blink", "run" },
- run = { "walk", "run_left", "idle_wobble" },
- walk_left = { "run_left", "idle_blink", "idle_wobble" },
- run_left = { "walk_left", "run", "idle_wobble" },
+ idle_blink = { "idle_blink", "idle_wobble", "walk", "walk_left", "run", "run_left" },
+ idle_wobble = { "divide" },
+ walk = { "walk", "idle_blink", "run" },
+ run = { "run", "walk", "run_left", "idle_wobble" },
+ walk_left = { "walk_left", "run_left", "idle_blink", "idle_wobble" },
+ run_left = { "run_left", "walk_left", "run", "idle_wobble" },
+ --
+ divide = { "split_walk" },
+ split_walk = { "split_walk", "join" },
+ join = { "idle_wobble" },
},
idle_actions = { "idle_wobble", "idle_blink" },
movements = {
right = {
normal = { "walk" },
fast = { "run" },
+ slow = { "split_walk" },
},
left = {
fast = { "run_left" },