refactor: replace direct manager instances with lazy accessors for improved performance
This commit is contained in:
@@ -10,11 +10,10 @@ namespace Mr.BrickAdventures.scripts.Events;
|
||||
/// </summary>
|
||||
public partial class SkillCollectHandler : Node
|
||||
{
|
||||
private SkillManager _skillManager;
|
||||
private SkillManager SkillManager => SkillManager.Instance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_skillManager = SkillManager.Instance;
|
||||
EventBus.Instance.SkillCollected += OnSkillCollected;
|
||||
}
|
||||
|
||||
@@ -35,7 +34,7 @@ public partial class SkillCollectHandler : Node
|
||||
|
||||
// Immediately activate the skill for the player
|
||||
skill.Level = 1;
|
||||
_skillManager?.AddSkill(skill);
|
||||
SkillManager?.AddSkill(skill);
|
||||
|
||||
// Emit skill unlocked event for UI/achievements
|
||||
EventBus.EmitSkillUnlocked(skill.Name, skill.Level);
|
||||
|
||||
@@ -9,12 +9,10 @@ namespace Mr.BrickAdventures.scripts.Events;
|
||||
/// </summary>
|
||||
public partial class StatisticsEventHandler : Node
|
||||
{
|
||||
private StatisticsManager _statisticsManager;
|
||||
private StatisticsManager StatisticsManager => StatisticsManager.Instance;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_statisticsManager = StatisticsManager.Instance;
|
||||
|
||||
// Subscribe to events
|
||||
EventBus.Instance.CoinCollected += OnCoinCollected;
|
||||
EventBus.Instance.EnemyDefeated += OnEnemyDefeated;
|
||||
@@ -36,26 +34,26 @@ public partial class StatisticsEventHandler : Node
|
||||
|
||||
private void OnCoinCollected(int amount, Vector2 position)
|
||||
{
|
||||
_statisticsManager.IncrementStat("coins_collected", amount);
|
||||
StatisticsManager.IncrementStat("coins_collected", amount);
|
||||
}
|
||||
|
||||
private void OnEnemyDefeated(Node enemy, Vector2 position)
|
||||
{
|
||||
_statisticsManager.IncrementStat("enemies_defeated");
|
||||
StatisticsManager.IncrementStat("enemies_defeated");
|
||||
}
|
||||
|
||||
private void OnPlayerDied(Vector2 position)
|
||||
{
|
||||
_statisticsManager.IncrementStat("deaths");
|
||||
StatisticsManager.IncrementStat("deaths");
|
||||
}
|
||||
|
||||
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
|
||||
{
|
||||
_statisticsManager.IncrementStat("levels_completed");
|
||||
StatisticsManager.IncrementStat("levels_completed");
|
||||
}
|
||||
|
||||
private void OnChildRescued(Vector2 position)
|
||||
{
|
||||
_statisticsManager.IncrementStat("children_rescued");
|
||||
StatisticsManager.IncrementStat("children_rescued");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -18,7 +17,7 @@ public partial class ChargeProgressBar : ProgressBar
|
||||
{
|
||||
ProgressBar.Hide();
|
||||
|
||||
_skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath);
|
||||
_skillManager = SkillManager.Instance;
|
||||
if (_skillManager == null)
|
||||
{
|
||||
GD.PrintErr("ChargeProgressBar: SkillManager autoload not found.");
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
@@ -15,13 +14,11 @@ public partial class DeathScreen : Control
|
||||
[Export] public float TimeoutTime { get; set; } = 2.0f;
|
||||
[Export] public Godot.Collections.Array<Node> NodesToDisable { get; set; } = new();
|
||||
|
||||
private GameManager _gameManager;
|
||||
private GameManager GameManager => GameManager.Instance;
|
||||
private Timer _timer;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GameManager.Instance;
|
||||
|
||||
// Subscribe to lives changed event for reactive updates
|
||||
EventBus.Instance.LivesChanged += OnLivesChanged;
|
||||
}
|
||||
@@ -74,7 +71,7 @@ public partial class DeathScreen : Control
|
||||
|
||||
public void OnPlayerDeath()
|
||||
{
|
||||
if (_gameManager == null) return;
|
||||
if (GameManager == null) return;
|
||||
|
||||
ToggleNodes();
|
||||
SetLabels();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -19,17 +18,14 @@ public partial class Marketplace : Control
|
||||
[Export] public PackedScene MarketplaceButtonScene { get; set; }
|
||||
[Export] public PackedScene SkillButtonScene { get; set; }
|
||||
|
||||
private GameManager _gameManager;
|
||||
private SkillManager _skillManager;
|
||||
private GameManager GameManager => GameManager.Instance;
|
||||
private SkillManager SkillManager => SkillManager.Instance;
|
||||
private readonly List<Button> _unlockButtons = [];
|
||||
private readonly List<SkillButton> _skillButtons = [];
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GameManager.Instance;
|
||||
_skillManager = SkillManager.Instance;
|
||||
|
||||
Skills = _skillManager.AvailableSkills;
|
||||
Skills = SkillManager.AvailableSkills;
|
||||
|
||||
var skillsToUnlock = new List<SkillData>();
|
||||
|
||||
@@ -37,7 +33,7 @@ public partial class Marketplace : Control
|
||||
|
||||
foreach (var skill in skillsToUnlock) CreateUpgradeButton(skill);
|
||||
|
||||
var unlockedSkills = _gameManager.GetUnlockedSkills();
|
||||
var unlockedSkills = GameManager.GetUnlockedSkills();
|
||||
foreach (var skill in unlockedSkills) CreateSkillButton(skill);
|
||||
|
||||
SkillUnlockerComponent.SkillUnlocked += OnSkillUnlocked;
|
||||
@@ -85,7 +81,7 @@ public partial class Marketplace : Control
|
||||
|
||||
foreach (var btn in _skillButtons)
|
||||
{
|
||||
if (_skillManager.IsSkillActive(btn.Data)) btn.Activate();
|
||||
if (SkillManager.IsSkillActive(btn.Data)) btn.Activate();
|
||||
else btn.Deactivate();
|
||||
}
|
||||
}
|
||||
@@ -117,7 +113,7 @@ public partial class Marketplace : Control
|
||||
|
||||
private void OnUpgradeButtonPressed(SkillData skill)
|
||||
{
|
||||
if (_gameManager.IsSkillUnlocked(skill))
|
||||
if (GameManager.IsSkillUnlocked(skill))
|
||||
{
|
||||
if (skill.Level < skill.MaxLevel)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user