extends Node const PORTAL_SCENE: PackedScene = preload("res://scenes/portal/portal.tscn") const GATE_SCENE: PackedScene = preload("res://scenes/portal/gate.tscn") const SPAWN_INTERVAL := 30.0 const MAX_PORTALS := 3 const MIN_DISTANCE := 20.0 const MAX_DISTANCE := 40.0 var portals: Array[Node] = [] var timer := 0.0 func _ready() -> void: if GameState.portal_position != Vector3.ZERO and not GameState.dungeon_cleared: call_deferred("_restore_gate") else: if GameState.dungeon_cleared: GameState.clear() call_deferred("_spawn_portal") func _restore_gate() -> void: var gate: Node3D = GATE_SCENE.instantiate() get_parent().add_child(gate) gate.global_position = GameState.portal_position func _process(delta: float) -> void: timer += delta if timer >= SPAWN_INTERVAL: timer = 0.0 _cleanup_dead() if portals.size() < MAX_PORTALS: _spawn_portal() func _spawn_portal() -> void: var angle: float = randf() * TAU var distance: float = randf_range(MIN_DISTANCE, MAX_DISTANCE) var pos := Vector3(cos(angle) * distance, 0, sin(angle) * distance) var portal: Node3D = PORTAL_SCENE.instantiate() get_parent().add_child(portal) portal.global_position = pos portals.append(portal) func _cleanup_dead() -> void: var valid: Array[Node] = [] for p in portals: if is_instance_valid(p): valid.append(p) portals = valid