Counter-based pause

This commit is contained in:
Michal Pikulski
2025-10-24 09:52:28 +02:00
parent ef3b4bf369
commit 7bb905eb6b
2 changed files with 55 additions and 58 deletions

View File

@@ -3,7 +3,6 @@ using System.Collections.Generic;
using AppleHills.Core.Interfaces; using AppleHills.Core.Interfaces;
using AppleHills.Core.Settings; using AppleHills.Core.Settings;
using Bootstrap; using Bootstrap;
using UI;
using UnityEngine; using UnityEngine;
namespace Core namespace Core
@@ -16,14 +15,13 @@ namespace Core
// Singleton implementation // Singleton implementation
private static GameManager _instance; private static GameManager _instance;
public static GameManager Instance => _instance; public static GameManager Instance => _instance;
private static bool _isQuitting = false;
private bool _settingsLoaded;
private bool _developerSettingsLoaded;
private bool _settingsLoaded = false; // Pausable implementation (counter-based)
private bool _developerSettingsLoaded = false; private int _pauseCount;
public bool IsPaused => _pauseCount > 0;
// Pausable implementation
private bool _isPaused = false;
public bool IsPaused => _isPaused;
// List of pausable components that have registered with the GameManager // List of pausable components that have registered with the GameManager
private List<IPausable> _pausableComponents = new List<IPausable>(); private List<IPausable> _pausableComponents = new List<IPausable>();
@@ -66,7 +64,7 @@ namespace Core
_pausableComponents.Add(component); _pausableComponents.Add(component);
// If the game is already paused, pause the component immediately // If the game is already paused, pause the component immediately
if (_isPaused) if (IsPaused)
{ {
component.Pause(); component.Pause();
} }
@@ -88,57 +86,61 @@ namespace Core
} }
} }
// TODO: Revisit this with proper pause menu request implementation /// <summary>
public void RequestGamePause() /// 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> /// <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> /// </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 _pauseCount = Mathf.Max(0, _pauseCount - 1);
if (_pauseCount == 0)
_isPaused = true; {
ApplyPause(false);
// Pause all registered components }
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) foreach (var component in _pausableComponents)
{ {
component.Pause(); if (shouldPause)
component.Pause();
else
component.DoResume();
} }
// Broadcast pause event // Fire events
OnGamePaused?.Invoke(); if (shouldPause)
OnGamePaused?.Invoke();
Logging.Debug($"[GameManager] Game paused. Paused {_pausableComponents.Count} components."); else
} OnGameResumed?.Invoke();
/// <summary> Logging.Debug($"[GameManager] Game {(shouldPause ? "paused" : "resumed")}. Paused {_pausableComponents.Count} components.");
/// 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.");
} }
private void InitializeSettings() private void InitializeSettings()
@@ -215,11 +217,6 @@ namespace Core
} }
} }
void OnApplicationQuit()
{
_isQuitting = true;
ServiceLocator.Clear();
}
// Helper method to get settings // Helper method to get settings
private T GetSettings<T>() where T : class private T GetSettings<T>() where T : class

View File

@@ -162,7 +162,7 @@ namespace UI
{ {
if (pauseButton != null) pauseButton.SetActive(false); if (pauseButton != null) pauseButton.SetActive(false);
InputManager.Instance.SetInputMode(InputMode.UI); InputManager.Instance.SetInputMode(InputMode.UI);
GameManager.Instance.RequestGamePause(); GameManager.Instance.RequestPause(this);;
Logging.Debug("[PauseMenu] Game Paused"); Logging.Debug("[PauseMenu] Game Paused");
} }
@@ -170,7 +170,7 @@ namespace UI
{ {
if (pauseButton != null) pauseButton.SetActive(true); if (pauseButton != null) pauseButton.SetActive(true);
InputManager.Instance.SetInputMode(InputMode.GameAndUI); InputManager.Instance.SetInputMode(InputMode.GameAndUI);
GameManager.Instance.RequestGameResume(); GameManager.Instance.ReleasePause(this);
Logging.Debug("[PauseMenu] Game Resumed"); Logging.Debug("[PauseMenu] Game Resumed");
} }