## OutfitItem — HoldableItem that applies an outfit layer to a Character when dropped
## within OUTFIT_APPLY_RADIUS of the character's center. Falls back to hand slot
## attachment if no character body is in range.
class_name OutfitItem extends HoldableItem

const OUTFIT_APPLY_RADIUS: float = 80.0

@export var outfit_layer: int = 1
@export var outfit_sprite: Texture2D


func _on_drag_released(_pos: Vector2) -> void:
	var character: Character = _find_nearest_character()
	if character != null:
		character.apply_outfit_item(outfit_layer, item_id, outfit_sprite, self)
		return
	super._on_drag_released(_pos)


func _find_nearest_character() -> Character:
	var best_dist: float = OUTFIT_APPLY_RADIUS
	var best: Character = null
	for node: Node in get_tree().get_nodes_in_group("characters"):
		var character: Character = node as Character
		if character == null:
			continue
		var dist: float = global_position.distance_to(character.global_position)
		if dist < best_dist:
			best_dist = dist
			best = character
	return best
