Files
Papay-Shooter/movement/states/state_ground.gd
T
2026-08-02 02:20:02 -04:00

179 lines
7.1 KiB
GDScript

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()
func exit() -> void:
pass
func update(delta: float) -> void:
var player := machine.player
var vel: Vector3 = player.velocity
# ── 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 + 1.0:
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 / floor snap ──────────────────────────────────────────────
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:
_try_step_up(player, wish_dir, hvel)
var current_hspeed = Vector2(player.velocity.x, player.velocity.z).length()
if current_hspeed > 1.0 and player.is_on_floor():
_footstep_timer -= delta
if _footstep_timer <= 0.0:
if player.has_method("play_footstep"):
player.play_footstep(current_hspeed)
_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 (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:
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
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 := 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
# ── Dash ──────────────────────────────────────────────────────────────
if machine.input_dash and machine.can_dash():
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