class_name EntityInteractingArea extends Area3D

var interactableAreas:Array[EntityInteractableArea] = []
@export var entity:Entity

func hasInteraction() -> bool:
  return interactableAreas.size() > 0

func interact() -> void:
  for area in interactableAreas:
    if !area.isInteractable():
      continue
  
    area.onInteract(self.entity)
    break

func _enter_tree() -> void:
  self.area_entered.connect(_onAreaEntered)
  self.area_exited.connect(_onAreaExited)

func _exit_tree() -> void:
  self.area_entered.disconnect(_onAreaEntered)
  self.area_exited.disconnect(_onAreaExited)

func _onAreaEntered(area:Area3D) -> void:
  if area is EntityInteractableArea:
    if area.entity == entity:
      return
    interactableAreas.append(area)

func _onAreaExited(area:Area3D) -> void:
  interactableAreas.erase(area)
