refactor: replace direct manager instances with lazy accessors for improved performance

This commit is contained in:
2026-03-19 02:10:36 +01:00
parent 470b0c3a8c
commit 1e9814a9d8
6 changed files with 20 additions and 36 deletions

View File

@@ -21,11 +21,7 @@ public partial class GameManager : Node
} }
private PlayerController _player; private PlayerController _player;
private SpeedRunManager _speedRunManager; private SpeedRunManager SpeedRunManager => SpeedRunManager.Instance;
/// <summary>
/// Lazy accessor for GameStateStore - avoids initialization order issues.
/// </summary>
private GameStateStore Store => GameStateStore.Instance; private GameStateStore Store => GameStateStore.Instance;
public static GameManager Instance { get; private set; } public static GameManager Instance { get; private set; }
@@ -33,7 +29,6 @@ public partial class GameManager : Node
public override void _Ready() public override void _Ready()
{ {
Instance = this; Instance = this;
_speedRunManager = SpeedRunManager.Instance;
EventBus.Instance.PlayerSpawned += OnPlayerSpawned; EventBus.Instance.PlayerSpawned += OnPlayerSpawned;
} }
@@ -197,7 +192,7 @@ public partial class GameManager : Node
public void StartNewGame() public void StartNewGame()
{ {
Store?.ResetAll(); Store?.ResetAll();
_speedRunManager?.StartTimer(); SpeedRunManager?.StartTimer();
GetTree().ChangeSceneToPacked(LevelScenes[0]); GetTree().ChangeSceneToPacked(LevelScenes[0]);
SaveSystem.Instance.SaveGame(); SaveSystem.Instance.SaveGame();
EventBus.EmitGameStarted(); EventBus.EmitGameStarted();
@@ -234,7 +229,7 @@ public partial class GameManager : Node
Store.CommitSessionCoins(); Store.CommitSessionCoins();
Store.CommitSessionSkills(); Store.CommitSessionSkills();
var completionTime = _speedRunManager?.GetCurrentLevelTime() ?? 0.0; var completionTime = SpeedRunManager?.GetCurrentLevelTime() ?? 0.0;
EventBus.EmitLevelCompleted(levelIndex, GetTree().CurrentScene, completionTime); EventBus.EmitLevelCompleted(levelIndex, GetTree().CurrentScene, completionTime);
Store.ResetSession(); Store.ResetSession();

View File

@@ -10,11 +10,10 @@ namespace Mr.BrickAdventures.scripts.Events;
/// </summary> /// </summary>
public partial class SkillCollectHandler : Node public partial class SkillCollectHandler : Node
{ {
private SkillManager _skillManager; private SkillManager SkillManager => SkillManager.Instance;
public override void _Ready() public override void _Ready()
{ {
_skillManager = SkillManager.Instance;
EventBus.Instance.SkillCollected += OnSkillCollected; EventBus.Instance.SkillCollected += OnSkillCollected;
} }
@@ -35,7 +34,7 @@ public partial class SkillCollectHandler : Node
// Immediately activate the skill for the player // Immediately activate the skill for the player
skill.Level = 1; skill.Level = 1;
_skillManager?.AddSkill(skill); SkillManager?.AddSkill(skill);
// Emit skill unlocked event for UI/achievements // Emit skill unlocked event for UI/achievements
EventBus.EmitSkillUnlocked(skill.Name, skill.Level); EventBus.EmitSkillUnlocked(skill.Name, skill.Level);

View File

@@ -9,12 +9,10 @@ namespace Mr.BrickAdventures.scripts.Events;
/// </summary> /// </summary>
public partial class StatisticsEventHandler : Node public partial class StatisticsEventHandler : Node
{ {
private StatisticsManager _statisticsManager; private StatisticsManager StatisticsManager => StatisticsManager.Instance;
public override void _Ready() public override void _Ready()
{ {
_statisticsManager = StatisticsManager.Instance;
// Subscribe to events // Subscribe to events
EventBus.Instance.CoinCollected += OnCoinCollected; EventBus.Instance.CoinCollected += OnCoinCollected;
EventBus.Instance.EnemyDefeated += OnEnemyDefeated; EventBus.Instance.EnemyDefeated += OnEnemyDefeated;
@@ -36,26 +34,26 @@ public partial class StatisticsEventHandler : Node
private void OnCoinCollected(int amount, Vector2 position) private void OnCoinCollected(int amount, Vector2 position)
{ {
_statisticsManager.IncrementStat("coins_collected", amount); StatisticsManager.IncrementStat("coins_collected", amount);
} }
private void OnEnemyDefeated(Node enemy, Vector2 position) private void OnEnemyDefeated(Node enemy, Vector2 position)
{ {
_statisticsManager.IncrementStat("enemies_defeated"); StatisticsManager.IncrementStat("enemies_defeated");
} }
private void OnPlayerDied(Vector2 position) private void OnPlayerDied(Vector2 position)
{ {
_statisticsManager.IncrementStat("deaths"); StatisticsManager.IncrementStat("deaths");
} }
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime) private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
{ {
_statisticsManager.IncrementStat("levels_completed"); StatisticsManager.IncrementStat("levels_completed");
} }
private void OnChildRescued(Vector2 position) private void OnChildRescued(Vector2 position)
{ {
_statisticsManager.IncrementStat("children_rescued"); StatisticsManager.IncrementStat("children_rescued");
} }
} }

View File

@@ -1,5 +1,4 @@
using Godot; using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads; using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.components; using Mr.BrickAdventures.scripts.components;
using Mr.BrickAdventures.scripts.Resources; using Mr.BrickAdventures.scripts.Resources;
@@ -18,7 +17,7 @@ public partial class ChargeProgressBar : ProgressBar
{ {
ProgressBar.Hide(); ProgressBar.Hide();
_skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath); _skillManager = SkillManager.Instance;
if (_skillManager == null) if (_skillManager == null)
{ {
GD.PrintErr("ChargeProgressBar: SkillManager autoload not found."); GD.PrintErr("ChargeProgressBar: SkillManager autoload not found.");

View File

@@ -1,5 +1,4 @@
using Godot; using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads; using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.Resources; using Mr.BrickAdventures.scripts.Resources;
using Mr.BrickAdventures.scripts.State; using Mr.BrickAdventures.scripts.State;
@@ -15,13 +14,11 @@ public partial class DeathScreen : Control
[Export] public float TimeoutTime { get; set; } = 2.0f; [Export] public float TimeoutTime { get; set; } = 2.0f;
[Export] public Godot.Collections.Array<Node> NodesToDisable { get; set; } = new(); [Export] public Godot.Collections.Array<Node> NodesToDisable { get; set; } = new();
private GameManager _gameManager; private GameManager GameManager => GameManager.Instance;
private Timer _timer; private Timer _timer;
public override void _Ready() public override void _Ready()
{ {
_gameManager = GameManager.Instance;
// Subscribe to lives changed event for reactive updates // Subscribe to lives changed event for reactive updates
EventBus.Instance.LivesChanged += OnLivesChanged; EventBus.Instance.LivesChanged += OnLivesChanged;
} }
@@ -74,7 +71,7 @@ public partial class DeathScreen : Control
public void OnPlayerDeath() public void OnPlayerDeath()
{ {
if (_gameManager == null) return; if (GameManager == null) return;
ToggleNodes(); ToggleNodes();
SetLabels(); SetLabels();

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using Godot; using Godot;
using Godot.Collections; using Godot.Collections;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads; using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.components; using Mr.BrickAdventures.scripts.components;
using Mr.BrickAdventures.scripts.Resources; using Mr.BrickAdventures.scripts.Resources;
@@ -19,17 +18,14 @@ public partial class Marketplace : Control
[Export] public PackedScene MarketplaceButtonScene { get; set; } [Export] public PackedScene MarketplaceButtonScene { get; set; }
[Export] public PackedScene SkillButtonScene { get; set; } [Export] public PackedScene SkillButtonScene { get; set; }
private GameManager _gameManager; private GameManager GameManager => GameManager.Instance;
private SkillManager _skillManager; private SkillManager SkillManager => SkillManager.Instance;
private readonly List<Button> _unlockButtons = []; private readonly List<Button> _unlockButtons = [];
private readonly List<SkillButton> _skillButtons = []; private readonly List<SkillButton> _skillButtons = [];
public override void _Ready() public override void _Ready()
{ {
_gameManager = GameManager.Instance; Skills = SkillManager.AvailableSkills;
_skillManager = SkillManager.Instance;
Skills = _skillManager.AvailableSkills;
var skillsToUnlock = new List<SkillData>(); var skillsToUnlock = new List<SkillData>();
@@ -37,7 +33,7 @@ public partial class Marketplace : Control
foreach (var skill in skillsToUnlock) CreateUpgradeButton(skill); foreach (var skill in skillsToUnlock) CreateUpgradeButton(skill);
var unlockedSkills = _gameManager.GetUnlockedSkills(); var unlockedSkills = GameManager.GetUnlockedSkills();
foreach (var skill in unlockedSkills) CreateSkillButton(skill); foreach (var skill in unlockedSkills) CreateSkillButton(skill);
SkillUnlockerComponent.SkillUnlocked += OnSkillUnlocked; SkillUnlockerComponent.SkillUnlocked += OnSkillUnlocked;
@@ -85,7 +81,7 @@ public partial class Marketplace : Control
foreach (var btn in _skillButtons) foreach (var btn in _skillButtons)
{ {
if (_skillManager.IsSkillActive(btn.Data)) btn.Activate(); if (SkillManager.IsSkillActive(btn.Data)) btn.Activate();
else btn.Deactivate(); else btn.Deactivate();
} }
} }
@@ -117,7 +113,7 @@ public partial class Marketplace : Control
private void OnUpgradeButtonPressed(SkillData skill) private void OnUpgradeButtonPressed(SkillData skill)
{ {
if (_gameManager.IsSkillUnlocked(skill)) if (GameManager.IsSkillUnlocked(skill))
{ {
if (skill.Level < skill.MaxLevel) if (skill.Level < skill.MaxLevel)
{ {