@tool
class_name EditorArrow extends MeshInstance3D

const SCALE_BUFFER := 0.1

@export var target: Node3D

var cached_target_position: Vector3
var cached_position: Vector3


func _process(_delta: float) -> void:
	if (
		is_instance_valid(target)
		and (global_position != cached_position or target.global_position != cached_target_position)
	):
		var up := global_basis.y
		if up.cross(target.global_position).length_squared() < 0.001:
			up = global_basis.x
		look_at(target.global_position, up)
		scale.z = max(0.001, global_position.distance_to(target.global_position) - SCALE_BUFFER)
		cached_target_position = target.global_position
		cached_position = global_position
