refactor
This commit is contained in:
@@ -1,13 +1,16 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures;
|
||||
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; } = [];
|
||||
@@ -22,24 +25,10 @@ public partial class GameManager : Node
|
||||
private PlayerController _player;
|
||||
private SpeedRunManager _speedRunManager;
|
||||
|
||||
|
||||
[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()
|
||||
{
|
||||
@@ -57,7 +46,6 @@ public partial class GameManager : Node
|
||||
public override void _Ready()
|
||||
{
|
||||
_speedRunManager = GetNode<SpeedRunManager>(Constants.SpeedRunManagerPath);
|
||||
|
||||
}
|
||||
|
||||
private void OnNodeAdded(Node node)
|
||||
@@ -74,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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,76 +136,79 @@ 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.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>(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>(Constants.SaveSystemPath).SaveGame();
|
||||
EventBus.EmitGameStarted();
|
||||
}
|
||||
|
||||
public void ContinueGame()
|
||||
@@ -216,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.EmitLevelCompleted(levelIndex, GetTree().CurrentScene, completionTime);
|
||||
|
||||
ResetCurrentSessionState();
|
||||
Store.ResetSession();
|
||||
TryToGoToNextLevel();
|
||||
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()
|
||||
{
|
||||
@@ -269,4 +271,6 @@ public partial class GameManager : Node
|
||||
GD.PrintErr("PlayerController not found in the scene tree.");
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
Reference in New Issue
Block a user