Add a pausable interface and make minigameobjecs pausable

This commit is contained in:
Michal Pikulski
2025-10-08 12:36:08 +02:00
parent 3807ac652c
commit 0e17516df7
11 changed files with 1101 additions and 395 deletions

View File

@@ -1,6 +1,9 @@
using UnityEngine;
using AppleHills.Core.Settings;
using System;
using System.Collections.Generic;
using AppleHills.Core.Interfaces;
using UI;
/// <summary>
/// Singleton manager for global game state and settings. Provides accessors for various gameplay parameters.
@@ -36,6 +39,21 @@ public class GameManager : MonoBehaviour
[SerializeField] private bool _developerSettingsLoaded = false;
public bool SettingsLoaded => _settingsLoaded;
public bool DeveloperSettingsLoaded => _developerSettingsLoaded;
[Header("Game State")]
[SerializeField] private bool _isPaused = false;
/// <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;
void Awake()
{
@@ -53,6 +71,136 @@ public class GameManager : MonoBehaviour
// DontDestroyOnLoad(gameObject);
}
private void Start()
{
// Find and subscribe to PauseMenu events
PauseMenu pauseMenu = PauseMenu.Instance;
if (pauseMenu != null)
{
pauseMenu.OnGamePaused += OnPauseMenuPaused;
pauseMenu.OnGameResumed += OnPauseMenuResumed;
Debug.Log("[GameManager] Subscribed to PauseMenu events");
}
else
{
Debug.LogWarning("[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))
{
_pausableComponents.Add(component);
// If the game is already paused, pause the component immediately
if (_isPaused)
{
component.Pause();
}
Debug.Log($"[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);
Debug.Log($"[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();
Debug.Log($"[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.Resume();
}
// Broadcast resume event
OnGameResumed?.Invoke();
Debug.Log($"[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()
{
@@ -164,5 +312,4 @@ public class GameManager : MonoBehaviour
public float PlayerStopDistance => GetSettings<IInteractionSettings>()?.PlayerStopDistance ?? 6.0f;
public float PlayerStopDistanceDirectInteraction => GetSettings<IInteractionSettings>()?.PlayerStopDistanceDirectInteraction ?? 2.0f;
public float DefaultPuzzlePromptRange => GetSettings<IInteractionSettings>()?.DefaultPuzzlePromptRange ?? 3.0f;
}