extends Node

const GATE_SCENE: PackedScene = preload("res://scenes/portal/gate.tscn")

func _ready() -> void:
  EventBus.entity_died.connect(_on_entity_died)

func _on_entity_died(entity: Node) -> void:
  if not entity.is_in_group("portals"):
    return
  if not entity.is_inside_tree():
    return
  var pos: Vector3 = entity.global_position
  var gate: Node3D = GATE_SCENE.instantiate()
  entity.get_parent().add_child(gate)
  gate.global_position = pos
  gate.dungeon_variant = entity.stats.variant
  EventBus.portal_defeated.emit(entity)
  entity.queue_free()
