Counter-based pause
This commit is contained in:
@@ -3,7 +3,6 @@ using System.Collections.Generic;
|
||||
using AppleHills.Core.Interfaces;
|
||||
using AppleHills.Core.Settings;
|
||||
using Bootstrap;
|
||||
using UI;
|
||||
using UnityEngine;
|
||||
|
||||
namespace Core
|
||||
@@ -16,14 +15,13 @@ namespace Core
|
||||
// Singleton implementation
|
||||
private static GameManager _instance;
|
||||
public static GameManager Instance => _instance;
|
||||
private static bool _isQuitting = false;
|
||||
|
||||
private bool _settingsLoaded;
|
||||
private bool _developerSettingsLoaded;
|
||||
|
||||
private bool _settingsLoaded = false;
|
||||
private bool _developerSettingsLoaded = false;
|
||||
|
||||
// Pausable implementation
|
||||
private bool _isPaused = false;
|
||||
public bool IsPaused => _isPaused;
|
||||
// Pausable implementation (counter-based)
|
||||
private int _pauseCount;
|
||||
public bool IsPaused => _pauseCount > 0;
|
||||
|
||||
// List of pausable components that have registered with the GameManager
|
||||
private List<IPausable> _pausableComponents = new List<IPausable>();
|
||||
@@ -66,7 +64,7 @@ namespace Core
|
||||
_pausableComponents.Add(component);
|
||||
|
||||
// If the game is already paused, pause the component immediately
|
||||
if (_isPaused)
|
||||
if (IsPaused)
|
||||
{
|
||||
component.Pause();
|
||||
}
|
||||
@@ -88,57 +86,61 @@ namespace Core
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Revisit this with proper pause menu request implementation
|
||||
public void RequestGamePause()
|
||||
/// <summary>
|
||||
/// Request a pause. Multiple systems can request pause; the game only resumes when all requests are released.
|
||||
/// </summary>
|
||||
/// <param name="requester">Optional object requesting the pause (for logging)</param>
|
||||
public void RequestPause(object requester = null)
|
||||
{
|
||||
PauseGame();
|
||||
_pauseCount++;
|
||||
if (_pauseCount == 1)
|
||||
{
|
||||
ApplyPause(true);
|
||||
}
|
||||
|
||||
Logging.Debug($"[GameManager] Pause requested by {requester?.ToString() ?? "Unknown"}. pauseCount = {_pauseCount}");
|
||||
}
|
||||
|
||||
public void RequestGameResume()
|
||||
{
|
||||
ResumeGame();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pause the game and notify all registered pausable components
|
||||
/// Release a previously requested pause. If this brings the pause count to zero the game resumes.
|
||||
/// </summary>
|
||||
private void PauseGame()
|
||||
/// <param name="requester">Optional object releasing the pause (for logging)</param>
|
||||
public void ReleasePause(object requester = null)
|
||||
{
|
||||
if (_isPaused) return; // Already paused
|
||||
|
||||
_isPaused = true;
|
||||
|
||||
// Pause all registered components
|
||||
_pauseCount = Mathf.Max(0, _pauseCount - 1);
|
||||
if (_pauseCount == 0)
|
||||
{
|
||||
ApplyPause(false);
|
||||
}
|
||||
|
||||
Logging.Debug($"[GameManager] Pause released by {requester?.ToString() ?? "Unknown"}. pauseCount = {_pauseCount}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Apply pause/resume state to registered components and global systems.
|
||||
/// </summary>
|
||||
/// <param name="shouldPause">True to pause; false to resume</param>
|
||||
private void ApplyPause(bool shouldPause)
|
||||
{
|
||||
// Example: stop time
|
||||
Time.timeScale = shouldPause ? 0f : 1f;
|
||||
|
||||
// Notify registered components
|
||||
foreach (var component in _pausableComponents)
|
||||
{
|
||||
component.Pause();
|
||||
if (shouldPause)
|
||||
component.Pause();
|
||||
else
|
||||
component.DoResume();
|
||||
}
|
||||
|
||||
// Broadcast pause event
|
||||
OnGamePaused?.Invoke();
|
||||
|
||||
Logging.Debug($"[GameManager] Game paused. Paused {_pausableComponents.Count} components.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resume the game and notify all registered pausable components
|
||||
/// </summary>
|
||||
private void ResumeGame()
|
||||
{
|
||||
if (!_isPaused) return; // Already running
|
||||
|
||||
_isPaused = false;
|
||||
|
||||
// Resume all registered components
|
||||
foreach (var component in _pausableComponents)
|
||||
{
|
||||
component.DoResume();
|
||||
}
|
||||
|
||||
// Broadcast resume event
|
||||
OnGameResumed?.Invoke();
|
||||
|
||||
Logging.Debug($"[GameManager] Game resumed. Resumed {_pausableComponents.Count} components.");
|
||||
|
||||
// Fire events
|
||||
if (shouldPause)
|
||||
OnGamePaused?.Invoke();
|
||||
else
|
||||
OnGameResumed?.Invoke();
|
||||
|
||||
Logging.Debug($"[GameManager] Game {(shouldPause ? "paused" : "resumed")}. Paused {_pausableComponents.Count} components.");
|
||||
}
|
||||
|
||||
private void InitializeSettings()
|
||||
@@ -215,11 +217,6 @@ namespace Core
|
||||
}
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
_isQuitting = true;
|
||||
ServiceLocator.Clear();
|
||||
}
|
||||
|
||||
// Helper method to get settings
|
||||
private T GetSettings<T>() where T : class
|
||||
|
||||
@@ -162,7 +162,7 @@ namespace UI
|
||||
{
|
||||
if (pauseButton != null) pauseButton.SetActive(false);
|
||||
InputManager.Instance.SetInputMode(InputMode.UI);
|
||||
GameManager.Instance.RequestGamePause();
|
||||
GameManager.Instance.RequestPause(this);;
|
||||
Logging.Debug("[PauseMenu] Game Paused");
|
||||
}
|
||||
|
||||
@@ -170,7 +170,7 @@ namespace UI
|
||||
{
|
||||
if (pauseButton != null) pauseButton.SetActive(true);
|
||||
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
|
||||
GameManager.Instance.RequestGameResume();
|
||||
GameManager.Instance.ReleasePause(this);
|
||||
Logging.Debug("[PauseMenu] Game Resumed");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user