Untitled

Anonymous
plain_text
02/23/2026 10:31 PM
5.8 KB
10
Indexable
extends Node

@onready var scene_root: Node2D = $SceneRoot
@onready var ui_root: CanvasLayer = $UIRoot
@onready var dialogue_ui: Control = $UIRoot/DialogueUI
@onready var note_ui: Control = $UIRoot/NoteUI
@onready var fade_rect: ColorRect = $UIRoot/FadeRect
@onready var item_get_overlay: Control = $UIRoot/ItemGetOverlay

var current_scene: Node = null

# Scene paths
const SCENE_INTRO := "res://scenes/locations/Intro.tscn"
const SCENE_HUB := "res://scenes/locations/Hub_Bus9.tscn"
const SCENE_CORRIDOR := "res://scenes/locations/Corridor_Gang.tscn"
const SCENE_AURORA := "res://scenes/locations/Aurora.tscn"
const SCENE_BANK := "res://scenes/locations/BankCenter.tscn"
const SCENE_FOXPOST := "res://scenes/locations/Foxpost.tscn"

# Home interior / ending location
const SCENE_HOME := "res://scenes/locations/Home.tscn"
# Backward compat (optional)
const SCENE_KITCHEN := "res://scenes/locations/Kitchen_End.tscn"

func _ready() -> void:
	load_intro()

func clear_scene() -> void:
	if current_scene != null:
		current_scene.queue_free()
		current_scene = null

# Find any node in the instantiated scene tree that implements bind_ui()
func _bind_ui_to_scene(scene: Node) -> void:
	if scene == null:
		return

	# Root has bind_ui
	if scene.has_method("bind_ui"):
		scene.call("bind_ui", dialogue_ui, note_ui, fade_rect, item_get_overlay, self)
		return

	# Otherwise search descendants (depth-first)
	var stack: Array[Node] = []
	stack.append(scene)

	while stack.size() > 0:
		var n := stack.pop_back()
		if n != null and n.has_method("bind_ui"):
			n.call("bind_ui", dialogue_ui, note_ui, fade_rect, item_get_overlay, self)
			return
		for c in n.get_children():
			if c is Node:
				stack.append(c)

	push_error("Main: No node with bind_ui() found in scene: " + str(scene.name))

func _set_spawn_on_scene(scene: Node, spawn_from: String) -> void:
	if scene == null:
		return

	if scene.has_method("set_spawn_from"):
		scene.call("set_spawn_from", spawn_from)
		return

	# If the script is on a child node, search descendants
	var stack: Array[Node] = []
	stack.append(scene)

	while stack.size() > 0:
		var n := stack.pop_back()
		if n != null and n.has_method("set_spawn_from"):
			n.call("set_spawn_from", spawn_from)
			return
		for c in n.get_children():
			if c is Node:
				stack.append(c)

func load_scene(path: String, spawn_from: String = "left") -> void:
	# Hide dialogue before switching
	if dialogue_ui and dialogue_ui.is_open():
		dialogue_ui.hide_dialogue()

	# --- Fade Out ---
	if fade_rect:
		fade_rect.visible = true
		fade_rect.modulate.a = 0.0
		var t_out := create_tween()
		t_out.tween_property(fade_rect, "modulate:a", 1.0, 0.25)
		await t_out.finished

	clear_scene()

	var packed := ResourceLoader.load(path)
	if packed == null:
		push_error("Main.load_scene: Failed to load: " + path)
		return
	if not (packed is PackedScene):
		push_error("Main.load_scene: Resource is not a PackedScene: " + path)
		return

	current_scene = (packed as PackedScene).instantiate()
	if current_scene == null:
		push_error("Main.load_scene: instantiate() failed: " + path)
		return

	# Add to tree first (so find_child + ready-time lookups are reliable)
	scene_root.add_child(current_scene)

	# Optional per-scene spawn direction (root OR any child)
	_set_spawn_on_scene(current_scene, spawn_from)

	# Bind UI to root OR any child that provides bind_ui()
	_bind_ui_to_scene(current_scene)

	# --- Fade In ---
	if fade_rect:
		var t_in := create_tween()
		t_in.tween_property(fade_rect, "modulate:a", 0.0, 0.25)
		await t_in.finished
		fade_rect.visible = false

func load_intro() -> void:
	load_scene(SCENE_INTRO, "left")

# Called by locations
func go_hub(spawn_from: String = "left") -> void:
	load_scene(SCENE_HUB, spawn_from)

func go_corridor(spawn_from: String = "left") -> void:
	load_scene(SCENE_CORRIDOR, spawn_from)

func go_aurora(spawn_from: String = "left") -> void:
	load_scene(SCENE_AURORA, spawn_from)

func go_bank(spawn_from: String = "left") -> void:
	load_scene(SCENE_BANK, spawn_from)

func go_foxpost(spawn_from: String = "left") -> void:
	load_scene(SCENE_FOXPOST, spawn_from)

# Home interior / ending
func go_home(spawn_from: String = "right") -> void:
	load_scene(SCENE_HOME, spawn_from)

# Backward compat if you still reference kitchen ending somewhere
func go_kitchen(spawn_from: String = "right") -> void:
	load_scene(SCENE_KITCHEN, spawn_from)

func quit_game() -> void:
	get_tree().quit()
Editor is loading...
Leave a Comment