extends CharacterBody3D

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

@onready var cam_pointer = $camPointer
@onready var player = $Player

func _ready():
	Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
	player.animation("idle")

func _physics_process(delta: float):
	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	#var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
	#
	#input_dir = cam_pointer
	
	var direction := Vector3(0, 0, cam_pointer.rotation.y).normalized()
	print(direction)
	
	if Input.is_action_pressed("ui_up"):
		direction = (transform.basis * direction)
	else:
		direction *= 0
	
	if direction:
		player.animation("walk")
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	else:
		player.animation("idle")
		velocity.x = move_toward(velocity.x, 0, SPEED)
		velocity.z = move_toward(velocity.z, 0, SPEED)

	move_and_slide()
	
func _input(event):
	if event is InputEventMouseMotion:
		cam_pointer.rotation.y -= event.relative.x * .01
		cam_pointer.rotation.x -= event.relative.y * .01
