Working single-purpose object pooling solution

This commit is contained in:
Michal Pikulski
2025-09-16 12:23:25 +02:00
parent 2c56d0e1de
commit bcc6f05058
19 changed files with 990 additions and 115 deletions

View File

@@ -10,39 +10,83 @@ namespace Minigames.DivingForPictures
public float speed = 1f;
public float wobbleSpeed = 1f;
private SpriteRenderer spriteRenderer;
private SpriteRenderer bottleSpriteRenderer;
private SpriteRenderer bubbleSpriteRenderer; // Renamed from bottleSpriteRenderer
private float timeOffset;
private float minScale = 0.2f;
private float maxScale = 1.2f;
private float baseScale = 1f; // Added to store the initial scale
private Camera mainCamera; // Cache camera reference
private BubblePool parentPool; // Reference to the pool this bubble came from
void Awake()
{
// Cache references and randomize time offset for wobble
spriteRenderer = GetComponent<SpriteRenderer>();
timeOffset = Random.value * 100f;
// Find the child named "BottleSprite" and get its SpriteRenderer
Transform bottleSpriteTransform = transform.Find("BubbleSprite");
if (bottleSpriteTransform != null)
// Find the child named "BubbleSprite" and get its SpriteRenderer
Transform bubbleSpriteTransform = transform.Find("BubbleSprite");
if (bubbleSpriteTransform != null)
{
bottleSpriteRenderer = bottleSpriteTransform.GetComponent<SpriteRenderer>();
bubbleSpriteRenderer = bubbleSpriteTransform.GetComponent<SpriteRenderer>();
}
// Cache camera reference
mainCamera = Camera.main;
}
void Update()
{
// Move bubble upward
transform.position += Vector3.up * speed * Time.deltaTime;
transform.position += Vector3.up * (speed * Time.deltaTime);
// Wobble effect (smooth oscillation between min and max scale)
float t = (Mathf.Sin((Time.time + timeOffset) * wobbleSpeed) + 1f) * 0.5f; // t in [0,1]
float newScale = Mathf.Lerp(minScale, maxScale, t);
transform.localScale = Vector3.one * newScale;
// Destroy when off screen
if (transform.position.y > Camera.main.orthographicSize + 2f)
float wobbleFactor = Mathf.Lerp(minScale, maxScale, t);
transform.localScale = Vector3.one * (baseScale * wobbleFactor);
// Destroy when off screen - using cached camera reference
if (mainCamera != null && transform.position.y > mainCamera.orthographicSize + 2f)
{
Destroy(gameObject);
OnBubbleDestroy();
}
}
/// <summary>
/// Called when bubble is about to be destroyed
/// </summary>
private void OnBubbleDestroy()
{
// Use the cached pool reference instead of finding it each time
if (parentPool != null)
{
parentPool.ReturnBubble(this);
}
else
{
// Fallback to find the pool if the reference is somehow lost
BubblePool pool = FindFirstObjectByType<BubblePool>();
if (pool != null)
{
Debug.LogWarning("Bubble is missing its parent pool reference, finding pool as fallback");
pool.ReturnBubble(this);
}
else
{
Destroy(gameObject);
}
}
}
/// <summary>
/// Sets the parent pool for this bubble
/// </summary>
/// <param name="pool">The bubble pool that created this bubble</param>
public void SetPool(BubblePool pool)
{
parentPool = pool;
}
/// <summary>
/// Sets the main sprite for the bubble.
/// </summary>
@@ -54,13 +98,22 @@ namespace Minigames.DivingForPictures
}
/// <summary>
/// Sets the sprite for the child "BottleSprite" renderer.
/// Sets the sprite for the child "BubbleSprite" renderer.
/// </summary>
/// <param name="sprite">Sprite to assign.</param>
public void SetBottleSprite(Sprite sprite)
public void SetBubbleSprite(Sprite sprite)
{
if (bottleSpriteRenderer != null)
bottleSpriteRenderer.sprite = sprite;
if (bubbleSpriteRenderer != null)
bubbleSpriteRenderer.sprite = sprite;
}
/// <summary>
/// Sets the base scale for the bubble
/// </summary>
/// <param name="scale">Base scale value</param>
public void SetBaseScale(float scale)
{
baseScale = scale;
}
/// <summary>
@@ -73,5 +126,13 @@ namespace Minigames.DivingForPictures
minScale = min;
maxScale = max;
}
/// <summary>
/// Resets the bubble state for reuse from object pool
/// </summary>
public void ResetState()
{
timeOffset = Random.value * 100f;
}
}
}