using Godot; using System; using System.Linq; using Babushka.scripts.CSharp.Common.WorldManagement; public partial class WorldContainer : Node { [Export] private PackedScene startingWorld; [Export] private SpawnPointResource startingSpawnPoint; [Signal] public delegate void WorldChangedEventHandler(Vector3 spawnPointPosition); public void SwitchWorld(PackedScene newWorldScene, SpawnPointResource spawnPoint) { // Unload the current world if (GetChildCount() > 0) { GetChild(0).QueueFree(); } // Load the new world var newWorld = newWorldScene.Instantiate(); AddChild(newWorld); // Switch to the new spawn point if (newWorld.SpawnPoints.TryGetValue(spawnPoint, out var spawnPointMarker)) { EmitSignalWorldChanged(spawnPointMarker.GlobalPosition); } else { GD.PrintErr("Selected spawn point not found in the new world."); EmitSignalWorldChanged(newWorld.SpawnPoints.First().Value.GlobalPosition); } } public override void _Ready() { SwitchWorld(startingWorld,startingSpawnPoint); } }