Add a pausable interface and make minigameobjecs pausable
This commit is contained in:
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
3
Assets/Scripts/Core/Interfaces.meta
Normal file
3
Assets/Scripts/Core/Interfaces.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ba87fe038e914973bc341817149a9ebe
|
||||
timeCreated: 1759916676
|
||||
25
Assets/Scripts/Core/Interfaces/IPausable.cs
Normal file
25
Assets/Scripts/Core/Interfaces/IPausable.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using UnityEngine;
|
||||
|
||||
namespace AppleHills.Core.Interfaces
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface for components that can be paused and resumed
|
||||
/// </summary>
|
||||
public interface IPausable
|
||||
{
|
||||
/// <summary>
|
||||
/// Pauses the component's functionality
|
||||
/// </summary>
|
||||
void Pause();
|
||||
|
||||
/// <summary>
|
||||
/// Resumes the component's functionality
|
||||
/// </summary>
|
||||
void Resume();
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the component is currently paused
|
||||
/// </summary>
|
||||
bool IsPaused { get; }
|
||||
}
|
||||
}
|
||||
3
Assets/Scripts/Core/Interfaces/IPausable.cs.meta
Normal file
3
Assets/Scripts/Core/Interfaces/IPausable.cs.meta
Normal file
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3a493ef684ce47208a3f6e7d67727882
|
||||
timeCreated: 1759916676
|
||||
Reference in New Issue
Block a user