feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics
This commit is contained in:
@@ -7,7 +7,15 @@ var params: MovementParams:
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
pass
|
||||
machine.on_ground = true
|
||||
machine.current_jump_count = 0
|
||||
# Clear wall tilt on landing
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.clear_wall_tilt()
|
||||
|
||||
# Ensure capsule is correct when entering ground state
|
||||
_update_capsule_height()
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -15,96 +23,113 @@ func exit() -> void:
|
||||
|
||||
|
||||
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 player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# ── Determine target speed ────────────────────────────────────────────
|
||||
var wish_dir: Vector3 = machine.wish_dir_world
|
||||
var speed := params.walk_speed
|
||||
|
||||
if machine.input_sprint and wish_dir.length_squared() > 0.1:
|
||||
if machine.input_crouch:
|
||||
speed = params.crouch_speed
|
||||
elif machine.input_sprint and wish_dir.length_squared() > 0.01:
|
||||
speed = params.sprint_speed
|
||||
|
||||
var effective_speed := machine.get_effective_speed(speed)
|
||||
|
||||
# Horizontal movement
|
||||
# ── Horizontal movement (exponential interpolation for smoothness) ───
|
||||
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))
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-params.ground_acceleration * 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
|
||||
# ── Gravity (correct direction: downward) ─────────────────────────────
|
||||
if not player.is_on_floor():
|
||||
vel.y -= params.gravity * delta
|
||||
else:
|
||||
vel.y -= actual_gravity * delta
|
||||
# Snap to floor: small downward velocity keeps is_on_floor() stable
|
||||
vel.y = -0.5
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
player.velocity = vel
|
||||
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")
|
||||
_update_capsule_height()
|
||||
|
||||
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():
|
||||
# ── Update ground status ──────────────────────────────────────────────
|
||||
if 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")
|
||||
# ── Jump (coyote time + jump buffer) ──────────────────────────────────
|
||||
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0:
|
||||
player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.coyote_timer = 0.0
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Dash
|
||||
if machine.input_dash and machine.player.velocity.length() > 0.0:
|
||||
machine.switch_to("dash")
|
||||
# Jump buffer: player pressed jump just before landing
|
||||
if machine.on_ground and machine.jump_buffer_time > 0.0:
|
||||
player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.jump_buffer_time = 0.0
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Falling off ledge → air state ─────────────────────────────────────
|
||||
if not machine.on_ground and machine.coyote_timer <= 0.0:
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Slide (crouch while moving fast enough) ───────────────────────────
|
||||
if machine.input_crouch and machine.slide_cooldown_timer <= 0.0:
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
if hspeed > params.slide_min_speed:
|
||||
machine.switch_to("slide")
|
||||
return
|
||||
|
||||
# ── Wall run check ────────────────────────────────────────────────────
|
||||
var wall_n := machine.detect_wall_horizontal()
|
||||
if wall_n != Vector3.ZERO and machine.input_dir.length() > 0.1:
|
||||
if not player.is_on_floor():
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# ── Dash ──────────────────────────────────────────────────────────────
|
||||
if machine.input_dash:
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
if hspeed > 0.1 or machine.input_dir.length() > 0.1:
|
||||
machine.switch_to("dash")
|
||||
return
|
||||
|
||||
|
||||
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 dir: Vector3 = (hvel.cross(Vector3.UP) * offset_sign).normalized()
|
||||
var offset: Vector3 = dir * 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: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
if n.y < 0.3 and n.length_squared() > 0.0:
|
||||
return n.normalized()
|
||||
return Vector3.ZERO
|
||||
func _get_camera_rig():
|
||||
if machine.player and machine.player.has_node("HeadPivot"):
|
||||
return machine.player.get_node("HeadPivot")
|
||||
return null
|
||||
|
||||
|
||||
func _update_capsule_height() -> void:
|
||||
var shape = _get_capsule()
|
||||
if shape:
|
||||
if machine.input_crouch:
|
||||
shape.height = machine.original_capsule_height * 0.5
|
||||
else:
|
||||
shape.height = machine.original_capsule_height
|
||||
|
||||
|
||||
func _get_capsule() -> CapsuleShape3D:
|
||||
for child in machine.player.get_children():
|
||||
if child is CollisionShape3D and child.shape is CapsuleShape3D:
|
||||
return child.shape as CapsuleShape3D
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user