Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 77 additions & 65 deletions assets/scripts/player.script
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion game.project
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ input_method = HiddenInputField
scale_mode = stretch

[project]
title = My Project
title = Bangin

[graphics]
default_texture_min_filter = nearest
Expand Down