feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics
This commit is contained in:
@@ -1,87 +1,114 @@
|
||||
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
|
||||
var _initial_y_vel: 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
|
||||
# Cap the upward momentum so they don't fly up the wall,
|
||||
# but preserve some if they just jumped onto it.
|
||||
machine.player.velocity.y = minf(machine.player.velocity.y, 1.5)
|
||||
machine.on_ground = false
|
||||
|
||||
# Camera tilt
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.set_wall_tilt(machine.wall_side)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
# Clear camera tilt
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.clear_wall_tilt()
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
if elapsed > params.wall_run_duration:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# Stick to wall
|
||||
vel.y = min(vel.y, params.wall_run_vertical_speed)
|
||||
# ── Gradual gravity pull (starts light, increases over time) ──────────
|
||||
var gravity_factor := 0.1 + 0.9 * (elapsed / params.wall_run_duration)
|
||||
vel.y -= params.wall_run_gravity * gravity_factor * delta
|
||||
|
||||
# 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:
|
||||
# ── Move along wall tangent ───────────────────────────────────────────
|
||||
var wall_tangent := machine.wall_normal.cross(Vector3.UP).normalized()
|
||||
# Choose tangent direction based on player's movement direction
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
if hvel.dot(wall_tangent) < 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
|
||||
var effective_speed := machine.get_effective_speed(params.wall_run_speed)
|
||||
vel.x = wall_tangent.x * effective_speed
|
||||
vel.z = wall_tangent.z * effective_speed
|
||||
|
||||
# ── Look away to break wall run ───────────────────────────────────────
|
||||
var look_dir := -player.global_transform.basis.z
|
||||
if look_dir.dot(machine.wall_normal) > 0.4:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Push slightly toward wall to maintain contact ─────────────────────
|
||||
vel -= machine.wall_normal * 2.0
|
||||
|
||||
# ── Wall jump ─────────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed:
|
||||
var jump_vel: Vector3 = machine.wall_normal * params.wall_run_jump_off_normal
|
||||
var h_look := Vector3(look_dir.x, 0.0, look_dir.z).normalized()
|
||||
jump_vel += h_look * params.wall_run_jump_horizontal
|
||||
jump_vel.y = params.wall_run_auto_jump_speed
|
||||
player.velocity = jump_vel
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.register_chain_mechanic("wall_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Wall cling fall-through
|
||||
# ── Wall cling (no movement input) ────────────────────────────────────
|
||||
if machine.input_dir.length() < 0.1:
|
||||
machine.switch_to("wall_cling")
|
||||
return
|
||||
|
||||
# Lose wall contact
|
||||
var still_on_wall: bool = machine.wall_normal != Vector3.ZERO and detect_wall(machine.wall_normal).length_squared() > 0.0
|
||||
if not still_on_wall:
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Check still on wall ───────────────────────────────────────────────
|
||||
var still_on_wall := machine.detect_wall_horizontal()
|
||||
if still_on_wall == Vector3.ZERO:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.register_chain_mechanic("wall_run_off")
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Hit floor during wall run ─────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
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: Vector3 = origin + offset * side * params.wall_detect_distance + Vector3.UP * params.wall_ray_up_height
|
||||
var ray_end: Vector3 = 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
|
||||
func _get_camera_rig():
|
||||
if machine.player and machine.player.has_node("HeadPivot"):
|
||||
return machine.player.get_node("HeadPivot")
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user