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

@@ -6,10 +6,12 @@ using UnityEngine.Events;
using UnityEngine.Playables;
using AppleHills.Core.Settings;
using Utility;
using AppleHills.Core.Interfaces;
using UI;
namespace Minigames.DivingForPictures
{
public class DivingGameManager : MonoBehaviour
public class DivingGameManager : MonoBehaviour, IPausable
{
[Header("Monster Prefabs")]
[Tooltip("Array of monster prefabs to spawn randomly")]
@@ -66,6 +68,15 @@ namespace Minigames.DivingForPictures
// Event for game components to subscribe to
public event Action OnGameInitialized;
// Pause state
private bool _isPaused = false;
// List of pausable components controlled by this manager
private List<IPausable> _pausableComponents = new List<IPausable>();
// IPausable implementation
public bool IsPaused => _isPaused;
private void Awake()
{
// Get settings from GameManager
@@ -81,6 +92,26 @@ namespace Minigames.DivingForPictures
private void Start()
{
// Find PauseMenu and subscribe to its events
PauseMenu pauseMenu = PauseMenu.Instance;
if (pauseMenu != null)
{
pauseMenu.OnGamePaused += Pause;
pauseMenu.OnGameResumed += Resume;
Debug.Log("[DivingGameManager] Subscribed to PauseMenu events");
}
else
{
Debug.LogWarning("[DivingGameManager] PauseMenu not found. Pause functionality won't work properly.");
}
// Register this manager with the global GameManager
if (GameManager.Instance != null)
{
GameManager.Instance.RegisterPausableComponent(this);
}
// Subscribe to SceneOrientationEnforcer's event
if (SceneOrientationEnforcer.Instance != null)
{
@@ -144,6 +175,23 @@ namespace Minigames.DivingForPictures
{
SceneOrientationEnforcer.Instance.OnOrientationCorrect -= InitializeGame;
}
// Unsubscribe from PauseMenu events
PauseMenu pauseMenu = PauseMenu.Instance;
if (pauseMenu != null)
{
pauseMenu.OnGamePaused -= Pause;
pauseMenu.OnGameResumed -= Resume;
}
// Unregister from GameManager
if (GameManager.Instance != null)
{
GameManager.Instance.UnregisterPausableComponent(this);
}
// Unregister all pausable components
_pausableComponents.Clear();
}
private void Update()
@@ -651,5 +699,74 @@ namespace Minigames.DivingForPictures
// Final assignment to ensure exact target value
OnVelocityFactorChanged?.Invoke(_currentVelocityFactor);
}
/// <summary>
/// Register a component as pausable with this manager
/// </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($"[DivingGameManager] 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($"[DivingGameManager] Unregistered pausable component: {(component as MonoBehaviour)?.name ?? "Unknown"}");
}
}
/// <summary>
/// Pause the game and all registered components
/// </summary>
public void Pause()
{
if (_isPaused) return; // Already paused
_isPaused = true;
// Pause all registered components
foreach (var component in _pausableComponents)
{
component.Pause();
}
Debug.Log($"[DivingGameManager] Game paused. Paused {_pausableComponents.Count} components.");
}
/// <summary>
/// Resume the game and all registered components
/// </summary>
public void Resume()
{
if (!_isPaused) return; // Already running
_isPaused = false;
// Resume all registered components
foreach (var component in _pausableComponents)
{
component.Resume();
}
Debug.Log($"[DivingGameManager] Game resumed. Resumed {_pausableComponents.Count} components.");
}
}
}