refactor (#6)

Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
2026-02-01 11:47:40 +00:00
parent dde3eaa52e
commit bfe951939d
71 changed files with 1583 additions and 661 deletions

View File

@@ -0,0 +1,29 @@
using Godot;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
/// <summary>
/// Handles coin collection events and updates the GameStateStore.
/// Replaces the manual coin logic in GameManager.
/// </summary>
public partial class CoinStateHandler : Node
{
public override void _Ready()
{
EventBus.Instance.CoinCollected += OnCoinCollected;
}
public override void _ExitTree()
{
if (EventBus.Instance != null)
{
EventBus.Instance.CoinCollected -= OnCoinCollected;
}
}
private void OnCoinCollected(int amount, Vector2 position)
{
GameStateStore.Instance?.AddSessionCoins(amount);
}
}

View File

@@ -0,0 +1 @@
uid://1qg3q53kkh0k

View File

@@ -1,4 +1,5 @@
using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
@@ -10,11 +11,10 @@ public partial class GhostEventHandler : Node
public override void _Ready()
{
_ghostManager = GetNode<GhostManager>("/root/GhostManager");
var eventBus = GetNode<EventBus>("/root/EventBus");
eventBus.LevelStarted += OnLevelStarted;
eventBus.LevelCompleted += OnLevelCompleted;
_ghostManager = GetNode<GhostManager>(Constants.GhostManagerPath);
EventBus.Instance.LevelStarted += OnLevelStarted;
EventBus.Instance.LevelCompleted += OnLevelCompleted;
}
private void OnLevelStarted(int levelIndex, Node currentScene)
@@ -23,7 +23,7 @@ public partial class GhostEventHandler : Node
_ghostManager.StartRecording(levelIndex);
_ghostManager.SpawnGhostPlayer(levelIndex, currentScene);
}
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
{
_ghostManager.StopRecording(true, completionTime);

View File

@@ -0,0 +1,39 @@
using Godot;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
/// <summary>
/// Handles level completion events and updates GameStateStore.
/// </summary>
public partial class LevelStateHandler : Node
{
public override void _Ready()
{
EventBus.Instance.LevelCompleted += OnLevelCompleted;
}
public override void _ExitTree()
{
if (EventBus.Instance != null)
{
EventBus.Instance.LevelCompleted -= OnLevelCompleted;
}
}
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
{
var store = GameStateStore.Instance;
if (store == null) return;
// Mark level complete and unlock next
store.MarkLevelComplete(levelIndex);
// Commit session data to persistent state
store.CommitSessionCoins();
store.CommitSessionSkills();
// Reset session for next level
store.ResetSession();
}
}

View File

@@ -0,0 +1 @@
uid://gx5vn7viphv

View File

@@ -0,0 +1,29 @@
using Godot;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
/// <summary>
/// Handles player death events and updates lives in GameStateStore.
/// </summary>
public partial class LivesStateHandler : Node
{
public override void _Ready()
{
EventBus.Instance.PlayerDied += OnPlayerDied;
}
public override void _ExitTree()
{
if (EventBus.Instance != null)
{
EventBus.Instance.PlayerDied -= OnPlayerDied;
}
}
private void OnPlayerDied(Vector2 position)
{
GameStateStore.Instance?.RemoveLife();
GameStateStore.Instance?.ResetSession();
}
}

View File

@@ -0,0 +1 @@
uid://b4ocg7g8vmtvp

View File

@@ -0,0 +1,43 @@
using Godot;
using Mr.BrickAdventures.Autoloads;
using Mr.BrickAdventures.scripts.Resources;
namespace Mr.BrickAdventures.scripts.Events;
/// <summary>
/// Handles skill collection events and unlocks skills via GameStateStore.
/// Skills are immediately activated but only persisted on level complete.
/// </summary>
public partial class SkillCollectHandler : Node
{
private SkillManager _skillManager;
public override void _Ready()
{
_skillManager = SkillManager.Instance;
EventBus.Instance.SkillCollected += OnSkillCollected;
}
public override void _ExitTree()
{
if (EventBus.Instance != null)
{
EventBus.Instance.SkillCollected -= OnSkillCollected;
}
}
private void OnSkillCollected(SkillData skill, Vector2 position)
{
if (skill == null) return;
// Unlock in session (will be committed on level complete, lost on death)
GameStateStore.Instance?.UnlockSkillInSession(skill);
// Immediately activate the skill for the player
skill.Level = 1;
_skillManager?.AddSkill(skill);
// Emit skill unlocked event for UI/achievements
EventBus.EmitSkillUnlocked(skill.Name, skill.Level);
}
}

View File

@@ -0,0 +1 @@
uid://c1po4hjvqbslm

View File

@@ -1,4 +1,5 @@
using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
@@ -10,10 +11,9 @@ public partial class SpeedRunEventHandler : Node
public override void _Ready()
{
_speedRunManager = GetNode<SpeedRunManager>("/root/SpeedRunManager");
var eventBus = GetNode<EventBus>("/root/EventBus");
eventBus.LevelCompleted += OnLevelCompleted;
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
EventBus.Instance.LevelCompleted += OnLevelCompleted;
}
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)

View File

@@ -0,0 +1,62 @@
using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
/// <summary>
/// Handles game events and updates statistics accordingly.
/// Listens to EventBus signals and increments relevant stats.
/// </summary>
public partial class StatisticsEventHandler : Node
{
private StatisticsManager _statisticsManager;
public override void _Ready()
{
_statisticsManager = GetNode<StatisticsManager>(Constants.StatisticsManagerPath);
// Subscribe to events
EventBus.Instance.CoinCollected += OnCoinCollected;
EventBus.Instance.EnemyDefeated += OnEnemyDefeated;
EventBus.Instance.PlayerDied += OnPlayerDied;
EventBus.Instance.LevelCompleted += OnLevelCompleted;
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.LevelCompleted -= OnLevelCompleted;
EventBus.Instance.ChildRescued -= OnChildRescued;
}
private void OnCoinCollected(int amount, Vector2 position)
{
_statisticsManager.IncrementStat("coins_collected", amount);
}
private void OnEnemyDefeated(Node enemy, Vector2 position)
{
_statisticsManager.IncrementStat("enemies_defeated");
}
private void OnPlayerDied(Vector2 position)
{
_statisticsManager.IncrementStat("deaths");
}
private void OnLevelCompleted(int levelIndex, Node currentScene, double completionTime)
{
_statisticsManager.IncrementStat("levels_completed");
}
private void OnChildRescued(Vector2 position)
{
_statisticsManager.IncrementStat("children_rescued");
}
}

View File

@@ -0,0 +1 @@
uid://l68tjau3k6bw