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,13 +1,15 @@
using UnityEngine;
using System.Collections;
using UnityEngine;
using Pooling;
using AppleHills.Core.Settings;
using AppleHills.Core.Interfaces;
namespace Minigames.DivingForPictures
{
/// <summary>
/// Spawns bubbles at intervals, randomizing their properties and assigning a random sprite to each.
/// </summary>
public class BubbleSpawner : MonoBehaviour
public class BubbleSpawner : MonoBehaviour, IPausable
{
public Bubble bubblePrefab;
public Sprite[] bubbleSprites; // Assign in inspector
@@ -20,6 +22,15 @@ namespace Minigames.DivingForPictures
private BubblePool _bubblePool;
private Camera _mainCamera; // Cache camera reference
private bool _isSurfacing = false;
// Pause state
private bool _isPaused = false;
// Coroutines for pause/resume
private Coroutine _spawnCoroutine;
// IPausable implementation
public bool IsPaused => _isPaused;
void Awake()
{
@@ -50,23 +61,110 @@ namespace Minigames.DivingForPictures
InvokeRepeating(nameof(LogPoolStats), 5f, 30f);
#endif
}
SetNextSpawnInterval();
}
void Start()
{
// Initialize the next spawn interval
_nextSpawnInterval = GetRandomizedInterval();
// Register with DivingGameManager for pause/resume events
DivingGameManager gameManager = FindFirstObjectByType<DivingGameManager>();
if (gameManager != null)
{
gameManager.RegisterPausableComponent(this);
}
// Start spawning if not paused
StartSpawningCoroutine();
}
void OnDestroy()
{
// Unregister from DivingGameManager
DivingGameManager gameManager = FindFirstObjectByType<DivingGameManager>();
if (gameManager != null)
{
gameManager.UnregisterPausableComponent(this);
}
// Clean up any active coroutines
StopAllCoroutines();
}
/// <summary>
/// Pauses the bubble spawner and all bubbles
/// </summary>
public void Pause()
{
if (_isPaused) return; // Already paused
_isPaused = true;
// Stop spawning coroutine
if (_spawnCoroutine != null)
{
StopCoroutine(_spawnCoroutine);
_spawnCoroutine = null;
}
// Pause all active bubbles
Bubble[] activeBubbles = FindObjectsOfType<Bubble>();
foreach (var bubble in activeBubbles)
{
if (bubble != null)
{
bubble.Pause();
}
}
Debug.Log("[BubbleSpawner] Paused");
}
/// <summary>
/// Resumes the bubble spawner and all bubbles
/// </summary>
public void Resume()
{
if (!_isPaused) return; // Already running
_isPaused = false;
// Restart spawning coroutine
StartSpawningCoroutine();
// Resume all active bubbles
Bubble[] activeBubbles = FindObjectsOfType<Bubble>();
foreach (var bubble in activeBubbles)
{
if (bubble != null)
{
bubble.Resume();
}
}
Debug.Log("[BubbleSpawner] Resumed");
}
/// <summary>
/// Starts the bubble spawning coroutine if it's not already running
/// </summary>
private void StartSpawningCoroutine()
{
if (_spawnCoroutine == null && !_isPaused)
{
_spawnCoroutine = StartCoroutine(SpawnBubblesRoutine());
}
}
void Update()
/// <summary>
/// Sets the next spawn interval using randomized timing
/// </summary>
private void SetNextSpawnInterval()
{
_timer += Time.deltaTime;
if (_timer >= _nextSpawnInterval)
{
SpawnBubble();
_timer = 0f;
_nextSpawnInterval = GetRandomizedInterval();
}
if (_devSettings == null) return;
_nextSpawnInterval = GetRandomizedInterval();
Debug.Log($"[BubbleSpawner] Next spawn interval set to: {_nextSpawnInterval:F2}s");
}
/// <summary>
@@ -128,6 +226,12 @@ namespace Minigames.DivingForPictures
// Pass min/max scale for wobble clamping
bubble.SetWobbleScaleLimits(_devSettings.BubbleWobbleMinScale, _devSettings.BubbleWobbleMaxScale);
// If the game is already paused, pause this bubble immediately
if (_isPaused)
{
bubble.Pause();
}
}
/// <summary>
@@ -159,5 +263,24 @@ namespace Minigames.DivingForPictures
_bubblePool.LogPoolStats();
}
}
/// <summary>
/// Coroutine that handles the bubble spawning logic
/// </summary>
private IEnumerator SpawnBubblesRoutine()
{
Debug.Log("[BubbleSpawner] Started bubble spawning coroutine");
while (enabled && gameObject.activeInHierarchy && !_isPaused)
{
SpawnBubble();
SetNextSpawnInterval(); // Set interval for next spawn
yield return new WaitForSeconds(_nextSpawnInterval);
}
_spawnCoroutine = null;
Debug.Log("[BubbleSpawner] Bubble spawning coroutine ended");
}
}
}