feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics
This commit is contained in:
+36
-21
@@ -5,20 +5,24 @@ class_name MovementParams
|
||||
@export var walk_speed: float = 7.0
|
||||
@export var sprint_speed: float = 11.0
|
||||
@export var crouch_speed: float = 4.0
|
||||
@export var ground_friction: float = 8.0
|
||||
@export var ground_acceleration: float = 30.0
|
||||
@export var ground_deceleration: float = 20.0
|
||||
@export var ground_friction: float = 10.0
|
||||
@export var ground_acceleration: float = 50.0
|
||||
@export var ground_deceleration: float = 40.0
|
||||
|
||||
# ── Jump / Air ────────────────────────────────────────────────────────────────
|
||||
@export var jump_velocity: float = 8.5
|
||||
@export var coyote_time: float = 0.15
|
||||
@export var jump_buffer: float = 0.12
|
||||
@export var air_control: float = 0.3
|
||||
@export var air_acceleration: float = 25.0
|
||||
@export var air_control: float = 0.35
|
||||
@export var air_acceleration: float = 30.0
|
||||
@export var max_air_speed: float = 16.0
|
||||
@export var gravity: float = 20.0
|
||||
@export var fall_multiplier: float = 2.0
|
||||
@export var low_jump_multiplier: float = 1.5
|
||||
@export var gravity: float = 22.0
|
||||
@export var fall_multiplier: float = 2.2
|
||||
@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
|
||||
|
||||
# ── Bunny Hop ─────────────────────────────────────────────────────────────────
|
||||
@export var bunny_hop_impulse: float = 1.15
|
||||
@@ -27,27 +31,27 @@ class_name MovementParams
|
||||
|
||||
# ── Slide ─────────────────────────────────────────────────────────────────────
|
||||
@export var slide_speed: float = 14.0
|
||||
@export var slide_friction: float = 0.88
|
||||
@export var slide_min_speed: float = 12.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_into_sprint_delay: float = 0.15
|
||||
@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
|
||||
|
||||
# ── Wall Run ──────────────────────────────────────────────────────────────────
|
||||
@export var wall_run_speed: float = 12.0
|
||||
@export var wall_run_vertical_speed: float = 8.0
|
||||
@export var wall_run_vertical_speed: float = 4.0
|
||||
@export var wall_run_duration: float = 1.2
|
||||
@export var wall_run_gravity: float = -2.0
|
||||
@export var wall_run_auto_jump_speed: float = 15.0
|
||||
@export var wall_run_gravity: float = 6.0
|
||||
@export var wall_run_auto_jump_speed: float = 10.0
|
||||
@export var wall_run_jump_horizontal: float = 14.0
|
||||
@export var wall_run_jump_off_normal: float = 9.0
|
||||
@export var wall_angle_threshold: float = 70.0
|
||||
@export var wall_detect_distance: float = 1.0
|
||||
@export var wall_ray_up_height: float = 1.2
|
||||
@export var wall_ray_down_height: float = 0.4
|
||||
@export var wall_detect_distance: float = 0.7
|
||||
@export var wall_ray_up_height: float = 0.6
|
||||
@export var wall_ray_down_height: float = -0.3
|
||||
|
||||
# ── Wall Cling ────────────────────────────────────────────────────────────────
|
||||
@export var wall_cling_horizontal_speed: float = 0.0
|
||||
@export var wall_cling_vertical_speed: float = 0.0
|
||||
@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
|
||||
|
||||
@@ -68,9 +72,20 @@ class_name MovementParams
|
||||
|
||||
# ── Double Jump ───────────────────────────────────────────────────────────────
|
||||
@export var double_jump_velocity: float = 8.0
|
||||
@export var double_jump_max_count: int = 1
|
||||
@export var double_jump_max_count: int = 2 # 1 base jump + 1 double
|
||||
|
||||
# ── Chaining ──────────────────────────────────────────────────────────────────
|
||||
@export var chain_bonus_per_success: float = 0.05
|
||||
@export var chain_bonus_cap: float = 0.50
|
||||
@export var chain_window: float = 0.4
|
||||
@export var chain_window: float = 1.5
|
||||
|
||||
# ── Camera ────────────────────────────────────────────────────────────────────
|
||||
@export var mouse_sensitivity: float = 0.002
|
||||
@export var base_fov: float = 90.0
|
||||
@export var sprint_fov: float = 100.0
|
||||
@export var dash_fov: float = 110.0
|
||||
@export var fov_lerp_speed: float = 10.0
|
||||
@export var head_bob_frequency: float = 12.0
|
||||
@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
|
||||
|
||||
@@ -3,7 +3,6 @@ class_name MovementStateMachine
|
||||
|
||||
## Generic state machine for player movement.
|
||||
## Each state is a Node child; the machine switches between them.
|
||||
## States must call `self.transition_to(new_state_name)` to change states.
|
||||
|
||||
signal state_changed(from_state: String, to_state: String)
|
||||
signal movement_event(event_name: String, data: Dictionary)
|
||||
@@ -13,13 +12,19 @@ var states: Dictionary = {}
|
||||
|
||||
var player: CharacterBody3D
|
||||
var params: MovementParams
|
||||
|
||||
# ── Input (set by controller each physics tick) ───────────────────────────────
|
||||
var input_dir: Vector2 = Vector2.ZERO
|
||||
var wish_dir_world: Vector3 = Vector3.ZERO
|
||||
var input_jump_pressed: bool = false
|
||||
var input_jump_just_pressed: bool = false
|
||||
var input_sprint: bool = false
|
||||
var input_crouch: bool = false
|
||||
var input_dash: bool = false
|
||||
|
||||
# ── State tracking ────────────────────────────────────────────────────────────
|
||||
var wall_normal: Vector3 = Vector3.ZERO
|
||||
var wall_side: float = 0.0 # -1 left, +1 right, 0 none
|
||||
var on_ground: bool = false
|
||||
var last_ground_time: float = 0.0
|
||||
var jump_buffer_time: float = 0.0
|
||||
@@ -28,6 +33,10 @@ var current_jump_count: int = 0
|
||||
var chain_timer: float = 0.0
|
||||
var chain_count: int = 0
|
||||
var current_chain_bonus: float = 0.0
|
||||
var original_capsule_height: float = 1.8
|
||||
var slide_cooldown_timer: float = 0.0 # Prevents instant slide re-entry
|
||||
var wall_cooldown_timer: float = 0.0 # Prevents instant re-attachment after wall jump
|
||||
var is_crouched: bool = false
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
@@ -44,52 +53,73 @@ func _physics_process(delta: float) -> void:
|
||||
if on_ground:
|
||||
coyote_timer = params.coyote_time
|
||||
else:
|
||||
coyote_timer -= delta
|
||||
coyote_timer = maxf(coyote_timer - delta, 0.0)
|
||||
|
||||
# Update jump buffer
|
||||
if input_jump_just_pressed:
|
||||
jump_buffer_time = params.jump_buffer
|
||||
else:
|
||||
jump_buffer_time -= delta
|
||||
jump_buffer_time = maxf(jump_buffer_time - delta, 0.0)
|
||||
|
||||
# Update chain timer
|
||||
if chain_timer > 0.0:
|
||||
chain_timer -= delta
|
||||
else:
|
||||
chain_count = 0
|
||||
current_chain_bonus = 0.0
|
||||
if chain_timer <= 0.0:
|
||||
chain_count = 0
|
||||
current_chain_bonus = 0.0
|
||||
|
||||
# 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)
|
||||
|
||||
if current_state.is_empty():
|
||||
return
|
||||
if not states.has(current_state):
|
||||
return
|
||||
var state_node = states[current_state]
|
||||
if state_node.has_method("update"):
|
||||
state_node.update(delta)
|
||||
# Apply gravity that was accumulated during state update, regardless of state.
|
||||
# CharacterBody3D handles gravity automatically via move_and_slide but
|
||||
# we tune it per-position here.
|
||||
|
||||
|
||||
func switch_to(new_state_name: String, data: Dictionary = {}) -> void:
|
||||
if new_state_name == current_state:
|
||||
return
|
||||
if not states.has(new_state_name):
|
||||
push_warning("MovementStateMachine: state '%s' not found" % new_state_name)
|
||||
return
|
||||
var prev = current_state
|
||||
# Exit only the current state
|
||||
if states.has(prev):
|
||||
var old_state = states[prev]
|
||||
if old_state.has_method("exit"):
|
||||
old_state.exit()
|
||||
# Enter the new state
|
||||
current_state = new_state_name
|
||||
for key in states:
|
||||
var st = states[key]
|
||||
if key == new_state_name:
|
||||
st.enter(data)
|
||||
else:
|
||||
st.exit()
|
||||
var new_state = states[new_state_name]
|
||||
if new_state.has_method("enter"):
|
||||
new_state.enter(data)
|
||||
state_changed.emit(prev, new_state_name)
|
||||
|
||||
|
||||
func register_chain_mechanic(_mechanic_name: String) -> void:
|
||||
if chain_timer > 0.0 and chain_count > 0:
|
||||
chain_count = min(chain_count + 1, 10)
|
||||
chain_count = mini(chain_count + 1, 10)
|
||||
else:
|
||||
chain_count = 1
|
||||
chain_timer = params.chain_window
|
||||
current_chain_bonus = min(
|
||||
current_chain_bonus = minf(
|
||||
float(chain_count) * params.chain_bonus_per_success,
|
||||
params.chain_bonus_cap
|
||||
)
|
||||
@@ -101,3 +131,71 @@ func register_chain_mechanic(_mechanic_name: String) -> void:
|
||||
|
||||
func get_effective_speed(base_speed: float) -> float:
|
||||
return base_speed * (1.0 + current_chain_bonus)
|
||||
|
||||
|
||||
## Utility: detect wall to left or right of player using horizontal raycasts.
|
||||
## Returns the wall normal and sets wall_side. Returns Vector3.ZERO if no wall.
|
||||
func detect_wall_horizontal() -> Vector3:
|
||||
if not player:
|
||||
return Vector3.ZERO
|
||||
var origin := player.global_position + Vector3.UP * params.wall_ray_up_height
|
||||
var right_dir := player.global_transform.basis.x.normalized()
|
||||
var space_state := player.get_world_3d().direct_space_state
|
||||
|
||||
# Cast two rays: one left, one right
|
||||
for side_data in [{"dir": right_dir, "side": 1.0}, {"dir": -right_dir, "side": -1.0}]:
|
||||
var ray_end: Vector3 = origin + side_data["dir"] * params.wall_detect_distance
|
||||
var ray := PhysicsRayQueryParameters3D.create(origin, ray_end)
|
||||
ray.exclude = [player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
var n: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
# Wall must be roughly vertical (normal mostly horizontal)
|
||||
if absf(n.y) < 0.3 and n.length_squared() > 0.0:
|
||||
wall_normal = n.normalized()
|
||||
wall_side = side_data["side"]
|
||||
return wall_normal
|
||||
|
||||
# Also cast along movement direction for head-on walls
|
||||
var hvel := Vector3(player.velocity.x, 0.0, player.velocity.z)
|
||||
if hvel.length_squared() > 0.1:
|
||||
var move_dir := hvel.normalized()
|
||||
var perp := move_dir.cross(Vector3.UP).normalized()
|
||||
for side_data in [{"dir": perp, "side": 1.0}, {"dir": -perp, "side": -1.0}]:
|
||||
var ray_end: Vector3 = origin + side_data["dir"] * params.wall_detect_distance
|
||||
var ray := PhysicsRayQueryParameters3D.create(origin, ray_end)
|
||||
ray.exclude = [player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
var n: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
if absf(n.y) < 0.3 and n.length_squared() > 0.0:
|
||||
wall_normal = n.normalized()
|
||||
wall_side = side_data["side"]
|
||||
return wall_normal
|
||||
|
||||
wall_normal = Vector3.ZERO
|
||||
wall_side = 0.0
|
||||
return Vector3.ZERO
|
||||
|
||||
|
||||
func _apply_crouch(crouched: bool) -> void:
|
||||
var shape_node = null
|
||||
for child in player.get_children():
|
||||
if child is CollisionShape3D and child.shape is CapsuleShape3D:
|
||||
shape_node = child
|
||||
break
|
||||
if not shape_node:
|
||||
return
|
||||
var shape = shape_node.shape as CapsuleShape3D
|
||||
var head = player.get_node_or_null("HeadPivot")
|
||||
|
||||
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
|
||||
|
||||
@@ -6,9 +6,22 @@ signal chain_updated(count: int, bonus: float)
|
||||
@export var params: MovementParams = preload("res://movement/movement_params.gd").new()
|
||||
|
||||
var _machine: MovementStateMachine = null
|
||||
var head_pivot: Node3D = null # FPSCameraRig node
|
||||
var camera: Camera3D = null
|
||||
|
||||
|
||||
func _ready() -> void:
|
||||
var sm := _ensure_machine()
|
||||
if sm:
|
||||
sm.player = self
|
||||
sm.params = params
|
||||
sm.movement_event.connect(_on_movement_event)
|
||||
|
||||
# Find camera rig
|
||||
head_pivot = get_node_or_null("HeadPivot")
|
||||
if head_pivot:
|
||||
camera = head_pivot.get_node_or_null("Camera3D")
|
||||
|
||||
# Explicitly ensure this node ticks after being attached by set_script().
|
||||
func _ensure_process() -> void:
|
||||
set_process(true)
|
||||
set_physics_process(true)
|
||||
|
||||
@@ -25,49 +38,35 @@ func _ensure_machine() -> MovementStateMachine:
|
||||
return null
|
||||
|
||||
|
||||
func _unhandled_input(event: InputEvent) -> void:
|
||||
func _physics_process(_delta: float) -> void:
|
||||
var sm := _ensure_machine()
|
||||
if not sm:
|
||||
return
|
||||
|
||||
# Poll input in _physics_process so it's synchronized with the state machine tick
|
||||
sm.input_jump_just_pressed = Input.is_action_just_pressed("jump")
|
||||
sm.input_jump_pressed = Input.is_action_pressed("jump")
|
||||
sm.input_sprint = Input.is_action_pressed("sprint")
|
||||
sm.input_crouch = Input.is_action_pressed("crouch")
|
||||
sm.input_dash = Input.is_action_just_pressed("dash")
|
||||
sm.input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
||||
|
||||
# Raw 2D input
|
||||
var raw_input := Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
||||
|
||||
func _ready() -> void:
|
||||
var sm := _ensure_machine()
|
||||
if sm:
|
||||
sm.player = self
|
||||
sm.params = params
|
||||
sm.movement_event.connect(_on_movement_event)
|
||||
_ensure_process()
|
||||
# Transform input direction by player yaw so movement is camera-relative
|
||||
var forward := -global_transform.basis.z
|
||||
forward.y = 0.0
|
||||
forward = forward.normalized()
|
||||
var right := global_transform.basis.x
|
||||
right.y = 0.0
|
||||
right = right.normalized()
|
||||
|
||||
var world_dir := (forward * (-raw_input.y) + right * raw_input.x)
|
||||
if world_dir.length_squared() > 1.0:
|
||||
world_dir = world_dir.normalized()
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
var sm := _ensure_machine()
|
||||
if not sm:
|
||||
return
|
||||
sm.input_jump_just_pressed = Input.is_action_just_pressed("jump")
|
||||
sm.input_jump_pressed = Input.is_action_pressed("jump")
|
||||
sm.input_sprint = Input.is_action_pressed("sprint")
|
||||
sm.input_crouch = Input.is_action_pressed("crouch")
|
||||
sm.input_dash = Input.is_action_just_pressed("dash")
|
||||
sm.input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
|
||||
|
||||
# DEBUG: emit every frame so we can see whether inputs reach the controller.
|
||||
var p = sm.player
|
||||
print("MOVEMENT_DEBUG state=%s vel=%s input_dir=%s jump=%s sprint=%s crouch=%s dash=%s" % [
|
||||
sm.current_state,
|
||||
p.velocity if p else "NO_PLAYER",
|
||||
sm.input_dir,
|
||||
sm.input_jump_pressed,
|
||||
sm.input_sprint,
|
||||
sm.input_crouch,
|
||||
sm.input_dash,
|
||||
])
|
||||
sm.input_dir = raw_input
|
||||
sm.wish_dir_world = world_dir
|
||||
|
||||
|
||||
func _on_movement_event(ev: String, data: Dictionary) -> void:
|
||||
|
||||
@@ -7,7 +7,7 @@ var params: MovementParams:
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
pass
|
||||
machine.on_ground = false
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -15,56 +15,89 @@ func exit() -> void:
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# Apply standard gravity
|
||||
vel.y -= params.gravity * delta
|
||||
|
||||
# Input direction
|
||||
var input_v := machine.input_dir
|
||||
var wish_dir := Vector3(input_v.x, 0.0, input_v.y)
|
||||
if wish_dir.length_squared() > 1.0:
|
||||
wish_dir = wish_dir.normalized()
|
||||
|
||||
var effective_speed := machine.get_effective_speed(params.max_air_speed)
|
||||
# ── Gravity with variable jump height ─────────────────────────────────
|
||||
var grav := params.gravity
|
||||
if vel.y < 0.0:
|
||||
# Falling: heavier gravity for snappy arcs
|
||||
grav *= params.fall_multiplier
|
||||
elif vel.y > 0.0 and not machine.input_jump_pressed:
|
||||
# Released jump early: cut the jump short
|
||||
grav *= params.low_jump_multiplier
|
||||
vel.y -= grav * delta
|
||||
|
||||
# ── Air control ───────────────────────────────────────────────────────
|
||||
var wish_dir: Vector3 = machine.wish_dir_world
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
var target_vel := wish_dir * effective_speed * params.air_control
|
||||
var current_speed := hvel.length()
|
||||
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-params.air_acceleration * delta))
|
||||
if wish_dir.length_squared() > 0.01:
|
||||
var target_speed := maxf(current_speed, params.walk_speed)
|
||||
target_speed = minf(target_speed, params.max_air_speed)
|
||||
|
||||
var target_vel := wish_dir * target_speed
|
||||
var new_hvel = hvel.lerp(target_vel, 1.0 - exp(-params.air_control * params.air_acceleration * delta))
|
||||
|
||||
# Prevent speed loss during sharp turns, but allow braking if holding backward
|
||||
if hvel.dot(target_vel) >= 0.0 and current_speed > params.walk_speed:
|
||||
if new_hvel.length_squared() > 0.01:
|
||||
new_hvel = new_hvel.normalized() * current_speed
|
||||
|
||||
hvel = new_hvel
|
||||
else:
|
||||
# No input: slight air drag (very subtle)
|
||||
hvel *= (1.0 - 0.5 * delta)
|
||||
|
||||
vel.x = hvel.x
|
||||
vel.z = hvel.z
|
||||
|
||||
# Bunny hop: if jump pressed and on ground near-peak of arc → extra boost
|
||||
if machine.input_jump_just_pressed and machine.on_ground:
|
||||
vel.y = params.jump_velocity * params.bunny_hop_impulse
|
||||
machine.player.velocity = vel
|
||||
machine.register_chain_mechanic("bunny_hop")
|
||||
machine.current_jump_count += 1
|
||||
machine.on_ground = false
|
||||
machine.switch_to("air")
|
||||
return
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# Double Jump
|
||||
if machine.input_jump_just_pressed and not machine.on_ground and machine.current_jump_count < params.double_jump_max_count:
|
||||
vel.y = params.double_jump_velocity
|
||||
machine.player.velocity = vel
|
||||
machine.current_jump_count += 1
|
||||
machine.register_chain_mechanic("double_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
|
||||
# Landing
|
||||
if machine.player.is_on_floor():
|
||||
# ── Landing ───────────────────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.current_jump_count = 0
|
||||
|
||||
# Bunny hop: if jump was buffered or pressed on landing frame
|
||||
if machine.input_jump_pressed or machine.jump_buffer_time > 0.0:
|
||||
var land_speed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
var bhop_speed := minf(land_speed + params.bunny_hop_speed_gain, params.bunny_hop_speed_cap)
|
||||
# Maintain horizontal direction, boost speed
|
||||
var hdir := Vector3(player.velocity.x, 0.0, player.velocity.z)
|
||||
if hdir.length_squared() > 0.01:
|
||||
hdir = hdir.normalized()
|
||||
else:
|
||||
hdir = machine.wish_dir_world
|
||||
player.velocity.x = hdir.x * bhop_speed
|
||||
player.velocity.z = hdir.z * bhop_speed
|
||||
player.velocity.y = params.jump_velocity * params.bunny_hop_impulse
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.jump_buffer_time = 0.0
|
||||
machine.register_chain_mechanic("bunny_hop")
|
||||
# Stay in air state
|
||||
return
|
||||
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
|
||||
# Wall run transition
|
||||
if not machine.wall_normal.is_zero_approx():
|
||||
# ── Double Jump ───────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed and machine.current_jump_count < params.double_jump_max_count:
|
||||
player.velocity.y = params.double_jump_velocity
|
||||
machine.current_jump_count += 1
|
||||
machine.register_chain_mechanic("double_jump")
|
||||
return
|
||||
|
||||
# ── Wall run transition ───────────────────────────────────────────────
|
||||
var wall_n := machine.detect_wall_horizontal()
|
||||
if wall_n != Vector3.ZERO and machine.input_dir.length() > 0.1 and machine.wall_cooldown_timer <= 0.0:
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# ── Dash ──────────────────────────────────────────────────────────────
|
||||
if machine.input_dash:
|
||||
machine.switch_to("dash")
|
||||
return
|
||||
|
||||
@@ -7,39 +7,54 @@ var params: MovementParams:
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var direction: Vector3 = Vector3.ZERO
|
||||
var dash_cooldown_timer: float = 0.0
|
||||
var _exit_speed: float = 0.0
|
||||
|
||||
# Cooldown tracked across dash instances
|
||||
static var _last_dash_time: float = -999.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
|
||||
dash_cooldown_timer = params.dash_cooldown
|
||||
var input_v := machine.input_dir
|
||||
var wish_dir := Vector3(input_v.x, 0.0, -input_v.y)
|
||||
if wish_dir.length_squared() > 0.0:
|
||||
direction = wish_dir.normalized()
|
||||
_last_dash_time = now
|
||||
|
||||
# Direction: use wish direction if available, else player forward
|
||||
var wish := machine.wish_dir_world
|
||||
if wish.length_squared() > 0.01:
|
||||
direction = wish.normalized()
|
||||
else:
|
||||
direction = -machine.player.global_transform.basis.z
|
||||
direction.y = 0.0
|
||||
direction = direction.normalized()
|
||||
direction = (machine.player.global_transform.basis * Vector3(direction.x, 0.0, direction.y)).normalized()
|
||||
|
||||
machine.register_chain_mechanic("dash")
|
||||
machine.player.velocity = direction * machine.get_effective_speed(params.dash_speed)
|
||||
_exit_speed = machine.get_effective_speed(params.dash_speed)
|
||||
machine.player.velocity = direction * _exit_speed
|
||||
machine.on_ground = false
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
# Preserve dash velocity on exit (don't cut speed abruptly)
|
||||
pass
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
dash_cooldown_timer -= delta
|
||||
if elapsed > params.dash_duration:
|
||||
# Transition to air with preserved velocity (slight decay)
|
||||
machine.player.velocity = direction * _exit_speed * 0.85
|
||||
machine.switch_to("air")
|
||||
return
|
||||
# Skip gravity, maintain dash velocity
|
||||
machine.player.velocity = direction * machine.get_effective_speed(params.dash_speed)
|
||||
|
||||
# Maintain dash velocity — no gravity, no friction
|
||||
machine.player.velocity = direction * _exit_speed
|
||||
machine.player.move_and_slide()
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.switch_to("ground")
|
||||
|
||||
@@ -7,7 +7,15 @@ var params: MovementParams:
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
pass
|
||||
machine.on_ground = true
|
||||
machine.current_jump_count = 0
|
||||
# Clear wall tilt on landing
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.clear_wall_tilt()
|
||||
|
||||
# Ensure capsule is correct when entering ground state
|
||||
_update_capsule_height()
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -15,96 +23,113 @@ func exit() -> void:
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
|
||||
# Input direction
|
||||
var input_v := machine.input_dir
|
||||
var wish_dir := Vector3(input_v.x, 0.0, input_v.y)
|
||||
if wish_dir.length_squared() > 1.0:
|
||||
wish_dir = wish_dir.normalized()
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# ── Determine target speed ────────────────────────────────────────────
|
||||
var wish_dir: Vector3 = machine.wish_dir_world
|
||||
var speed := params.walk_speed
|
||||
|
||||
if machine.input_sprint and wish_dir.length_squared() > 0.1:
|
||||
if machine.input_crouch:
|
||||
speed = params.crouch_speed
|
||||
elif machine.input_sprint and wish_dir.length_squared() > 0.01:
|
||||
speed = params.sprint_speed
|
||||
|
||||
var effective_speed := machine.get_effective_speed(speed)
|
||||
|
||||
# Horizontal movement
|
||||
# ── Horizontal movement (exponential interpolation for smoothness) ───
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
var target_vel := wish_dir * effective_speed
|
||||
|
||||
if wish_dir.length_squared() > 0.0:
|
||||
var accel := params.ground_acceleration
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-accel * delta))
|
||||
hvel = hvel.lerp(target_vel, 1.0 - exp(-params.ground_acceleration * delta))
|
||||
else:
|
||||
hvel = hvel.lerp(Vector3.ZERO, 1.0 - exp(-params.ground_deceleration * delta))
|
||||
|
||||
vel.x = hvel.x
|
||||
vel.z = hvel.z
|
||||
|
||||
# Gravity (we apply tuned gravity so we don't double-apply from _velocity)
|
||||
var actual_gravity := params.gravity
|
||||
if vel.y < 0.0:
|
||||
vel.y += actual_gravity * params.fall_multiplier * delta
|
||||
elif vel.y > 0.0 and not machine.input_jump_pressed:
|
||||
vel.y += actual_gravity * params.low_jump_multiplier * delta
|
||||
# ── Gravity (correct direction: downward) ─────────────────────────────
|
||||
if not player.is_on_floor():
|
||||
vel.y -= params.gravity * delta
|
||||
else:
|
||||
vel.y -= actual_gravity * delta
|
||||
# Snap to floor: small downward velocity keeps is_on_floor() stable
|
||||
vel.y = -0.5
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
var on_wall := detect_wall()
|
||||
if on_wall and machine.input_dir.length() > 0.0:
|
||||
machine.wall_normal = on_wall
|
||||
machine.switch_to("wall_run")
|
||||
_update_capsule_height()
|
||||
|
||||
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0:
|
||||
machine.player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Update ground status ──────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.last_ground_time = machine.get_process_delta_time()
|
||||
machine.current_jump_count = 0
|
||||
else:
|
||||
machine.on_ground = false
|
||||
if machine.input_jump_just_pressed and machine.jump_buffer_time > 0.0:
|
||||
machine.player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
elif vel.y < 0.0:
|
||||
machine.switch_to("air")
|
||||
|
||||
# Slide
|
||||
if machine.input_crouch and machine.input_sprint and machine.player.velocity.length() > params.slide_min_speed:
|
||||
machine.switch_to("slide")
|
||||
# ── Jump (coyote time + jump buffer) ──────────────────────────────────
|
||||
if machine.input_jump_just_pressed and machine.coyote_timer > 0.0:
|
||||
player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.coyote_timer = 0.0
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Dash
|
||||
if machine.input_dash and machine.player.velocity.length() > 0.0:
|
||||
machine.switch_to("dash")
|
||||
# Jump buffer: player pressed jump just before landing
|
||||
if machine.on_ground and machine.jump_buffer_time > 0.0:
|
||||
player.velocity.y = params.jump_velocity
|
||||
machine.current_jump_count = 1
|
||||
machine.on_ground = false
|
||||
machine.jump_buffer_time = 0.0
|
||||
machine.register_chain_mechanic("jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Falling off ledge → air state ─────────────────────────────────────
|
||||
if not machine.on_ground and machine.coyote_timer <= 0.0:
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Slide (crouch while moving fast enough) ───────────────────────────
|
||||
if machine.input_crouch and machine.slide_cooldown_timer <= 0.0:
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
if hspeed > params.slide_min_speed:
|
||||
machine.switch_to("slide")
|
||||
return
|
||||
|
||||
# ── Wall run check ────────────────────────────────────────────────────
|
||||
var wall_n := machine.detect_wall_horizontal()
|
||||
if wall_n != Vector3.ZERO and machine.input_dir.length() > 0.1:
|
||||
if not player.is_on_floor():
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# ── Dash ──────────────────────────────────────────────────────────────
|
||||
if machine.input_dash:
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
if hspeed > 0.1 or machine.input_dir.length() > 0.1:
|
||||
machine.switch_to("dash")
|
||||
return
|
||||
|
||||
|
||||
func detect_wall() -> Vector3:
|
||||
var origin := machine.player.global_position
|
||||
var hvel := Vector3(machine.player.velocity.x, 0.0, machine.player.velocity.z).normalized()
|
||||
if hvel.length_squared() < 0.01:
|
||||
hvel = Vector3.FORWARD
|
||||
var space_state := machine.player.get_world_3d().direct_space_state
|
||||
for offset_sign in [1.0, -1.0]:
|
||||
var dir: Vector3 = (hvel.cross(Vector3.UP) * offset_sign).normalized()
|
||||
var offset: Vector3 = dir * 0.4
|
||||
var ray := PhysicsRayQueryParameters3D.create(origin + offset + Vector3.UP * params.wall_ray_up_height, origin + offset - Vector3.DOWN * params.wall_ray_down_height)
|
||||
ray.exclude = [machine.player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
var n: Vector3 = hit.get("normal", Vector3.ZERO)
|
||||
if n.y < 0.3 and n.length_squared() > 0.0:
|
||||
return n.normalized()
|
||||
return Vector3.ZERO
|
||||
func _get_camera_rig():
|
||||
if machine.player and machine.player.has_node("HeadPivot"):
|
||||
return machine.player.get_node("HeadPivot")
|
||||
return null
|
||||
|
||||
|
||||
func _update_capsule_height() -> void:
|
||||
var shape = _get_capsule()
|
||||
if shape:
|
||||
if machine.input_crouch:
|
||||
shape.height = machine.original_capsule_height * 0.5
|
||||
else:
|
||||
shape.height = machine.original_capsule_height
|
||||
|
||||
|
||||
func _get_capsule() -> CapsuleShape3D:
|
||||
for child in machine.player.get_children():
|
||||
if child is CollisionShape3D and child.shape is CapsuleShape3D:
|
||||
return child.shape as CapsuleShape3D
|
||||
return null
|
||||
|
||||
@@ -6,55 +6,105 @@ 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
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
elapsed = 0.0
|
||||
machine.register_chain_mechanic("slide")
|
||||
# Lower player capsule
|
||||
var player_node = machine.player
|
||||
var shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
|
||||
# Save original capsule height and halve it
|
||||
var shape: CapsuleShape3D = _get_capsule()
|
||||
if shape:
|
||||
shape.height = shape.height * 0.5
|
||||
_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:
|
||||
_slide_direction = hvel.normalized()
|
||||
else:
|
||||
# Fallback: player forward
|
||||
_slide_direction = -machine.player.global_transform.basis.z
|
||||
_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)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
machine.slide_cooldown_timer = params.slide_cooldown
|
||||
# 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 vel: Vector3 = machine.player.velocity
|
||||
var forward: Vector3 = -machine.player.global_transform.basis.z
|
||||
if forward.length_squared() > 1.0:
|
||||
forward = forward.normalized()
|
||||
vel.x = forward.x * params.slide_speed * machine.get_effective_speed(1.0)
|
||||
vel.z = forward.z * params.slide_speed * machine.get_effective_speed(1.0)
|
||||
vel.x *= pow(params.slide_friction, delta)
|
||||
vel.z *= pow(params.slide_friction, delta)
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
var speed := Vector3(vel.x, 0.0, vel.z).length()
|
||||
if speed < params.slide_min_speed or elapsed > params.slide_duration:
|
||||
var end_shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
if end_shape:
|
||||
end_shape.height = end_shape.height * 2.0
|
||||
machine.register_chain_mechanic("slide_end")
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
# ── Apply friction to horizontal velocity ─────────────────────────────
|
||||
vel.x *= pow(params.slide_friction, delta * 10.0)
|
||||
vel.z *= pow(params.slide_friction, delta * 10.0)
|
||||
|
||||
# ── Gravity (for slopes) ──────────────────────────────────────────────
|
||||
if not player.is_on_floor():
|
||||
vel.y -= params.gravity * delta
|
||||
else:
|
||||
var fall_shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
if fall_shape:
|
||||
fall_shape.height = fall_shape.height * 2.0
|
||||
machine.on_ground = false
|
||||
machine.switch_to("air")
|
||||
vel.y = -2.0 # Strong floor snap
|
||||
|
||||
if not machine.input_crouch or not machine.input_sprint:
|
||||
var cancel_shape: CapsuleShape3D = machine.player.shape_owner_get_shape(0, 0)
|
||||
if cancel_shape:
|
||||
cancel_shape.height = cancel_shape.height * 2.0
|
||||
machine.register_chain_mechanic("slide_cancel")
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Update ground status ──────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.current_jump_count = 0
|
||||
else:
|
||||
machine.on_ground = false
|
||||
|
||||
# ── End conditions ────────────────────────────────────────────────────
|
||||
var hspeed := Vector3(player.velocity.x, 0.0, player.velocity.z).length()
|
||||
|
||||
# Slide ended: too slow
|
||||
if hspeed < params.slide_min_speed:
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
|
||||
# Player released crouch
|
||||
if not machine.input_crouch:
|
||||
machine.switch_to("ground")
|
||||
return
|
||||
|
||||
# Fell off edge
|
||||
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:
|
||||
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 jump_speed := minf(hvel.length() + 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.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
|
||||
|
||||
@@ -6,11 +6,15 @@ var params: MovementParams:
|
||||
get: return machine.params
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var stamina: float = 0.0
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
@@ -19,34 +23,56 @@ func exit() -> void:
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
vel.x = params.wall_cling_horizontal_speed
|
||||
vel.y = params.wall_cling_vertical_speed
|
||||
vel.z = params.wall_cling_horizontal_speed
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
stamina -= params.wall_cling_stamina_drain * delta
|
||||
|
||||
# Jump off wall
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# ── Slow slide down ───────────────────────────────────────────────────
|
||||
var slide_factor := 1.0 - (stamina / params.wall_cling_max_stamina) # 0→1 as stamina drains
|
||||
vel.x = 0.0
|
||||
vel.z = 0.0
|
||||
vel.y = params.wall_cling_slide_speed * (1.0 + slide_factor * 2.0)
|
||||
|
||||
# Push into wall to maintain contact
|
||||
vel -= machine.wall_normal * 1.0
|
||||
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Stamina depleted → fall ───────────────────────────────────────────
|
||||
if stamina <= 0.0:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Jump off wall ─────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed:
|
||||
var push_dir := machine.wall_normal * params.wall_run_jump_off_normal
|
||||
var vel2 := Vector3(push_dir.x, params.jump_velocity, push_dir.z)
|
||||
machine.player.velocity = vel2
|
||||
player.velocity = Vector3(push_dir.x, params.jump_velocity, push_dir.z)
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.register_chain_mechanic("wall_cling_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Re-enter wall run with input
|
||||
# ── Re-enter wall run with movement input ─────────────────────────────
|
||||
if machine.input_dir.length() > 0.1:
|
||||
machine.switch_to("wall_run")
|
||||
return
|
||||
|
||||
# Fall
|
||||
if not machine.wall_normal or machine.wall_normal.is_zero_approx():
|
||||
# ── Lost wall contact ─────────────────────────────────────────────────
|
||||
var wall := machine.detect_wall_horizontal()
|
||||
if wall == Vector3.ZERO:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Hit floor ─────────────────────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.register_chain_mechanic("cling_land")
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("ground")
|
||||
|
||||
@@ -1,87 +1,114 @@
|
||||
extends Node
|
||||
class_name StateWallRun
|
||||
|
||||
const FLAG: int = 0 # unused
|
||||
|
||||
var machine: MovementStateMachine
|
||||
var params: MovementParams:
|
||||
get: return machine.params
|
||||
|
||||
var elapsed: float = 0.0
|
||||
var _initial_y_vel: float = 0.0
|
||||
|
||||
|
||||
func enter(_data: Dictionary = {}) -> void:
|
||||
elapsed = 0.0
|
||||
machine.register_chain_mechanic("wall_run")
|
||||
machine.player.velocity.y = params.wall_run_vertical_speed
|
||||
# Cap the upward momentum so they don't fly up the wall,
|
||||
# but preserve some if they just jumped onto it.
|
||||
machine.player.velocity.y = minf(machine.player.velocity.y, 1.5)
|
||||
machine.on_ground = false
|
||||
|
||||
# Camera tilt
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.set_wall_tilt(machine.wall_side)
|
||||
|
||||
|
||||
func exit() -> void:
|
||||
pass
|
||||
# Clear camera tilt
|
||||
var rig = _get_camera_rig()
|
||||
if rig:
|
||||
rig.clear_wall_tilt()
|
||||
|
||||
|
||||
func update(delta: float) -> void:
|
||||
elapsed += delta
|
||||
if elapsed > params.wall_run_duration:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
var vel: Vector3 = machine.player.velocity
|
||||
var player := machine.player
|
||||
var vel: Vector3 = player.velocity
|
||||
|
||||
# Stick to wall
|
||||
vel.y = min(vel.y, params.wall_run_vertical_speed)
|
||||
# ── Gradual gravity pull (starts light, increases over time) ──────────
|
||||
var gravity_factor := 0.1 + 0.9 * (elapsed / params.wall_run_duration)
|
||||
vel.y -= params.wall_run_gravity * gravity_factor * delta
|
||||
|
||||
# Move along wall: project velocity onto wall tangent
|
||||
var wall_tangent := (machine.wall_normal.cross(Vector3.UP)).normalized()
|
||||
if machine.input_dir.y < 0.0:
|
||||
# ── Move along wall tangent ───────────────────────────────────────────
|
||||
var wall_tangent := machine.wall_normal.cross(Vector3.UP).normalized()
|
||||
# Choose tangent direction based on player's movement direction
|
||||
var hvel := Vector3(vel.x, 0.0, vel.z)
|
||||
if hvel.dot(wall_tangent) < 0.0:
|
||||
wall_tangent = -wall_tangent
|
||||
var move_dir: Vector3 = wall_tangent * params.wall_run_speed
|
||||
vel.x = move_dir.x
|
||||
vel.z = move_dir.z
|
||||
|
||||
if machine.input_jump_just_pressed:
|
||||
var jump_vel: Vector3 = vel + (machine.wall_normal * params.wall_run_jump_off_normal)
|
||||
jump_vel.y = params.wall_run_auto_jump_speed
|
||||
machine.player.velocity = jump_vel
|
||||
var effective_speed := machine.get_effective_speed(params.wall_run_speed)
|
||||
vel.x = wall_tangent.x * effective_speed
|
||||
vel.z = wall_tangent.z * effective_speed
|
||||
|
||||
# ── Look away to break wall run ───────────────────────────────────────
|
||||
var look_dir := -player.global_transform.basis.z
|
||||
if look_dir.dot(machine.wall_normal) > 0.4:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# ── Push slightly toward wall to maintain contact ─────────────────────
|
||||
vel -= machine.wall_normal * 2.0
|
||||
|
||||
# ── Wall jump ─────────────────────────────────────────────────────────
|
||||
if machine.input_jump_just_pressed:
|
||||
var jump_vel: Vector3 = machine.wall_normal * params.wall_run_jump_off_normal
|
||||
var h_look := Vector3(look_dir.x, 0.0, look_dir.z).normalized()
|
||||
jump_vel += h_look * params.wall_run_jump_horizontal
|
||||
jump_vel.y = params.wall_run_auto_jump_speed
|
||||
player.velocity = jump_vel
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.wall_cooldown_timer = 0.3
|
||||
machine.register_chain_mechanic("wall_jump")
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
# Wall cling fall-through
|
||||
# ── Wall cling (no movement input) ────────────────────────────────────
|
||||
if machine.input_dir.length() < 0.1:
|
||||
machine.switch_to("wall_cling")
|
||||
return
|
||||
|
||||
# Lose wall contact
|
||||
var still_on_wall: bool = machine.wall_normal != Vector3.ZERO and detect_wall(machine.wall_normal).length_squared() > 0.0
|
||||
if not still_on_wall:
|
||||
player.velocity = vel
|
||||
player.move_and_slide()
|
||||
|
||||
# ── Check still on wall ───────────────────────────────────────────────
|
||||
var still_on_wall := machine.detect_wall_horizontal()
|
||||
if still_on_wall == Vector3.ZERO:
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.register_chain_mechanic("wall_run_off")
|
||||
machine.wall_side = 0.0
|
||||
machine.switch_to("air")
|
||||
return
|
||||
|
||||
machine.player.velocity = vel
|
||||
machine.player.move_and_slide()
|
||||
|
||||
if machine.player.is_on_floor():
|
||||
# ── Hit floor during wall run ─────────────────────────────────────────
|
||||
if player.is_on_floor():
|
||||
machine.on_ground = true
|
||||
machine.wall_normal = Vector3.ZERO
|
||||
machine.wall_side = 0.0
|
||||
machine.current_jump_count = 0
|
||||
machine.register_chain_mechanic("wall_run_land")
|
||||
machine.switch_to("ground")
|
||||
|
||||
|
||||
func detect_wall(expected_normal: Vector3) -> Vector3:
|
||||
var origin := machine.player.global_position
|
||||
var offset := expected_normal.cross(Vector3.UP).normalized()
|
||||
var space_state := machine.player.get_world_3d().direct_space_state
|
||||
for side in [1.0, -1.0]:
|
||||
var ray_origin: Vector3 = origin + offset * side * params.wall_detect_distance + Vector3.UP * params.wall_ray_up_height
|
||||
var ray_end: Vector3 = origin + offset * side * params.wall_detect_distance - Vector3.DOWN * params.wall_ray_down_height
|
||||
var ray := PhysicsRayQueryParameters3D.create(ray_origin, ray_end)
|
||||
ray.exclude = [machine.player.get_rid()]
|
||||
var hit := space_state.intersect_ray(ray)
|
||||
if not hit.is_empty():
|
||||
return hit.get("normal", Vector3.ZERO).normalized()
|
||||
return Vector3.ZERO
|
||||
func _get_camera_rig():
|
||||
if machine.player and machine.player.has_node("HeadPivot"):
|
||||
return machine.player.get_node("HeadPivot")
|
||||
return null
|
||||
|
||||
Reference in New Issue
Block a user