refactor
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
|
||||
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
|
||||
@@ -1,34 +0,0 @@
|
||||
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}");
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://cs4cfk7g5vh2v
|
||||
Reference in New Issue
Block a user