refactor: fix bugs and improve architecture
- Fix double-execution bug in LevelStateHandler (coins/skills were committed twice per level) - Fix DamageComponent to track multiple targets via HashSet instead of single node - Fix HealthComponent: update health immediately, decouple from PlayerController via signal wiring in PlayerController - Remove dead loop in SkillManager.RemoveSkill; simplify O(n²) throw skill loop - Replace GetNode<T>(path) with .Instance across managers; add Instance to StatisticsManager/SpeedRunManager - GameManager.GetPlayer() now uses EventBus.PlayerSpawned instead of scanning all scene nodes - UIManager.UiStack: remove [Export], use private List<Control> - PlayerState: extract DefaultLives constant, simplify CreateDefault()
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
@@ -21,7 +20,6 @@ public partial class GameManager : Node
|
||||
private set => _player = value;
|
||||
}
|
||||
|
||||
private List<Node> _sceneNodes = [];
|
||||
private PlayerController _player;
|
||||
private SpeedRunManager _speedRunManager;
|
||||
|
||||
@@ -32,38 +30,24 @@ public partial class GameManager : Node
|
||||
|
||||
public static GameManager Instance { get; private set; }
|
||||
|
||||
public override void _EnterTree()
|
||||
public override void _Ready()
|
||||
{
|
||||
GetTree().NodeAdded += OnNodeAdded;
|
||||
GetTree().NodeRemoved += OnNodeRemoved;
|
||||
Instance = this;
|
||||
_speedRunManager = SpeedRunManager.Instance;
|
||||
EventBus.Instance.PlayerSpawned += OnPlayerSpawned;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (Instance == this) Instance = null;
|
||||
GetTree().NodeAdded -= OnNodeAdded;
|
||||
GetTree().NodeRemoved -= OnNodeRemoved;
|
||||
_sceneNodes.Clear();
|
||||
if (EventBus.Instance != null)
|
||||
EventBus.Instance.PlayerSpawned -= OnPlayerSpawned;
|
||||
}
|
||||
|
||||
public override void _Ready()
|
||||
private void OnPlayerSpawned(PlayerController player)
|
||||
{
|
||||
Instance = this;
|
||||
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
|
||||
}
|
||||
|
||||
private void OnNodeAdded(Node node)
|
||||
{
|
||||
_sceneNodes.Add(node);
|
||||
}
|
||||
|
||||
private void OnNodeRemoved(Node node)
|
||||
{
|
||||
_sceneNodes.Remove(node);
|
||||
if (node == _player)
|
||||
{
|
||||
_player = null;
|
||||
}
|
||||
_player = player;
|
||||
player.TreeExiting += () => { if (_player == player) _player = null; };
|
||||
}
|
||||
|
||||
#region Coin Operations
|
||||
@@ -187,7 +171,7 @@ public partial class GameManager : Node
|
||||
{
|
||||
Store?.ResetAll();
|
||||
GetTree().ChangeSceneToPacked(LevelScenes[0]);
|
||||
GetNode<SaveSystem>(Constants.SaveSystemPath).SaveGame();
|
||||
SaveSystem.Instance.SaveGame();
|
||||
}
|
||||
|
||||
public void QuitGame() => GetTree().Quit();
|
||||
@@ -209,13 +193,13 @@ public partial class GameManager : Node
|
||||
Store?.ResetAll();
|
||||
_speedRunManager?.StartTimer();
|
||||
GetTree().ChangeSceneToPacked(LevelScenes[0]);
|
||||
GetNode<SaveSystem>(Constants.SaveSystemPath).SaveGame();
|
||||
SaveSystem.Instance.SaveGame();
|
||||
EventBus.EmitGameStarted();
|
||||
}
|
||||
|
||||
public void ContinueGame()
|
||||
{
|
||||
var save = GetNode<SaveSystem>(Constants.SaveSystemPath);
|
||||
var save = SaveSystem.Instance;
|
||||
if (!save.LoadGame())
|
||||
{
|
||||
GD.PrintErr("Failed to load game. Starting a new game instead.");
|
||||
@@ -249,7 +233,7 @@ public partial class GameManager : Node
|
||||
|
||||
Store.ResetSession();
|
||||
TryToGoToNextLevel();
|
||||
GetNode<SaveSystem>(Constants.SaveSystemPath).SaveGame();
|
||||
SaveSystem.Instance.SaveGame();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -259,18 +243,7 @@ public partial class GameManager : Node
|
||||
public PlayerController GetPlayer()
|
||||
{
|
||||
if (_player != null && IsInstanceValid(_player)) return _player;
|
||||
|
||||
_player = null;
|
||||
|
||||
foreach (var node in _sceneNodes)
|
||||
{
|
||||
if (node is not PlayerController player) continue;
|
||||
|
||||
_player = player;
|
||||
return _player;
|
||||
}
|
||||
|
||||
GD.PrintErr("PlayerController not found in the scene tree.");
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -11,8 +10,8 @@ namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
public partial class SkillManager : Node
|
||||
{
|
||||
private GameManager _gameManager;
|
||||
private PlayerController _player;
|
||||
private GameManager GameManager => GameManager.Instance;
|
||||
|
||||
[Export] public Array<SkillData> AvailableSkills { get; set; } = [];
|
||||
|
||||
@@ -28,7 +27,6 @@ public partial class SkillManager : Node
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
@@ -85,21 +83,11 @@ public partial class SkillManager : Node
|
||||
|
||||
if (skillData.Type == SkillType.Throw)
|
||||
{
|
||||
var unlocked = _gameManager.GetUnlockedSkills();
|
||||
foreach (var sd in unlocked)
|
||||
// Remove any other active throw skill before adding the new one
|
||||
foreach (var sd in AvailableSkills)
|
||||
{
|
||||
SkillData data = null;
|
||||
foreach (var s in AvailableSkills)
|
||||
{
|
||||
if (s == sd)
|
||||
{
|
||||
data = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Remove other throw skills if a new one is added
|
||||
if (data is { Type: SkillType.Throw })
|
||||
RemoveSkill(data.Name);
|
||||
if (sd != skillData && sd.Type == SkillType.Throw)
|
||||
RemoveSkill(sd.Name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,14 +134,6 @@ public partial class SkillManager : Node
|
||||
if (IsInstanceValid(inst))
|
||||
inst.QueueFree();
|
||||
|
||||
var skills = _gameManager.GetUnlockedSkills();
|
||||
foreach (var s in skills)
|
||||
{
|
||||
if (s.Name == skillName)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
ActiveComponents.Remove(skillName);
|
||||
var sd = GetSkillByName(skillName);
|
||||
if (sd != null) EmitSignalSkillRemoved(sd);
|
||||
@@ -174,10 +154,11 @@ public partial class SkillManager : Node
|
||||
public void ApplyUnlockedSkills()
|
||||
{
|
||||
if (_player == null || !IsInstanceValid(_player)) return;
|
||||
if (GameManager == null) return;
|
||||
|
||||
foreach (var sd in AvailableSkills)
|
||||
{
|
||||
if (_gameManager.IsSkillUnlocked(sd))
|
||||
if (GameManager.IsSkillUnlocked(sd))
|
||||
{
|
||||
CallDeferred(MethodName.AddSkill, sd);
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
public partial class SpeedRunManager : Node
|
||||
{
|
||||
public static SpeedRunManager Instance { get; private set; }
|
||||
|
||||
public override void _EnterTree() => Instance = this;
|
||||
public override void _ExitTree() { if (Instance == this) Instance = null; }
|
||||
|
||||
public bool IsRunning { get; private set; } = false;
|
||||
public bool IsVisible { get; private set; } = false;
|
||||
|
||||
|
||||
@@ -9,6 +9,10 @@ namespace Mr.BrickAdventures.Autoloads;
|
||||
/// </summary>
|
||||
public partial class StatisticsManager : Node
|
||||
{
|
||||
public static StatisticsManager Instance { get; private set; }
|
||||
|
||||
public override void _Ready() => Instance = this;
|
||||
public override void _ExitTree() { if (Instance == this) Instance = null; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the statistics dictionary from the store.
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
public partial class UIManager : Node
|
||||
{
|
||||
[Export] public Array<Control> UiStack { get; set; } = new();
|
||||
private readonly System.Collections.Generic.List<Control> UiStack = new();
|
||||
|
||||
[Signal] public delegate void ScreenPushedEventHandler(Control screen);
|
||||
[Signal] public delegate void ScreenPoppedEventHandler(Control screen);
|
||||
@@ -34,19 +33,19 @@ public partial class UIManager : Node
|
||||
return;
|
||||
}
|
||||
|
||||
var top = (Control)UiStack[^1];
|
||||
var top = UiStack[^1];
|
||||
UiStack.RemoveAt(UiStack.Count - 1);
|
||||
top.Hide();
|
||||
top.SetProcessInput(false);
|
||||
EmitSignalScreenPopped(top);
|
||||
top.AcceptEvent();
|
||||
|
||||
if (UiStack.Count > 0) ((Control)UiStack[^1]).GrabFocus();
|
||||
if (UiStack.Count > 0) UiStack[^1].GrabFocus();
|
||||
}
|
||||
|
||||
public Control TopScreen() => UiStack.Count > 0 ? (Control)UiStack[^1] : null;
|
||||
|
||||
public bool IsScreenOnTop(Control screen) => UiStack.Count > 0 && (Control)UiStack[^1] == screen;
|
||||
public Control TopScreen() => UiStack.Count > 0 ? UiStack[^1] : null;
|
||||
|
||||
public bool IsScreenOnTop(Control screen) => UiStack.Count > 0 && UiStack[^1] == screen;
|
||||
|
||||
public bool IsVisibleOnStack(Control screen) => UiStack.Contains(screen) && screen.Visible;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user