aboutsummaryrefslogtreecommitdiff
path: root/src/objs/player.lua
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-03-13 20:07:31 -0400
committeriamcheeseman <[email protected]>2026-03-13 20:07:31 -0400
commitcc679988babf47d5752c609ee90444db22d2d1fe (patch)
tree46d1e8abd7ae56535afdf4910f9ba41ad65d40d9 /src/objs/player.lua
parente09caf3cf41ac3e3ea7ec252a21a7ac5b7fbb52c (diff)
Player can dash
Diffstat (limited to 'src/objs/player.lua')
-rw-r--r--src/objs/player.lua70
1 files changed, 55 insertions, 15 deletions
diff --git a/src/objs/player.lua b/src/objs/player.lua
index 1d89f8a..2893321 100644
--- a/src/objs/player.lua
+++ b/src/objs/player.lua
@@ -1,22 +1,17 @@
-PLAYER_SPEED = 100
-PLAYER_ACCEL = 30
-PLAYER_JUMP_FORCE = 350
-
-register_comp("Body", function (ent, x, y, w, h, opts)
- ent.vx = 0
- ent.vy = 0
- ent.box = phys.Box.new(x, y, w, h, opts)
-end)
+local PLAYER_SPEED = 100
+local PLAYER_DASH_SPEED = 500
+local PLAYER_ACCEL = 30
+local PLAYER_JUMP_FORCE = 350
+local PLAYER_DASH_TIMER = 0.15
+local PLAYER_DASH_COOLDOWN = 0.5
register_comp("Player", TAGCOMP)
-function body_sys(ent, dt)
- ent.vx, ent.vy = ent.box:update(ent.vx, ent.vy, dt)
- ent.x = ent.box.x
- ent.y = ent.box.y
+local function player_normal_state_init(state)
+ state.dash_cooldown = 0
end
-function player_movement_sys(player, dt)
+local function player_normal_state_update(player, state, dt)
local inputx = bton(is_input_pressed("Right")) - bton(is_input_pressed("Left"))
player.vx = dlerp(player.vx, inputx * PLAYER_SPEED, PLAYER_ACCEL * dt)
@@ -25,21 +20,66 @@ function player_movement_sys(player, dt)
if player.box:touching_down() and is_input_just_pressed("Jump") then
player.vy = -PLAYER_JUMP_FORCE
end
+
+ if state.dash_cooldown <= 0 and is_input_just_pressed("Dash") then
+ set_state(player, "Dash")
+ state.dash_cooldown = PLAYER_DASH_COOLDOWN
+ end
+
+ state.dash_cooldown = state.dash_cooldown - dt
+end
+
+local function player_dash_state_enter(player, state)
+ local inputx = bton(is_input_pressed("Right")) - bton(is_input_pressed("Left"))
+ if inputx == 0 then
+ inputx = 1 --TODO: Change this to be the direction that the player is facing
+ end
+ player.vx = PLAYER_DASH_SPEED * inputx
+ player.vy = 0
+
+ state.dash_timer = PLAYER_DASH_TIMER
+end
+
+local function player_dash_state_update(player, state, dt)
+ state.dash_timer = state.dash_timer - dt
+ if state.dash_timer <= 0 then
+ set_state(player, "Normal")
+ end
+end
+
+local function player_dash_state_exit(player, state, entering)
+ player.vx = player.vx / 2
end
function new_player(x, y)
local ent = new_entity()
+
+ add_comp(ent, "Player")
+
add_comp(ent, "Body", x, y, 8, 14, {
offsetx = -4,
offsety = -6,
layers = {},
mask = {"hard"},
})
- add_comp(ent, "Player")
+
add_comp(ent, "Sprite", "res/img/player.ase", {
offsetx = 0.5,
offsety = 0.5,
})
+
+ add_comp(ent, "State_Machine", "Normal", {
+ Normal = {
+ init = player_normal_state_init,
+ update = player_normal_state_update,
+ },
+ Dash = {
+ enter = player_dash_state_enter,
+ update = player_dash_state_update,
+ exit = player_dash_state_exit,
+ },
+ })
+
return ent
end