Files
przygody-pana-cegly/scripts/components/SkillComponentBase.cs
Gabriel Kaszewski 360f73ba24 feat: coins and skills move to the new domain (commit B of the swap)
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.
2026-07-28 20:13:24 +02:00

52 lines
1.6 KiB
C#

using Godot;
using Mr.BrickAdventures.scripts.interfaces;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.components;
/// <summary>
/// Base class for all skill components, to reduce boilerplate.
/// </summary>
public abstract partial class SkillComponentBase : Node, ISkill
{
protected PlayerController Player { get; private set; }
protected SkillData Data { get; private set; }
/// <summary>
/// How far this skill is upgraded, as owned by the Playthrough. Never read from
/// <see cref="Data"/> — see <see cref="ISkill.Initialize"/>.
/// </summary>
protected int Level { get; private set; } = 1;
public virtual void Initialize(Node owner, SkillData data, int level)
{
Player = owner as PlayerController;
Data = data;
Level = level;
if (Player == null)
{
GD.PrintErr($"{GetType().Name} must be a child of a PlayerController.");
}
}
/// <summary>
/// Applies the upgrade matching the level this skill was initialised at, if the authored table
/// goes that far.
/// </summary>
/// <remarks>
/// Lives here because three components had this guard copy-pasted and one of them — the brick
/// armor — was missing it, indexing <c>Upgrades[Level - 1]</c> unchecked.
/// </remarks>
protected void ApplyCurrentUpgrade()
{
if (Data == null || Level < 1 || Data.Upgrades.Count < Level) return;
ApplyUpgrade(Data.Upgrades[Level - 1]);
}
public abstract void Activate();
public abstract void Deactivate();
public virtual void ApplyUpgrade(SkillUpgrade upgrade) { }
}