extends Node class_name StateGround var machine: MovementStateMachine var params: MovementParams: get: return machine.params var _footstep_timer: float = 0.0 func enter(_data: Dictionary = {}) -> void: machine.on_ground = true machine.current_jump_count = 0 machine.can_wall_climb = true # 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: pass 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 # ── 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 # ── Determine target speed ──────────────────────────────────────────── var wish_dir: Vector3 = machine.wish_dir_world var speed := params.walk_speed if machine.input_crouch: speed = params.crouch_speed var effective_speed := machine.get_effective_speed(speed) # ── 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: 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 (correct direction: downward) ───────────────────────────── if not player.is_on_floor(): vel.y -= params.gravity * delta else: # Snap to floor: small downward velocity keeps is_on_floor() stable # ONLY if we aren't being launched upward by an impulse! if vel.y <= 0.0: vel.y = -0.5 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() var current_hspeed = Vector2(player.velocity.x, player.velocity.z).length() if current_hspeed > 1.0: _footstep_timer -= delta if _footstep_timer <= 0.0: if player.footstep_player: player.footstep_player.play() _footstep_timer = max(0.2, 3.0 / current_hspeed) else: _footstep_timer = 0.0 # ── Update ground status ────────────────────────────────────────────── if player.is_on_floor(): machine.on_ground = true machine.current_jump_count = 0 else: machine.on_ground = false # ── Launch or Falling off ledge → air state ─────────────────────────── if not machine.on_ground: if player.velocity.y > 0.0 or machine.coyote_timer <= 0.0: 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: 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.switch_to("air") 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: 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 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