extends Node

func _ready() -> void:
  EventBus.game_over.connect(_on_game_over)
  if GameState.force_return_to_world:
    call_deferred("_handle_force_return")

func _handle_force_return() -> void:
  GameState.force_return_to_world = false
  var player: Node3D = get_tree().get_first_node_in_group("player")
  var tavern: Node3D = get_tree().get_first_node_in_group("tavern")
  if player and tavern:
    player.global_position = tavern.global_position + Vector3(0, 1, -6)
  var invasion: Node = get_node_or_null("../Systems/InvasionSystem")
  if invasion:
    invasion.trigger()

func _on_game_over() -> void:
  var overlay: CanvasLayer = get_node_or_null("../GameOverOverlay")
  if overlay and overlay.has_method("show_overlay"):
    overlay.show_overlay(GameState.current_wave)
