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.
This commit is contained in:
@@ -116,15 +116,13 @@ public partial class GameManager : Node
|
||||
var seconds = SpeedRunManager?.GetCurrentLevelTime() ?? 0.0;
|
||||
LevelTime? time = seconds > 0 ? LevelTime.Of(seconds) : null;
|
||||
|
||||
// Old path: coins and skills earned this attempt.
|
||||
Store?.CommitSessionCoins();
|
||||
Store?.CommitSessionSkills();
|
||||
|
||||
EventBus.EmitLevelCompleted(IndexOf(finished), GetTree().CurrentScene, seconds);
|
||||
|
||||
// New path: banks, marks cleared, and advances campaign position if this was a first clear.
|
||||
// Banks the attempt's coins and skills, marks the level cleared, and advances campaign
|
||||
// position if this was a First Clear.
|
||||
Progression.FinishLevel(time);
|
||||
Store?.ResetSession();
|
||||
|
||||
// Still needed for lives, which move with the death path in the next commit.
|
||||
SaveSystem.Instance.SaveGame();
|
||||
|
||||
if (Progression.CurrentLevel is { } next && !next.Equals(finished))
|
||||
|
||||
@@ -1,22 +1,40 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.Progression;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Owns the live skill components attached to the player, and keeps them matching what the Playthrough
|
||||
/// says is owned.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// Reconciles from <see cref="EventBus.SkillSetChanged"/> rather than being told by each caller. The old
|
||||
/// arrangement had four call sites doing "record it in the store, then tell SkillManager" as two
|
||||
/// separate calls — anything between them, including <c>AddSkill</c>'s own early return when no player
|
||||
/// is registered, left a skill owned but inert.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// Tracks the level each component was built at, so an upgrade rebuilds it. Previously
|
||||
/// <c>AddSkill</c> returned early for an already-active skill, and the upgrade was applied by the
|
||||
/// purchase code reaching into the component instead.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public partial class SkillManager : Node
|
||||
{
|
||||
private PlayerController _player;
|
||||
private GameStateStore Store => GameStateStore.Instance;
|
||||
|
||||
[Export] public Array<SkillData> AvailableSkills { get; set; } = [];
|
||||
|
||||
public Dictionary ActiveComponents { get; private set; } = new();
|
||||
|
||||
private readonly System.Collections.Generic.Dictionary<string, int> _activeLevels = new();
|
||||
|
||||
public static SkillManager Instance { get; private set; }
|
||||
|
||||
[Signal]
|
||||
@@ -27,10 +45,14 @@ public partial class SkillManager : Node
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
EventBus.Instance.SkillSetChanged += Reconcile;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (EventBus.Instance != null)
|
||||
EventBus.Instance.SkillSetChanged -= Reconcile;
|
||||
|
||||
if (Instance == this) Instance = null;
|
||||
}
|
||||
|
||||
@@ -41,24 +63,17 @@ public partial class SkillManager : Node
|
||||
{
|
||||
if (_player == player) return;
|
||||
|
||||
// If a player is already registered (e.g., from a previous scene), unregister it first.
|
||||
if (_player != null && IsInstanceValid(_player))
|
||||
{
|
||||
UnregisterPlayer();
|
||||
}
|
||||
|
||||
_player = player;
|
||||
if (_player != null)
|
||||
{
|
||||
// Automatically unregister when the player node is removed from the scene.
|
||||
_player.TreeExiting += UnregisterPlayer;
|
||||
ApplyUnlockedSkills();
|
||||
Reconcile();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cleans up skills and references related to the current player.
|
||||
/// </summary>
|
||||
private void UnregisterPlayer()
|
||||
{
|
||||
if (_player != null && IsInstanceValid(_player))
|
||||
@@ -69,9 +84,37 @@ public partial class SkillManager : Node
|
||||
_player = null;
|
||||
}
|
||||
|
||||
public void AddSkill(SkillData skillData)
|
||||
/// <summary>
|
||||
/// Brings the live components in line with what is owned: adds what is missing, rebuilds anything
|
||||
/// whose level changed, removes what is no longer owned.
|
||||
/// </summary>
|
||||
public void Reconcile()
|
||||
{
|
||||
if (_player == null || !IsInstanceValid(_player)) return;
|
||||
|
||||
var progression = ProgressionRoot.Instance;
|
||||
if (progression == null) return;
|
||||
|
||||
foreach (var skill in AvailableSkills)
|
||||
{
|
||||
if (skill == null || string.IsNullOrWhiteSpace(skill.Name)) continue;
|
||||
|
||||
var owned = progression.LevelOf(SkillId.Of(skill.Name))?.Value;
|
||||
var active = _activeLevels.TryGetValue(skill.Name, out var at) ? at : (int?)null;
|
||||
|
||||
if (owned == active) continue;
|
||||
|
||||
if (active is not null) RemoveSkill(skill.Name);
|
||||
if (owned is not null) AddSkill(skill, owned.Value);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Kept for the marketplace's activate/deactivate toggle, which is a display concern.</summary>
|
||||
public void AddSkill(SkillData skillData) =>
|
||||
AddSkill(skillData, ProgressionRoot.Instance?.LevelOf(SkillId.Of(skillData.Name))?.Value ?? 1);
|
||||
|
||||
private void AddSkill(SkillData skillData, int level)
|
||||
{
|
||||
// Ensure a valid player is registered before adding a skill.
|
||||
if (_player == null || !IsInstanceValid(_player))
|
||||
{
|
||||
GD.Print("SkillManager: Player not available to add skill.");
|
||||
@@ -83,10 +126,9 @@ public partial class SkillManager : Node
|
||||
|
||||
if (skillData.Type == SkillType.Throw)
|
||||
{
|
||||
// Remove any other active throw skill before adding the new one
|
||||
foreach (var sd in AvailableSkills)
|
||||
{
|
||||
if (sd != skillData && sd.Type == SkillType.Throw)
|
||||
if (sd != skillData && sd?.Type == SkillType.Throw)
|
||||
RemoveSkill(sd.Name);
|
||||
}
|
||||
}
|
||||
@@ -94,8 +136,7 @@ public partial class SkillManager : Node
|
||||
var instance = skillData.Node.Instantiate();
|
||||
if (instance is ISkill skill)
|
||||
{
|
||||
// Initialize the skill with the registered player instance.
|
||||
skill.Initialize(_player, skillData);
|
||||
skill.Initialize(_player, skillData, level);
|
||||
skill.Activate();
|
||||
}
|
||||
else
|
||||
@@ -105,31 +146,27 @@ public partial class SkillManager : Node
|
||||
return;
|
||||
}
|
||||
|
||||
// Add the skill node as a child of the player.
|
||||
_player.AddChild(instance);
|
||||
ActiveComponents[skillData.Name] = instance;
|
||||
_activeLevels[skillData.Name] = level;
|
||||
|
||||
if (instance is BrickThrowComponent btc)
|
||||
{
|
||||
EmitSignalActiveThrowSkillChanged(btc);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveSkill(string skillName)
|
||||
{
|
||||
_activeLevels.Remove(skillName);
|
||||
|
||||
if (!ActiveComponents.TryGetValue(skillName, out var component))
|
||||
return;
|
||||
|
||||
if (component.AsGodotObject() is BrickThrowComponent)
|
||||
{
|
||||
EmitSignalActiveThrowSkillChanged(null);
|
||||
}
|
||||
|
||||
var inst = (Node)component;
|
||||
if (inst is ISkill skill)
|
||||
{
|
||||
skill.Deactivate();
|
||||
}
|
||||
|
||||
if (IsInstanceValid(inst))
|
||||
inst.QueueFree();
|
||||
@@ -141,56 +178,30 @@ public partial class SkillManager : Node
|
||||
|
||||
private void RemoveAllActiveSkills()
|
||||
{
|
||||
// Create a copy of keys to avoid modification during iteration
|
||||
var keys = ActiveComponents.Keys.ToArray();
|
||||
var skillNames = keys.Select(key => key.ToString()).ToList();
|
||||
|
||||
foreach (var skillName in skillNames)
|
||||
{
|
||||
foreach (var skillName in ActiveComponents.Keys.Select(k => k.ToString()).ToList())
|
||||
RemoveSkill(skillName);
|
||||
}
|
||||
}
|
||||
|
||||
public void ApplyUnlockedSkills()
|
||||
{
|
||||
if (_player == null || !IsInstanceValid(_player)) return;
|
||||
if (Store == null) return;
|
||||
|
||||
foreach (var sd in AvailableSkills)
|
||||
{
|
||||
if (Store.IsSkillUnlocked(sd))
|
||||
CallDeferred(MethodName.AddSkill, sd);
|
||||
else
|
||||
RemoveSkill(sd.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public SkillData GetSkillByName(string skillName)
|
||||
{
|
||||
foreach (var sd in AvailableSkills)
|
||||
if (sd.Name == skillName) return sd;
|
||||
if (sd?.Name == skillName) return sd;
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsSkillActive(SkillData skill)
|
||||
{
|
||||
return skill != null && ActiveComponents.ContainsKey(skill.Name);
|
||||
}
|
||||
public bool IsSkillActive(SkillData skill) =>
|
||||
skill != null && ActiveComponents.ContainsKey(skill.Name);
|
||||
|
||||
public void ActivateSkill(SkillData skill)
|
||||
{
|
||||
if (!ActiveComponents.ContainsKey(skill.Name))
|
||||
{
|
||||
AddSkill(skill);
|
||||
}
|
||||
}
|
||||
|
||||
public void DeactivateSkill(SkillData skill)
|
||||
{
|
||||
if (ActiveComponents.ContainsKey(skill.Name))
|
||||
{
|
||||
RemoveSkill(skill.Name);
|
||||
}
|
||||
}
|
||||
|
||||
public void ToggleSkillActivation(SkillData skill)
|
||||
@@ -202,4 +213,4 @@ public partial class SkillManager : Node
|
||||
else
|
||||
ActivateSkill(skill);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user