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

92 lines
2.4 KiB
C#
Raw Normal View History

2026-01-12 03:23:48 +00:00
using System.Collections.Generic;
using UnityEngine;
2026-01-11 23:53:38 +00:00
public class ProjectileMgr : MonoBehaviourSingletonTemplate<ProjectileMgr>
{
2026-01-12 03:23:48 +00:00
[Header("Pool Setting")]
public Transform tf_parent;
2026-01-13 00:46:52 +00:00
public int defaultPoolSize = 10;
2026-01-12 03:23:48 +00:00
2026-01-13 00:46:52 +00:00
// prefabPath -> pool
Dictionary<string, Queue<Projectile>> pools = new Dictionary<string, Queue<Projectile>>();
2026-01-12 03:23:48 +00:00
2026-01-13 00:46:52 +00:00
public void InitPools()
2026-01-12 03:23:48 +00:00
{
2026-01-13 00:46:52 +00:00
var lst = table_projectile.Ins.Get_DataList();
2026-01-12 03:23:48 +00:00
2026-01-13 00:46:52 +00:00
for (int i = 0; i < lst.Count; i++)
2026-01-12 03:23:48 +00:00
{
2026-01-13 00:46:52 +00:00
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);
}
2026-01-12 03:23:48 +00:00
}
}
2026-01-14 00:22:53 +00:00
public void AllOff()
{
// 부모 아래에 있는 모든 Projectile 검색
var projectiles = tf_parent.GetComponentsInChildren<Projectile>(true);
for (int i = 0; i < projectiles.Length; i++)
{
Projectile p = projectiles[i];
// 이미 비활성화된 건 스킵
if (!p.gameObject.activeSelf)
continue;
Return(p);
}
}
public void Shoot_Projectile(ProjectileData pd)
{
var proj = Get(pd.m_Data.s_ProjectilePrefabs);
proj.Set(pd);
}
2026-01-13 00:46:52 +00:00
public Projectile Get(string prefabPath)
2026-01-12 03:23:48 +00:00
{
2026-01-13 00:46:52 +00:00
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);
2026-01-12 03:23:48 +00:00
p.gameObject.SetActive(true);
return p;
}
public void Return(Projectile p)
{
p.gameObject.SetActive(false);
2026-01-13 00:46:52 +00:00
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;
}
2026-01-12 03:23:48 +00:00
pool.Enqueue(p);
}
2026-01-11 23:53:38 +00:00
2026-01-13 00:46:52 +00:00
Projectile CreateExtra(string prefabPath)
2026-01-12 03:23:48 +00:00
{
2026-01-13 00:46:52 +00:00
Projectile p = DSUtil.Get_Clone<Projectile>(prefabPath, tf_parent);
p.SetOwner(this, prefabPath);
2026-01-12 03:23:48 +00:00
return p;
}
2026-01-11 23:53:38 +00:00
}