Revamp game pausing and input handling. Fix minigame tutorial and end sequence. (#39)
- Revamp pausing and centralize management in GameManager - Switch Pause implementation to be counter-based to solve corner case of multiple pause requests - Remove duplicated Pause logic from other components - Add pausing when browsing the card album - Fully deliver the exclusive UI implementation - Spruce up the MiniGame tutorial with correct pausing, hiding other UI - Correctly unpause after showing tutorial - Fix minigame ending sequence. The cinematic correctly plays only once now - Replaying the minigame works Co-authored-by: Michal Adam Pikulski <michal@foolhardyhorizons.com> Co-authored-by: Michal Pikulski <michal@foolhardyhorizons.com> Reviewed-on: #39
This commit is contained in:
@@ -1,304 +1,261 @@
|
||||
using UnityEngine;
|
||||
using AppleHills.Core.Settings;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using AppleHills.Core.Interfaces;
|
||||
using Core;
|
||||
using UI;
|
||||
using AppleHills.Core.Settings;
|
||||
using Bootstrap;
|
||||
using Input;
|
||||
using UnityEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton manager for global game state and settings. Provides accessors for various gameplay parameters.
|
||||
/// </summary>
|
||||
public class GameManager : MonoBehaviour
|
||||
namespace Core
|
||||
{
|
||||
private static GameManager _instance;
|
||||
private static bool _isQuitting = false;
|
||||
|
||||
/// <summary>
|
||||
/// Singleton instance of the GameManager. No longer creates an instance if one doesn't exist.
|
||||
/// Singleton manager for global game state and settings. Provides accessors for various gameplay parameters.
|
||||
/// </summary>
|
||||
public static GameManager Instance => _instance;
|
||||
public class GameManager : MonoBehaviour
|
||||
{
|
||||
// Singleton implementation
|
||||
private static GameManager _instance;
|
||||
public static GameManager Instance => _instance;
|
||||
|
||||
[Header("Settings Status")]
|
||||
[SerializeField] private bool _settingsLoaded = false;
|
||||
[SerializeField] private bool _developerSettingsLoaded = false;
|
||||
public bool SettingsLoaded => _settingsLoaded;
|
||||
public bool DeveloperSettingsLoaded => _developerSettingsLoaded;
|
||||
private bool _settingsLoaded;
|
||||
private bool _developerSettingsLoaded;
|
||||
|
||||
// Pausable implementation (counter-based)
|
||||
private int _pauseCount;
|
||||
public bool IsPaused => _pauseCount > 0;
|
||||
|
||||
[Header("Game State")]
|
||||
[SerializeField] private bool _isPaused = false;
|
||||
// List of pausable components that have registered with the GameManager
|
||||
private List<IPausable> _pausableComponents = new List<IPausable>();
|
||||
|
||||
/// <summary>
|
||||
/// Current pause state of the game
|
||||
/// </summary>
|
||||
public bool IsPaused => _isPaused;
|
||||
|
||||
// List of pausable components that have registered with the GameManager
|
||||
private List<IPausable> _pausableComponents = new List<IPausable>();
|
||||
|
||||
// Events for pause state changes
|
||||
public event Action OnGamePaused;
|
||||
public event Action OnGameResumed;
|
||||
// Events for pause state changes
|
||||
public event Action OnGamePaused;
|
||||
public event Action OnGameResumed;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
void Awake()
|
||||
{
|
||||
_instance = this;
|
||||
|
||||
// Create settings provider if it doesn't exist
|
||||
SettingsProvider.Instance.gameObject.name = "Settings Provider";
|
||||
// Create settings providers if it doesn't exist
|
||||
SettingsProvider.Instance.gameObject.name = "Settings Provider";
|
||||
DeveloperSettingsProvider.Instance.gameObject.name = "Developer Settings Provider";
|
||||
|
||||
// Create developer settings provider if it doesn't exist
|
||||
DeveloperSettingsProvider.Instance.gameObject.name = "Developer Settings Provider";
|
||||
// Load all settings synchronously during Awake
|
||||
InitializeSettings();
|
||||
InitializeDeveloperSettings();
|
||||
|
||||
// Load all settings synchronously during Awake
|
||||
InitializeSettings();
|
||||
InitializeDeveloperSettings();
|
||||
// Register for post-boot initialization
|
||||
BootCompletionService.RegisterInitAction(InitializePostBoot);
|
||||
|
||||
// Register for post-boot initialization
|
||||
BootCompletionService.RegisterInitAction(InitializePostBoot);
|
||||
|
||||
// DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
// DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
private void InitializePostBoot()
|
||||
{
|
||||
// Find and subscribe to PauseMenu events
|
||||
PauseMenu pauseMenu = PauseMenu.Instance;
|
||||
if (pauseMenu != null)
|
||||
private void InitializePostBoot()
|
||||
{
|
||||
pauseMenu.OnGamePaused += OnPauseMenuPaused;
|
||||
pauseMenu.OnGameResumed += OnPauseMenuResumed;
|
||||
|
||||
Logging.Debug("[GameManager] Subscribed to PauseMenu events");
|
||||
// For post-boot correct initialization order
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("[GameManager] PauseMenu not found. Pause functionality won't work properly.");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
// Unsubscribe from PauseMenu events
|
||||
PauseMenu pauseMenu = FindFirstObjectByType<PauseMenu>();
|
||||
if (pauseMenu != null)
|
||||
{
|
||||
pauseMenu.OnGamePaused -= OnPauseMenuPaused;
|
||||
pauseMenu.OnGameResumed -= OnPauseMenuResumed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Register a component as pausable, so it receives pause/resume notifications
|
||||
/// </summary>
|
||||
/// <param name="component">The pausable component to register</param>
|
||||
public void RegisterPausableComponent(IPausable component)
|
||||
{
|
||||
if (component != null && !_pausableComponents.Contains(component))
|
||||
/// <summary>
|
||||
/// Register a component as pausable, so it receives pause/resume notifications
|
||||
/// </summary>
|
||||
/// <param name="component">The pausable component to register</param>
|
||||
public void RegisterPausableComponent(IPausable component)
|
||||
{
|
||||
_pausableComponents.Add(component);
|
||||
|
||||
// If the game is already paused, pause the component immediately
|
||||
if (_isPaused)
|
||||
if (component != null && !_pausableComponents.Contains(component))
|
||||
{
|
||||
component.Pause();
|
||||
_pausableComponents.Add(component);
|
||||
|
||||
// If the game is already paused, pause the component immediately
|
||||
if (IsPaused)
|
||||
{
|
||||
component.Pause();
|
||||
}
|
||||
|
||||
Logging.Debug($"[GameManager] Registered pausable component: {(component as MonoBehaviour)?.name ?? "Unknown"}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregister a pausable component
|
||||
/// </summary>
|
||||
/// <param name="component">The pausable component to unregister</param>
|
||||
public void UnregisterPausableComponent(IPausable component)
|
||||
{
|
||||
if (component != null && _pausableComponents.Contains(component))
|
||||
{
|
||||
_pausableComponents.Remove(component);
|
||||
Logging.Debug($"[GameManager] Unregistered pausable component: {(component as MonoBehaviour)?.name ?? "Unknown"}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
_pauseCount++;
|
||||
if (_pauseCount == 1)
|
||||
{
|
||||
ApplyPause(true);
|
||||
}
|
||||
|
||||
Logging.Debug($"[GameManager] Pause requested by {requester?.ToString() ?? "Unknown"}. pauseCount = {_pauseCount}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Release a previously requested pause. If this brings the pause count to zero the game resumes.
|
||||
/// </summary>
|
||||
/// <param name="requester">Optional object releasing the pause (for logging)</param>
|
||||
public void ReleasePause(object requester = null)
|
||||
{
|
||||
_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)
|
||||
{
|
||||
// TODO: Do we want to stop time?
|
||||
// Time.timeScale = shouldPause ? 0f : 1f;
|
||||
|
||||
// Notify registered components
|
||||
foreach (var component in _pausableComponents)
|
||||
{
|
||||
if (shouldPause)
|
||||
component.Pause();
|
||||
else
|
||||
component.DoResume();
|
||||
}
|
||||
|
||||
// Fire events
|
||||
if (shouldPause)
|
||||
{
|
||||
// TODO: Stop input here?
|
||||
InputManager.Instance.SetInputMode(InputMode.UI);
|
||||
OnGamePaused?.Invoke();
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: Release input here?
|
||||
InputManager.Instance.SetInputMode(InputMode.GameAndUI);
|
||||
OnGameResumed?.Invoke();
|
||||
}
|
||||
|
||||
Logging.Debug($"[GameManager] Registered pausable component: {(component as MonoBehaviour)?.name ?? "Unknown"}");
|
||||
Logging.Debug($"[GameManager] Game {(shouldPause ? "paused" : "resumed")}. Paused {_pausableComponents.Count} components.");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregister a pausable component
|
||||
/// </summary>
|
||||
/// <param name="component">The pausable component to unregister</param>
|
||||
public void UnregisterPausableComponent(IPausable component)
|
||||
{
|
||||
if (component != null && _pausableComponents.Contains(component))
|
||||
{
|
||||
_pausableComponents.Remove(component);
|
||||
Logging.Debug($"[GameManager] Unregistered pausable component: {(component as MonoBehaviour)?.name ?? "Unknown"}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the PauseMenu broadcasts a pause event
|
||||
/// </summary>
|
||||
private void OnPauseMenuPaused()
|
||||
{
|
||||
PauseGame();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when the PauseMenu broadcasts a resume event
|
||||
/// </summary>
|
||||
private void OnPauseMenuResumed()
|
||||
{
|
||||
ResumeGame();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pause the game and notify all registered pausable components
|
||||
/// </summary>
|
||||
public void PauseGame()
|
||||
{
|
||||
if (_isPaused) return; // Already paused
|
||||
|
||||
_isPaused = true;
|
||||
|
||||
// Pause all registered components
|
||||
foreach (var component in _pausableComponents)
|
||||
{
|
||||
component.Pause();
|
||||
}
|
||||
|
||||
// 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>
|
||||
public 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.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Toggle the pause state of the game
|
||||
/// </summary>
|
||||
public void TogglePause()
|
||||
{
|
||||
if (_isPaused)
|
||||
ResumeGame();
|
||||
else
|
||||
PauseGame();
|
||||
}
|
||||
|
||||
private void InitializeSettings()
|
||||
{
|
||||
Logging.Debug("Starting settings initialization...");
|
||||
|
||||
// Load settings synchronously
|
||||
var playerSettings = SettingsProvider.Instance.LoadSettingsSynchronous<PlayerFollowerSettings>();
|
||||
var interactionSettings = SettingsProvider.Instance.LoadSettingsSynchronous<InteractionSettings>();
|
||||
var minigameSettings = SettingsProvider.Instance.LoadSettingsSynchronous<DivingMinigameSettings>();
|
||||
|
||||
// Register settings with service locator
|
||||
if (playerSettings != null)
|
||||
private void InitializeSettings()
|
||||
{
|
||||
ServiceLocator.Register<IPlayerFollowerSettings>(playerSettings);
|
||||
Logging.Debug("PlayerFollowerSettings registered successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load PlayerFollowerSettings");
|
||||
}
|
||||
Logging.Debug("Starting settings initialization...");
|
||||
|
||||
// Load settings synchronously
|
||||
var playerSettings = SettingsProvider.Instance.LoadSettingsSynchronous<PlayerFollowerSettings>();
|
||||
var interactionSettings = SettingsProvider.Instance.LoadSettingsSynchronous<InteractionSettings>();
|
||||
var minigameSettings = SettingsProvider.Instance.LoadSettingsSynchronous<DivingMinigameSettings>();
|
||||
|
||||
// Register settings with service locator
|
||||
if (playerSettings != null)
|
||||
{
|
||||
ServiceLocator.Register<IPlayerFollowerSettings>(playerSettings);
|
||||
Logging.Debug("PlayerFollowerSettings registered successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load PlayerFollowerSettings");
|
||||
}
|
||||
|
||||
if (interactionSettings != null)
|
||||
{
|
||||
ServiceLocator.Register<IInteractionSettings>(interactionSettings);
|
||||
Logging.Debug("InteractionSettings registered successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load InteractionSettings");
|
||||
}
|
||||
if (interactionSettings != null)
|
||||
{
|
||||
ServiceLocator.Register<IInteractionSettings>(interactionSettings);
|
||||
Logging.Debug("InteractionSettings registered successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load InteractionSettings");
|
||||
}
|
||||
|
||||
if (minigameSettings != null)
|
||||
{
|
||||
ServiceLocator.Register<IDivingMinigameSettings>(minigameSettings);
|
||||
Logging.Debug("MinigameSettings registered successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load MinigameSettings");
|
||||
if (minigameSettings != null)
|
||||
{
|
||||
ServiceLocator.Register<IDivingMinigameSettings>(minigameSettings);
|
||||
Logging.Debug("MinigameSettings registered successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to load MinigameSettings");
|
||||
}
|
||||
|
||||
// Log success
|
||||
_settingsLoaded = playerSettings != null && interactionSettings != null && minigameSettings != null;
|
||||
if (_settingsLoaded)
|
||||
{
|
||||
Logging.Debug("All settings loaded and registered with ServiceLocator");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("Some settings failed to load - check that all settings assets exist and are marked as Addressables");
|
||||
}
|
||||
}
|
||||
|
||||
// Log success
|
||||
_settingsLoaded = playerSettings != null && interactionSettings != null && minigameSettings != null;
|
||||
if (_settingsLoaded)
|
||||
/// <summary>
|
||||
/// Check for and initialize developer settings.
|
||||
/// </summary>
|
||||
private void InitializeDeveloperSettings()
|
||||
{
|
||||
Logging.Debug("All settings loaded and registered with ServiceLocator");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("Some settings failed to load - check that all settings assets exist and are marked as Addressables");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check for and initialize developer settings.
|
||||
/// </summary>
|
||||
private void InitializeDeveloperSettings()
|
||||
{
|
||||
Logging.Debug("Starting developer settings initialization...");
|
||||
Logging.Debug("Starting developer settings initialization...");
|
||||
|
||||
// Load developer settings
|
||||
var divingDevSettings = DeveloperSettingsProvider.Instance.GetSettings<DivingDeveloperSettings>();
|
||||
// Load developer settings
|
||||
var divingDevSettings = DeveloperSettingsProvider.Instance.GetSettings<DivingDeveloperSettings>();
|
||||
|
||||
_developerSettingsLoaded = divingDevSettings != null;
|
||||
_developerSettingsLoaded = divingDevSettings != null;
|
||||
|
||||
if (_developerSettingsLoaded)
|
||||
{
|
||||
Logging.Debug("All developer settings loaded successfully");
|
||||
if (_developerSettingsLoaded)
|
||||
{
|
||||
Logging.Debug("All developer settings loaded successfully");
|
||||
}
|
||||
else
|
||||
{
|
||||
Logging.Warning("Some developer settings failed to load");
|
||||
}
|
||||
}
|
||||
else
|
||||
|
||||
|
||||
// Helper method to get settings
|
||||
private T GetSettings<T>() where T : class
|
||||
{
|
||||
Logging.Warning("Some developer settings failed to load");
|
||||
return ServiceLocator.Get<T>();
|
||||
}
|
||||
}
|
||||
|
||||
void OnApplicationQuit()
|
||||
{
|
||||
_isQuitting = true;
|
||||
ServiceLocator.Clear();
|
||||
}
|
||||
|
||||
// Helper method to get settings
|
||||
private T GetSettings<T>() where T : class
|
||||
{
|
||||
return ServiceLocator.Get<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the entire settings object of specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of settings to retrieve</typeparam>
|
||||
/// <returns>The settings object or null if not found</returns>
|
||||
public static T GetSettingsObject<T>() where T : class
|
||||
{
|
||||
return Instance?.GetSettings<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns the entire settings object of specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of settings to retrieve</typeparam>
|
||||
/// <returns>The settings object or null if not found</returns>
|
||||
public static T GetSettingsObject<T>() where T : class
|
||||
{
|
||||
return Instance?.GetSettings<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the developer settings object of specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of developer settings to retrieve</typeparam>
|
||||
/// <returns>The developer settings object or null if not found</returns>
|
||||
public static T GetDeveloperSettings<T>() where T : BaseDeveloperSettings
|
||||
{
|
||||
return DeveloperSettingsProvider.Instance?.GetSettings<T>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Returns the developer settings object of specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of developer settings to retrieve</typeparam>
|
||||
/// <returns>The developer settings object or null if not found</returns>
|
||||
public static T GetDeveloperSettings<T>() where T : BaseDeveloperSettings
|
||||
{
|
||||
return DeveloperSettingsProvider.Instance?.GetSettings<T>();
|
||||
}
|
||||
|
||||
// LEFTOVER LEGACY SETTINGS
|
||||
public float PlayerStopDistance => GetSettings<IInteractionSettings>()?.PlayerStopDistance ?? 6.0f;
|
||||
public float PlayerStopDistanceDirectInteraction => GetSettings<IInteractionSettings>()?.PlayerStopDistanceDirectInteraction ?? 2.0f;
|
||||
public float DefaultPuzzlePromptRange => GetSettings<IInteractionSettings>()?.DefaultPuzzlePromptRange ?? 3.0f;
|
||||
// LEFTOVER LEGACY SETTINGS
|
||||
public float PlayerStopDistance => GetSettings<IInteractionSettings>()?.PlayerStopDistance ?? 6.0f;
|
||||
public float PlayerStopDistanceDirectInteraction => GetSettings<IInteractionSettings>()?.PlayerStopDistanceDirectInteraction ?? 2.0f;
|
||||
public float DefaultPuzzlePromptRange => GetSettings<IInteractionSettings>()?.DefaultPuzzlePromptRange ?? 3.0f;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user