diff --git a/assets/scripts/player.script b/assets/scripts/player.script index 4a02d9b..ad42809 100644 --- a/assets/scripts/player.script +++ b/assets/scripts/player.script @@ -1,74 +1,86 @@ function init(self) - msg.post("#", "acquire_input_focus") - _G.direction = "south" + msg.post("#", "acquire_input_focus") + self.direction = "south" + self.input = vmath.vector3() +end + +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) + local position = go.get_position() + local movement = self.input * 150 * dt + if not self.animating_walk or self.direction ~= compass_direction then + sprite.play_flipbook("#sprite", "walk_" .. compass_direction) + self.animating_walk = true + end + go.set_position(position + movement) + self.direction = compass_direction + 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 position = go.get_position() - - local function player_move(compass_direction, x_or_y, position_change) - position[x_or_y] = position[x_or_y] + position_change - _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 + local function stop_walk_animation() + sprite.play_flipbook("#sprite", "idle_" .. self.direction) + self.animating_walk = false end - if action_id == hash("Right") then - player_move("east", "x", 1.5) - - elseif action_id == hash("Left") then - player_move("west", "x", -1.5) - - elseif action_id == hash("Up") then - player_move("north", "y", 1.5) - - elseif action_id == hash("Down") then - player_move("south", "y", -1.5) - - 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 - go.set_position(position) + -- Idle animation in direction + 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 + self.input.x = 1 + + elseif action_id == hash("Left") then + self.input.x = -1 + + elseif action_id == hash("Up") then + self.input.y = 1 + + elseif action_id == hash("Down") then + self.input.y = -1 + end + + local function attack_animation() + if not self.attack_on_cooldown then + sprite.play_flipbook("#sprite", "attack_" .. self.direction) + 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") then + attack_animation() + end end 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