refactor (#6)
Reviewed-on: #6
This commit was merged in pull request #6.
This commit is contained in:
29
scripts/Events/CoinStateHandler.cs
Normal file
29
scripts/Events/CoinStateHandler.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
scripts/Events/CoinStateHandler.cs.uid
Normal file
1
scripts/Events/CoinStateHandler.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://1qg3q53kkh0k
|
||||
@@ -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);
|
||||
|
||||
39
scripts/Events/LevelStateHandler.cs
Normal file
39
scripts/Events/LevelStateHandler.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
1
scripts/Events/LevelStateHandler.cs.uid
Normal file
1
scripts/Events/LevelStateHandler.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://gx5vn7viphv
|
||||
29
scripts/Events/LivesStateHandler.cs
Normal file
29
scripts/Events/LivesStateHandler.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
1
scripts/Events/LivesStateHandler.cs.uid
Normal file
1
scripts/Events/LivesStateHandler.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://b4ocg7g8vmtvp
|
||||
43
scripts/Events/SkillCollectHandler.cs
Normal file
43
scripts/Events/SkillCollectHandler.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
scripts/Events/SkillCollectHandler.cs.uid
Normal file
1
scripts/Events/SkillCollectHandler.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://c1po4hjvqbslm
|
||||
@@ -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)
|
||||
|
||||
62
scripts/Events/StatisticsEventHandler.cs
Normal file
62
scripts/Events/StatisticsEventHandler.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
1
scripts/Events/StatisticsEventHandler.cs.uid
Normal file
1
scripts/Events/StatisticsEventHandler.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://l68tjau3k6bw
|
||||
Reference in New Issue
Block a user