using Godot;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.Progression;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.components;
///
/// Buys skills on the player's behalf.
///
///
/// Now a thin adapter. Unlocking and upgrading used to be two methods that each checked affordability,
/// debited coins, mutated SkillData.Level and told SkillManager separately — four steps a
/// caller had to get right, in order. PurchaseSkill is one operation covering both; it charges
/// the Attempt's ledger, so dying undoes the spend and the skill together, and SkillManager reconciles
/// itself rather than being told.
///
[GlobalClass]
public partial class SkillUnlockerComponent : Node
{
public SkillManager SkillManager { get; private set; }
[Signal]
public delegate void SkillUnlockedEventHandler(SkillData skill);
private static ProgressionRoot Progression => ProgressionRoot.Instance;
public override void _Ready()
{
SkillManager = SkillManager.Instance;
}
/// Buys the next level of a skill, whether that is the first or an upgrade.
public PurchaseOutcome TryBuy(SkillData skill)
{
if (skill == null || Progression == null) return PurchaseOutcome.NotPlaying;
var outcome = Progression.PurchaseSkill(SkillId.Of(skill.Name));
if (outcome == PurchaseOutcome.Purchased)
EmitSignalSkillUnlocked(skill);
return outcome;
}
/// Grants every purchasable skill, ignoring cost. Console only.
public void UnlockAllSkills()
{
if (SkillManager == null || Progression == null) return;
foreach (var skill in SkillManager.AvailableSkills)
{
if (skill == null) continue;
Progression.GrantSkill(SkillId.Of(skill.Name));
EmitSignalSkillUnlocked(skill);
}
}
}