You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Babushka/scripts/CSharp/Common/WorldManagement/WorldContainer.cs

42 lines
1.2 KiB

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<World>();
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);
}
}