extends CharacterBody3D

class_name AIBase

@export var speed: float = 4.0
var _enable_state_machine: bool = true
@export var enable_state_machine: bool: 
	set(value):
		_enable_state_machine = value
		toggle_enable_state_machine()
	get:
		return _enable_state_machine


@onready var patrol_radius_shape: CollisionShape3D = $%PatrolRadiusShape
@onready var nav_agent: NavigationAgent3D = $%NavigationAgent3D
@onready var state_machine: StateMachine = $%StateMachine


func _ready() -> void:
	randomize()
	toggle_enable_state_machine()

func toggle_enable_state_machine() -> void:
	state_machine.enable = _enable_state_machine

func _physics_process(delta: float) -> void:
	if !_enable_state_machine:
		return

	state_machine.physics_process(delta)

	if nav_agent.is_navigation_finished():
		velocity = Vector3.ZERO
		move_and_slide()
		return

	var next_point = nav_agent.get_next_path_position()
	var direction = global_position.direction_to(next_point)
	
	velocity.x = direction.x * speed
	velocity.z = direction.z * speed
	
	move_and_slide()

func navigate_to_random_point() -> void:
	var nav_map_rid = get_world_3d().get_navigation_map()
	var patrol_radius = patrol_radius_shape.shape.radius
	var target_point_in_space = global_position + Vector3(randf_range(-1, 1), 0, randf_range(-1, 1)).normalized() * randf_range(0, patrol_radius)
	var closest_valid_point = NavigationServer3D.map_get_closest_point(nav_map_rid, target_point_in_space)
	nav_agent.target_position = closest_valid_point
