[gd_scene load_steps=5 format=3 uid="uid://cdmksnsttot3j"]

[sub_resource type="GDScript" id="GDScript_j6w7d"]
script/source = "extends CharacterBody3D

@export var speed : float = 10.0
@export var target_distance : float = 5.0
@export var detect_distance : float = 100.0
@export var max_home_distance : float = 50.0
@onready var player := $\"../Player\"
@onready var last_velocity : Vector3 = player.velocity
@onready var direction := 1.0 if randi() % 2 == 0 else -1.0
@onready var home := global_position
var homing : bool = true

func _process(_delta : float):
	if homing || global_position.distance_squared_to(home) > max_home_distance * max_home_distance:
		self.homing = true
		self.process_homing()
	elif player.global_position.distance_squared_to(global_position) > detect_distance * detect_distance:
		self.homing = true
	elif player.velocity.normalized().dot(global_position - player.global_position) > -10.0:
		self.process_ahead()
	else:
		self.process_behind()

func process_homing():
	if player.global_position.distance_squared_to(global_position) < detect_distance * detect_distance:
		self.homing = false
	else:
		self.velocity = (Vector3(home.x, global_position.y, home.z) - global_position).normalized() * speed

func process_ahead():
	var target : Vector3 = player.global_position + last_velocity
	target.y = self.global_position.y
	var forward : Vector3 = player.velocity.normalized()
	if player.global_position.distance_squared_to(self.global_position) < target_distance * target_distance or abs((player.global_position - global_position).dot(forward.cross(Vector3.UP))) < 0.25:
		self.velocity = Vector3()
		return
	if !forward.is_zero_approx():
		last_velocity = player.velocity
		forward.y = 0.0
		forward = forward.normalized()
		self.global_basis = Basis(Vector3.UP.cross(forward), Vector3.UP, forward)
	self.velocity = (target - self.global_position).normalized() * speed

func process_behind():
	var diff : Vector3 = player.global_position - global_position
	var forward := diff.normalized()
	var left := Vector3.UP.cross(forward)
	self.velocity = forward + left * direction * min(10.0, diff.length())
	self.look_at(Vector3(player.global_position.x, global_position.y, player.global_position.z))

func _physics_process(_delta : float):
	self.velocity += Vector3(0, -0.25, 0)
	self.move_and_slide()
"

[sub_resource type="BoxMesh" id="BoxMesh_j6w7d"]
size = Vector3(4.29, 3.77, 2.705)

[sub_resource type="SphereShape3D" id="SphereShape3D_j6w7d"]
radius = 1.83616

[sub_resource type="BoxShape3D" id="BoxShape3D_j6w7d"]
size = Vector3(4.29688, 3.86325, 2.76758)

[node name="WallEnemy" type="CharacterBody3D"]
collision_layer = 2
script = SubResource("GDScript_j6w7d")

[node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.302, 0)
mesh = SubResource("BoxMesh_j6w7d")

[node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.83717, 0)
shape = SubResource("SphereShape3D_j6w7d")

[node name="AnimatableBody3D" type="AnimatableBody3D" parent="."]
collision_layer = 2
collision_mask = 0
sync_to_physics = false

[node name="CollisionShape3D2" type="CollisionShape3D" parent="AnimatableBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.29157, 0)
shape = SubResource("BoxShape3D_j6w7d")
