Chelimbalo/scripts/player/states/crouching.gd
2025-11-26 23:04:24 +05:00

39 lines
1.3 KiB
GDScript

extends State
@export var SPEED: float = 2.5
@export var toggle: bool = false
@export var stand_up_area: Area3D
@export var player: Player
@export var animation_player: AnimationPlayer
@export var crouch_time: float = 0.1
func enter() -> void:
animation_player.play("crouch",-1,1/crouch_time)
func exit() -> void:
animation_player.play("crouch",-1,-1/crouch_time,true)
func physics_update(_delta: float) -> void:
if not is_multiplayer_authority():
return
if Input.is_action_just_pressed("plr_jump") and player.is_on_floor() and toggle:
transition.emit("Stand")
return
if not player.is_on_floor():
transition.emit("Fall")
return
var input_dir := Input.get_vector("plr_strafe_r","plr_strafe_l", "plr_back","plr_forward")
var direction := (player.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
player.velocity.x = direction.x * SPEED
player.velocity.z = direction.z * SPEED
else:
player.velocity.x = move_toward(player.velocity.x, 0, SPEED)
player.velocity.z = move_toward(player.velocity.z, 0, SPEED)
func state_input(event: InputEvent) -> void:
if (toggle == true and event.is_action_pressed("plr_crouch")) or (toggle == false and event.is_action_released("plr_crouch")):
if stand_up_area.has_overlapping_bodies() == false:
transition.emit("Stand")