extends CharacterBody3D
class_name Character

@export_group("Movement Settings")
@export var normal_speed: float = 6.0
@export var sprint_speed: float = 10.0
@export var jump_velocity: float = 10.0
@export var gravity_scale: float = 1.0  # Позволяет регулировать гравитацию
@export var rotation_speed: float = 10.0

@export_group("References")
@export var body: Node3D = null
@export var spring_arm_offset: Node3D = null
@export var respawn_point: Vector3 = Vector3(0, 5, 0)

var _current_speed: float
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
	
func _ready():
	$SpringArmOffset/SpringArm3D/Camera3D.current = false
	
func _physics_process(delta):	
	var current_scene = get_tree().get_current_scene()
	
	if not is_on_floor():
		velocity.y -= gravity * delta
		
	if is_on_floor():
		if Input.is_action_just_pressed("jump"):
			velocity.y = jump_velocity
	else:
		velocity.y -= gravity * delta
	
	_move()
	move_and_slide()
	
	if body and body.has_method("animate"):
		body.animate(velocity)
	
func _process(_delta):
	_check_fall_and_respawn()
	
func freeze():
	velocity.x = 0
	velocity.z = 0
	_current_speed = 0
	
func _move() -> void:
	var input_direction = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
	
	# Получаем направление движения относительно камеры
	var camera_basis = $SpringArmOffset/SpringArm3D/Camera3D.global_transform.basis
	var direction = (camera_basis * Vector3(input_direction.x, 0, input_direction.y)).normalized()
	
	_update_speed()
	
	if direction:
		velocity.x = direction.x * _current_speed
		velocity.z = direction.z * _current_speed
		
		# Поворачиваем тело персонажа в направлении движения
		if velocity.length() > 0.1:  # Только если есть значительное движение
			var target_rotation = atan2(velocity.x, velocity.z)
			body.rotation.y = lerp_angle(body.rotation.y, target_rotation, rotation_speed * get_process_delta_time())
	
	else:
		velocity.x = move_toward(velocity.x, 0, _current_speed)
		velocity.z = move_toward(velocity.z, 0, _current_speed)
		
func is_running() -> bool:
	return Input.is_action_pressed("shift") and _current_speed == sprint_speed
	
func _update_speed() -> void:
	_current_speed = sprint_speed if Input.is_action_pressed("shift") else normal_speed
		
func _check_fall_and_respawn():
	if global_transform.origin.y < -15.0:
		_respawn()
		
func _respawn():
	global_transform.origin = respawn_point
	velocity = Vector3.ZERO
