feat: implement player movement controller and state machine with dash, slide, and wall-run mechanics
This commit is contained in:
@@ -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