- A Settings Provider system to utilize addressables for loading settings at runtime - An editor UI for easy modifications of the settings objects - A split out developer settings functionality to keep gameplay and nitty-gritty details separately - Most settings migrated out of game objects and into the new system - An additional Editor utility for fetching the settings at editor runtime, for gizmos, visualization etc Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com> Co-authored-by: AlexanderT <alexander@foolhardyhorizons.com> Reviewed-on: #7
163 lines
5.8 KiB
C#
163 lines
5.8 KiB
C#
using UnityEngine;
|
|
using Pooling;
|
|
using AppleHills.Core.Settings;
|
|
|
|
namespace Minigames.DivingForPictures
|
|
{
|
|
/// <summary>
|
|
/// Spawns bubbles at intervals, randomizing their properties and assigning a random sprite to each.
|
|
/// </summary>
|
|
public class BubbleSpawner : MonoBehaviour
|
|
{
|
|
public Bubble bubblePrefab;
|
|
public Sprite[] bubbleSprites; // Assign in inspector
|
|
|
|
private DivingDeveloperSettings _devSettings;
|
|
private IDivingMinigameSettings _gameSettings;
|
|
|
|
private float _timer;
|
|
private float _nextSpawnInterval;
|
|
private BubblePool _bubblePool;
|
|
private Camera _mainCamera; // Cache camera reference
|
|
private bool _isSurfacing = false;
|
|
|
|
void Awake()
|
|
{
|
|
_mainCamera = Camera.main;
|
|
|
|
// Get developer settings and game settings
|
|
_devSettings = GameManager.GetDeveloperSettings<DivingDeveloperSettings>();
|
|
_gameSettings = GameManager.GetSettingsObject<IDivingMinigameSettings>();
|
|
|
|
if (_devSettings == null)
|
|
{
|
|
Debug.LogError("[BubbleSpawner] Failed to load developer settings!");
|
|
return;
|
|
}
|
|
|
|
if (_devSettings.BubbleUseObjectPooling)
|
|
{
|
|
// Create the bubble pool
|
|
GameObject poolGO = new GameObject("BubblePool");
|
|
poolGO.transform.SetParent(transform);
|
|
_bubblePool = poolGO.AddComponent<BubblePool>();
|
|
_bubblePool.initialPoolSize = _devSettings.BubbleInitialPoolSize;
|
|
_bubblePool.maxPoolSize = _devSettings.BubbleMaxPoolSize;
|
|
_bubblePool.Initialize(bubblePrefab);
|
|
|
|
// Periodically check for pool statistics in debug builds
|
|
#if DEVELOPMENT_BUILD || UNITY_EDITOR
|
|
InvokeRepeating(nameof(LogPoolStats), 5f, 30f);
|
|
#endif
|
|
}
|
|
}
|
|
|
|
void Start()
|
|
{
|
|
// Initialize the next spawn interval
|
|
_nextSpawnInterval = GetRandomizedInterval();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
_timer += Time.deltaTime;
|
|
if (_timer >= _nextSpawnInterval)
|
|
{
|
|
SpawnBubble();
|
|
_timer = 0f;
|
|
_nextSpawnInterval = GetRandomizedInterval();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns a randomized interval for bubble spawning.
|
|
/// </summary>
|
|
/// <returns>Randomized interval in seconds.</returns>
|
|
float GetRandomizedInterval()
|
|
{
|
|
return _devSettings.BubbleSpawnInterval * Random.Range(0.8f, 1.2f);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spawns a bubble with randomized properties and assigns a random sprite.
|
|
/// </summary>
|
|
void SpawnBubble()
|
|
{
|
|
float x = Random.Range(_devSettings.BubbleSpawnXMin, _devSettings.BubbleSpawnXMax);
|
|
Vector3 spawnPos = new Vector3(x, _devSettings.BubbleSpawnY, 0f);
|
|
|
|
Bubble bubble;
|
|
if (_devSettings.BubbleUseObjectPooling && _bubblePool != null)
|
|
{
|
|
bubble = _bubblePool.GetBubble();
|
|
bubble.transform.position = spawnPos;
|
|
}
|
|
else
|
|
{
|
|
bubble = Instantiate(bubblePrefab, spawnPos, Quaternion.identity, transform);
|
|
}
|
|
|
|
// Randomize bubble properties
|
|
float baseSpeed = Random.Range(_devSettings.BubbleSpeedRange.x, _devSettings.BubbleSpeedRange.y);
|
|
|
|
// Apply surfacing speed reduction if needed
|
|
if (_isSurfacing)
|
|
{
|
|
bubble.speed = baseSpeed * _devSettings.BubbleSurfacingSpeedFactor;
|
|
}
|
|
else
|
|
{
|
|
bubble.speed = baseSpeed;
|
|
}
|
|
|
|
bubble.wobbleSpeed = Random.Range(_devSettings.BubbleWobbleSpeedRange.x, _devSettings.BubbleWobbleSpeedRange.y);
|
|
|
|
// Set base scale (initial size) for the bubble
|
|
float baseScale = Random.Range(_devSettings.BubbleScaleRange.x, _devSettings.BubbleScaleRange.y);
|
|
bubble.SetBaseScale(baseScale);
|
|
|
|
// Assign random sprite to BubbleSprite
|
|
if (bubbleSprites != null && bubbleSprites.Length > 0)
|
|
{
|
|
Sprite randomSprite = bubbleSprites[Random.Range(0, bubbleSprites.Length)];
|
|
bubble.SetBubbleSprite(randomSprite);
|
|
}
|
|
|
|
// Random rotation
|
|
bubble.transform.rotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
|
|
|
|
// Pass min/max scale for wobble clamping
|
|
bubble.SetWobbleScaleLimits(_devSettings.BubbleWobbleMinScale, _devSettings.BubbleWobbleMaxScale);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Start surfacing mode - slow down all bubbles
|
|
/// </summary>
|
|
public void StartSurfacing()
|
|
{
|
|
if (_isSurfacing) return; // Already surfacing
|
|
|
|
_isSurfacing = true;
|
|
|
|
// Slow down all existing bubbles
|
|
Bubble[] activeBubbles = FindObjectsByType<Bubble>(FindObjectsSortMode.None);
|
|
foreach (Bubble bubble in activeBubbles)
|
|
{
|
|
bubble.speed *= _devSettings.BubbleSurfacingSpeedFactor;
|
|
}
|
|
|
|
Debug.Log($"[BubbleSpawner] Started surfacing mode. Bubbles slowed to {_devSettings.BubbleSurfacingSpeedFactor * 100}% speed.");
|
|
}
|
|
|
|
/// <summary>
|
|
/// Logs the current pool statistics for debugging
|
|
/// </summary>
|
|
private void LogPoolStats()
|
|
{
|
|
if (_bubblePool != null)
|
|
{
|
|
_bubblePool.LogPoolStats();
|
|
}
|
|
}
|
|
}
|
|
} |