feat(movement): add movement state machine with air/ground/wallrun/slide/dash/ground states

- MovementStateMachine: core state machine base
- MovementParams: export-rich tunable resource
- StateGround/StateAir: grounded & aeriel locomotion with coyote, buffered jump, bunny hop
- StateWallRun / StateWallCling: wall-based traversal with wall-jump off
- StateSlide: sprint-parallel slide with friction fallout
- StateDash: burst dash with momentum brake
- PlayerMovementController: entry point wiring to CharacterBody3D
- input_map.cfg: full action map reference file
- characters/player/player_base.tscn: placeholder scene
- movement/tests/test_fsm_runner.gd: 9-state unit test runner

Closes #1
This commit is contained in:
2026-06-02 22:36:00 -04:00
parent a4863a7b08
commit 202f67fa32
14 changed files with 993 additions and 6 deletions
+70
View File
@@ -0,0 +1,70 @@
extends Node
class_name StateAir
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
func enter(_data: Dictionary = {}) -> void:
pass
func exit() -> void:
pass
func update(delta: float) -> void:
var vel: Vector3 = machine.player.velocity
# Apply standard gravity
vel.y -= params.gravity * delta
# Input direction
var input_v := machine.input_dir
var wish_dir := Vector3(input_v.x, 0.0, -input_v.y)
if wish_dir.length_squared() > 1.0:
wish_dir = wish_dir.normalized()
var effective_speed := machine.get_effective_speed(params.max_air_speed)
var hvel := Vector3(vel.x, 0.0, vel.z)
var target_vel := wish_dir * effective_speed * params.air_control
hvel = hvel.lerp(Vector3(hvel.x + target_vel.x, 0.0, hvel.z + target_vel.z), 1.0 - exp(-params.air_acceleration * delta))
vel.x = hvel.x
vel.z = hvel.z
# Bunny hop: if jump pressed and on ground near-peak of arc → extra boost
if machine.input_jump_just_pressed and machine.on_ground:
vel.y = params.jump_velocity * params.bunny_hop_impulse
machine.player.velocity = vel
machine.register_chain_mechanic("bunny_hop")
machine.current_jump_count += 1
machine.on_ground = false
machine.switch_to("air")
return
# Double Jump
if machine.input_jump_just_pressed and not machine.on_ground and machine.current_jump_count < params.double_jump_max_count:
vel.y = params.double_jump_velocity
machine.player.velocity = vel
machine.current_jump_count += 1
machine.register_chain_mechanic("double_jump")
machine.switch_to("air")
return
machine.player.velocity = vel
machine.player.move_and_slide()
# Landing
if machine.player.is_on_floor():
machine.on_ground = true
machine.current_jump_count = 0
machine.switch_to("ground")
return
# Wall run transition
if not machine.wall_normal.is_zero_approx():
machine.switch_to("wall_run")
+45
View File
@@ -0,0 +1,45 @@
extends Node
class_name StateDash
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
var direction: Vector3 = Vector3.ZERO
var dash_cooldown_timer: float = 0.0
func enter(_data: Dictionary = {}) -> void:
elapsed = 0.0
dash_cooldown_timer = params.dash_cooldown
var input_v := machine.input_dir
var wish_dir := Vector3(input_v.x, 0.0, -input_v.y)
if wish_dir.length_squared() > 0.0:
direction = wish_dir.normalized()
else:
direction = -machine.player.global_transform.basis.z.normalized()
direction.y = 0.0
direction = direction.normalized()
direction = (machine.player.global_transform.basis * Vector4(direction.x, 0.0, direction.y, 0.0)).normalized()
machine.register_chain_mechanic("dash")
machine.player.velocity = direction * machine.get_effective_speed(params.dash_speed)
machine.on_ground = false
func exit() -> void:
pass
func update(delta: float) -> void:
elapsed += delta
dash_cooldown_timer -= delta
if elapsed > params.dash_duration:
machine.switch_to("air")
return
# Skip gravity, maintain dash velocity
machine.player.velocity = direction * machine.get_effective_speed(params.dash_speed)
machine.player.move_and_slide()
if machine.player.is_on_floor():
machine.on_ground = true
machine.switch_to("ground")
+109
View File
@@ -0,0 +1,109 @@
extends Node
class_name StateGround
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
func enter(_data: Dictionary = {}) -> void:
pass
func exit() -> void:
pass
func update(delta: float) -> void:
var vel: Vector3 = machine.player.velocity
# Input direction
var input_v := machine.input_dir
var wish_dir := Vector3(input_v.x, 0.0, -input_v.y)
if wish_dir.length_squared() > 1.0:
wish_dir = wish_dir.normalized()
var speed := params.walk_speed
if machine.input_sprint and wish_dir.length_squared() > 0.1:
speed = params.sprint_speed
var effective_speed := machine.get_effective_speed(speed)
# Horizontal movement
var hvel := Vector3(vel.x, 0.0, vel.z)
var target_vel := wish_dir * effective_speed
if wish_dir.length_squared() > 0.0:
var accel := params.ground_acceleration
hvel = hvel.lerp(target_vel, 1.0 - exp(-accel * delta))
else:
hvel = hvel.lerp(Vector3.ZERO, 1.0 - exp(-params.ground_deceleration * delta))
vel.x = hvel.x
vel.z = hvel.z
# Gravity (we apply tuned gravity so we don't double-apply from _velocity)
var actual_gravity := params.gravity
if vel.y < 0.0:
vel.y += actual_gravity * params.fall_multiplier * delta
elif vel.y > 0.0 and not machine.input_jump_pressed:
vel.y += actual_gravity * params.low_jump_multiplier * delta
else:
vel.y -= actual_gravity * delta
machine.player.velocity = vel
machine.player.move_and_slide()
var on_wall := detect_wall()
if on_wall and machine.input_dir.length() > 0.0:
machine.wall_normal = on_wall
machine.switch_to("wall_run")
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0:
machine.player.velocity.y = params.jump_velocity
machine.current_jump_count = 1
machine.on_ground = false
machine.register_chain_mechanic("jump")
machine.switch_to("air")
if machine.player.is_on_floor():
machine.on_ground = true
machine.last_ground_time = machine.get_process_delta_time()
machine.current_jump_count = 0
else:
machine.on_ground = false
if machine.input_jump_just_pressed and machine.jump_buffer_time > 0.0:
machine.player.velocity.y = params.jump_velocity
machine.current_jump_count = 1
machine.register_chain_mechanic("jump")
machine.switch_to("air")
elif vel.y < 0.0:
machine.switch_to("air")
# Slide
if machine.input_crouch and machine.input_sprint and machine.player.velocity.length() > params.slide_min_speed:
machine.switch_to("slide")
return
# Dash
if machine.input_dash and machine.player.velocity.length() > 0.0:
machine.switch_to("dash")
func detect_wall() -> Vector3:
var origin := machine.player.global_position
var hvel := Vector3(machine.player.velocity.x, 0.0, machine.player.velocity.z).normalized()
if hvel.length_squared() < 0.01:
hvel = Vector3.FORWARD
var space_state := machine.player.get_world_3d().direct_space_state
for offset_sign in [1.0, -1.0]:
var offset := hvel.cross(Vector3.UP) * offset_sign * machine.player.shape_owner_get_owner(0).shape_get_radius() if false else hvel.cross(Vector3.UP) * offset_sign * 0.4
var ray := PhysicsRayQueryParameters3D.create(origin + offset + Vector3.UP * params.wall_ray_up_height, origin + offset + Vector3.DOWN * params.wall_ray_down_height)
ray.exclude = [machine.player.get_rid()]
var hit := space_state.intersect_ray(ray)
if not hit.is_empty():
var n := hit.get("normal", Vector3.ZERO)
if n.y < 0.3 and n.length_squared() > 0.0:
return n.normalized()
return Vector3.ZERO
+60
View File
@@ -0,0 +1,60 @@
extends Node
class_name StateSlide
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
func enter(_data: Dictionary = {}) -> void:
elapsed = 0.0
machine.register_chain_mechanic("slide")
# Lower player capsule
var player_node = machine.player
var shape: CapsuleShape3D = player_node.shape_owner_get_shape(0)
if shape:
shape.height = shape.height * 0.5
func exit() -> void:
pass
func update(delta: float) -> void:
elapsed += delta
var vel: Vector3 = machine.player.velocity
var forward: Vector3 = -machine.player.global_transform.basis.z
if forward.length_squared() > 1.0:
forward = forward.normalized()
vel.x = forward.x * params.slide_speed * machine.get_effective_speed(1.0)
vel.z = forward.z * params.slide_speed * machine.get_effective_speed(1.0)
vel.x *= pow(params.slide_friction, delta)
vel.z *= pow(params.slide_friction, delta)
machine.player.velocity = vel
machine.player.move_and_slide()
if machine.player.is_on_floor():
machine.on_ground = true
var speed := Vector3(vel.x, 0.0, vel.z).length()
if speed < params.slide_min_speed or elapsed > params.slide_duration:
var shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0)
if shape:
shape.height = shape.height * 2.0
machine.register_chain_mechanic("slide_end")
machine.switch_to("ground")
return
else:
var shape2: CapsuleShape3D = machine.player.shape_owner_get_shape(0)
if shape2:
shape2.height = shape2.height * 2.0
machine.on_ground = false
machine.switch_to("air")
if not machine.input_crouch or not machine.input_sprint:
var shape3: CapsuleShape3D = machine.player.shape_owner_get_shape(0)
if shape3:
shape3.height = shape3.height * 2.0
machine.register_chain_mechanic("slide_cancel")
machine.switch_to("ground")
+52
View File
@@ -0,0 +1,52 @@
extends Node
class_name StateWallCling
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
func enter(_data: Dictionary = {}) -> void:
elapsed = 0.0
machine.register_chain_mechanic("wall_cling")
func exit() -> void:
pass
func update(delta: float) -> void:
elapsed += delta
var vel: Vector3 = machine.player.velocity
vel.x = params.wall_cling_horizontal_speed
vel.y = params.wall_cling_vertical_speed
vel.z = params.wall_cling_horizontal_speed
machine.player.velocity = vel
machine.player.move_and_slide()
# Jump off wall
if machine.input_jump_just_pressed:
var push_dir := machine.wall_normal * params.wall_run_jump_off_normal
var vel2 := Vector3(push_dir.x, params.jump_velocity, push_dir.z)
machine.player.velocity = vel2
machine.register_chain_mechanic("wall_cling_jump")
machine.switch_to("air")
return
# Re-enter wall run with input
if machine.input_dir.length() > 0.1:
machine.switch_to("wall_run")
return
# Fall
if not machine.wall_normal or machine.wall_normal.is_zero_approx():
machine.switch_to("air")
return
if machine.player.is_on_floor():
machine.on_ground = true
machine.wall_normal = Vector3.ZERO
machine.register_chain_mechanic("cling_land")
machine.switch_to("ground")
+88
View File
@@ -0,0 +1,88 @@
extends Node
class_name StateWallRun
const FLAG: int = 0 # unused
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
func enter(_data: Dictionary = {}) -> void:
elapsed = 0.0
machine.register_chain_mechanic("wall_run")
machine.player.velocity.y = params.wall_run_vertical_speed
func exit() -> void:
pass
func update(delta: float) -> void:
elapsed += delta
if elapsed > params.wall_run_duration:
machine.wall_normal = Vector3.ZERO
machine.switch_to("air")
return
var vel: Vector3 = machine.player.velocity
# Stick to wall
vel.y = min(vel.y, params.wall_run_vertical_speed)
# Move along wall: project velocity onto wall tangent
var wall_tangent := (machine.wall_normal.cross(Vector3.UP)).normalized()
if machine.input_dir.y < 0.0:
wall_tangent = -wall_tangent
var move_dir: Vector3 = wall_tangent * params.wall_run_speed
vel.x = move_dir.x
vel.z = move_dir.z
if machine.input_jump_just_pressed:
var jump_vel: Vector3 = vel + (machine.wall_normal * params.wall_run_jump_off_normal)
jump_vel.y = params.wall_run_auto_jump_speed
machine.player.velocity = jump_vel
machine.wall_normal = Vector3.ZERO
machine.register_chain_mechanic("wall_jump")
machine.switch_to("air")
return
# Wall cling fall-through
if machine.input_dir.length() < 0.1:
machine.switch_to("wall_cling")
return
# Lose wall contact
var still_on_wall := machine.wall_normal != Vector3.ZERO and
detect_wall(machine.wall_normal).length_squared() > 0.0
if not still_on_wall:
machine.wall_normal = Vector3.ZERO
machine.register_chain_mechanic("wall_run_off")
machine.switch_to("air")
machine.player.velocity = vel
machine.player.move_and_slide()
if machine.player.is_on_floor():
machine.on_ground = true
machine.wall_normal = Vector3.ZERO
machine.current_jump_count = 0
machine.register_chain_mechanic("wall_run_land")
machine.switch_to("ground")
func detect_wall(expected_normal: Vector3) -> Vector3:
var origin := machine.player.global_position
var offset := expected_normal.cross(Vector3.UP).normalized()
var space_state := machine.player.get_world_3d().direct_space_state
for side in [1.0, -1.0]:
var ray_origin := origin + offset * side * params.wall_detect_distance + Vector3.UP * params.wall_ray_up_height
var ray_end := origin + offset * side * params.wall_detect_distance - Vector3.DOWN * params.wall_ray_down_height
var ray := PhysicsRayQueryParameters3D.create(ray_origin, ray_end)
ray.exclude = [machine.player.get_rid()]
var hit := space_state.intersect_ray(ray)
if not hit.is_empty():
return hit.get("normal", Vector3.ZERO).normalized()
return Vector3.ZERO