using UnityEngine;
using Pooling;
namespace Minigames.DivingForPictures
{
///
/// Manages a pool of floating obstacle objects to reduce garbage collection overhead.
/// Optimized for handling a large number of different obstacle prefab types.
///
public class ObstaclePool : MultiPrefabPool
{
///
/// Returns an obstacle to the pool
///
/// The obstacle to return to the pool
/// The index of the prefab this obstacle was created from
public void ReturnObstacle(GameObject obstacle, int prefabIndex)
{
if (obstacle != null)
{
FloatingObstacle obstacleComponent = obstacle.GetComponent();
if (obstacleComponent != null)
{
Debug.Log($"[ObstaclePool] Returning obstacle {obstacle.name} to pool");
Return(obstacleComponent, prefabIndex);
}
else
{
Debug.LogWarning($"Attempted to return a GameObject without a FloatingObstacle component: {obstacle.name}");
Destroy(obstacle);
}
}
}
///
/// Gets an obstacle from the pool, or creates a new one if the pool is empty
///
/// An obstacle instance ready to use
public GameObject GetObstacle(int prefabIndex)
{
Debug.Log($"[ObstaclePool] GetObstacle called for prefab index {prefabIndex}");
FloatingObstacle obstacleComponent = Get(prefabIndex);
if (obstacleComponent == null)
{
Debug.LogError($"[ObstaclePool] Get() returned null for prefab index {prefabIndex}");
return null;
}
Debug.Log($"[ObstaclePool] Get() returned obstacle {obstacleComponent.name}, active state: {obstacleComponent.gameObject.activeInHierarchy}");
return obstacleComponent.gameObject;
}
}
}