Compare commits
5 Commits
master
...
432a71a00c
| Author | SHA1 | Date | |
|---|---|---|---|
| 432a71a00c | |||
| 2fc988da27 | |||
| 3f02f1d4ac | |||
| b62478bbea | |||
| 62bdf1ba39 |
@@ -1,23 +1,22 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Manages achievements using GameStateStore.
|
||||
/// </summary>
|
||||
public partial class AchievementManager : Node
|
||||
{
|
||||
[Export] private string AchievementsFolderPath = "res://achievements/";
|
||||
[Export] private PackedScene AchievementPopupScene { get; set; }
|
||||
|
||||
private System.Collections.Generic.Dictionary<string, AchievementResource> _achievements = new();
|
||||
private Array<string> _unlockedAchievementIds = [];
|
||||
private GameManager _gameManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
LoadAchievementsFromFolder();
|
||||
LoadUnlockedAchievements();
|
||||
}
|
||||
|
||||
private void LoadAchievementsFromFolder()
|
||||
@@ -45,6 +44,14 @@ public partial class AchievementManager : Node
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the list of unlocked achievement IDs from the store.
|
||||
/// </summary>
|
||||
private System.Collections.Generic.List<string> GetUnlockedIds()
|
||||
{
|
||||
return GameStateStore.Instance?.Player.UnlockedAchievements ?? new System.Collections.Generic.List<string>();
|
||||
}
|
||||
|
||||
public void UnlockAchievement(string achievementId)
|
||||
{
|
||||
if (!_achievements.TryGetValue(achievementId, out var achievement))
|
||||
@@ -53,13 +60,14 @@ public partial class AchievementManager : Node
|
||||
return;
|
||||
}
|
||||
|
||||
if (_unlockedAchievementIds.Contains(achievementId))
|
||||
var unlockedIds = GetUnlockedIds();
|
||||
if (unlockedIds.Contains(achievementId))
|
||||
{
|
||||
return; // Already unlocked
|
||||
}
|
||||
|
||||
// 1. Mark as unlocked internally
|
||||
_unlockedAchievementIds.Add(achievementId);
|
||||
// 1. Mark as unlocked
|
||||
unlockedIds.Add(achievementId);
|
||||
GD.Print($"Achievement Unlocked: {achievement.DisplayName}");
|
||||
|
||||
// 2. Show the UI popup
|
||||
@@ -75,31 +83,19 @@ public partial class AchievementManager : Node
|
||||
{
|
||||
SteamManager.UnlockAchievement(achievement.Id);
|
||||
}
|
||||
|
||||
// 4. Save progress
|
||||
SaveUnlockedAchievements();
|
||||
}
|
||||
|
||||
public void LockAchievement(string achievementId)
|
||||
{
|
||||
if (_unlockedAchievementIds.Contains(achievementId))
|
||||
var unlockedIds = GetUnlockedIds();
|
||||
if (unlockedIds.Contains(achievementId))
|
||||
{
|
||||
_unlockedAchievementIds.Remove(achievementId);
|
||||
SaveUnlockedAchievements();
|
||||
unlockedIds.Remove(achievementId);
|
||||
}
|
||||
}
|
||||
|
||||
private void SaveUnlockedAchievements()
|
||||
public bool IsAchievementUnlocked(string achievementId)
|
||||
{
|
||||
_gameManager.PlayerState["unlocked_achievements"] = _unlockedAchievementIds;
|
||||
// You might want to trigger a save game here, depending on your SaveSystem
|
||||
}
|
||||
|
||||
private void LoadUnlockedAchievements()
|
||||
{
|
||||
if (_gameManager.PlayerState.TryGetValue("unlocked_achievements", out var unlocked))
|
||||
{
|
||||
_unlockedAchievementIds = (Array<string>)unlocked;
|
||||
}
|
||||
return GetUnlockedIds().Contains(achievementId);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
@@ -12,9 +13,9 @@ public partial class ConsoleManager : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_achievementManager = GetNode<AchievementManager>("/root/AchievementManager");
|
||||
_skillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_achievementManager = GetNode<AchievementManager>(Constants.AchievementManagerPath);
|
||||
_skillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
}
|
||||
|
||||
private void AddCoinsCommand(int amount)
|
||||
|
||||
@@ -1,9 +1,151 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Global event bus for decoupled communication between game systems.
|
||||
/// Use the static Instance property for easy access from anywhere.
|
||||
/// </summary>
|
||||
public partial class EventBus : Node
|
||||
{
|
||||
/// <summary>
|
||||
/// Singleton instance. Available after the autoload is initialized.
|
||||
/// </summary>
|
||||
public static EventBus Instance { get; private set; }
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (Instance == this)
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
#region Level Events
|
||||
|
||||
[Signal] public delegate void LevelStartedEventHandler(int levelIndex, Node currentScene);
|
||||
[Signal] public delegate void LevelCompletedEventHandler(int levelIndex, Node currentScene, double completionTime);
|
||||
[Signal] public delegate void LevelRestartedEventHandler(int levelIndex);
|
||||
|
||||
public static void EmitLevelStarted(int levelIndex, Node currentScene)
|
||||
=> Instance?.EmitSignal(SignalName.LevelStarted, levelIndex, currentScene);
|
||||
|
||||
public static void EmitLevelCompleted(int levelIndex, Node currentScene, double completionTime)
|
||||
=> Instance?.EmitSignal(SignalName.LevelCompleted, levelIndex, currentScene, completionTime);
|
||||
|
||||
public static void EmitLevelRestarted(int levelIndex)
|
||||
=> Instance?.EmitSignal(SignalName.LevelRestarted, levelIndex);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Player Events
|
||||
|
||||
[Signal] public delegate void PlayerSpawnedEventHandler(PlayerController player);
|
||||
[Signal] public delegate void PlayerDiedEventHandler(Vector2 position);
|
||||
[Signal] public delegate void PlayerDamagedEventHandler(float damage, float remainingHealth, Vector2 position);
|
||||
[Signal] public delegate void PlayerHealedEventHandler(float amount, float newHealth, Vector2 position);
|
||||
|
||||
public static void EmitPlayerSpawned(PlayerController player)
|
||||
=> Instance?.EmitSignal(SignalName.PlayerSpawned, player);
|
||||
|
||||
public static void EmitPlayerDied(Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.PlayerDied, position);
|
||||
|
||||
public static void EmitPlayerDamaged(float damage, float remainingHealth, Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.PlayerDamaged, damage, remainingHealth, position);
|
||||
|
||||
public static void EmitPlayerHealed(float amount, float newHealth, Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.PlayerHealed, amount, newHealth, position);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Combat Events
|
||||
|
||||
[Signal] public delegate void EnemyDefeatedEventHandler(Node enemy, Vector2 position);
|
||||
[Signal] public delegate void EnemyDamagedEventHandler(Node enemy, float damage, Vector2 position);
|
||||
|
||||
public static void EmitEnemyDefeated(Node enemy, Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.EnemyDefeated, enemy, position);
|
||||
|
||||
public static void EmitEnemyDamaged(Node enemy, float damage, Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.EnemyDamaged, enemy, damage, position);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Collection Events
|
||||
|
||||
[Signal] public delegate void CoinCollectedEventHandler(int amount, Vector2 position);
|
||||
[Signal] public delegate void ItemCollectedEventHandler(CollectableType itemType, float amount, Vector2 position);
|
||||
[Signal] public delegate void ChildRescuedEventHandler(Vector2 position);
|
||||
|
||||
public static void EmitCoinCollected(int amount, Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.CoinCollected, amount, position);
|
||||
|
||||
public static void EmitItemCollected(CollectableType itemType, float amount, Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.ItemCollected, (int)itemType, amount, position);
|
||||
|
||||
public static void EmitChildRescued(Vector2 position)
|
||||
=> Instance?.EmitSignal(SignalName.ChildRescued, position);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Skill Events
|
||||
|
||||
[Signal] public delegate void SkillUnlockedEventHandler(string skillName, int level);
|
||||
[Signal] public delegate void SkillActivatedEventHandler(string skillName);
|
||||
[Signal] public delegate void SkillDeactivatedEventHandler(string skillName);
|
||||
|
||||
public static void EmitSkillUnlocked(string skillName, int level = 1)
|
||||
=> Instance?.EmitSignal(SignalName.SkillUnlocked, skillName, level);
|
||||
|
||||
public static void EmitSkillActivated(string skillName)
|
||||
=> Instance?.EmitSignal(SignalName.SkillActivated, skillName);
|
||||
|
||||
public static void EmitSkillDeactivated(string skillName)
|
||||
=> Instance?.EmitSignal(SignalName.SkillDeactivated, skillName);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Game State Events
|
||||
|
||||
[Signal] public delegate void GamePausedEventHandler();
|
||||
[Signal] public delegate void GameResumedEventHandler();
|
||||
[Signal] public delegate void GameSavedEventHandler();
|
||||
[Signal] public delegate void GameStartedEventHandler();
|
||||
[Signal] public delegate void GameContinuedEventHandler();
|
||||
|
||||
public static void EmitGamePaused()
|
||||
=> Instance?.EmitSignal(SignalName.GamePaused);
|
||||
|
||||
public static void EmitGameResumed()
|
||||
=> Instance?.EmitSignal(SignalName.GameResumed);
|
||||
|
||||
public static void EmitGameSaved()
|
||||
=> Instance?.EmitSignal(SignalName.GameSaved);
|
||||
|
||||
public static void EmitGameStarted()
|
||||
=> Instance?.EmitSignal(SignalName.GameStarted);
|
||||
|
||||
public static void EmitGameContinued()
|
||||
=> Instance?.EmitSignal(SignalName.GameContinued);
|
||||
|
||||
#endregion
|
||||
|
||||
#region State Change Events
|
||||
|
||||
[Signal] public delegate void CoinsChangedEventHandler(int totalCoins);
|
||||
[Signal] public delegate void LivesChangedEventHandler(int lives);
|
||||
|
||||
public static void EmitCoinsChanged(int totalCoins)
|
||||
=> Instance?.EmitSignal(SignalName.CoinsChanged, totalCoins);
|
||||
|
||||
public static void EmitLivesChanged(int lives)
|
||||
=> Instance?.EmitSignal(SignalName.LivesChanged, lives);
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -3,15 +3,20 @@ using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Double = System.Double;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Game orchestrator - handles scene management and game flow.
|
||||
/// State is delegated to GameStateStore for better separation of concerns.
|
||||
/// </summary>
|
||||
public partial class GameManager : Node
|
||||
{
|
||||
[Export] public Array<PackedScene> LevelScenes { get; set; } = [];
|
||||
|
||||
public PlayerController Player {
|
||||
public PlayerController Player
|
||||
{
|
||||
get => GetPlayer();
|
||||
private set => _player = value;
|
||||
}
|
||||
@@ -19,25 +24,11 @@ public partial class GameManager : Node
|
||||
private List<Node> _sceneNodes = [];
|
||||
private PlayerController _player;
|
||||
private SpeedRunManager _speedRunManager;
|
||||
private EventBus _eventBus;
|
||||
|
||||
[Export]
|
||||
public Dictionary PlayerState { get; set; } = new()
|
||||
{
|
||||
{ "coins", 0 },
|
||||
{ "lives", 3 },
|
||||
{ "current_level", 0 },
|
||||
{ "completed_levels", new Array<int>() },
|
||||
{ "unlocked_levels", new Array<int>() {0}},
|
||||
{ "unlocked_skills", new Array<SkillData>() }
|
||||
};
|
||||
|
||||
[Export]
|
||||
public Dictionary CurrentSessionState { get; private set; } = new()
|
||||
{
|
||||
{ "coins_collected", 0 },
|
||||
{ "skills_unlocked", new Array<SkillData>() }
|
||||
};
|
||||
/// <summary>
|
||||
/// Lazy accessor for GameStateStore - avoids initialization order issues.
|
||||
/// </summary>
|
||||
private GameStateStore Store => GameStateStore.Instance;
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
@@ -54,8 +45,7 @@ public partial class GameManager : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_speedRunManager = GetNode<SpeedRunManager>("/root/SpeedRunManager");
|
||||
_eventBus = GetNode<EventBus>("/root/EventBus");
|
||||
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
|
||||
}
|
||||
|
||||
private void OnNodeAdded(Node node)
|
||||
@@ -72,57 +62,71 @@ public partial class GameManager : Node
|
||||
}
|
||||
}
|
||||
|
||||
#region Coin Operations
|
||||
|
||||
public void AddCoins(int amount)
|
||||
{
|
||||
PlayerState["coins"] = Mathf.Max(0, (int)PlayerState["coins"] + amount);
|
||||
}
|
||||
|
||||
public void SetCoins(int amount) => PlayerState["coins"] = Mathf.Max(0, amount);
|
||||
|
||||
public int GetCoins() => (int)PlayerState["coins"] + (int)CurrentSessionState["coins_collected"];
|
||||
|
||||
public void RemoveCoins(int amount)
|
||||
{
|
||||
var sessionCoins = (int)CurrentSessionState["coins_collected"];
|
||||
if (amount <= sessionCoins)
|
||||
if (Store != null)
|
||||
{
|
||||
CurrentSessionState["coins_collected"] = sessionCoins - amount;
|
||||
Store.Player.Coins += amount;
|
||||
EventBus.EmitCoinsChanged(Store.GetTotalCoins());
|
||||
}
|
||||
else
|
||||
{
|
||||
var remaining = amount - sessionCoins;
|
||||
CurrentSessionState["coins_collected"] = 0;
|
||||
PlayerState["coins"] = Mathf.Max(0, (int)PlayerState["coins"] - remaining);
|
||||
}
|
||||
PlayerState["coins"] = Mathf.Max(0, (int)PlayerState["coins"]);
|
||||
}
|
||||
|
||||
public void AddLives(int amount) => PlayerState["lives"] = (int)PlayerState["lives"] + amount;
|
||||
public void RemoveLives(int amount) => PlayerState["lives"] = (int)PlayerState["lives"] - amount;
|
||||
public void SetLives(int amount) => PlayerState["lives"] = amount;
|
||||
public int GetLives() => (int)PlayerState["lives"];
|
||||
|
||||
public bool IsSkillUnlocked(SkillData skill)
|
||||
public void SetCoins(int amount)
|
||||
{
|
||||
return ((Array)PlayerState["unlocked_skills"]).Contains(skill)
|
||||
|| ((Array)CurrentSessionState["skills_unlocked"]).Contains(skill);
|
||||
if (Store != null)
|
||||
{
|
||||
Store.Player.Coins = Mathf.Max(0, amount);
|
||||
EventBus.EmitCoinsChanged(Store.GetTotalCoins());
|
||||
}
|
||||
}
|
||||
|
||||
public int GetCoins() => Store?.GetTotalCoins() ?? 0;
|
||||
|
||||
public void RemoveCoins(int amount) => Store?.RemoveCoins(amount);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lives Operations
|
||||
|
||||
public void AddLives(int amount) => Store?.AddLives(amount);
|
||||
public void RemoveLives(int amount) => Store?.RemoveLife();
|
||||
public void SetLives(int amount)
|
||||
{
|
||||
if (Store != null)
|
||||
{
|
||||
Store.Player.Lives = amount;
|
||||
EventBus.EmitLivesChanged(amount);
|
||||
}
|
||||
}
|
||||
public int GetLives() => Store?.Player.Lives ?? 0;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Skill Operations
|
||||
|
||||
public bool IsSkillUnlocked(SkillData skill) => Store?.IsSkillUnlocked(skill) ?? false;
|
||||
|
||||
public void UnlockSkill(SkillData skill)
|
||||
{
|
||||
if (!IsSkillUnlocked(skill))
|
||||
((Array)PlayerState["unlocked_skills"]).Add(skill);
|
||||
if (Store != null && !Store.IsSkillUnlocked(skill))
|
||||
{
|
||||
Store.Player.UnlockedSkills.Add(skill);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveSkill(string skillName)
|
||||
{
|
||||
var arr = (Array)PlayerState["unlocked_skills"];
|
||||
foreach (SkillData s in arr)
|
||||
if (Store == null) return;
|
||||
var skills = Store.Player.UnlockedSkills;
|
||||
for (int i = 0; i < skills.Count; i++)
|
||||
{
|
||||
if (s.Name != skillName) continue;
|
||||
|
||||
arr.Remove(s);
|
||||
break;
|
||||
if (skills[i].Name == skillName)
|
||||
{
|
||||
skills.RemoveAt(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -132,81 +136,84 @@ public partial class GameManager : Node
|
||||
UnlockSkill(s);
|
||||
}
|
||||
|
||||
public void ResetPlayerState()
|
||||
public Array<SkillData> GetUnlockedSkills()
|
||||
{
|
||||
PlayerState = new Dictionary
|
||||
{
|
||||
{ "coins", 0 },
|
||||
{ "lives", 3 },
|
||||
{ "current_level", 0 },
|
||||
{ "completed_levels", new Array<int>() },
|
||||
{ "unlocked_levels", new Array<int>() {0}},
|
||||
{ "unlocked_skills", new Array<SkillData>() },
|
||||
{ "statistics", new Godot.Collections.Dictionary<string, Variant>()}
|
||||
};
|
||||
if (Store == null) return new Array<SkillData>();
|
||||
|
||||
var result = new Array<SkillData>();
|
||||
foreach (var s in Store.Player.UnlockedSkills)
|
||||
result.Add(s);
|
||||
foreach (var s in Store.Session.SkillsUnlocked)
|
||||
if (!result.Contains(s)) result.Add(s);
|
||||
return result;
|
||||
}
|
||||
|
||||
public void UnlockLevel(int levelIndex)
|
||||
{
|
||||
var unlocked = (Array)PlayerState["unlocked_levels"];
|
||||
if (!unlocked.Contains(levelIndex)) unlocked.Add(levelIndex);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Level Operations
|
||||
|
||||
public void UnlockLevel(int levelIndex) => Store?.UnlockLevel(levelIndex);
|
||||
|
||||
public void MarkLevelComplete(int levelIndex) => Store?.MarkLevelComplete(levelIndex);
|
||||
|
||||
public void TryToGoToNextLevel()
|
||||
{
|
||||
var next = (int)PlayerState["current_level"] + 1;
|
||||
var unlocked = (Array)PlayerState["unlocked_levels"];
|
||||
if (next < LevelScenes.Count && unlocked.Contains(next))
|
||||
if (Store == null) return;
|
||||
|
||||
var next = Store.Session.CurrentLevel + 1;
|
||||
if (next < LevelScenes.Count && Store.IsLevelUnlocked(next))
|
||||
{
|
||||
PlayerState["current_level"] = next;
|
||||
Store.Session.CurrentLevel = next;
|
||||
GetTree().ChangeSceneToPacked(LevelScenes[next]);
|
||||
_eventBus.EmitSignal(EventBus.SignalName.LevelStarted, next, GetTree().CurrentScene);
|
||||
EventBus.EmitLevelStarted(next, GetTree().CurrentScene);
|
||||
}
|
||||
}
|
||||
|
||||
public void MarkLevelComplete(int levelIndex)
|
||||
{
|
||||
UnlockLevel(levelIndex + 1);
|
||||
var completed = (Array)PlayerState["completed_levels"];
|
||||
if (!completed.Contains(levelIndex)) completed.Add(levelIndex);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public void ResetCurrentSessionState()
|
||||
{
|
||||
CurrentSessionState = new Dictionary
|
||||
{
|
||||
{ "coins_collected", 0 },
|
||||
{ "skills_unlocked", new Array<SkillData>() }
|
||||
};
|
||||
}
|
||||
#region State Reset
|
||||
|
||||
public void ResetPlayerState() => Store?.ResetAll();
|
||||
|
||||
public void ResetCurrentSessionState() => Store?.ResetSession();
|
||||
|
||||
#endregion
|
||||
|
||||
#region Game Flow
|
||||
|
||||
public void RestartGame()
|
||||
{
|
||||
ResetPlayerState();
|
||||
ResetCurrentSessionState();
|
||||
Store?.ResetAll();
|
||||
GetTree().ChangeSceneToPacked(LevelScenes[0]);
|
||||
GetNode<SaveSystem>("/root/SaveSystem").SaveGame();
|
||||
GetNode<SaveSystem>(Constants.SaveSystemPath).SaveGame();
|
||||
}
|
||||
|
||||
public void QuitGame() => GetTree().Quit();
|
||||
|
||||
public void PauseGame() => Engine.TimeScale = 0;
|
||||
public void ResumeGame() => Engine.TimeScale = 1;
|
||||
public void PauseGame()
|
||||
{
|
||||
Engine.TimeScale = 0;
|
||||
EventBus.EmitGamePaused();
|
||||
}
|
||||
|
||||
public void ResumeGame()
|
||||
{
|
||||
Engine.TimeScale = 1;
|
||||
EventBus.EmitGameResumed();
|
||||
}
|
||||
|
||||
public void StartNewGame()
|
||||
{
|
||||
ResetPlayerState();
|
||||
ResetCurrentSessionState();
|
||||
|
||||
Store?.ResetAll();
|
||||
_speedRunManager?.StartTimer();
|
||||
|
||||
GetTree().ChangeSceneToPacked(LevelScenes[0]);
|
||||
GetNode<SaveSystem>("/root/SaveSystem").SaveGame();
|
||||
GetNode<SaveSystem>(Constants.SaveSystemPath).SaveGame();
|
||||
EventBus.EmitGameStarted();
|
||||
}
|
||||
|
||||
public void ContinueGame()
|
||||
{
|
||||
var save = GetNode<SaveSystem>("/root/SaveSystem");
|
||||
var save = GetNode<SaveSystem>(Constants.SaveSystemPath);
|
||||
if (!save.LoadGame())
|
||||
{
|
||||
GD.PrintErr("Failed to load game. Starting a new game instead.");
|
||||
@@ -214,41 +221,38 @@ public partial class GameManager : Node
|
||||
return;
|
||||
}
|
||||
|
||||
var idx = (int)PlayerState["current_level"];
|
||||
var idx = Store?.Session.CurrentLevel ?? 0;
|
||||
if (idx < LevelScenes.Count)
|
||||
{
|
||||
GetTree().ChangeSceneToPacked(LevelScenes[idx]);
|
||||
EventBus.EmitGameContinued();
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.PrintErr("No levels unlocked to continue.");
|
||||
}
|
||||
}
|
||||
|
||||
public void OnLevelComplete()
|
||||
{
|
||||
var levelIndex = (int)PlayerState["current_level"];
|
||||
MarkLevelComplete(levelIndex);
|
||||
if (Store == null) return;
|
||||
|
||||
AddCoins((int)CurrentSessionState["coins_collected"]);
|
||||
foreach (var s in (Array)CurrentSessionState["skills_unlocked"])
|
||||
UnlockSkill((SkillData)s);
|
||||
var levelIndex = Store.Session.CurrentLevel;
|
||||
Store.MarkLevelComplete(levelIndex);
|
||||
Store.CommitSessionCoins();
|
||||
Store.CommitSessionSkills();
|
||||
|
||||
var completionTime = _speedRunManager?.GetCurrentLevelTime() ?? 0.0;
|
||||
_eventBus.EmitSignal(EventBus.SignalName.LevelCompleted, levelIndex, GetTree().CurrentScene, completionTime);
|
||||
EventBus.EmitLevelCompleted(levelIndex, GetTree().CurrentScene, completionTime);
|
||||
|
||||
ResetCurrentSessionState();
|
||||
Store.ResetSession();
|
||||
TryToGoToNextLevel();
|
||||
GetNode<SaveSystem>("/root/SaveSystem").SaveGame();
|
||||
GetNode<SaveSystem>(Constants.SaveSystemPath).SaveGame();
|
||||
}
|
||||
|
||||
public Array<SkillData> GetUnlockedSkills()
|
||||
{
|
||||
var unlocked = (Array<SkillData>)PlayerState["unlocked_skills"];
|
||||
var session = (Array<SkillData>)CurrentSessionState["skills_unlocked"];
|
||||
if (session!.Count == 0) return unlocked;
|
||||
if (unlocked!.Count == 0) return session;
|
||||
var joined = new Array<SkillData>();
|
||||
joined.AddRange(unlocked);
|
||||
joined.AddRange(session);
|
||||
return joined;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Player Lookup
|
||||
|
||||
public PlayerController GetPlayer()
|
||||
{
|
||||
@@ -267,4 +271,6 @@ public partial class GameManager : Node
|
||||
GD.PrintErr("PlayerController not found in the scene tree.");
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
185
Autoloads/GameStateStore.cs
Normal file
185
Autoloads/GameStateStore.cs
Normal file
@@ -0,0 +1,185 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Central store for game state - single source of truth.
|
||||
/// Use the static Instance property for easy access.
|
||||
/// </summary>
|
||||
public partial class GameStateStore : Node
|
||||
{
|
||||
/// <summary>
|
||||
/// Singleton instance.
|
||||
/// </summary>
|
||||
public static GameStateStore Instance { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Persistent player state (saved to disk).
|
||||
/// </summary>
|
||||
public PlayerState Player { get; set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// Current session state (transient, reset on death/level complete).
|
||||
/// </summary>
|
||||
public SessionState Session { get; set; } = new();
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (Instance == this)
|
||||
Instance = null;
|
||||
}
|
||||
|
||||
#region Coin Operations
|
||||
|
||||
/// <summary>
|
||||
/// Gets total coins (saved + session).
|
||||
/// </summary>
|
||||
public int GetTotalCoins() => Player.Coins + Session.CoinsCollected;
|
||||
|
||||
/// <summary>
|
||||
/// Adds coins to the session (not saved until level complete).
|
||||
/// </summary>
|
||||
public void AddSessionCoins(int amount)
|
||||
{
|
||||
Session.CoinsCollected += amount;
|
||||
EventBus.EmitCoinsChanged(GetTotalCoins());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Commits session coins to player state.
|
||||
/// </summary>
|
||||
public void CommitSessionCoins()
|
||||
{
|
||||
Player.Coins += Session.CoinsCollected;
|
||||
Session.CoinsCollected = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes coins, first from session then from saved.
|
||||
/// </summary>
|
||||
public void RemoveCoins(int amount)
|
||||
{
|
||||
if (amount <= Session.CoinsCollected)
|
||||
{
|
||||
Session.CoinsCollected -= amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
var remaining = amount - Session.CoinsCollected;
|
||||
Session.CoinsCollected = 0;
|
||||
Player.Coins = Mathf.Max(0, Player.Coins - remaining);
|
||||
}
|
||||
EventBus.EmitCoinsChanged(GetTotalCoins());
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Lives Operations
|
||||
|
||||
/// <summary>
|
||||
/// Decrements lives by 1.
|
||||
/// </summary>
|
||||
public void RemoveLife()
|
||||
{
|
||||
Player.Lives = Mathf.Max(0, Player.Lives - 1);
|
||||
EventBus.EmitLivesChanged(Player.Lives);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds lives.
|
||||
/// </summary>
|
||||
public void AddLives(int amount)
|
||||
{
|
||||
Player.Lives += amount;
|
||||
EventBus.EmitLivesChanged(Player.Lives);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Level Operations
|
||||
|
||||
/// <summary>
|
||||
/// Unlocks a level for access.
|
||||
/// </summary>
|
||||
public void UnlockLevel(int levelIndex)
|
||||
{
|
||||
if (!Player.UnlockedLevels.Contains(levelIndex))
|
||||
Player.UnlockedLevels.Add(levelIndex);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marks a level as completed and unlocks the next.
|
||||
/// </summary>
|
||||
public void MarkLevelComplete(int levelIndex)
|
||||
{
|
||||
if (!Player.CompletedLevels.Contains(levelIndex))
|
||||
Player.CompletedLevels.Add(levelIndex);
|
||||
UnlockLevel(levelIndex + 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a level is unlocked.
|
||||
/// </summary>
|
||||
public bool IsLevelUnlocked(int levelIndex) => Player.UnlockedLevels.Contains(levelIndex);
|
||||
|
||||
#endregion
|
||||
|
||||
#region Skill Operations
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a skill is unlocked (saved or session).
|
||||
/// </summary>
|
||||
public bool IsSkillUnlocked(SkillData skill)
|
||||
{
|
||||
return Player.UnlockedSkills.Contains(skill) || Session.SkillsUnlocked.Contains(skill);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unlocks a skill in the session.
|
||||
/// </summary>
|
||||
public void UnlockSkillInSession(SkillData skill)
|
||||
{
|
||||
if (!IsSkillUnlocked(skill))
|
||||
Session.SkillsUnlocked.Add(skill);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Commits session skills to player state.
|
||||
/// </summary>
|
||||
public void CommitSessionSkills()
|
||||
{
|
||||
foreach (var skill in Session.SkillsUnlocked)
|
||||
{
|
||||
if (!Player.UnlockedSkills.Contains(skill))
|
||||
Player.UnlockedSkills.Add(skill);
|
||||
}
|
||||
Session.SkillsUnlocked.Clear();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Reset Operations
|
||||
|
||||
/// <summary>
|
||||
/// Resets only the session state.
|
||||
/// </summary>
|
||||
public void ResetSession() => Session.Reset();
|
||||
|
||||
/// <summary>
|
||||
/// Resets everything to defaults.
|
||||
/// </summary>
|
||||
public void ResetAll()
|
||||
{
|
||||
Player.Reset();
|
||||
Session.ResetAll();
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
1
Autoloads/GameStateStore.cs.uid
Normal file
1
Autoloads/GameStateStore.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bwrhkipwecytk
|
||||
@@ -1,61 +1,180 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Save system that serializes state to JSON using DTOs.
|
||||
/// </summary>
|
||||
public partial class SaveSystem : Node
|
||||
{
|
||||
[Export] public string SavePath { get; set; } = "user://savegame.save";
|
||||
[Export] public int Version { get; set; } = 1;
|
||||
[Export] public string SavePath { get; set; } = "user://savegame.json";
|
||||
[Export] public int Version { get; set; } = 2;
|
||||
|
||||
private GameManager _gameManager;
|
||||
|
||||
public override void _Ready()
|
||||
private static readonly JsonSerializerOptions JsonOptions = new()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
}
|
||||
WriteIndented = true,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
};
|
||||
|
||||
public void SaveGame()
|
||||
{
|
||||
var saveData = new Dictionary
|
||||
var store = GameStateStore.Instance;
|
||||
if (store == null)
|
||||
{
|
||||
{ "player_state", _gameManager.PlayerState},
|
||||
{ "version", Version}
|
||||
GD.PrintErr("SaveSystem: GameStateStore not available.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert to DTO (only serializable data)
|
||||
var saveData = new SaveDataDto
|
||||
{
|
||||
Version = Version,
|
||||
Coins = store.Player.Coins,
|
||||
Lives = store.Player.Lives,
|
||||
CurrentLevel = store.Session.CurrentLevel,
|
||||
CompletedLevels = [.. store.Player.CompletedLevels],
|
||||
UnlockedLevels = new List<int>(store.Player.UnlockedLevels),
|
||||
UnlockedSkillNames = GetSkillNames(store.Player.UnlockedSkills),
|
||||
UnlockedAchievements = new List<string>(store.Player.UnlockedAchievements),
|
||||
Statistics = new Dictionary<string, int>(store.Player.Statistics)
|
||||
};
|
||||
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
file.StoreVar(saveData);
|
||||
GD.Print("Game state saved to: ", SavePath);
|
||||
try
|
||||
{
|
||||
var json = JsonSerializer.Serialize(saveData, JsonOptions);
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Write);
|
||||
file.StoreString(json);
|
||||
GD.Print("Game saved to: ", SavePath);
|
||||
EventBus.EmitGameSaved();
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
GD.PrintErr($"SaveSystem: Failed to save game: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public bool LoadGame()
|
||||
{
|
||||
if (!FileAccess.FileExists(SavePath))
|
||||
return false;
|
||||
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Read);
|
||||
var saveDataObj = (Dictionary)file.GetVar();
|
||||
|
||||
if (saveDataObj.ContainsKey("version") && (int)saveDataObj["version"] != Version)
|
||||
{
|
||||
GD.Print($"Save file version mismatch. Expected: {Version}, Found: {saveDataObj["version"]}");
|
||||
GD.Print("SaveSystem: No save file found.");
|
||||
return false;
|
||||
}
|
||||
|
||||
GD.Print("Game state loaded from: ", SavePath);
|
||||
GD.Print("Player state: ", saveDataObj["player_state"]);
|
||||
_gameManager.PlayerState = (Dictionary)saveDataObj["player_state"];
|
||||
|
||||
var skills = new Array<SkillData>();
|
||||
foreach (var skill in (Array<SkillData>)_gameManager.PlayerState["unlocked_skills"])
|
||||
try
|
||||
{
|
||||
skills.Add(skill);
|
||||
using var file = FileAccess.Open(SavePath, FileAccess.ModeFlags.Read);
|
||||
var json = file.GetAsText();
|
||||
var saveData = JsonSerializer.Deserialize<SaveDataDto>(json, JsonOptions);
|
||||
|
||||
if (saveData == null)
|
||||
{
|
||||
GD.PrintErr("SaveSystem: Failed to deserialize save data.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (saveData.Version != Version)
|
||||
{
|
||||
GD.PrintErr($"SaveSystem: Version mismatch. Expected {Version}, found {saveData.Version}");
|
||||
return false;
|
||||
}
|
||||
|
||||
var store = GameStateStore.Instance;
|
||||
if (store == null)
|
||||
{
|
||||
GD.PrintErr("SaveSystem: GameStateStore not available.");
|
||||
return false;
|
||||
}
|
||||
|
||||
// Apply loaded state
|
||||
store.Player.Coins = saveData.Coins;
|
||||
store.Player.Lives = saveData.Lives;
|
||||
store.Session.CurrentLevel = saveData.CurrentLevel;
|
||||
|
||||
store.Player.CompletedLevels = saveData.CompletedLevels ?? new List<int>();
|
||||
store.Player.UnlockedLevels = saveData.UnlockedLevels ?? new List<int> { 0 };
|
||||
store.Player.UnlockedAchievements = saveData.UnlockedAchievements ?? new List<string>();
|
||||
store.Player.Statistics = saveData.Statistics ?? new Dictionary<string, int>();
|
||||
|
||||
// Reload skills by name from SkillManager
|
||||
store.Player.UnlockedSkills = LoadSkillsByName(saveData.UnlockedSkillNames);
|
||||
|
||||
GD.Print("Game loaded from: ", SavePath);
|
||||
return true;
|
||||
}
|
||||
catch (System.Exception e)
|
||||
{
|
||||
GD.PrintErr($"SaveSystem: Failed to load game: {e.Message}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static List<string> GetSkillNames(List<SkillData> skills)
|
||||
{
|
||||
var names = new List<string>();
|
||||
foreach (var skill in skills)
|
||||
{
|
||||
if (skill != null)
|
||||
names.Add(skill.Name);
|
||||
}
|
||||
return names;
|
||||
}
|
||||
|
||||
private List<SkillData> LoadSkillsByName(List<string> skillNames)
|
||||
{
|
||||
var skills = new List<SkillData>();
|
||||
if (skillNames == null) return skills;
|
||||
|
||||
var skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath);
|
||||
if (skillManager == null)
|
||||
{
|
||||
GD.PrintErr("SaveSystem: SkillManager not available to resolve skill names.");
|
||||
return skills;
|
||||
}
|
||||
|
||||
_gameManager.UnlockSkills(skills);
|
||||
return true;
|
||||
foreach (var name in skillNames)
|
||||
{
|
||||
var skill = skillManager.GetSkillByName(name);
|
||||
if (skill != null)
|
||||
{
|
||||
skills.Add(skill);
|
||||
}
|
||||
else
|
||||
{
|
||||
GD.PrintErr($"SaveSystem: Skill '{name}' not found in SkillManager.");
|
||||
}
|
||||
}
|
||||
return skills;
|
||||
}
|
||||
|
||||
public bool CheckSaveExists() => FileAccess.FileExists(SavePath);
|
||||
|
||||
public void DeleteSave()
|
||||
{
|
||||
if (FileAccess.FileExists(SavePath))
|
||||
{
|
||||
DirAccess.RemoveAbsolute(ProjectSettings.GlobalizePath(SavePath));
|
||||
GD.Print("Save file deleted.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serializable DTO for save data - no Godot types.
|
||||
/// </summary>
|
||||
public class SaveDataDto
|
||||
{
|
||||
public int Version { get; set; }
|
||||
public int Coins { get; set; }
|
||||
public int Lives { get; set; }
|
||||
public int CurrentLevel { get; set; }
|
||||
public List<int> CompletedLevels { get; set; }
|
||||
public List<int> UnlockedLevels { get; set; }
|
||||
public List<string> UnlockedSkillNames { get; set; }
|
||||
public List<string> UnlockedAchievements { get; set; }
|
||||
public Dictionary<string, int> Statistics { get; set; }
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -24,7 +25,7 @@ public partial class SkillManager : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -114,7 +115,7 @@ public partial class SkillManager : Node
|
||||
|
||||
if (instance is BrickThrowComponent btc)
|
||||
{
|
||||
EmitSignalActiveThrowSkillChanged(btc);
|
||||
EmitSignalActiveThrowSkillChanged(btc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,21 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
|
||||
/// <summary>
|
||||
/// Manages game statistics using GameStateStore.
|
||||
/// </summary>
|
||||
public partial class StatisticsManager : Node
|
||||
{
|
||||
private GameManager _gameManager;
|
||||
private AchievementManager _achievementManager;
|
||||
private Dictionary<string, Variant> _stats = new();
|
||||
|
||||
public override void _Ready()
|
||||
/// <summary>
|
||||
/// Gets the statistics dictionary from the store.
|
||||
/// </summary>
|
||||
private Dictionary<string, int> GetStats()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_achievementManager = GetNode<AchievementManager>("/root/AchievementManager");
|
||||
LoadStatistics();
|
||||
}
|
||||
|
||||
private void LoadStatistics()
|
||||
{
|
||||
if (_gameManager.PlayerState.TryGetValue("statistics", out var statsObj))
|
||||
{
|
||||
_stats = (Dictionary<string, Variant>)statsObj;
|
||||
}
|
||||
else
|
||||
{
|
||||
_stats = new Dictionary<string, Variant>();
|
||||
_gameManager.PlayerState["statistics"] = _stats;
|
||||
}
|
||||
return GameStateStore.Instance?.Player.Statistics ?? new Dictionary<string, int>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -34,45 +23,40 @@ public partial class StatisticsManager : Node
|
||||
/// </summary>
|
||||
public void IncrementStat(string statName, int amount = 1)
|
||||
{
|
||||
if (_stats.TryGetValue(statName, out var currentValue))
|
||||
var stats = GetStats();
|
||||
if (stats.TryGetValue(statName, out var currentValue))
|
||||
{
|
||||
_stats[statName] = (int)currentValue + amount;
|
||||
stats[statName] = currentValue + amount;
|
||||
}
|
||||
else
|
||||
{
|
||||
_stats[statName] = amount;
|
||||
stats[statName] = amount;
|
||||
}
|
||||
GD.Print($"Stat '{statName}' updated to: {_stats[statName]}");
|
||||
CheckAchievementsForStat(statName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a statistic to a specific value.
|
||||
/// </summary>
|
||||
public void SetStat(string statName, int value)
|
||||
{
|
||||
var stats = GetStats();
|
||||
stats[statName] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of a statistic.
|
||||
/// </summary>
|
||||
public Variant GetStat(string statName, Variant defaultValue = default)
|
||||
public int GetStat(string statName)
|
||||
{
|
||||
return _stats.TryGetValue(statName, out var value) ? value : defaultValue;
|
||||
var stats = GetStats();
|
||||
return stats.TryGetValue(statName, out var value) ? value : 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the updated stat meets the criteria for any achievements.
|
||||
/// Gets a copy of all statistics.
|
||||
/// </summary>
|
||||
private void CheckAchievementsForStat(string statName)
|
||||
public Dictionary<string, int> GetAllStats()
|
||||
{
|
||||
switch (statName)
|
||||
{
|
||||
case "enemies_defeated":
|
||||
if ((int)GetStat(statName, 0) >= 100)
|
||||
{
|
||||
_achievementManager.UnlockAchievement("slayer_100_enemies");
|
||||
}
|
||||
break;
|
||||
case "jumps_made":
|
||||
if ((int)GetStat(statName, 0) >= 1000)
|
||||
{
|
||||
_achievementManager.UnlockAchievement("super_jumper");
|
||||
}
|
||||
break;
|
||||
}
|
||||
return new Dictionary<string, int>(GetStats());
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
[gd_scene load_steps=58 format=3 uid="uid://bqi5s710xb1ju"]
|
||||
[gd_scene load_steps=57 format=3 uid="uid://bqi5s710xb1ju"]
|
||||
|
||||
[ext_resource type="Script" uid="uid://csel4s0e4g5uf" path="res://scripts/components/PlayerController.cs" id="1_yysbb"]
|
||||
[ext_resource type="Shader" uid="uid://bs4xvm4qkurpr" path="res://shaders/hit_flash.tres" id="2_lgb3u"]
|
||||
@@ -19,7 +19,6 @@
|
||||
[ext_resource type="PackedScene" uid="uid://dre1vit1m4d2n" path="res://objects/movement_abilities/grid_movement_ability.tscn" id="8_xuhvf"]
|
||||
[ext_resource type="Script" uid="uid://dy78ak8eykw6e" path="res://scripts/components/FlipComponent.cs" id="9_yysbb"]
|
||||
[ext_resource type="Script" uid="uid://mnjg3p0aw1ow" path="res://scripts/components/CanPickUpComponent.cs" id="10_yysbb"]
|
||||
[ext_resource type="Script" uid="uid://ccqb8kd5m0eh7" path="res://scripts/components/ScoreComponent.cs" id="11_o1ihh"]
|
||||
[ext_resource type="Script" uid="uid://dgb8bqcri7nsj" path="res://scripts/components/HealthComponent.cs" id="12_ur2y5"]
|
||||
[ext_resource type="Script" uid="uid://byw1legrv1ep2" path="res://scripts/components/PlayerDeathComponent.cs" id="13_7til7"]
|
||||
[ext_resource type="Script" uid="uid://cecelixl41t3j" path="res://scripts/components/InvulnerabilityComponent.cs" id="15_xuhvf"]
|
||||
@@ -175,9 +174,6 @@ shape = SubResource("RectangleShape2D_vad0t")
|
||||
[node name="CanPickUpComponent" type="Node" parent="."]
|
||||
script = ExtResource("10_yysbb")
|
||||
|
||||
[node name="ScoreComponent" type="Node" parent="."]
|
||||
script = ExtResource("11_o1ihh")
|
||||
|
||||
[node name="HealthComponent" type="Node2D" parent="." node_paths=PackedStringArray("HurtSfx", "HealSfx")]
|
||||
script = ExtResource("12_ur2y5")
|
||||
HurtSfx = NodePath("../sfx_hurt")
|
||||
|
||||
@@ -29,7 +29,6 @@ config/icon="uid://jix7wdn0isr3"
|
||||
|
||||
[autoload]
|
||||
|
||||
GameManager="*res://objects/game_manager.tscn"
|
||||
PhantomCameraManager="*res://addons/phantom_camera/scripts/managers/phantom_camera_manager.gd"
|
||||
AudioController="*res://objects/audio_controller.tscn"
|
||||
UIManager="*res://Autoloads/UIManager.cs"
|
||||
@@ -46,6 +45,12 @@ EventBus="*res://Autoloads/EventBus.cs"
|
||||
StatisticsManager="*res://Autoloads/StatisticsManager.cs"
|
||||
SpeedRunManager="res://Autoloads/SpeedRunManager.cs"
|
||||
GhostManager="res://objects/ghost_manager.tscn"
|
||||
StatisticsEventHandler="*res://scripts/Events/StatisticsEventHandler.cs"
|
||||
CoinStateHandler="*res://scripts/Events/CoinStateHandler.cs"
|
||||
LevelStateHandler="*res://scripts/Events/LevelStateHandler.cs"
|
||||
LivesStateHandler="*res://scripts/Events/LivesStateHandler.cs"
|
||||
GameStateStore="*res://Autoloads/GameStateStore.cs"
|
||||
GameManager="*res://objects/game_manager.tscn"
|
||||
|
||||
[debug]
|
||||
|
||||
|
||||
25
scripts/Constants.cs
Normal file
25
scripts/Constants.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
namespace Mr.BrickAdventures;
|
||||
|
||||
/// <summary>
|
||||
/// Constants for autoload paths and other commonly used values.
|
||||
/// </summary>
|
||||
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";
|
||||
public const string AchievementManagerPath = "/root/AchievementManager";
|
||||
public const string StatisticsManagerPath = "/root/StatisticsManager";
|
||||
public const string SkillManagerPath = "/root/SkillManager";
|
||||
public const string FloatingTextManagerPath = "/root/FloatingTextManager";
|
||||
public const string UIManagerPath = "/root/UIManager";
|
||||
public const string ConsoleManagerPath = "/root/ConsoleManager";
|
||||
public const string ConfigFileHandlerPath = "/root/ConfigFileHandler";
|
||||
|
||||
// Group names
|
||||
public const string CoinsGroup = "coins";
|
||||
}
|
||||
1
scripts/Constants.cs.uid
Normal file
1
scripts/Constants.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bn7o3n3bomvrd
|
||||
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");
|
||||
_ghostManager = GetNode<GhostManager>(Constants.GhostManagerPath);
|
||||
|
||||
eventBus.LevelStarted += OnLevelStarted;
|
||||
eventBus.LevelCompleted += OnLevelCompleted;
|
||||
EventBus.Instance.LevelStarted += OnLevelStarted;
|
||||
EventBus.Instance.LevelCompleted += OnLevelCompleted;
|
||||
}
|
||||
|
||||
private void OnLevelStarted(int levelIndex, Node currentScene)
|
||||
|
||||
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,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");
|
||||
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
|
||||
|
||||
eventBus.LevelCompleted += OnLevelCompleted;
|
||||
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
|
||||
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
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -16,8 +17,8 @@ public partial class AudioSettings : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_uiManager = GetNode<UIManager>("/root/UIManager");
|
||||
_configFileHandler = GetNode<ConfigFileHandler>("/root/ConfigFileHandler");
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
_configFileHandler = GetNode<ConfigFileHandler>(Constants.ConfigFileHandlerPath);
|
||||
Initialize();
|
||||
MasterVolumeSlider.ValueChanged += OnMasterVolumeChanged;
|
||||
MusicVolumeSlider.ValueChanged += OnMusicVolumeChanged;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -17,7 +18,7 @@ public partial class ChargeProgressBar : ProgressBar
|
||||
{
|
||||
ProgressBar.Hide();
|
||||
|
||||
_skillManager = GetNodeOrNull<SkillManager>("/root/SkillManager");
|
||||
_skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath);
|
||||
if (_skillManager == null)
|
||||
{
|
||||
GD.PrintErr("ChargeProgressBar: SkillManager autoload not found.");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -9,7 +10,7 @@ public partial class Credits : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_uiManager = GetNode<UIManager>("/root/UIManager");
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
}
|
||||
|
||||
public override void _UnhandledInput(InputEvent @event)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
@@ -18,19 +20,36 @@ public partial class DeathScreen : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
SetLabels();
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
|
||||
// Subscribe to lives changed event for reactive updates
|
||||
EventBus.Instance.LivesChanged += OnLivesChanged;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
if (EventBus.Instance != null)
|
||||
{
|
||||
EventBus.Instance.LivesChanged -= OnLivesChanged;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLivesChanged(int lives)
|
||||
{
|
||||
// Update the label when lives change
|
||||
LivesLeftLabel.Text = $" x {lives}";
|
||||
}
|
||||
|
||||
private void SetLabels()
|
||||
{
|
||||
if (_gameManager == null) return;
|
||||
|
||||
if (CurrentLevel != null)
|
||||
{
|
||||
CurrentLevelLabel.Text = CurrentLevel.LevelName;
|
||||
}
|
||||
LivesLeftLabel.Text = $" x {_gameManager.GetLives()}";
|
||||
|
||||
// Read current lives from store
|
||||
var lives = GameStateStore.Instance?.Player.Lives ?? 0;
|
||||
LivesLeftLabel.Text = $" x {lives}";
|
||||
}
|
||||
|
||||
private void SetupTimer()
|
||||
@@ -65,7 +84,8 @@ public partial class DeathScreen : Control
|
||||
|
||||
private void OnTimeout()
|
||||
{
|
||||
if (_gameManager == null || _gameManager.GetLives() == 0) return;
|
||||
var lives = GameStateStore.Instance?.Player.Lives ?? 0;
|
||||
if (lives == 0) return;
|
||||
|
||||
GetTree().ReloadCurrentScene();
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -14,7 +15,7 @@ public partial class GameOverScreen : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
RestartButton.Pressed += OnRestartClicked;
|
||||
MainMenuButton.Pressed += OnMainMenuClicked;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -15,7 +16,7 @@ public partial class Hud : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -21,9 +22,9 @@ public partial class MainMenu : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_saveSystem = GetNode<SaveSystem>("/root/SaveSystem");
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_uiManager = GetNode<UIManager>("/root/UIManager");
|
||||
_saveSystem = GetNode<SaveSystem>(Constants.SaveSystemPath);
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
|
||||
NewGameButton.Pressed += OnNewGamePressed;
|
||||
ContinueButton.Pressed += OnContinuePressed;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -25,8 +26,8 @@ public partial class Marketplace : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_skillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_skillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
_skillManager.SkillRemoved += OnSkillRemoved;
|
||||
|
||||
Skills = _skillManager.AvailableSkills;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -18,7 +19,7 @@ public partial class MarketplaceButton : Button
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
var player = _gameManager.Player;
|
||||
if (player == null) return;
|
||||
|
||||
@@ -28,7 +29,7 @@ public partial class MarketplaceButton : Button
|
||||
_skillUnlockerComponent.SkillUnlocked += OnSkillStateChanged;
|
||||
}
|
||||
|
||||
_skillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_skillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
_skillManager.SkillRemoved += OnSkillStateChanged;
|
||||
|
||||
UpdateButtonState();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -18,8 +19,8 @@ public partial class PauseMenu : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_uiManager = GetNode<UIManager>("/root/UIManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
|
||||
ResumeButton.Pressed += OnResumePressed;
|
||||
MainMenuButton.Pressed += OnMainMenuPressed;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -19,7 +20,7 @@ public partial class SettingsMenu : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_uiManager = GetNode<UIManager>("/root/UIManager");
|
||||
_uiManager = GetNode<UIManager>(Constants.UIManagerPath);
|
||||
|
||||
InputSettingsButton.Pressed += OnInputSettingsPressed;
|
||||
AudioSettingsButton.Pressed += OnAudioSettingsPressed;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
@@ -12,7 +13,7 @@ public partial class SpeedRunHud : Control
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_speedRunManager = GetNode<SpeedRunManager>("/root/SpeedRunManager");
|
||||
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
|
||||
|
||||
_speedRunManager.TimeUpdated += OnTimerUpdated;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
@@ -19,8 +20,8 @@ public partial class BrickShieldSkillComponent : Node, ISkill
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_skillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_skillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
}
|
||||
|
||||
public void Initialize(Node owner, SkillData data)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
@@ -35,7 +36,7 @@ public partial class CollectableComponent : Node
|
||||
if (Owner.HasNode("FadeAwayComponent"))
|
||||
_hasFadeAway = true;
|
||||
|
||||
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
|
||||
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
|
||||
}
|
||||
|
||||
private async void OnArea2DBodyEntered(Node2D body)
|
||||
@@ -53,12 +54,18 @@ public partial class CollectableComponent : Node
|
||||
{
|
||||
case CollectableType.Coin:
|
||||
_floatingTextManager?.ShowCoin((int)Data.Amount, ownerNode.GlobalPosition);
|
||||
EventBus.EmitCoinCollected((int)Data.Amount, ownerNode.GlobalPosition);
|
||||
break;
|
||||
case CollectableType.Health:
|
||||
_floatingTextManager?.ShowMessage("Healed!", ownerNode.GlobalPosition);
|
||||
EventBus.EmitItemCollected(Data.Type, Data.Amount, ownerNode.GlobalPosition);
|
||||
break;
|
||||
case CollectableType.Kid:
|
||||
_floatingTextManager?.ShowMessage("Rescued!", ownerNode.GlobalPosition);
|
||||
EventBus.EmitChildRescued(ownerNode.GlobalPosition);
|
||||
break;
|
||||
default:
|
||||
EventBus.EmitItemCollected(Data.Type, Data.Amount, ownerNode.GlobalPosition);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -34,6 +35,12 @@ public partial class EnemyDeathComponent : Node
|
||||
|
||||
private async Task Die()
|
||||
{
|
||||
// Emit enemy defeated event for statistics and other systems
|
||||
if (Owner is Node2D ownerNode)
|
||||
{
|
||||
EventBus.EmitEnemyDefeated(Owner, ownerNode.GlobalPosition);
|
||||
}
|
||||
|
||||
CollisionShape.SetDisabled(true);
|
||||
var tween = CreateTween();
|
||||
tween.TweenProperty(Owner, "scale", Vector2.Zero, TweenDuration);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.interfaces;
|
||||
using Mr.BrickAdventures.scripts.State;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
@@ -20,8 +22,8 @@ public partial class ExitDoorComponent : Area2D, IUnlockable
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_achievementManager = GetNode<AchievementManager>("/root/AchievementManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
_achievementManager = GetNode<AchievementManager>(Constants.AchievementManagerPath);
|
||||
|
||||
BodyEntered += OnExitAreaBodyEntered;
|
||||
|
||||
@@ -33,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));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -19,7 +20,7 @@ public partial class HealthComponent : Node2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
|
||||
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
|
||||
}
|
||||
|
||||
public void SetHealth(float newValue)
|
||||
@@ -70,10 +71,21 @@ public partial class HealthComponent : Node2D
|
||||
if (Health <= 0f)
|
||||
{
|
||||
EmitSignalDeath();
|
||||
// Emit global event if this is the player
|
||||
if (Owner is PlayerController)
|
||||
EventBus.EmitPlayerDied(GlobalPosition);
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitSignalHealthChanged(delta, Health);
|
||||
// Emit global events if this is the player
|
||||
if (Owner is PlayerController)
|
||||
{
|
||||
if (delta < 0f)
|
||||
EventBus.EmitPlayerDamaged(Mathf.Abs(delta), Health, GlobalPosition);
|
||||
else
|
||||
EventBus.EmitPlayerHealed(delta, Health, GlobalPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -20,7 +21,7 @@ public partial class LeverComponent : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_floatingTextManager = GetNode<FloatingTextManager>("/root/FloatingTextManager");
|
||||
_floatingTextManager = GetNode<FloatingTextManager>(Constants.FloatingTextManagerPath);
|
||||
|
||||
if (Area == null)
|
||||
{
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -33,7 +34,7 @@ public partial class PlayerController : CharacterBody2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var skillManager = GetNodeOrNull<SkillManager>("/root/SkillManager");
|
||||
var skillManager = GetNodeOrNull<SkillManager>(Constants.SkillManagerPath);
|
||||
skillManager?.RegisterPlayer(this);
|
||||
|
||||
_inputHandler = GetNode<PlayerInputHandler>("PlayerInputHandler");
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
@@ -15,7 +16,7 @@ public partial class PlayerDeathComponent : Node2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
HealthComponent.Death += OnDeath;
|
||||
}
|
||||
|
||||
@@ -31,7 +32,7 @@ public partial class PlayerDeathComponent : Node2D
|
||||
effect.Scale = EffectScale;
|
||||
}
|
||||
|
||||
_gameManager.RemoveLives(1);
|
||||
_gameManager.ResetCurrentSessionState();
|
||||
// Lives are now decremented by LivesStateHandler via PlayerDied event
|
||||
// Session state is reset by LivesStateHandler as well
|
||||
}
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class ScoreComponent : Node
|
||||
{
|
||||
private GameManager _gameManager;
|
||||
private const string CoinsGroupName = "coins";
|
||||
|
||||
public override async void _Ready()
|
||||
{
|
||||
await ToSignal(GetTree(), SceneTree.SignalName.ProcessFrame);
|
||||
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
if (_gameManager == null)
|
||||
{
|
||||
GD.PrintErr("GameManager not found in the scene tree.");
|
||||
return;
|
||||
}
|
||||
|
||||
var coins = GetTree().GetNodesInGroup(CoinsGroupName);
|
||||
foreach (var coin in coins)
|
||||
{
|
||||
var c = coin.GetNodeOrNull<CollectableComponent>("CollectableComponent");
|
||||
if (c != null)
|
||||
{
|
||||
c.Collected += OnCollected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnCollected(float amount, CollectableType type, Node2D body)
|
||||
{
|
||||
if (type != CollectableType.Coin) return;
|
||||
|
||||
var coinAmount = (int)amount;
|
||||
var currentCoins = (int)_gameManager.CurrentSessionState["coins_collected"];
|
||||
_gameManager.CurrentSessionState["coins_collected"] = currentCoins + coinAmount;
|
||||
}
|
||||
}
|
||||
@@ -1 +0,0 @@
|
||||
uid://ccqb8kd5m0eh7
|
||||
@@ -1,8 +1,10 @@
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
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;
|
||||
|
||||
@@ -18,8 +20,8 @@ public partial class SkillUnlockerComponent : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
SkillManager = GetNode<SkillManager>("/root/SkillManager");
|
||||
_gameManager = GetNode<GameManager>(Constants.GameManagerPath);
|
||||
SkillManager = GetNode<SkillManager>(Constants.SkillManagerPath);
|
||||
}
|
||||
|
||||
private bool HasEnoughCoins(int amount)
|
||||
@@ -37,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