OneShotOneKill/Assets/Script/InGame/Projectile/ProjectileMgr.cs

75 lines
2.0 KiB
C#

using System.Collections.Generic;
using UnityEngine;
public class ProjectileMgr : MonoBehaviourSingletonTemplate<ProjectileMgr>
{
[Header("Pool Setting")]
public Transform tf_parent;
public int defaultPoolSize = 10;
// prefabPath -> pool
Dictionary<string, Queue<Projectile>> pools = new Dictionary<string, Queue<Projectile>>();
public void InitPools()
{
var lst = table_projectile.Ins.Get_DataList();
for (int i = 0; i < lst.Count; i++)
{
string prefabPath = lst[i].s_ProjectilePrefabs;
if (pools.ContainsKey(prefabPath))
continue;
Queue<Projectile> q = new Queue<Projectile>();
pools.Add(prefabPath, q);
for (int j = 0; j < defaultPoolSize; j++)
{
Projectile p = CreateExtra(prefabPath);
p.gameObject.SetActive(false);
q.Enqueue(p);
}
}
}
public void Shoot_Projectile(ProjectileData pd)
{
var proj = Get(pd.m_Data.s_ProjectilePrefabs);
proj.Set(pd);
}
public Projectile Get(string prefabPath)
{
if (!pools.TryGetValue(prefabPath, out Queue<Projectile> pool))
{
Debug.LogError($"[ProjectileMgr] Pool not found : {prefabPath}");
return null;
}
Projectile p = pool.Count > 0 ? pool.Dequeue() : CreateExtra(prefabPath);
p.gameObject.SetActive(true);
return p;
}
public void Return(Projectile p)
{
p.gameObject.SetActive(false);
string key = p.Get_ProjectileTData().s_ProjectilePrefabs;
if (!pools.TryGetValue(key, out Queue<Projectile> pool))
{
Debug.LogError($"[ProjectileMgr] Return failed. Pool not found : {key}");
return;
}
pool.Enqueue(p);
}
Projectile CreateExtra(string prefabPath)
{
Projectile p = DSUtil.Get_Clone<Projectile>(prefabPath, tf_parent);
p.SetOwner(this, prefabPath);
return p;
}
}