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

@@ -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;
}
}
}