Skip to content

Commit

Permalink
last level completed now returns you to menu
Browse files Browse the repository at this point in the history
  • Loading branch information
ar1a committed Oct 6, 2024
1 parent 34dd597 commit f092a55
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
23 changes: 14 additions & 9 deletions scenes/autoload/LevelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@ namespace Game.Autoload;

public partial class LevelManager : Node
{
public static LevelManager Instance { get; private set; }
private static LevelManager instance;

[Export]
private LevelDefinitionResource[] levelDefinitions;

private int currentLevelIndex;
private static int currentLevelIndex;

public void ChangeLevel(int levelIndex)
public static void ChangeLevel(int levelIndex)
{
if (levelIndex >= levelDefinitions.Length || levelIndex < 0)
if (levelIndex >= instance.levelDefinitions.Length || levelIndex < 0)
{
return;
}
currentLevelIndex = levelIndex;
var levelDefinition = levelDefinitions[currentLevelIndex];
GetTree().ChangeSceneToFile(levelDefinition.LevelScenePath);
var levelDefinition = instance.levelDefinitions[currentLevelIndex];
instance.GetTree().ChangeSceneToFile(levelDefinition.LevelScenePath);
}

public static LevelDefinitionResource[] GetLevelDefinitions()
{
return Instance.levelDefinitions.ToArray();
return instance.levelDefinitions.ToArray();
}

public void ChangeToNextLevel()
public static void ChangeToNextLevel()
{
ChangeLevel(currentLevelIndex + 1);
}
Expand All @@ -38,7 +38,12 @@ public override void _Notification(int what)
{
if (what == NotificationSceneInstantiated)
{
Instance = this;
instance = this;
}
}

public static bool IsLastLevel()
{
return currentLevelIndex == instance.levelDefinitions.Length - 1;
}
}
20 changes: 18 additions & 2 deletions scenes/ui/LevelCompleteScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,33 @@ namespace Game.UI;

public partial class LevelCompleteScreen : CanvasLayer
{
[Export(PropertyHint.File, "*.tscn")]
private string mainMenuScenePath;

private Button nextLevelButton;

public override void _Ready()
{
nextLevelButton = GetNode<Button>("%NextLevelButton");
nextLevelButton.Pressed += OnNextLevelButtonPressed;
AudioHelpers.PlayVictory();

if (LevelManager.IsLastLevel())
{
nextLevelButton.Text = "Return to Menu";
}

nextLevelButton.Pressed += OnNextLevelButtonPressed;
}

private void OnNextLevelButtonPressed()
{
LevelManager.Instance.ChangeToNextLevel();
if (!LevelManager.IsLastLevel())
{
LevelManager.ChangeToNextLevel();
}
else
{
GetTree().ChangeSceneToFile(mainMenuScenePath);
}
}
}
1 change: 1 addition & 0 deletions scenes/ui/LevelCompleteScreen.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ region_rect = Rect2(3, 0, 186, 61)

[node name="LevelCompleteScreen" type="CanvasLayer"]
script = ExtResource("1_ji8ed")
mainMenuScenePath = "res://scenes/ui/MainMenu.tscn"

[node name="MarginContainer" type="MarginContainer" parent="."]
anchors_preset = 15
Expand Down
3 changes: 1 addition & 2 deletions scenes/ui/LevelSelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ private void ShowPage()

levelSelectSection.SetLevelDefinition(levelDefinitions[i]);
levelSelectSection.SetLevelIndex(i);
levelSelectSection.LevelSelected += static (index) =>
LevelManager.Instance.ChangeLevel(index);
levelSelectSection.LevelSelected += static (index) => LevelManager.ChangeLevel(index);
}
}
}

0 comments on commit f092a55

Please sign in to comment.