extends CharacterBody2D

class_name player
@export var gravity = 400
@export var playerSpeed = 200
@export var jumpforce = 200
@onready var animated_sprite = $AnimatedSprite2D
var clampedYVelocity = 500
var active = true


func jump(force):
	AudioPlayer.play_sfx("jump")
	velocity.y = -force

func _process(delta):	
	if Input.is_action_just_pressed("move_right"):
		if velocity.y > clampedYVelocity:
			velocity.y = clampedYVelocity

func _physics_process(delta):
	if is_on_floor() == false:
		velocity.y += gravity * delta
	
	var direction = 0
	if active == true:
		if Input.is_action_just_pressed("jump") && is_on_floor():
			jump(jumpforce)
			
	
		direction = Input.get_axis("move_left","move_right")
	if direction != 0: 
		animated_sprite.flip_h = (direction == -1)
	
	velocity.x = direction * playerSpeed 
	move_and_slide()
	updateAnimations(direction)

func updateAnimations(direction):
	if is_on_floor():
		if direction == 0:
			animated_sprite.play("Idle")
		else:
			animated_sprite.play("Run")
	else:
		if velocity.y <0:
			animated_sprite.play("Jump")
		else:
			animated_sprite.play("Fall")


