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:
co-authored by
Claude Fable 5
parent
c4a538a469
commit
0ba14a9469
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user