Load level
This commit is contained in:
@@ -1,8 +1,42 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using System.Linq;
|
||||
|
||||
public partial class Main : Node
|
||||
{
|
||||
[Export]
|
||||
public Array<PackedScene> Levels;
|
||||
|
||||
public void LoadLevel(int indexToLoad)
|
||||
{
|
||||
var sceneToLoad = Levels.ElementAt(indexToLoad);
|
||||
CallDeferred(nameof(DeferredGoToScene), sceneToLoad);
|
||||
}
|
||||
|
||||
public void LoadNextLevel(int currentSceneIndex)
|
||||
{
|
||||
if (currentSceneIndex < Levels.Count())
|
||||
{
|
||||
var currentScene = Levels.ToList().ElementAt(currentSceneIndex);
|
||||
var nextScene = currentSceneIndex + 1;
|
||||
CallDeferred(nameof(DeferredGoToScene), Levels.ElementAt(nextScene), currentScene);
|
||||
}
|
||||
}
|
||||
|
||||
private void DeferredGoToScene(PackedScene sceneToAdd)
|
||||
{
|
||||
GD.Print(sceneToAdd.ResourcePath);
|
||||
var nextScene = GD.Load<PackedScene>(sceneToAdd.ResourcePath);
|
||||
var newScene = nextScene.Instantiate();
|
||||
GetTree().Root.AddChild(newScene);
|
||||
}
|
||||
|
||||
private void DeferredGoToScene(PackedScene sceneToAdd, PackedScene sceneToRemove)
|
||||
{
|
||||
GD.Print(sceneToAdd.ResourcePath);
|
||||
GetTree().GetFirstNodeInGroup("Level").QueueFree();
|
||||
var nextScene = GD.Load<PackedScene>(sceneToAdd.ResourcePath);
|
||||
var newScene = nextScene.Instantiate();
|
||||
GetTree().Root.AddChild(newScene);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user