From f9beefbba6ed45d0e9fbe0333ee02f74e80c18bd Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 29 Jan 2025 23:34:21 -0500 Subject: [PATCH 1/5] improvements: use velocity vectors to update position in update, normalize diagonal movement --- assets/scripts/player.script | 72 +++++++++++++++++++++++++++--------- 1 file changed, 54 insertions(+), 18 deletions(-) diff --git a/assets/scripts/player.script b/assets/scripts/player.script index 4a02d9b..0ec684e 100644 --- a/assets/scripts/player.script +++ b/assets/scripts/player.script @@ -1,37 +1,74 @@ function init(self) msg.post("#", "acquire_input_focus") _G.direction = "south" - + self.input = vmath.vector3() end -function on_input(self, action_id, action) - local position = go.get_position() +function update(self, dt) + if vmath.length_sqr(self.input) > 1 then + self.input = vmath.normalize(self.input) + end - local function player_move(compass_direction, x_or_y, position_change) - position[x_or_y] = position[x_or_y] + position_change + local function player_move(compass_direction) + local position = go.get_position() + local movement = self.input * 150 * dt _G.direction = compass_direction if not self.animating then sprite.play_flipbook("#sprite", "walk_" .. compass_direction) self.animating = true end - if action.released then - sprite.play_flipbook("#sprite", "idle_" .. compass_direction) - self.animating = false - end + go.set_position(position + movement) + self.input = vmath.vector3() + end + + if self.input.x > 0 then + player_move("east") + elseif self.input.x < 0 then + player_move("west") + elseif self.input.y > 0 then + player_move("north") + elseif self.input.y < 0 then + player_move("south") + end + +end + +function on_input(self, action_id, action) + + local function stop_animation(compass_direction) + sprite.play_flipbook("#sprite", "idle_" .. compass_direction) + self.animating = false end - if action_id == hash("Right") then - player_move("east", "x", 1.5) + -- Idle animation in direction + if action.released then + if action_id == hash("Right") then + stop_animation("east") + + elseif action_id == hash("Left") then + stop_animation("west") + + elseif action_id == hash("Up") then + stop_animation("north") + + elseif action_id == hash("Down") then + stop_animation("south") + end + + -- Add velocity in direction + elseif action_id == hash("Right") then + self.input.x = 1 - elseif action_id == hash("Left") then - player_move("west", "x", -1.5) + elseif action_id == hash("Left") then + self.input.x = -1 - elseif action_id == hash("Up") then - player_move("north", "y", 1.5) + elseif action_id == hash("Up") then + self.input.y = 1 - elseif action_id == hash("Down") then - player_move("south", "y", -1.5) + elseif action_id == hash("Down") then + self.input.y = -1 + -- Attack animations elseif _G.direction == "south" and action_id == hash("Attack") then if not self.animating then sprite.play_flipbook("#sprite", "attack_south") @@ -69,6 +106,5 @@ function on_input(self, action_id, action) self.animating = false end end - go.set_position(position) end From eb91d23e7830d55fb36c62c10831881a8bb31c0d Mon Sep 17 00:00:00 2001 From: Tyler Date: Wed, 29 Jan 2025 23:36:32 -0500 Subject: [PATCH 2/5] other: replaced tabs with spaces --- assets/scripts/player.script | 106 +++++++++++++++++------------------ 1 file changed, 53 insertions(+), 53 deletions(-) diff --git a/assets/scripts/player.script b/assets/scripts/player.script index 0ec684e..604a37e 100644 --- a/assets/scripts/player.script +++ b/assets/scripts/player.script @@ -1,6 +1,6 @@ function init(self) - msg.post("#", "acquire_input_focus") - _G.direction = "south" + msg.post("#", "acquire_input_focus") + _G.direction = "south" self.input = vmath.vector3() end @@ -11,24 +11,24 @@ function update(self, dt) local function player_move(compass_direction) local position = go.get_position() - local movement = self.input * 150 * dt - _G.direction = compass_direction - if not self.animating then - sprite.play_flipbook("#sprite", "walk_" .. compass_direction) - self.animating = true - end + local movement = self.input * 150 * dt + _G.direction = compass_direction + if not self.animating then + sprite.play_flipbook("#sprite", "walk_" .. compass_direction) + self.animating = true + end go.set_position(position + movement) self.input = vmath.vector3() end - if self.input.x > 0 then - player_move("east") - elseif self.input.x < 0 then - player_move("west") - elseif self.input.y > 0 then - player_move("north") - elseif self.input.y < 0 then - player_move("south") + if self.input.x > 0 then + player_move("east") + elseif self.input.x < 0 then + player_move("west") + elseif self.input.y > 0 then + player_move("north") + elseif self.input.y < 0 then + player_move("south") end end @@ -69,42 +69,42 @@ function on_input(self, action_id, action) self.input.y = -1 -- Attack animations - elseif _G.direction == "south" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_south") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_south") - self.animating = false - end - elseif _G.direction == "north" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_north") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_north") - self.animating = false - end - elseif _G.direction == "east" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_east") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_east") - self.animating = false - end - elseif _G.direction == "west" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_west") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_west") - self.animating = false - end - end + elseif _G.direction == "south" and action_id == hash("Attack") then + if not self.animating then + sprite.play_flipbook("#sprite", "attack_south") + self.animating = true + end + if action.released then + sprite.play_flipbook("#sprite", "idle_south") + self.animating = false + end + elseif _G.direction == "north" and action_id == hash("Attack") then + if not self.animating then + sprite.play_flipbook("#sprite", "attack_north") + self.animating = true + end + if action.released then + sprite.play_flipbook("#sprite", "idle_north") + self.animating = false + end + elseif _G.direction == "east" and action_id == hash("Attack") then + if not self.animating then + sprite.play_flipbook("#sprite", "attack_east") + self.animating = true + end + if action.released then + sprite.play_flipbook("#sprite", "idle_east") + self.animating = false + end + elseif _G.direction == "west" and action_id == hash("Attack") then + if not self.animating then + sprite.play_flipbook("#sprite", "attack_west") + self.animating = true + end + if action.released then + sprite.play_flipbook("#sprite", "idle_west") + self.animating = false + end + end end From 1328ae0aa59d9b2d0f8c686fe0265455e4348d55 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 30 Jan 2025 22:30:31 -0500 Subject: [PATCH 3/5] other: added project name --- game.project | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/game.project b/game.project index ae60bf3..89cec2e 100644 --- a/game.project +++ b/game.project @@ -16,7 +16,7 @@ input_method = HiddenInputField scale_mode = stretch [project] -title = My Project +title = Bangin [graphics] default_texture_min_filter = nearest From 8c624e5ae2ac78edea0bffc2151230baa9b8aa68 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 30 Jan 2025 22:31:40 -0500 Subject: [PATCH 4/5] improvements: reduced code, reset walking animation direction if it does not match self --- assets/scripts/player.script | 80 ++++++++++++------------------------ 1 file changed, 26 insertions(+), 54 deletions(-) diff --git a/assets/scripts/player.script b/assets/scripts/player.script index 604a37e..21defae 100644 --- a/assets/scripts/player.script +++ b/assets/scripts/player.script @@ -1,6 +1,6 @@ function init(self) msg.post("#", "acquire_input_focus") - _G.direction = "south" + self.direction = "south" self.input = vmath.vector3() end @@ -12,12 +12,12 @@ function update(self, dt) local function player_move(compass_direction) local position = go.get_position() local movement = self.input * 150 * dt - _G.direction = compass_direction - if not self.animating then + if not self.animating_walk or self.direction ~= compass_direction then sprite.play_flipbook("#sprite", "walk_" .. compass_direction) - self.animating = true + self.animating_walk = true end go.set_position(position + movement) + self.direction = compass_direction self.input = vmath.vector3() end @@ -35,25 +35,19 @@ end function on_input(self, action_id, action) - local function stop_animation(compass_direction) - sprite.play_flipbook("#sprite", "idle_" .. compass_direction) - self.animating = false + local function stop_walk_animation() + sprite.play_flipbook("#sprite", "idle_" .. self.direction) + self.animating_walk = false end -- Idle animation in direction - if action.released then - if action_id == hash("Right") then - stop_animation("east") - - elseif action_id == hash("Left") then - stop_animation("west") - - elseif action_id == hash("Up") then - stop_animation("north") - - elseif action_id == hash("Down") then - stop_animation("south") - end + if action.released + and (action_id == hash("Right") + or action_id == hash("Left") + or action_id == hash("Up") + or action_id == hash("Down") + ) then + stop_walk_animation() -- Add velocity in direction elseif action_id == hash("Right") then @@ -67,44 +61,22 @@ function on_input(self, action_id, action) elseif action_id == hash("Down") then self.input.y = -1 + end - -- Attack animations - elseif _G.direction == "south" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_south") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_south") - self.animating = false - end - elseif _G.direction == "north" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_north") - self.animating = true + local function attack_animation() + if not self.animating_attack then + sprite.play_flipbook("#sprite", "attack_" .. self.direction) + self.animating_attack = true end if action.released then - sprite.play_flipbook("#sprite", "idle_north") - self.animating = false - end - elseif _G.direction == "east" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_east") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_east") - self.animating = false - end - elseif _G.direction == "west" and action_id == hash("Attack") then - if not self.animating then - sprite.play_flipbook("#sprite", "attack_west") - self.animating = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_west") - self.animating = false + sprite.play_flipbook("#sprite", "idle_" .. self.direction) + self.animating_attack = false end end + -- Attack animations + if action_id == hash("Attack") and not action.released then + attack_animation() + end + end From 4899b139b0d79d9f7dd5b8786f29ed9c441fe121 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 30 Jan 2025 22:56:32 -0500 Subject: [PATCH 5/5] fix: restored attack cooldown --- assets/scripts/player.script | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/assets/scripts/player.script b/assets/scripts/player.script index 21defae..ad42809 100644 --- a/assets/scripts/player.script +++ b/assets/scripts/player.script @@ -64,18 +64,22 @@ function on_input(self, action_id, action) end local function attack_animation() - if not self.animating_attack then + if not self.attack_on_cooldown then sprite.play_flipbook("#sprite", "attack_" .. self.direction) - self.animating_attack = true - end - if action.released then - sprite.play_flipbook("#sprite", "idle_" .. self.direction) - self.animating_attack = false + self.attack_on_cooldown = true + + -- Wait for attack animation, 4 frames / 12 fps = .33 + timer.delay(.33, false, function () + sprite.play_flipbook("#sprite", "idle_" .. self.direction) + self.animating_walk = false -- Allow for walking animation to take over + self.attack_on_cooldown = false + end) + end end -- Attack animations - if action_id == hash("Attack") and not action.released then + if action_id == hash("Attack") then attack_animation() end