Files
HelixJump/Assets/Scripts/PlatformScripts/PlatformPool.cs
2025-12-14 18:16:49 +05:45

67 lines
1.7 KiB
C#

using System.Collections.Generic;
using UnityEngine;
using VContainer;
using VContainer.Unity;
public class PlatformPool : MonoBehaviour,IPool
{
[SerializeField] private Platform platformPrefab;
[SerializeField] private Transform platformParent;
[SerializeField] private int poolSize = 10;
private Queue<Platform> _queue = new Queue<Platform>();
public IReadOnlyCollection<Platform> All => _queue;
[Inject] IObjectResolver resolver;
private void Awake()
{
SetupPool();
}
void SetupPool()
{
Platform instance = null;
for(int i = 0; i < poolSize; i++)
{
instance = Instantiate(platformPrefab,platformParent);
resolver.Inject(instance);
if (i == 0) instance.SetPlatformRule(new FirstPlatform());
else instance.SetPlatformRule(new OtherPlatform());
instance.gameObject.SetActive(false);
_queue.Enqueue(instance);
}
}
public Platform GetFromPool()
{
if(_queue.Count == 0)
{
Platform newObj = Instantiate(platformPrefab,platformParent);
newObj.SetPlatformRule(new OtherPlatform());
_queue.Enqueue(newObj);
return newObj;
}
Platform pooledObj = _queue.Dequeue();
pooledObj.SetPlatformRule(new OtherPlatform());
pooledObj.gameObject.SetActive(true);
return pooledObj;
}
public void ReturnToPool(Platform obj)
{
obj.gameObject.SetActive(false);
_queue.Enqueue(obj);
}
}
public interface IPool
{
IReadOnlyCollection<Platform> All { get; }
void ReturnToPool(Platform obj);
Platform GetFromPool();
}