extends NodeState

@export var player: Player
@export var animatedSprite2D: AnimatedSprite2D
@export var collisionShapeMove: AnimationPlayer

const GRAVITY = player.GRAVITY
@onready var speed: float = player.speed /3
@onready var maxSpeed: float = player.maxSpeed /3

@warning_ignore("unused_parameter")
func on_process(delta: float) -> void:
	pass

func on_phyisics_process(delta: float) -> void:
	var direction = GameInputEvents.movement_input()
	
	if direction:
		player.velocity.x += direction * speed * delta
		player.velocity.x = clamp(player.velocity.x, -maxSpeed, maxSpeed)
	
	if direction != 0:
		animatedSprite2D.flip_h = false if direction > 0 else true
	
	
	transition_states(direction)
	

func enter() -> void:
	collisionShapeMove.play("Crouch")
	animatedSprite2D.play("Crouch Walk")

func exit() -> void:
	animatedSprite2D.stop()

func transition_states(direction) -> void:
	
	#transition states
	
	#fall state
	if !player.is_on_floor():
		collisionShapeMove.play("Normal")
		transition.emit("fall")
	
	#idle state
	if direction == 0:
		transition.emit("crouchidle")
	
	#jump state
	if GameInputEvents.jump_input():
		collisionShapeMove.play("Normal")
		transition.emit("jump")
	
	if not Input.is_action_pressed("crouch"):
		collisionShapeMove.play("Normal")
		transition.emit("run")
