extends CharacterBody3D

@export var speed = 10.0
@export var lateral_speed = 4.0
@export var turn_speed = 2
@export var jump_impulse = 20
@export var fall_acceleration = 50

@onready var playback : AnimationNodeStateMachinePlayback = $Rogue_Hooded/AnimationTree.get("parameters/playback")
@onready var anim : AnimationPlayer = $Rogue_Hooded/AnimationPlayer

var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready():
	playback.travel("Idle")
	#anim.play("Idle")

func _physics_process(delta):
	if not is_on_floor():
		velocity.y -= fall_acceleration * delta
	get_input(delta)
	move_and_slide()
	
func _process(delta):
	if not is_on_floor():
		playback.travel("Jump_Full_Short")
		#anim.play("Jump_Full_Short")
	else:
		if velocity != Vector3.ZERO:
			playback.travel("Walking_B")
			#anim.play("Walking_B")
		else:
			playback.travel("Idle")
			#anim.play("Idle")

func get_input(delta):
	var vy = velocity.y
	velocity = Vector3.ZERO
	var move_z = Input.get_axis("move_forward", "move_back")
	var move_x = Input.get_axis("move_left", "move_right")
	var turn = Input.get_axis("spin_right", "spin_left")
	velocity += -transform.basis.z * move_z * speed
	velocity += -transform.basis.x * move_x * speed
	rotate_y(turn_speed * turn * delta)
	velocity.y = vy
	
	if is_on_floor() and Input.is_action_just_pressed("jump"):
		velocity.y = jump_impulse

func align_with_y(xform, new_y):
	xform.basis.y = new_y
	xform.basis.x = -xform.basis.z.cross(new_y)
	xform.basis = xform.basis.orthonormalized()
	return xform
