From 7bb905eb6b24205128ae68ba8e411d38dff8eb6c Mon Sep 17 00:00:00 2001 From: Michal Pikulski Date: Fri, 24 Oct 2025 09:52:28 +0200 Subject: [PATCH] Counter-based pause --- Assets/Scripts/Core/GameManager.cs | 109 ++++++++++++++--------------- Assets/Scripts/UI/PauseMenu.cs | 4 +- 2 files changed, 55 insertions(+), 58 deletions(-) diff --git a/Assets/Scripts/Core/GameManager.cs b/Assets/Scripts/Core/GameManager.cs index b6db57ae..cb570c16 100644 --- a/Assets/Scripts/Core/GameManager.cs +++ b/Assets/Scripts/Core/GameManager.cs @@ -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 _pausableComponents = new List(); @@ -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() + /// + /// Request a pause. Multiple systems can request pause; the game only resumes when all requests are released. + /// + /// Optional object requesting the pause (for logging) + 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(); - } - /// - /// 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. /// - private void PauseGame() + /// Optional object releasing the pause (for logging) + 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}"); + } + + /// + /// Apply pause/resume state to registered components and global systems. + /// + /// True to pause; false to resume + 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."); - } - - /// - /// Resume the game and notify all registered pausable components - /// - 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() where T : class diff --git a/Assets/Scripts/UI/PauseMenu.cs b/Assets/Scripts/UI/PauseMenu.cs index 3046fac7..71775c90 100644 --- a/Assets/Scripts/UI/PauseMenu.cs +++ b/Assets/Scripts/UI/PauseMenu.cs @@ -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"); }