64 lines
1.6 KiB
C#
64 lines
1.6 KiB
C#
using Darkmatter.Core;
|
|
using Darkmatter.Domain;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using VContainer;
|
|
|
|
public class PlatformPool : MonoBehaviour, IPool<Platform>
|
|
{
|
|
[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);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|