refactor
This commit is contained in:
@@ -8,6 +8,7 @@ public static class Constants
|
||||
// Autoload paths
|
||||
public const string EventBusPath = "/root/EventBus";
|
||||
public const string GameManagerPath = "/root/GameManager";
|
||||
public const string GameStateStorePath = "/root/GameStateStore";
|
||||
public const string SaveSystemPath = "/root/SaveSystem";
|
||||
public const string SpeedRunManagerPath = "/root/SpeedRunManager";
|
||||
public const string GhostManagerPath = "/root/GhostManager";
|
||||
|
||||
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
|
||||
73
scripts/State/PlayerState.cs
Normal file
73
scripts/State/PlayerState.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System.Collections.Generic;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.State;
|
||||
|
||||
/// <summary>
|
||||
/// Persistent player data that survives across sessions.
|
||||
/// This is a POCO (Plain Old C# Object) for predictable state management.
|
||||
/// </summary>
|
||||
public class PlayerState
|
||||
{
|
||||
/// <summary>
|
||||
/// Saved coins (not including current session).
|
||||
/// </summary>
|
||||
public int Coins { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Remaining lives.
|
||||
/// </summary>
|
||||
public int Lives { get; set; } = 3;
|
||||
|
||||
/// <summary>
|
||||
/// Indices of completed levels.
|
||||
/// </summary>
|
||||
public List<int> CompletedLevels { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Indices of levels the player can access.
|
||||
/// </summary>
|
||||
public List<int> UnlockedLevels { get; set; } = new() { 0 };
|
||||
|
||||
/// <summary>
|
||||
/// Skills the player has permanently unlocked.
|
||||
/// </summary>
|
||||
public List<SkillData> UnlockedSkills { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Statistics dictionary for tracking game stats.
|
||||
/// </summary>
|
||||
public Dictionary<string, int> Statistics { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// IDs of unlocked achievements.
|
||||
/// </summary>
|
||||
public List<string> UnlockedAchievements { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a fresh default player state.
|
||||
/// </summary>
|
||||
public static PlayerState CreateDefault() => new()
|
||||
{
|
||||
Coins = 0,
|
||||
Lives = 3,
|
||||
CompletedLevels = new List<int>(),
|
||||
UnlockedLevels = new List<int> { 0 },
|
||||
UnlockedSkills = new List<SkillData>(),
|
||||
Statistics = new Dictionary<string, int>()
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Resets this state to default values.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
Coins = 0;
|
||||
Lives = 3;
|
||||
CompletedLevels.Clear();
|
||||
UnlockedLevels.Clear();
|
||||
UnlockedLevels.Add(0);
|
||||
UnlockedSkills.Clear();
|
||||
Statistics.Clear();
|
||||
}
|
||||
}
|
||||
1
scripts/State/PlayerState.cs.uid
Normal file
1
scripts/State/PlayerState.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://gtr1e60jq7iv
|
||||
55
scripts/State/SessionState.cs
Normal file
55
scripts/State/SessionState.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Collections.Generic;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.State;
|
||||
|
||||
/// <summary>
|
||||
/// Data for the current gameplay session.
|
||||
/// Reset when player dies or completes a level.
|
||||
/// </summary>
|
||||
public class SessionState
|
||||
{
|
||||
/// <summary>
|
||||
/// Current level index being played.
|
||||
/// </summary>
|
||||
public int CurrentLevel { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Coins collected during this session (not yet saved).
|
||||
/// </summary>
|
||||
public int CoinsCollected { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Skills unlocked during this session (not yet saved).
|
||||
/// </summary>
|
||||
public List<SkillData> SkillsUnlocked { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a fresh session state.
|
||||
/// </summary>
|
||||
public static SessionState CreateDefault() => new()
|
||||
{
|
||||
CurrentLevel = 0,
|
||||
CoinsCollected = 0,
|
||||
SkillsUnlocked = new List<SkillData>()
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Resets session state to defaults.
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
CoinsCollected = 0;
|
||||
SkillsUnlocked.Clear();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets completely including level.
|
||||
/// </summary>
|
||||
public void ResetAll()
|
||||
{
|
||||
CurrentLevel = 0;
|
||||
CoinsCollected = 0;
|
||||
SkillsUnlocked.Clear();
|
||||
}
|
||||
}
|
||||
1
scripts/State/SessionState.cs.uid
Normal file
1
scripts/State/SessionState.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://chqsdleqrnl7b
|
||||
@@ -2,6 +2,7 @@ using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -34,7 +35,9 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
|
||||
|
||||
EmitSignalExitTriggered();
|
||||
_achievementManager.UnlockAchievement(AchievementId);
|
||||
_gameManager.UnlockLevel((int)_gameManager.PlayerState["current_level"] + 1);
|
||||
// Get current level from GameStateStore
|
||||
var currentLevel = GameStateStore.Instance?.Session.CurrentLevel ?? 0;
|
||||
_gameManager.UnlockLevel(currentLevel + 1);
|
||||
CallDeferred(nameof(GoToNextLevel));
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -38,8 +39,8 @@ public partial class SkillUnlockerComponent : Node
|
||||
skill.IsActive = true;
|
||||
_gameManager.RemoveCoins(skill.Upgrades[0].Cost);
|
||||
|
||||
var skillsUnlocked = (Array<SkillData>)_gameManager.CurrentSessionState["skills_unlocked"];
|
||||
skillsUnlocked.Add(skill);
|
||||
// Add to session state via GameStateStore
|
||||
GameStateStore.Instance?.UnlockSkillInSession(skill);
|
||||
SkillManager.AddSkill(skill);
|
||||
EmitSignalSkillUnlocked(skill);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user