refactor: enhance GameStateStore integration and improve skill management

This commit is contained in:
2026-03-19 02:33:07 +01:00
parent 3e36e48e97
commit eeefca4d4e
31 changed files with 260 additions and 419 deletions

View File

@@ -12,13 +12,11 @@ public partial class BrickShieldSkillComponent : SkillComponentBase
[Export] public PackedScene ShieldScene { get; set; }
private Node2D _shieldInstance;
private GameManager _gameManager;
private SkillManager _skillManager;
private HealthComponent _shieldHealth;
public override void _Ready()
{
_gameManager = GameManager.Instance;
_skillManager = SkillManager.Instance;
}
@@ -55,9 +53,9 @@ public partial class BrickShieldSkillComponent : SkillComponentBase
private void OnShieldDestroyed()
{
if (_gameManager != null && Data != null && _skillManager != null)
if (Data != null && _skillManager != null)
{
_gameManager.RemoveSkill(Data.Name);
GameStateStore.Instance?.RemoveUnlockedSkill(Data.Name);
_skillManager.RemoveSkill(Data.Name);
}
_shieldInstance = null;

View File

@@ -1,7 +1,6 @@
using Godot;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.State;
namespace Mr.BrickAdventures.scripts.components;
@@ -28,8 +27,6 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
EmitSignalExitTriggered();
AchievementManager.Instance?.UnlockAchievement(AchievementId);
var currentLevel = GameStateStore.Instance?.Session.CurrentLevel ?? 0;
GameManager.Instance?.UnlockLevel(currentLevel + 1);
CallDeferred(nameof(GoToNextLevel));
}

View File

@@ -1,10 +1,8 @@
using Godot;
using Godot.Collections;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.Resources;
using Mr.BrickAdventures.scripts.State;
namespace Mr.BrickAdventures.scripts.components;
@@ -16,30 +14,24 @@ public partial class SkillUnlockerComponent : Node
[Signal]
public delegate void SkillUnlockedEventHandler(SkillData skill);
private GameManager _gameManager;
private GameStateStore Store => GameStateStore.Instance;
public override void _Ready()
{
_gameManager = GameManager.Instance;
SkillManager = SkillManager.Instance;
}
private bool HasEnoughCoins(int amount)
{
return _gameManager != null && _gameManager.GetCoins() >= amount;
}
private bool HasEnoughCoins(int amount) => (Store?.GetTotalCoins() ?? 0) >= amount;
public bool TryUnlockSkill(SkillData skill)
{
if (_gameManager == null) return false;
if (_gameManager.IsSkillUnlocked(skill)) return false;
if (Store == null) return false;
if (Store.IsSkillUnlocked(skill)) return false;
if (!HasEnoughCoins(skill.Upgrades[0].Cost)) return false;
skill.Level = 1;
_gameManager.RemoveCoins(skill.Upgrades[0].Cost);
// Add to session state via GameStateStore
GameStateStore.Instance?.UnlockSkillInSession(skill);
Store.RemoveCoins(skill.Upgrades[0].Cost);
Store.UnlockSkillInSession(skill);
SkillManager.AddSkill(skill);
EmitSignalSkillUnlocked(skill);
@@ -52,31 +44,29 @@ public partial class SkillUnlockerComponent : Node
foreach (var skill in availableSkills)
{
Store?.UnlockSkillPermanently(skill);
EmitSignalSkillUnlocked(skill);
}
_gameManager.UnlockSkills(availableSkills);
SkillManager.ApplyUnlockedSkills();
}
public bool TryUpgradeSkill(SkillData skill)
{
if (_gameManager == null) return false;
if (!_gameManager.IsSkillUnlocked(skill)) return false;
if (Store == null) return false;
if (!Store.IsSkillUnlocked(skill)) return false;
if (skill.Level >= skill.MaxLevel) return false;
if (!HasEnoughCoins(skill.Upgrades[skill.Level].Cost)) return false;
_gameManager.RemoveCoins(skill.Upgrades[skill.Level].Cost);
Store.RemoveCoins(skill.Upgrades[skill.Level].Cost);
skill.Level++;
if (SkillManager.ActiveComponents.TryGetValue(skill.Name, out Variant componentVariant))
{
var component = componentVariant.AsGodotObject();
if (component is ISkill skillInstance)
{
skillInstance.ApplyUpgrade(skill.Upgrades[skill.Level - 1]);
}
}
EmitSignalSkillUnlocked(skill);
return true;
}
}
}