32 lines
787 B
GDScript
32 lines
787 B
GDScript
extends Area3D
|
|
|
|
var current_players : Array[Node3D]
|
|
var current_target : Node3D
|
|
signal new_target
|
|
|
|
|
|
func _on_body_entered(body: Node3D) -> void:
|
|
current_players.append(body)
|
|
|
|
func _on_body_exited(body: Node3D) -> void:
|
|
current_players.erase(body)
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
var candidate : Node3D
|
|
for body in current_players:
|
|
if !candidate:
|
|
candidate = body
|
|
else:
|
|
if global_position.distance_to(candidate.global_position) > global_position.distance_to(body.global_position):
|
|
candidate = body
|
|
if is_instance_valid(candidate):
|
|
if candidate == current_target:
|
|
return
|
|
else:
|
|
current_target = candidate
|
|
new_target.emit(current_target)
|
|
else:
|
|
if is_instance_valid(current_target):
|
|
current_target = null
|
|
new_target.emit(current_target)
|