aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/input.lua27
-rw-r--r--src/objs/player.lua30
-rw-r--r--src/sprite.lua5
-rw-r--r--src/state_machine.lua2
4 files changed, 59 insertions, 5 deletions
diff --git a/src/input.lua b/src/input.lua
index b2951b2..cbc3f5b 100644
--- a/src/input.lua
+++ b/src/input.lua
@@ -25,10 +25,28 @@ function is_input_just_pressed(name)
code = trig[2]
if type == "mouse" then
- if mouseEvents[code] ~= nil then return true end
+ if mouseEvents[code] == "Pressed" then return true end
end
if type == "key" then
- if keyEvents[code] ~= nil then return true end
+ if keyEvents[code] == "Pressed" then return true end
+ end
+ end
+end
+
+function is_input_just_released(name)
+ local inp = inputs[name]
+ local type
+ local code
+
+ for _, trig in ipairs(inp) do
+ type = trig[1]
+ code = trig[2]
+
+ if type == "mouse" then
+ if mouseEvents[code] == "Released" then return true end
+ end
+ if type == "key" then
+ if keyEvents[code] == "Released" then return true end
end
end
end
@@ -52,7 +70,10 @@ function is_input_pressed(name)
end
function love.keypressed(key)
- keyEvents[key] = true
+ keyEvents[key] = "Pressed"
+end
+function love.keyreleased(key)
+ keyEvents[key] = "Released"
end
function love.mousepressed(_, _, btn)
diff --git a/src/objs/player.lua b/src/objs/player.lua
index 8ba664b..84e42f0 100644
--- a/src/objs/player.lua
+++ b/src/objs/player.lua
@@ -17,8 +17,28 @@ local function player_normal_state_update(player, state, dt)
player.vx = dlerp(player.vx, inputx * PLAYER_SPEED, PLAYER_ACCEL * dt)
player.vy = math.min(player.vy + GRAVITY * dt, TERMINAL_VELOCITY)
- if player.box:touching_down() and is_input_just_pressed("Jump") then
- player.vy = -PLAYER_JUMP_FORCE
+ if inputx ~= 0 then
+ player.sprite:set_tag("run")
+ elseif not player.box:touching_down() then
+ if player.vy < -100 then
+ player.sprite:set_tag("jump_up")
+ elseif player.vy > 100 then
+ player.sprite:set_tag("jump_down")
+ else
+ player.sprite:set_tag("jump_trans")
+ end
+ else
+ player.sprite:set_tag("idle")
+ end
+
+ if player.box:touching_down() then
+ if is_input_just_pressed("Jump") then
+ player.vy = -PLAYER_JUMP_FORCE
+ end
+ else
+ if is_input_just_released("Jump") and player.vy < -100 then
+ player.vy = -100
+ end
end
if state.dash_cooldown <= 0 and is_input_just_pressed("Dash") then
@@ -51,6 +71,12 @@ local function player_dash_state_exit(player, state, entering)
player.vx = player.vx / 2
end
+function player_update_sys(player, dt)
+ if player.vx ~= 0 then
+ player.scalex = sign(player.vx)
+ end
+end
+
function new_player(x, y)
local ent = new_entity()
diff --git a/src/sprite.lua b/src/sprite.lua
index 79a38bf..b790a9e 100644
--- a/src/sprite.lua
+++ b/src/sprite.lua
@@ -35,6 +35,11 @@ function Sprite.new(tex_name, opts)
return self
end
+function Sprite:set_tag(name)
+ assert(self.anim.tags[name], "Sprite does not have tag '" .. name .. "'.")
+ self.active_tag = name
+end
+
register_comp("Sprite", function(ent, tex_name, opts)
ent.sprite = Sprite.new(tex_name, opts)
end)
diff --git a/src/state_machine.lua b/src/state_machine.lua
index 7aac1bc..76eaf9b 100644
--- a/src/state_machine.lua
+++ b/src/state_machine.lua
@@ -22,3 +22,5 @@ function set_state(ent, to)
ent.state = to
try(ent.states[ent.state].enter, ent, ent.states[ent.state].data)
end
+
+