feat: Implement a comprehensive global event bus with new event handlers and integrate player health and collection events.

This commit is contained in:
2026-01-31 15:15:52 +01:00
parent dde3eaa52e
commit 62bdf1ba39
15 changed files with 322 additions and 54 deletions

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,34 @@
using Godot;
using Mr.BrickAdventures;
using Mr.BrickAdventures.Autoloads;
namespace Mr.BrickAdventures.scripts.Events;
/// <summary>
/// Handles coin collection events and updates the session state.
/// Replaces the manual signal wiring in ScoreComponent.
/// </summary>
public partial class ScoreEventHandler : Node
{
private GameManager _gameManager;
public override void _Ready()
{
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
EventBus.Instance.CoinCollected += OnCoinCollected;
}
public override void _ExitTree()
{
if (EventBus.Instance != null)
EventBus.Instance.CoinCollected -= OnCoinCollected;
}
private void OnCoinCollected(int amount, Vector2 position)
{
var currentCoins = (int)_gameManager.CurrentSessionState["coins_collected"];
_gameManager.CurrentSessionState["coins_collected"] = currentCoins + amount;
GD.Print($"ScoreEventHandler: Collected {amount} coins. Total session coins: {currentCoins + amount}");
}
}

View File

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

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