feat: overhaul movement for flow — momentum-preserving states, smooth crouch, landing feel

- Quake-style air-strafe accelerate (real speed gain from strafing; external
  speed from dash/rockets never clamped)
- Wall run: keeps entry momentum (incl. upward carry), accelerates along the
  wall instead of hard-setting velocity, gravity fades in over the run
- Slide: slope physics (gravity projected along floor accelerates downhill),
  flat-ground decel instead of multiplicative friction, entry boost, direct
  slide->wallrun and slide->dash transitions
- Dash: cooldown 10s -> 2s, per-player (was a static shared across instances),
  momentum fully kept on exit, FOV punch event
- Grapple: taut pendulum with active rope control (W reels in, S pays out),
  release keeps swing energy
- Wall climb: carries upward momentum in, jump-off kick, shared vault helper
- Crouch capsule: single owner in the machine, smoothly lerped, ceiling check;
  camera eye height follows via crouch factor (no more head snapping)
- Landing: machine emits 'land' events scaled by impact; camera dip + pitched
  down thud; footsteps get pitch variation
- Camera rig: event-driven (land dip, dash FOV kick, vault pitch impulse),
  slide roll tilt
- Model: Jump vs Fall split by vertical velocity, Land one-shot on heavy
  landings, wall-run body lean (synced to remotes via synced_wall_side)

Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
Nicholas Butzke
2026-07-17 11:58:14 -04:00
co-authored by Claude Fable 5
parent c4a538a469
commit 0ba14a9469
16 changed files with 653 additions and 493 deletions
+52 -35
View File
@@ -3,10 +3,10 @@ class_name MovementParams
# ── Ground ────────────────────────────────────────────────────────────────────
@export var walk_speed: float = 11.0
@export var crouch_speed: float = 4.0
@export var crouch_speed: float = 4.5
@export var ground_friction: float = 10.0
@export var ground_acceleration: float = 50.0
@export var ground_deceleration: float = 40.0
@export var ground_acceleration: float = 60.0
@export var ground_deceleration: float = 45.0
# ── Jump / Air ────────────────────────────────────────────────────────────────
@export var jump_velocity: float = 8.5
@@ -14,70 +14,84 @@ class_name MovementParams
@export var jump_buffer: float = 0.12
@export var air_control: float = 0.35
@export var air_acceleration: float = 30.0
@export var max_air_speed: float = 16.0
@export var max_air_speed: float = 18.0
@export var gravity: float = 22.0
@export var fall_multiplier: float = 2.2
@export var fall_multiplier: float = 2.0
@export var low_jump_multiplier: float = 1.6
# ── Air Strafe (Quake-style) ──────────────────────────────────────────────────
@export var air_strafe_accel: float = 80.0
@export var air_strafe_max_gain: float = 1.0
# Classic accelerate: only the velocity component along wish_dir is capped, so
# turning while strafing genuinely gains speed (up to max_air_speed overall).
@export var air_strafe_accel: float = 55.0
@export var air_wish_speed_cap: float = 1.2 # per-tick projection cap (m/s), quake-style
# ── Bunny Hop ─────────────────────────────────────────────────────────────────
@export var bunny_hop_impulse: float = 1.15
@export var bunny_hop_speed_gain: float = 0.5
@export var bunny_hop_speed_cap: float = 16.0
@export var bunny_hop_impulse: float = 1.1
@export var bunny_hop_speed_gain: float = 0.7
@export var bunny_hop_speed_cap: float = 20.0
# ── Slide ─────────────────────────────────────────────────────────────────────
@export var slide_speed: float = 14.0
@export var slide_friction: float = 0.96
@export var slide_min_speed: float = 5.0
@export var slide_duration: float = 0.8
@export var slide_cooldown: float = 0.4 # Prevents rapid slide re-entry
@export var slide_jump_speed_boost: float = 1.5 # Extra m/s added when jumping out of slide
@export var slide_boost: float = 3.0 # flat entry boost when grounded and fast
@export var slide_friction_flat: float = 2.2 # m/s^2 decel on flat ground (low = long slides)
@export var slide_slope_accel: float = 16.0 # downhill acceleration from gravity projection
@export var slide_min_speed: float = 4.0
@export var slide_cooldown: float = 0.35 # prevents rapid slide re-entry
@export var slide_steer_rate: float = 2.5 # how fast slide direction bends toward input
@export var slide_jump_speed_boost: float = 1.5 # extra m/s added when jumping out of slide
# ── Wall Run ──────────────────────────────────────────────────────────────────
@export var wall_run_speed: float = 12.0
@export var wall_run_vertical_speed: float = 4.0
@export var wall_run_duration: float = 1.2
@export var wall_run_gravity: float = 6.0
@export var wall_run_speed: float = 13.0
@export var wall_run_accel: float = 18.0 # accelerate toward run speed (no hard set)
@export var wall_run_duration: float = 1.8
@export var wall_run_gravity: float = 5.0
@export var wall_run_entry_max_up: float = 3.5 # keep this much upward momentum on attach
@export var wall_run_auto_jump_speed: float = 10.0
@export var wall_run_jump_horizontal: float = 8.0
@export var wall_run_jump_off_normal: float = 5.0
@export var wall_run_jump_off_normal: float = 6.0
@export var wall_angle_threshold: float = 70.0
@export var wall_detect_distance: float = 0.7
@export var wall_detect_distance: float = 0.8
@export var wall_ray_up_height: float = 0.6
@export var wall_ray_down_height: float = 0.0
# ── Wall Climb ────────────────────────────────────────────────────────────────
@export var wall_climb_speed: float = 6.0
@export var wall_climb_duration: float = 3.0
@export var wall_climb_speed: float = 6.5
@export var wall_climb_duration: float = 2.2
@export var wall_climb_max_angle: float = 75.0
@export var wall_climb_vault_forward: float = 16.0
@export var wall_climb_vault_up: float = 14.0
@export var wall_climb_vault_forward: float = 14.0
@export var wall_climb_vault_up: float = 12.0
@export var wall_climb_vault_height_check: float = 1.0
# ── Jumping ───────────────────────────────────────────────────────────────────
@export var jump_cooldown: float = 0.27
@export var jump_cooldown: float = 0.12
# ── Wall Cling ────────────────────────────────────────────────────────────────
@export var wall_cling_slide_speed: float = -1.5
@export var wall_cling_stamina_drain: float = 2.0
@export var wall_cling_max_stamina: float = 2.0
@export var wall_cling_stamina_drain: float = 1.0
@export var wall_cling_max_stamina: float = 2.5
# ── Grapple ───────────────────────────────────────────────────────────────────
@export var grapple_range: float = 30.0
@export var grapple_pull_force: float = 25.0
@export var grapple_range: float = 35.0
@export var grapple_pull_force: float = 18.0 # constant reel toward the hook
@export var grapple_reel_force: float = 22.0 # extra reel while holding forward
@export var grapple_jump_boost: float = 12.0
@export var grapple_spring_strength: float = 10.0
@export var grapple_air_control: float = 20.0
@export var grapple_spring_strength: float = 14.0
@export var grapple_air_control: float = 24.0
@export var grapple_shoot_speed: float = 70.0 # hook travel speed (m/s)
# ── Dash ──────────────────────────────────────────────────────────────────────
@export var dash_speed: float = 10.0
@export var dash_duration: float = 0.25
@export var dash_cooldown: float = 10.0
@export var dash_speed: float = 13.0
@export var dash_duration: float = 0.18
@export var dash_cooldown: float = 2.0
@export var dash_invulnerability_time: float = 0.1
# ── Landing feel ──────────────────────────────────────────────────────────────
@export var land_soft_speed: float = 8.0 # fall speed where landing feedback starts
@export var land_heavy_speed: float = 18.0 # fall speed considered a heavy landing
# ── Crouch ────────────────────────────────────────────────────────────────────
@export var crouch_transition_speed: float = 12.0 # capsule/camera height lerp rate
# ── Rocket Jump ───────────────────────────────────────────────────────────────
@export var rocket_jump_self_damage: float = 15.0
@export var rocket_jump_up_impulse: float = 20.0
@@ -106,3 +120,6 @@ class_name MovementParams
@export var head_bob_amplitude: float = 0.04
@export var wall_run_tilt_angle: float = 12.0
@export var wall_run_tilt_speed: float = 8.0
@export var slide_tilt_angle: float = 4.0
@export var land_dip_scale: float = 0.012 # camera dip per m/s of landing speed
@export var land_dip_max: float = 0.25 # max camera dip on heavy landings
+116 -46
View File
@@ -3,6 +3,15 @@ class_name MovementStateMachine
## Generic state machine for player movement.
## Each state is a Node child; the machine switches between them.
##
## The machine owns cross-state concerns so individual states stay small:
## - input snapshot (written by the controller each tick)
## - jump buffering / coyote time / jump cooldown
## - crouch capsule resizing (smoothly lerped, single owner)
## - dash cooldown (per-instance, not shared between players)
## - grapple hook raycast + travel
## - wall detection helpers
## - chain-bonus bookkeeping
signal state_changed(from_state: String, to_state: String)
signal movement_event(event_name: String, data: Dictionary)
@@ -19,6 +28,7 @@ var wish_dir_world: Vector3 = Vector3.ZERO
var input_jump_pressed: bool = false
var input_jump_just_pressed: bool = false
var input_crouch: bool = false
var input_sprint: bool = false
var input_dash: bool = false
var input_grapple: bool = false
var input_grapple_just_pressed: bool = false
@@ -38,6 +48,7 @@ var last_ground_time: float = 0.0
var jump_buffer_time: float = 0.0
var coyote_timer: float = 0.0
var jump_cooldown_timer: float = 0.0
var dash_cooldown_timer: float = 0.0
var current_jump_count: int = 0
var chain_timer: float = 0.0
var chain_count: int = 0
@@ -48,6 +59,9 @@ var wall_cooldown_timer: float = 0.0 # Prevents instant re-attachment after wa
var is_crouched: bool = false
var can_wall_climb: bool = true
# Smooth capsule crouch: the machine is the single owner of capsule height.
var _capsule_current_height: float = 0.0
func _ready() -> void:
# Ensure grapple state is injected
@@ -86,10 +100,12 @@ func _physics_process(delta: float) -> void:
jump_buffer_time = params.jump_buffer
else:
jump_buffer_time = maxf(jump_buffer_time - delta, 0.0)
# Update jump cooldown
if jump_cooldown_timer > 0.0:
jump_cooldown_timer = maxf(jump_cooldown_timer - delta, 0.0)
# Update timers
jump_cooldown_timer = maxf(jump_cooldown_timer - delta, 0.0)
dash_cooldown_timer = maxf(dash_cooldown_timer - delta, 0.0)
slide_cooldown_timer = maxf(slide_cooldown_timer - delta, 0.0)
wall_cooldown_timer = maxf(wall_cooldown_timer - delta, 0.0)
# Update chain timer
if chain_timer > 0.0 and on_ground:
@@ -112,21 +128,9 @@ func _physics_process(delta: float) -> void:
movement_event.emit("grapple_latch", {})
switch_to("grapple")
# Manage global crouch state
var want_crouch = input_crouch
if current_state == "slide":
want_crouch = true
if want_crouch != is_crouched:
is_crouched = want_crouch
_apply_crouch(is_crouched)
# Update slide cooldown
if slide_cooldown_timer > 0.0:
slide_cooldown_timer = maxf(slide_cooldown_timer - delta, 0.0)
# Update wall cooldown
if wall_cooldown_timer > 0.0:
wall_cooldown_timer = maxf(wall_cooldown_timer - delta, 0.0)
# Manage global crouch state (slide keeps the capsule low)
is_crouched = input_crouch or current_state == "slide"
_update_crouch_capsule(delta)
if current_state.is_empty():
return
@@ -157,6 +161,33 @@ func switch_to(new_state_name: String, data: Dictionary = {}) -> void:
state_changed.emit(prev, new_state_name)
## Called by states when the player touches down. Emits the landing event so
## camera/audio/animation can react proportionally to impact speed.
func notify_landed(fall_speed: float) -> void:
on_ground = true
current_jump_count = 0
can_wall_climb = true
if fall_speed > params.land_soft_speed:
movement_event.emit("land", {
"fall_speed": fall_speed,
"heavy": fall_speed >= params.land_heavy_speed,
})
## Shared jump executed from ground-like states. Keeps horizontal momentum.
func do_jump(extra_boost: float = 0.0) -> void:
player.velocity.y = params.jump_velocity + extra_boost
current_jump_count = 1
on_ground = false
coyote_timer = 0.0
jump_buffer_time = 0.0
jump_cooldown_timer = params.jump_cooldown
register_chain_mechanic("jump")
movement_event.emit("jump", {})
if player.jump_player:
player.jump_player.play()
func _try_start_grapple() -> void:
if not player or not player.camera:
return
@@ -170,9 +201,7 @@ func _try_start_grapple() -> void:
if not hit.is_empty():
grapple_point = hit.position
grapple_length = origin.distance_to(grapple_point)
# Travel at 45 m/s
grapple_travel_time = grapple_length / 45
grapple_travel_time = grapple_length / params.grapple_shoot_speed
grapple_shoot_time = 0.0
is_grapple_shooting = true
movement_event.emit("grapple_shoot", {})
@@ -183,12 +212,12 @@ func register_chain_mechanic(_mechanic_name: String) -> void:
chain_count += 1
else:
chain_count = 1
# Shrink the chain window as the chain gets longer to make it increasingly punishing
# Drops by 0.05 seconds per successful chain, down to a minimum of 0.4 seconds
var current_window = maxf(0.4, params.chain_window - (float(chain_count) * 0.05))
chain_timer = current_window
var raw_bonus = float(chain_count) * params.chain_bonus_per_success
if raw_bonus <= params.chain_bonus_cap:
current_chain_bonus = raw_bonus
@@ -196,7 +225,7 @@ func register_chain_mechanic(_mechanic_name: String) -> void:
# Soft cap: diminishing returns past the cap
var over_bonus = raw_bonus - params.chain_bonus_cap
current_chain_bonus = params.chain_bonus_cap + (over_bonus / (1.0 + over_bonus * 3.0))
movement_event.emit("chain_updated", {
"count": chain_count,
"bonus": current_chain_bonus
@@ -262,17 +291,17 @@ func detect_wall_horizontal() -> Vector3:
func detect_wall_forward() -> Dictionary:
if not player:
return {"hit": false, "normal": Vector3.ZERO, "is_short": false}
var move_dir := -player.global_transform.basis.z
move_dir.y = 0.0
if move_dir.length_squared() > 0.01:
move_dir = move_dir.normalized()
else:
return {"hit": false, "normal": Vector3.ZERO, "is_short": false}
var space_state := player.get_world_3d().direct_space_state
var dist := 1.2 # Increased to reliably detect walls at an angle
# Lower ray (feet/knees)
var origin_low := player.global_position + Vector3.UP * params.wall_ray_down_height
var ray_low := PhysicsRayQueryParameters3D.create(origin_low, origin_low + move_dir * dist)
@@ -294,22 +323,44 @@ func detect_wall_forward() -> Dictionary:
var collider = best_hit.get("collider")
if collider is PlayerMovementController or collider is CharacterBody3D or collider.has_method("take_damage"):
return {"hit": false, "normal": Vector3.ZERO, "is_short": false}
var normal: Vector3 = best_hit.get("normal", Vector3.ZERO)
if abs(normal.y) >= 0.3:
return {"hit": false, "normal": Vector3.ZERO, "is_short": false} # Not a vertical wall
# Check if wall is short (ledge check) using a top ray
var origin_high := player.global_position + Vector3.UP * (params.wall_ray_up_height + params.wall_climb_vault_height_check)
var ray_high := PhysicsRayQueryParameters3D.create(origin_high, origin_high + move_dir * dist)
ray_high.exclude = [player.get_rid()]
var hit_high := space_state.intersect_ray(ray_high)
return {"hit": true, "normal": normal.normalized(), "is_short": hit_high.is_empty()}
func _apply_crouch(crouched: bool) -> void:
var shape_node = null
## Perform an instant vault over a short wall: forward+up impulse, camera kick
## via the rig, cooldown so we don't immediately re-detect the same wall.
func do_vault() -> void:
var look_dir := -player.global_transform.basis.z
var h_look := Vector3(look_dir.x, 0.0, look_dir.z)
if h_look.length_squared() > 0.01:
h_look = h_look.normalized()
player.velocity = h_look * params.wall_climb_vault_forward
player.velocity.y = params.wall_climb_vault_up
if player.vault_player:
player.vault_player.play()
movement_event.emit("vault", {})
var rig = player.get_node_or_null("HeadPivot")
if rig and rig.has_method("add_pitch_impulse"):
rig.add_pitch_impulse(6.0)
wall_cooldown_timer = 0.3
register_chain_mechanic("vault")
## Smoothly lerp the collision capsule toward the crouch/stand height and keep
## the capsule's bottom anchored so shrinking doesn't lift the player off the
## floor. Standing back up is blocked while there's no headroom.
func _update_crouch_capsule(delta: float) -> void:
var shape_node: CollisionShape3D = null
for child in player.get_children():
if child is CollisionShape3D and child.shape is CapsuleShape3D:
shape_node = child
@@ -317,19 +368,38 @@ func _apply_crouch(crouched: bool) -> void:
if not shape_node:
return
var shape = shape_node.shape as CapsuleShape3D
var head = player.get_node_or_null("HeadPivot")
if _capsule_current_height <= 0.0:
_capsule_current_height = shape.height
var target_height := original_capsule_height * (0.5 if is_crouched else 1.0)
# Don't stand up into a ceiling
if not is_crouched and target_height > _capsule_current_height + 0.01:
var space_state := player.get_world_3d().direct_space_state
var from := player.global_position + Vector3.UP * (_capsule_current_height * 0.5)
var to := player.global_position + Vector3.UP * (original_capsule_height * 0.55)
var ray := PhysicsRayQueryParameters3D.create(from, to)
ray.exclude = [player.get_rid()]
if not space_state.intersect_ray(ray).is_empty():
target_height = _capsule_current_height # hold until clear
var t := 1.0 - exp(-params.crouch_transition_speed * delta)
_capsule_current_height = lerpf(_capsule_current_height, target_height, t)
if absf(_capsule_current_height - target_height) < 0.005:
_capsule_current_height = target_height
shape.height = _capsule_current_height
# Keep feet planted: offset the shape down by half the height loss.
shape_node.position.y = -(original_capsule_height - _capsule_current_height) * 0.5
## 0 (standing) → 1 (fully crouched); used by the camera rig for eye height.
func get_crouch_factor() -> float:
if original_capsule_height <= 0.0 or _capsule_current_height <= 0.0:
return 0.0
return clampf((original_capsule_height - _capsule_current_height)
/ (original_capsule_height * 0.5), 0.0, 1.0)
if crouched:
shape.height = original_capsule_height * 0.5
shape_node.position.y = -original_capsule_height * 0.25
if head:
head.position.y = 0.7 - (original_capsule_height * 0.5)
else:
shape.height = original_capsule_height
shape_node.position.y = 0.0
if head:
head.position.y = 0.7
func get_dash_cooldown_remaining() -> float:
var now := Time.get_ticks_msec() / 1000.0
return maxf(0.0, params.dash_cooldown - (now - StateDash._last_dash_time))
return dash_cooldown_timer
+14
View File
@@ -74,6 +74,7 @@ var synced_is_crouching: bool = false
var synced_position: Vector3 = Vector3.ZERO
var synced_velocity: Vector3 = Vector3.ZERO
var synced_is_ads: bool = false
var synced_wall_side: float = 0.0 # -1 wall left, +1 wall right (wall-run lean)
@export var synced_skin_id: String = ""
@export var synced_weapon_path: String = ""
@@ -876,11 +877,14 @@ func _physics_process(_delta: float) -> void:
if visual.has_method("set_locomotion"):
var d := _local_move_dir()
visual.set_locomotion(d.x, d.y, 1.0 if synced_is_ads else 0.0)
if visual.has_method("set_wall_side"):
visual.set_wall_side(sm.wall_side)
# Publish state for remote peers
synced_movement_state = sm.current_state
synced_movement_speed = Vector2(velocity.x, velocity.z).length()
synced_is_crouching = sm.input_crouch
synced_wall_side = sm.wall_side
synced_position = position
synced_velocity = velocity
@@ -914,6 +918,14 @@ func _on_movement_event(ev: String, data: Dictionary) -> void:
grapple_shoot_player.play()
elif ev == "grapple_latch":
grapple_latch_player.play()
elif ev == "land":
# Landing thud: reuse the footstep sample, pitched down and louder
# with impact. Ground state resets pitch/volume before each step.
if footstep_player:
var heavy: bool = data.get("heavy", false)
footstep_player.pitch_scale = 0.55 if heavy else 0.7
footstep_player.volume_db = 2.0 if heavy else -2.0
footstep_player.play()
func _process(delta: float) -> void:
# Remote players: interpolate toward the owner's synced transform and
@@ -946,6 +958,8 @@ func _process(delta: float) -> void:
if visual.has_method("set_locomotion"):
var d := _local_move_dir()
visual.set_locomotion(d.x, d.y, 1.0 if synced_is_ads else 0.0)
if visual.has_method("set_wall_side"):
visual.set_wall_side(synced_wall_side)
# Check for weapon changes
if synced_weapon_path != "" and synced_weapon_path != visual.get_meta("current_weapon_path", ""):
visual.set_weapon(synced_weapon_path)
+19 -42
View File
@@ -28,27 +28,23 @@ func update(delta: float) -> void:
grav *= params.low_jump_multiplier
vel.y -= grav * delta
# ── Air control ───────────────────────────────────────────────────────
# ── Air control: Quake-style accelerate ───────────────────────────────
# Only the velocity component ALONG wish_dir is capped, so turning while
# strafing genuinely gains speed. Total input-driven speed is bounded by
# max_air_speed; externally-gained speed (dash, rockets) is never clamped.
var wish_dir: Vector3 = machine.wish_dir_world
var hvel := Vector3(vel.x, 0.0, vel.z)
if wish_dir.length_squared() > 0.01:
wish_dir = wish_dir.normalized()
var current_speed = hvel.length()
# Add a strong force in the wish direction
var air_accel = params.air_strafe_accel * delta
var new_hvel = hvel + wish_dir * air_accel
# Limit the speed so we don't gain speed purely from air control.
# We only allow air control to change our direction (strafing)
# or to accelerate us up to our walk speed if we started slow.
var max_allowed_speed = maxf(current_speed, params.walk_speed)
if new_hvel.length() > max_allowed_speed:
new_hvel = new_hvel.normalized() * max_allowed_speed
hvel = new_hvel
var wish_speed: float = minf(machine.get_effective_speed(params.walk_speed), params.max_air_speed)
var current_along := hvel.dot(wish_dir)
var add_speed := wish_speed - current_along
if add_speed > 0.0:
var accel_speed: float = minf(params.air_strafe_accel * delta, add_speed)
# Quake's trick: cap per-tick projection so sharp turns give the gain
accel_speed = minf(accel_speed, params.air_wish_speed_cap)
hvel += wish_dir * accel_speed
else:
# No input: slight air drag (very subtle)
hvel *= (1.0 - 0.5 * delta)
@@ -61,8 +57,8 @@ func update(delta: float) -> void:
# ── Landing ───────────────────────────────────────────────────────────
if player.is_on_floor():
machine.on_ground = true
machine.current_jump_count = 0
var fall_speed: float = maxf(-vel.y, 0.0)
machine.notify_landed(fall_speed)
# Bunny hop: if jump was buffered or pressed on landing frame
if (machine.input_jump_pressed or machine.jump_buffer_time > 0.0) and machine.jump_cooldown_timer <= 0.0:
@@ -105,44 +101,25 @@ func update(delta: float) -> void:
var fwd_wall = machine.detect_wall_forward()
if fwd_wall.hit:
if fwd_wall.is_short:
# Instant Vault
var look_dir := -player.global_transform.basis.z
var h_look := Vector3(look_dir.x, 0.0, look_dir.z)
if h_look.length_squared() > 0.01:
h_look = h_look.normalized()
player.velocity = h_look * params.wall_climb_vault_forward
player.velocity.y = params.wall_climb_vault_up
if player.vault_player:
player.vault_player.play()
# Quick camera animation (tilt up and forward bob)
var camera = player.camera
if camera:
var tween = player.create_tween()
tween.tween_property(camera, "rotation_degrees:x", camera.rotation_degrees.x + 10.0, 0.15).set_trans(Tween.TRANS_SINE)
tween.parallel().tween_property(camera, "v_offset", -0.2, 0.15).set_trans(Tween.TRANS_SINE)
tween.chain().tween_property(camera, "rotation_degrees:x", camera.rotation_degrees.x, 0.2).set_trans(Tween.TRANS_SINE)
tween.parallel().tween_property(camera, "v_offset", 0.0, 0.2).set_trans(Tween.TRANS_SINE)
machine.wall_cooldown_timer = 0.3
machine.do_vault()
return
else:
# Check if moving directly into wall and looking generally towards it
var look_dir := -player.global_transform.basis.z
var looking_towards := look_dir.dot(-fwd_wall.normal) > 0.0
var moving_towards := wish_dir.dot(-fwd_wall.normal) > cos(deg_to_rad(params.wall_climb_max_angle))
if looking_towards and moving_towards and machine.can_wall_climb:
machine.wall_normal = fwd_wall.normal
machine.switch_to("wall_climb")
return
# Fallback to wall run
var wall_n := machine.detect_wall_horizontal()
if wall_n != Vector3.ZERO:
var w_look_dir := -player.global_transform.basis.z
var h_look := Vector3(w_look_dir.x, 0.0, w_look_dir.z).normalized()
# Require looking mostly along the wall (angle > 41 degrees from normal)
# and inputting mostly along the wall (angle > 31 degrees from normal, allows W+A/D diagonally into wall)
if abs(h_look.dot(wall_n)) < 0.75 and abs(wish_dir.dot(wall_n)) < 0.85:
@@ -150,6 +127,6 @@ func update(delta: float) -> void:
return
# ── Dash ──────────────────────────────────────────────────────────────
if machine.input_dash:
if machine.input_dash and machine.dash_cooldown_timer <= 0.0:
machine.switch_to("dash")
return
+23 -35
View File
@@ -1,28 +1,22 @@
extends Node
class_name StateDash
## Short burst dash. Cooldown lives on the machine (per-player instance) and is
## checked by the states that enter dash, so entering this state always dashes.
## Momentum is fully preserved on exit — the dash ADDS speed to your run.
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
var direction: Vector3 = Vector3.ZERO
var _exit_speed: float = 0.0
var _last_vel: Vector3 = Vector3.ZERO
# Cooldown tracked across dash instances
static var _last_dash_time: float = -999.0
var _dash_speed: float = 0.0
func enter(_data: Dictionary = {}) -> void:
# Check cooldown
var now := Time.get_ticks_msec() / 1000.0
if now - _last_dash_time < params.dash_cooldown:
machine.switch_to("air")
return
elapsed = 0.0
_last_dash_time = now
machine.dash_cooldown_timer = params.dash_cooldown
var player := machine.player
if player.dash_player:
@@ -38,55 +32,49 @@ func enter(_data: Dictionary = {}) -> void:
direction = direction.normalized()
machine.register_chain_mechanic("dash")
machine.movement_event.emit("dash", {})
var current_hvel := Vector3(machine.player.velocity.x, 0.0, machine.player.velocity.z)
var current_speed := current_hvel.length()
var proj := current_hvel.dot(direction)
var base_dash_speed = machine.get_effective_speed(params.dash_speed)
if proj >= 0.0:
# Dashing forward or diagonally forward: add base dash speed to the projected speed
_exit_speed = proj + base_dash_speed
_dash_speed = proj + base_dash_speed
else:
# Dashing backward or diagonally backward:
# Reflect current momentum into the new direction, smoothly scaling from base_dash_speed (at sideways) to current_speed (at exactly backward).
# Reflect current momentum into the new direction, smoothly scaling from
# base_dash_speed (at sideways) to current_speed (at exactly backward).
var backward_factor = -proj / current_speed if current_speed > 0.001 else 0.0
_exit_speed = lerpf(base_dash_speed, maxf(current_speed, base_dash_speed), backward_factor)
machine.player.velocity = direction * _exit_speed
_dash_speed = lerpf(base_dash_speed, maxf(current_speed, base_dash_speed), backward_factor)
machine.player.velocity = direction * _dash_speed
machine.on_ground = false
_last_vel = machine.player.velocity
func exit() -> void:
# Preserve dash velocity on exit (don't cut speed abruptly)
pass
func update(delta: float) -> void:
elapsed += delta
var player := machine.player
if elapsed > params.dash_duration:
machine.player.velocity = direction * _exit_speed * 0.85
if machine.player.is_on_floor():
# Keep the full dash velocity — the dash is a speed investment
player.velocity = direction * _dash_speed
if player.is_on_floor():
machine.on_ground = true
machine.switch_to("ground")
else:
machine.switch_to("air")
return
# Maintain dash velocity
var player := machine.player
var vel: Vector3 = player.velocity
if vel.distance_squared_to(_last_vel) > 100.0:
machine.switch_to("air")
return
vel = direction * _exit_speed
# Maintain dash velocity, flat trajectory
var vel := direction * _dash_speed
if player.is_on_floor():
vel.y = -0.5 # Snap to floor to handle ramps
player.velocity = vel
player.move_and_slide()
_last_vel = player.velocity
+62 -38
View File
@@ -1,42 +1,65 @@
extends Node
class_name StateGrapple
## Swing grapple with active rope control:
## - the rope acts as a hard pendulum constraint at its current length
## - holding forward reels in (rope shortens, converts to speed)
## - holding back pays rope out (up to the original latch length)
## - strafe input steers tangentially around the swing sphere
## - jump detaches with a boost along your current motion
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var _rope_length: float = 0.0
func enter(_data: Dictionary = {}) -> void:
machine.on_ground = false
machine.wall_normal = Vector3.ZERO
machine.wall_side = 0.0
machine.register_chain_mechanic("grapple")
_rope_length = machine.grapple_length
func exit() -> void:
pass
func update(delta: float) -> void:
var player := machine.player
var vel: Vector3 = player.velocity
# If player releases the button, disconnect
if not machine.input_grapple:
machine.switch_to("air")
return
var grapple_pos: Vector3 = machine.grapple_point
var current_dist: float = player.global_position.distance_to(grapple_pos)
var dir_to_point: Vector3 = (grapple_pos - player.global_position).normalized()
var to_point: Vector3 = grapple_pos - player.global_position
var current_dist: float = to_point.length()
if current_dist < 0.01:
machine.switch_to("air")
return
var dir_to_point: Vector3 = to_point / current_dist
# Jump to detach and get a boost
if machine.input_jump_just_pressed and machine.jump_cooldown_timer <= 0.0:
vel.y = params.jump_velocity
# Add a directional boost if jumping while swinging
vel.y = maxf(vel.y, params.jump_velocity)
# Boost along current motion so the release keeps the swing's energy
var h_vel := Vector3(vel.x, 0.0, vel.z)
if h_vel.length_squared() > 0.1:
var boost := h_vel.normalized() * params.grapple_jump_boost * 0.5
vel.x += boost.x
vel.z += boost.z
# Plus a steer boost toward held direction
var h_look = machine.wish_dir_world
if h_look.length_squared() > 0.1:
var h_boost = h_look.normalized() * params.grapple_jump_boost
var h_boost = h_look.normalized() * params.grapple_jump_boost * 0.5
vel.x += h_boost.x
vel.z += h_boost.z
player.velocity = vel
machine.jump_cooldown_timer = params.jump_cooldown
machine.register_chain_mechanic("grapple_jump")
@@ -44,43 +67,44 @@ func update(delta: float) -> void:
if player.jump_player:
player.jump_player.play()
return
# Apply gravity
vel.y -= params.gravity * delta
# Apply slight inward pull (reeling in)
# Base reel: constant gentle pull toward the hook
vel += dir_to_point * params.grapple_pull_force * delta
# Pendulum / Rope physics
# If the player tries to go further than the original rope length, pull them back aggressively
var max_len: float = machine.grapple_length
if current_dist > max_len:
# Project velocity onto the tangent of the sphere (prevent moving further away)
# ── Active rope control ───────────────────────────────────────────────
var fwd_input := -machine.input_dir.y # +1 holding forward, -1 back
if fwd_input > 0.1:
# Reel in: shorten rope and pull hard — converts to swing speed
vel += dir_to_point * params.grapple_reel_force * fwd_input * delta
_rope_length = maxf(_rope_length - 8.0 * fwd_input * delta, 2.0)
elif fwd_input < -0.1:
# Pay out rope back toward the original latch length
_rope_length = minf(_rope_length + 8.0 * -fwd_input * delta, machine.grapple_length)
# Keep the working length taut to where we actually are (swinging inside
# the sphere shortens the constraint, giving crisp Tarzan arcs)
_rope_length = minf(_rope_length, maxf(current_dist, 2.0))
# ── Pendulum constraint at the current rope length ────────────────────
if current_dist > _rope_length:
# Kill outward radial velocity (the rope is taut and inextensible)
var radial_vel = vel.project(dir_to_point)
# If radial_vel is pointing AWAY from the grapple point (dot < 0), kill it
if radial_vel.dot(dir_to_point) < 0:
vel -= radial_vel
# Add a spring force to pull them back to the sphere
var diff = current_dist - max_len
var spring_force = dir_to_point * diff * params.grapple_spring_strength
vel += spring_force * delta
# Spring correction toward the sphere surface
var diff = current_dist - _rope_length
vel += dir_to_point * diff * params.grapple_spring_strength * delta
# Air control while swinging
var wish_dir: Vector3 = machine.wish_dir_world
if wish_dir.length_squared() > 0.01:
wish_dir = wish_dir.normalized()
# Add force tangentially to the rope
# We want to steer, but not pull away from or directly into the rope.
var tangent_wish = wish_dir - wish_dir.project(dir_to_point)
if tangent_wish.length_squared() > 0.01:
vel += tangent_wish.normalized() * params.grapple_air_control * delta
# ── Tangential steering (strafe around the swing sphere) ──────────────
var strafe_input := machine.input_dir.x
if absf(strafe_input) > 0.1:
var right := player.global_transform.basis.x
var tangent_steer := right - right.project(dir_to_point)
if tangent_steer.length_squared() > 0.01:
vel += tangent_steer.normalized() * params.grapple_air_control * strafe_input * delta
player.velocity = vel
player.move_and_slide()
# Remove ground detach so players can grapple along the floor
# if player.is_on_floor():
# machine.on_ground = true
# machine.switch_to("ground")
# return
+75 -136
View File
@@ -7,6 +7,7 @@ var params: MovementParams:
var _footstep_timer: float = 0.0
func enter(_data: Dictionary = {}) -> void:
machine.on_ground = true
machine.current_jump_count = 0
@@ -16,9 +17,6 @@ func enter(_data: Dictionary = {}) -> void:
if rig:
rig.clear_wall_tilt()
# Ensure capsule is correct when entering ground state
_update_capsule_height()
func exit() -> void:
pass
@@ -28,36 +26,19 @@ func update(delta: float) -> void:
var player := machine.player
var vel: Vector3 = player.velocity
# ── Jump (coyote time + jump buffer) ──────────────────────────────────
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0 and machine.jump_cooldown_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.jump_cooldown_timer = params.jump_cooldown
if player.jump_player:
player.jump_player.play()
machine.switch_to("air")
return
# Jump buffer: player pressed jump just before landing
if machine.on_ground and machine.jump_buffer_time > 0.0 and machine.jump_cooldown_timer <= 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.jump_cooldown_timer = params.jump_cooldown
if player.jump_player:
player.jump_player.play()
machine.switch_to("air")
return
# ── Jump (direct press w/ coyote, or buffered from before landing) ────
if machine.jump_cooldown_timer <= 0.0:
var pressed := machine.input_jump_just_pressed and machine.coyote_timer > 0.0
var buffered := machine.on_ground and machine.jump_buffer_time > 0.0
if pressed or buffered:
machine.do_jump()
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:
if hspeed > params.slide_min_speed + 1.0:
machine.switch_to("slide")
return
@@ -80,7 +61,7 @@ func update(delta: float) -> void:
vel.x = hvel.x
vel.z = hvel.z
# ── Gravity (correct direction: downward) ─────────────────────────────
# ── Gravity / floor snap ──────────────────────────────────────────────
if not player.is_on_floor():
vel.y -= params.gravity * delta
else:
@@ -91,59 +72,20 @@ func update(delta: float) -> void:
player.velocity = vel
player.move_and_slide()
# ── Stair Stepping ────────────────────────────────────────────────────
if wish_dir.length_squared() > 0.01 and not machine.input_crouch:
for i in range(player.get_slide_collision_count()):
var col = player.get_slide_collision(i)
if abs(col.get_normal().y) < 0.3: # Hit a wall
var max_step_height = 0.95
var space_state = player.get_world_3d().direct_space_state
var feet_y = player.global_position.y - (machine.original_capsule_height / 2.0)
# Raycast down from above the step
var fwd = wish_dir.normalized()
var ray_start = player.global_position + fwd * 0.65
ray_start.y = feet_y + max_step_height + 0.1
var ray_end = ray_start - Vector3.UP * (max_step_height + 0.2)
var ray = PhysicsRayQueryParameters3D.create(ray_start, ray_end)
ray.exclude = [player.get_rid()]
var hit = space_state.intersect_ray(ray)
if not hit.is_empty() and hit.normal.y > 0.7:
var step_height = hit.position.y - feet_y
if step_height > 0.01 and step_height <= max_step_height:
# Check if we have headroom to move up
var head_ray_start = player.global_position
head_ray_start.y += (machine.original_capsule_height / 2.0)
var head_ray_end = head_ray_start + Vector3.UP * (step_height + 0.1)
var head_ray = PhysicsRayQueryParameters3D.create(head_ray_start, head_ray_end)
head_ray.exclude = [player.get_rid()]
if space_state.intersect_ray(head_ray).is_empty():
# Check if there is enough space to move forward after stepping up
var test_transform = player.global_transform
test_transform.origin.y += step_height + 0.05
if not player.test_move(test_transform, fwd * 0.15):
# We can safely step up
player.global_position.y += step_height + 0.01
if player.head_pivot and player.head_pivot.has_method("add_step_offset"):
player.head_pivot.add_step_offset(-(step_height + 0.01))
# Push slightly forward to get onto the step
player.global_position += fwd * 0.1
# Restore horizontal velocity that was lost from hitting the wall
player.velocity.x = hvel.x
player.velocity.z = hvel.z
break
_update_capsule_height()
_try_step_up(player, wish_dir, hvel)
var current_hspeed = Vector2(player.velocity.x, player.velocity.z).length()
if current_hspeed > 1.0:
if current_hspeed > 1.0 and player.is_on_floor():
_footstep_timer -= delta
if _footstep_timer <= 0.0:
if player.footstep_player:
# Reset pitch/volume (the landing thud reuses this player) and
# add slight variation so steps don't machine-gun.
player.footstep_player.pitch_scale = randf_range(0.92, 1.08)
player.footstep_player.volume_db = -6.0
player.footstep_player.play()
_footstep_timer = max(0.2, 3.0 / current_hspeed)
else:
@@ -162,82 +104,79 @@ func update(delta: float) -> void:
machine.switch_to("air")
return
# ── Wall interaction (Run, Climb, Vault) ──────────────────────────────
if machine.input_dir.length() > 0.1 and machine.input_dir.y <= 0.0 and machine.wall_cooldown_timer <= 0.0 and not machine.input_crouch:
# ── Wall interaction (Vault, Climb) ───────────────────────────────────
if machine.input_dir.length() > 0.1 and machine.input_dir.y <= 0.0 \
and machine.wall_cooldown_timer <= 0.0 and not machine.input_crouch:
var fwd_wall = machine.detect_wall_forward()
if fwd_wall.hit:
if fwd_wall.is_short:
# Instant Vault
var look_dir := -player.global_transform.basis.z
var h_look := Vector3(look_dir.x, 0.0, look_dir.z)
if h_look.length_squared() > 0.01:
h_look = h_look.normalized()
player.velocity = h_look * params.wall_climb_vault_forward
player.velocity.y = params.wall_climb_vault_up
if player.vault_player:
player.vault_player.play()
# Quick camera animation (tilt up and forward bob)
var camera = player.camera
if camera:
var tween = player.create_tween()
tween.tween_property(camera, "rotation_degrees:x", camera.rotation_degrees.x + 10.0, 0.15).set_trans(Tween.TRANS_SINE)
tween.parallel().tween_property(camera, "v_offset", -0.2, 0.15).set_trans(Tween.TRANS_SINE)
tween.chain().tween_property(camera, "rotation_degrees:x", camera.rotation_degrees.x, 0.2).set_trans(Tween.TRANS_SINE)
tween.parallel().tween_property(camera, "v_offset", 0.0, 0.2).set_trans(Tween.TRANS_SINE)
machine.wall_cooldown_timer = 0.3
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
if fwd_wall.is_short and hspeed > 3.0:
machine.do_vault()
machine.switch_to("air")
return
else:
elif not fwd_wall.is_short:
# Check if moving directly into wall and looking generally towards it
var look_dir := -player.global_transform.basis.z
var looking_towards := look_dir.dot(-fwd_wall.normal) > 0.0
var moving_towards := wish_dir.dot(-fwd_wall.normal) > cos(deg_to_rad(params.wall_climb_max_angle))
var moving_towards := machine.wish_dir_world.dot(-fwd_wall.normal) > cos(deg_to_rad(params.wall_climb_max_angle))
if looking_towards and moving_towards and machine.can_wall_climb:
machine.wall_normal = fwd_wall.normal
machine.switch_to("wall_climb")
return
# Fallback to wall run
var wall_n := machine.detect_wall_horizontal()
if wall_n != Vector3.ZERO:
if not player.is_on_floor():
var w_look_dir := -player.global_transform.basis.z
var h_look := Vector3(w_look_dir.x, 0.0, w_look_dir.z).normalized()
# Require looking mostly along the wall (angle > 41 degrees from normal)
# and inputting mostly along the wall (angle > 31 degrees from normal, allows W+A/D diagonally into wall)
if abs(h_look.dot(wall_n)) < 0.75 and abs(wish_dir.dot(wall_n)) < 0.85:
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
if machine.input_dash and machine.dash_cooldown_timer <= 0.0:
machine.switch_to("dash")
return
func _try_step_up(player: CharacterBody3D, wish_dir: Vector3, hvel: Vector3) -> void:
for i in range(player.get_slide_collision_count()):
var col = player.get_slide_collision(i)
if abs(col.get_normal().y) < 0.3: # Hit a wall
var max_step_height = 0.95
var space_state = player.get_world_3d().direct_space_state
var feet_y = player.global_position.y - (machine.original_capsule_height / 2.0)
# Raycast down from above the step
var fwd = wish_dir.normalized()
var ray_start = player.global_position + fwd * 0.65
ray_start.y = feet_y + max_step_height + 0.1
var ray_end = ray_start - Vector3.UP * (max_step_height + 0.2)
var ray = PhysicsRayQueryParameters3D.create(ray_start, ray_end)
ray.exclude = [player.get_rid()]
var hit = space_state.intersect_ray(ray)
if not hit.is_empty() and hit.normal.y > 0.7:
var step_height = hit.position.y - feet_y
if step_height > 0.01 and step_height <= max_step_height:
# Check if we have headroom to move up
var head_ray_start = player.global_position
head_ray_start.y += (machine.original_capsule_height / 2.0)
var head_ray_end = head_ray_start + Vector3.UP * (step_height + 0.1)
var head_ray = PhysicsRayQueryParameters3D.create(head_ray_start, head_ray_end)
head_ray.exclude = [player.get_rid()]
if space_state.intersect_ray(head_ray).is_empty():
# Check if there is enough space to move forward after stepping up
var test_transform = player.global_transform
test_transform.origin.y += step_height + 0.05
if not player.test_move(test_transform, fwd * 0.15):
# We can safely step up
player.global_position.y += step_height + 0.01
if player.head_pivot and player.head_pivot.has_method("add_step_offset"):
player.head_pivot.add_step_offset(-(step_height + 0.01))
# Push slightly forward to get onto the step
player.global_position += fwd * 0.1
# Restore horizontal velocity that was lost from hitting the wall
player.velocity.x = hvel.x
player.velocity.z = hvel.z
break
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
+83 -62
View File
@@ -1,12 +1,16 @@
extends Node
class_name StateSlide
## Momentum slide. Friction is a flat deceleration on level ground but gravity
## projected along the floor accelerates you downhill, so slopes are the fast
## route. The capsule stays low via the machine's crouch handling (slide counts
## as crouched); no capsule fiddling here.
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
var _saved_capsule_height: float = 0.0
var _slide_direction: Vector3 = Vector3.ZERO
@@ -17,12 +21,6 @@ func enter(_data: Dictionary = {}) -> void:
if machine.player.slide_player and not machine.player.slide_player.playing:
machine.player.slide_player.play()
# Save original capsule height and halve it
var shape: CapsuleShape3D = _get_capsule()
if shape:
_saved_capsule_height = shape.height
shape.height = _saved_capsule_height * 0.5
# Slide in the direction of current velocity (momentum-based)
var hvel := Vector3(machine.player.velocity.x, 0.0, machine.player.velocity.z)
if hvel.length_squared() > 0.01:
@@ -33,54 +31,70 @@ func enter(_data: Dictionary = {}) -> void:
_slide_direction.y = 0.0
_slide_direction = _slide_direction.normalized()
# Set initial slide velocity
var slide_speed := maxf(hvel.length(), params.slide_speed)
machine.player.velocity.x = _slide_direction.x * machine.get_effective_speed(slide_speed)
machine.player.velocity.z = _slide_direction.z * machine.get_effective_speed(slide_speed)
# Entry speed: keep momentum, add a flat boost when actually moving fast on
# the ground (rewards slide-cancelling sprints without making crouch a brake)
var entry_speed := hvel.length()
if machine.player.is_on_floor() and entry_speed > params.walk_speed * 0.8:
entry_speed += params.slide_boost
entry_speed = maxf(entry_speed, params.slide_speed * 0.75)
machine.player.velocity.x = _slide_direction.x * machine.get_effective_speed(entry_speed)
machine.player.velocity.z = _slide_direction.z * machine.get_effective_speed(entry_speed)
func exit() -> void:
machine.slide_cooldown_timer = params.slide_cooldown
if machine.player.slide_player:
machine.player.slide_player.stop()
# Restore original capsule height
var shape: CapsuleShape3D = _get_capsule()
if shape and _saved_capsule_height > 0.0:
shape.height = _saved_capsule_height
func update(delta: float) -> void:
elapsed += delta
var player := machine.player
if player.slide_player and not player.slide_player.playing:
player.slide_player.play()
var vel: Vector3 = player.velocity
var hvel := Vector3(vel.x, 0.0, vel.z)
var hspeed := hvel.length()
# ── Apply friction to horizontal velocity ─────────────────────────────
vel.x *= pow(params.slide_friction, delta * 10.0)
vel.z *= pow(params.slide_friction, delta * 10.0)
# ── Slope physics: gravity projected along the floor plane ────────────
var on_slope := false
if player.is_on_floor():
var floor_n := player.get_floor_normal()
if floor_n.y < 0.999:
# Downhill direction on this slope
var downhill := (Vector3.DOWN - floor_n * Vector3.DOWN.dot(floor_n))
if downhill.length_squared() > 0.0001:
downhill = downhill.normalized()
var steepness := 1.0 - floor_n.y # 0 flat .. ~0.3 steep
var slope_pull := downhill * params.slide_slope_accel * steepness * 8.0
hvel += Vector3(slope_pull.x, 0.0, slope_pull.z) * delta
on_slope = downhill.dot(_slide_direction) > 0.1
# ── Allow slight steering ─────────────────────────────────────────────
# ── Friction: flat decel on level ground, nearly free downhill ────────
if hspeed > 0.01:
var friction := params.slide_friction_flat
if on_slope:
friction *= 0.15
var new_speed := maxf(hvel.length() - friction * delta, 0.0)
if hvel.length() > 0.001:
hvel = hvel.normalized() * new_speed
# ── Steering: bend velocity toward input without changing speed ───────
var wish_dir := machine.wish_dir_world
if wish_dir.length_squared() > 0.01:
if wish_dir.length_squared() > 0.01 and hvel.length_squared() > 0.01:
wish_dir = wish_dir.normalized()
var hvel := Vector3(vel.x, 0.0, vel.z)
var current_speed := hvel.length()
# Slowly bend velocity towards wish_dir
var steer_speed = 3.0 * delta
var new_hvel = hvel.lerp(wish_dir * current_speed, steer_speed)
vel.x = new_hvel.x
vel.z = new_hvel.z
if new_hvel.length_squared() > 0.01:
_slide_direction = new_hvel.normalized()
var speed_now := hvel.length()
var steered := hvel.lerp(wish_dir * speed_now, params.slide_steer_rate * delta)
if steered.length_squared() > 0.01:
hvel = steered.normalized() * speed_now
_slide_direction = hvel.normalized()
# ── Gravity (for slopes) ──────────────────────────────────────────────
vel.x = hvel.x
vel.z = hvel.z
# ── Gravity (for slopes / edges) ──────────────────────────────────────
if not player.is_on_floor():
vel.y -= params.gravity * delta
else:
@@ -97,10 +111,41 @@ func update(delta: float) -> void:
machine.on_ground = false
# ── End conditions ────────────────────────────────────────────────────
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
var out_speed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
# Jump out of slide: momentum-preserving hop
if machine.input_jump_just_pressed and machine.jump_cooldown_timer <= 0.0:
var hv := Vector3(player.velocity.x, 0.0, player.velocity.z)
if hv.length_squared() > 0.01:
var dir := hv.normalized()
var jump_speed := maxf(out_speed, minf(out_speed + params.slide_jump_speed_boost, params.bunny_hop_speed_cap))
player.velocity.x = dir.x * jump_speed
player.velocity.z = dir.z * jump_speed
player.velocity.y = params.jump_velocity
machine.current_jump_count = 1
machine.jump_cooldown_timer = params.jump_cooldown
machine.on_ground = false
machine.register_chain_mechanic("slide_jump")
machine.movement_event.emit("jump", {})
if player.jump_player:
player.jump_player.play()
machine.switch_to("air")
return
# Slide straight onto a wall → wall run (keeps the flow going)
if not player.is_on_floor() and machine.wall_cooldown_timer <= 0.0 and out_speed > params.slide_min_speed:
var wall_n := machine.detect_wall_horizontal()
if wall_n != Vector3.ZERO and abs(machine.wish_dir_world.dot(wall_n)) < 0.85:
machine.switch_to("wall_run")
return
# Dash out of slide
if machine.input_dash and machine.dash_cooldown_timer <= 0.0:
machine.switch_to("dash")
return
# Slide ended: too slow
if hspeed < params.slide_min_speed:
if out_speed < params.slide_min_speed:
machine.switch_to("ground")
return
@@ -113,27 +158,3 @@ func update(delta: float) -> void:
if not machine.on_ground and machine.coyote_timer <= 0.0:
machine.switch_to("air")
return
# Jump out of slide
if machine.input_jump_just_pressed and machine.jump_cooldown_timer <= 0.0:
player.velocity.y = params.jump_velocity
var hvel := Vector3(player.velocity.x, 0.0, player.velocity.z)
if hvel.length_squared() > 0.01:
var dir := hvel.normalized()
var current_speed := hvel.length()
var jump_speed := maxf(current_speed, minf(current_speed + params.slide_jump_speed_boost, params.bunny_hop_speed_cap))
player.velocity.x = dir.x * jump_speed
player.velocity.z = dir.z * jump_speed
machine.current_jump_count = 1
machine.jump_cooldown_timer = params.jump_cooldown
machine.on_ground = false
machine.register_chain_mechanic("slide_jump")
machine.switch_to("air")
return
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
+27 -41
View File
@@ -15,10 +15,10 @@ func enter(_data: Dictionary = {}) -> void:
if machine.player.wallrun_player and not machine.player.wallrun_player.playing:
machine.player.wallrun_player.play()
# Give an initial upward boost
# Carry existing upward momentum into the climb so jump→climb chains flow
var player := machine.player
player.velocity.y = params.wall_climb_speed
player.velocity.y = maxf(player.velocity.y, params.wall_climb_speed)
var hvel := Vector3(player.velocity.x, 0.0, player.velocity.z)
player.velocity.x = hvel.x * 0.5
player.velocity.z = hvel.z * 0.5
@@ -30,7 +30,7 @@ func exit() -> void:
func update(delta: float) -> void:
elapsed += delta
var player := machine.player
if player.wallrun_player and not player.wallrun_player.playing:
player.wallrun_player.play()
@@ -39,15 +39,15 @@ func update(delta: float) -> void:
_detach()
return
# ── Look/Move away to transition to wall run ──────────────────────────────
# ── Look/Move away to transition to wall run ──────────────────────────
var look_dir := -player.global_transform.basis.z
var wish_dir := machine.wish_dir_world
var looking_towards := look_dir.dot(-machine.wall_normal) > 0.0
var moving_sideways := false
if wish_dir.length_squared() > 0.01:
moving_sideways = wish_dir.dot(-machine.wall_normal) <= cos(deg_to_rad(params.wall_climb_max_angle))
if not looking_towards or moving_sideways:
# We are looking away or moving sideways, transition to wall run
# The wall run state will figure out tangent and side
@@ -58,22 +58,36 @@ func update(delta: float) -> void:
else:
_detach()
return
# ── Jump off the wall (backward kick) ─────────────────────────────────
if machine.input_jump_just_pressed and machine.jump_cooldown_timer <= 0.0:
player.velocity = machine.wall_normal * params.wall_run_jump_off_normal
player.velocity.y = params.jump_velocity
machine.register_chain_mechanic("wall_jump")
machine.jump_cooldown_timer = params.jump_cooldown
machine.movement_event.emit("jump", {})
if player.jump_player:
player.jump_player.play()
_detach()
return
# ── Ledge Vault Check ─────────────────────────────────────────────────
var fwd_wall = machine.detect_wall_forward()
if not fwd_wall.hit:
# The forward lower ray missed, we fell off the wall completely
_detach()
return
if fwd_wall.is_short:
# The upper ray missed, meaning we reached the top of the wall!
_vault()
machine.do_vault()
machine.wall_normal = Vector3.ZERO
machine.wall_side = 0.0
machine.switch_to("air")
return
# ── Movement ──────────────────────────────────────────────────────────
# Slow down over time
var speed_factor = maxf(0.0, 1.0 - (elapsed / params.wall_climb_duration))
# ── Movement: ease out over the climb ─────────────────────────────────
var speed_factor: float = pow(maxf(0.0, 1.0 - (elapsed / params.wall_climb_duration)), 0.7)
player.velocity.y = params.wall_climb_speed * speed_factor
# Push slightly into the wall to maintain contact
@@ -95,31 +109,3 @@ func _detach() -> void:
machine.wall_cooldown_timer = 0.3
machine.can_wall_climb = false
machine.switch_to("air")
func _vault() -> void:
var player := machine.player
var look_dir := -player.global_transform.basis.z
var h_look := Vector3(look_dir.x, 0.0, look_dir.z)
if h_look.length_squared() > 0.01:
h_look = h_look.normalized()
# Impulse
player.velocity = h_look * params.wall_climb_vault_forward
player.velocity.y = params.wall_climb_vault_up
if player.vault_player:
player.vault_player.play()
# Quick camera animation (tilt up and forward bob)
var camera = player.camera
if camera:
var tween = player.create_tween()
tween.tween_property(camera, "rotation_degrees:x", camera.rotation_degrees.x + 10.0, 0.15).set_trans(Tween.TRANS_SINE)
tween.parallel().tween_property(camera, "v_offset", -0.2, 0.15).set_trans(Tween.TRANS_SINE)
tween.chain().tween_property(camera, "rotation_degrees:x", camera.rotation_degrees.x, 0.2).set_trans(Tween.TRANS_SINE)
tween.parallel().tween_property(camera, "v_offset", 0.0, 0.2).set_trans(Tween.TRANS_SINE)
machine.wall_normal = Vector3.ZERO
machine.wall_side = 0.0
machine.wall_cooldown_timer = 0.3
machine.switch_to("air")
+5 -2
View File
@@ -13,8 +13,8 @@ func enter(_data: Dictionary = {}) -> void:
elapsed = 0.0
stamina = params.wall_cling_max_stamina
machine.register_chain_mechanic("wall_cling")
# Kill most velocity but keep slight downward
machine.player.velocity = Vector3.ZERO
# Keep a touch of momentum so the stop reads as a grab, not a freeze
machine.player.velocity *= 0.15
func exit() -> void:
@@ -56,6 +56,9 @@ func update(delta: float) -> void:
machine.wall_cooldown_timer = 0.3
machine.register_chain_mechanic("wall_cling_jump")
machine.jump_cooldown_timer = params.jump_cooldown
machine.movement_event.emit("jump", {})
if machine.player.jump_player:
machine.player.jump_player.play()
machine.switch_to("air")
return
+43 -45
View File
@@ -1,25 +1,27 @@
extends Node
class_name StateWallRun
## Wall run that preserves momentum: on attach we keep speed along the wall
## (plus some upward carry) and accelerate toward wall_run_speed instead of
## hard-setting velocity, so entering fast stays fast and entering slow ramps
## up smoothly. Gravity fades in over the run so the start feels planted.
var machine: MovementStateMachine
var params: MovementParams:
get: return machine.params
var elapsed: float = 0.0
var _run_speed: float = 0.0
var _last_vel: Vector3 = Vector3.ZERO
var _current_tangent: Vector3 = Vector3.ZERO
func enter(_data: Dictionary = {}) -> void:
elapsed = 0.0
machine.register_chain_mechanic("wall_run")
# Cap the upward momentum so they don't fly up the wall,
# and prevent negative y momentum so the wall "catches" them.
machine.player.velocity.y = clampf(machine.player.velocity.y, 0.0, 1.5)
machine.on_ground = false
_last_vel = machine.player.velocity
# Keep some upward carry so jumping into a wall run flows; the wall
# still "catches" downward momentum.
machine.player.velocity.y = clampf(machine.player.velocity.y, 0.0, params.wall_run_entry_max_up)
if machine.player.wallrun_player and not machine.player.wallrun_player.playing:
machine.player.wallrun_player.play()
@@ -29,11 +31,6 @@ func enter(_data: Dictionary = {}) -> void:
if hvel.dot(_current_tangent) < 0.0:
_current_tangent = -_current_tangent
# Preserve speed along the wall if it's faster than base wall_run_speed
var base_speed = machine.get_effective_speed(params.wall_run_speed)
var projected_speed = hvel.dot(_current_tangent)
_run_speed = maxf(base_speed, projected_speed)
# Camera tilt
var rig = _get_camera_rig()
if rig:
@@ -45,7 +42,7 @@ func exit() -> void:
var rig = _get_camera_rig()
if rig:
rig.clear_wall_tilt()
if machine.player.wallrun_player:
machine.player.wallrun_player.stop()
@@ -53,61 +50,51 @@ func exit() -> void:
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")
_detach(0.4)
return
var player := machine.player
var vel: Vector3 = player.velocity
if player.wallrun_player and not player.wallrun_player.playing:
player.wallrun_player.play()
if vel.distance_squared_to(_last_vel) > 100.0:
machine.wall_normal = Vector3.ZERO
machine.wall_side = 0.0
machine.switch_to("air")
return
# ── Gradual gravity pull (starts light, increases over time) ──────────
var gravity_factor := 0.1 + 0.9 * (elapsed / params.wall_run_duration)
# ── Gradual gravity pull (starts weightless, ramps up) ────────────────
var gravity_factor := 0.05 + 0.95 * pow(elapsed / params.wall_run_duration, 1.5)
vel.y -= params.wall_run_gravity * gravity_factor * delta
# ── Move along wall tangent ───────────────────────────────────────────
# ── Accelerate along wall tangent (keeps momentum, no hard set) ───────
var wall_tangent := machine.wall_normal.cross(Vector3.UP).normalized()
# Ensure the new tangent aligns with our locked forward direction
if _current_tangent.dot(wall_tangent) < 0.0:
wall_tangent = -wall_tangent
_current_tangent = wall_tangent
vel.x = wall_tangent.x * _run_speed
vel.z = wall_tangent.z * _run_speed
var hvel := Vector3(vel.x, 0.0, vel.z)
var along := hvel.dot(wall_tangent)
var target_speed: float = maxf(machine.get_effective_speed(params.wall_run_speed), along)
along = move_toward(along, target_speed, params.wall_run_accel * delta)
# Redirect all horizontal velocity along the wall (kills the into-wall part)
hvel = wall_tangent * along
vel.x = hvel.x
vel.z = hvel.z
# ── 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")
_detach(0.3)
return
# ── Push slightly toward wall to maintain contact ─────────────────────
vel -= machine.wall_normal * 2.0
# ── Wall jump ─────────────────────────────────────────────────────────
if machine.input_jump_just_pressed and machine.jump_cooldown_timer <= 0.0:
# Base jump off velocity on current preserved momentum
var jump_vel: Vector3 = _current_tangent * _run_speed
var jump_vel: Vector3 = _current_tangent * along
# Spring off the wall
jump_vel += machine.wall_normal * params.wall_run_jump_off_normal
var h_look := Vector3(look_dir.x, 0.0, look_dir.z)
if h_look.length_squared() > 0.01:
jump_vel += h_look.normalized() * params.wall_run_jump_horizontal
jump_vel.y = params.wall_run_auto_jump_speed
player.velocity = jump_vel
machine.wall_normal = Vector3.ZERO
@@ -115,6 +102,9 @@ func update(delta: float) -> void:
machine.wall_cooldown_timer = 0.3
machine.register_chain_mechanic("wall_jump")
machine.jump_cooldown_timer = params.jump_cooldown
machine.movement_event.emit("jump", {})
if player.jump_player:
player.jump_player.play()
machine.switch_to("air")
return
@@ -123,16 +113,16 @@ func update(delta: float) -> void:
machine.switch_to("wall_cling")
return
# ── Push slightly toward wall to maintain contact ─────────────────────
vel -= machine.wall_normal * 2.0
player.velocity = vel
player.move_and_slide()
_last_vel = player.velocity
# ── Check still on wall ───────────────────────────────────────────────
var still_on_wall := machine.detect_wall_horizontal()
if still_on_wall == Vector3.ZERO:
machine.wall_normal = Vector3.ZERO
machine.wall_side = 0.0
machine.switch_to("air")
_detach(0.0)
return
# ── Hit floor during wall run ─────────────────────────────────────────
@@ -144,6 +134,14 @@ func update(delta: float) -> void:
machine.switch_to("ground")
func _detach(cooldown: float) -> void:
machine.wall_normal = Vector3.ZERO
machine.wall_side = 0.0
if cooldown > 0.0:
machine.wall_cooldown_timer = cooldown
machine.switch_to("air")
func _get_camera_rig():
if machine.player and machine.player.has_node("HeadPivot"):
return machine.player.get_node("HeadPivot")