using Godot;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.Progression;
namespace Mr.BrickAdventures.scripts.Events;
///
/// Records lifetime statistics on the Profile.
///
///
/// Statistics moved to the Profile alongside level progression, because they outlive a Playthrough —
/// see docs/adr/0001-three-progression-scopes.md. is a closed set, so a typo is
/// now a compile error rather than a silently invented sixth statistic.
///
public partial class StatisticsEventHandler : Node
{
public override void _Ready()
{
EventBus.Instance.CoinCollected += OnCoinCollected;
EventBus.Instance.EnemyDefeated += OnEnemyDefeated;
EventBus.Instance.PlayerDied += OnPlayerDied;
EventBus.Instance.ChildRescued += OnChildRescued;
}
public override void _ExitTree()
{
if (EventBus.Instance == null) return;
EventBus.Instance.CoinCollected -= OnCoinCollected;
EventBus.Instance.EnemyDefeated -= OnEnemyDefeated;
EventBus.Instance.PlayerDied -= OnPlayerDied;
EventBus.Instance.ChildRescued -= OnChildRescued;
}
private static void Record(StatName stat, int amount = 1) =>
ProgressionRoot.Instance?.RecordStat(stat, amount);
// Record rejects non-positive amounts, so a zero-value coin is skipped rather than throwing.
private void OnCoinCollected(int amount, Vector2 position)
{
if (amount > 0) Record(StatName.CoinsCollected, amount);
}
private void OnEnemyDefeated(Node enemy, Vector2 position) => Record(StatName.EnemiesDefeated);
private void OnPlayerDied(Vector2 position) => Record(StatName.Deaths);
private void OnChildRescued(Vector2 position) => Record(StatName.ChildrenRescued);
// LevelsCompleted is recorded by ProgressionService.FinishLevel, not here. It counts completions
// including Replays — deliberately a different fact from ClearedLevels.Count — and keeping it next
// to the settlement is what stops the two drifting apart the way the old pair did.
}