atomic pair - purchasing couples them, so neither moves alone. SkillData.Level is gone. It was an [Export] on a cached Resource: one instance per process, so the level never persisted, leaked between save slots, and made "unowned" indistinguishable from "level 1". Level now comes from the Playthrough and is passed to ISkill.Initialize. SkillManager reconciles from SkillSetChanged instead of being told by each caller. The old shape was "record it, then tell SkillManager" as two calls at four sites; AddSkill's early return when no player is registered left the skill owned but inert. Reconciliation also tracks the level each component was built at, so an upgrade rebuilds it rather than being patched in from outside. SkillUnlockerComponent collapses to one TryBuy - unlock and upgrade were two methods each doing affordability, debit, level mutation and manager notification in order. The guard three components copy-pasted around Upgrades[Level - 1] - and which BrickArmor omitted - now lives once in SkillComponentBase. Hud reads coins from CoinsChanged instead of rebuilding translated strings every frame. Lives still polls; it moves with the death path.
59 lines
1.9 KiB
C#
59 lines
1.9 KiB
C#
using Godot;
|
|
using Mr.BrickAdventures.Autoloads;
|
|
using Mr.BrickAdventures.Progression;
|
|
using Mr.BrickAdventures.scripts.Resources;
|
|
|
|
namespace Mr.BrickAdventures.scripts.components;
|
|
|
|
/// <summary>
|
|
/// Buys skills on the player's behalf.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Now a thin adapter. Unlocking and upgrading used to be two methods that each checked affordability,
|
|
/// debited coins, mutated <c>SkillData.Level</c> and told <c>SkillManager</c> separately — four steps a
|
|
/// caller had to get right, in order. <c>PurchaseSkill</c> 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.
|
|
/// </remarks>
|
|
[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;
|
|
}
|
|
|
|
/// <summary>Buys the next level of a skill, whether that is the first or an upgrade.</summary>
|
|
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;
|
|
}
|
|
|
|
/// <summary>Grants every purchasable skill, ignoring cost. Console only.</summary>
|
|
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);
|
|
}
|
|
}
|
|
}
|