Project_WL/Assets/Script/Info/ProjectileInfo.cs

117 lines
3.5 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ProjectileInfo : MonoBehaviour
{
public static bool ProjectileInfoOn = false;
public static ProjectileInfo Ins;
public Material[] arr_materials; // 0 얼음, 1 히트
Dictionary<string, List<ProjectileBase>> dic_projectile = new Dictionary<string, List<ProjectileBase>>();
// ProjectileData Pool
private Stack<ProjectileData> projectileDataPool = new Stack<ProjectileData>();
private void Awake()
{
Ins = this;
ProjectileInfoOn = true;
}
IEnumerator Start()
{
while (!TableChecker.Ins.CheckAllLoad())
yield return new WaitForSeconds(0.1f);
var lst = table_effectlist.Ins.Get_DataList(2);
for (int i = 0; i < lst.Count; i++)
{
dic_projectile.Add(lst[i].s_EffectPrefab, new List<ProjectileBase>());
for (int j = 0; j < lst[i].n_PreLoadCount; j++)
Load_Projectile(true, lst[i].s_EffectPrefab, null);
}
}
void Load_Projectile(bool loadFirst, string projectile, Action<ProjectileBase> act)
{
AddrResourceMgr.Ins.LoadObject<GameObject>($"Assets/Res_Addr/Projectile/{projectile}.prefab", handle =>
{
var temp = DSUtil.Get_Clone<ProjectileBase>(handle.Result, transform);
if (!loadFirst) temp.Off_FisrLoad();
dic_projectile[projectile].Add(temp);
act?.Invoke(temp);
});
}
void Get_Projectile(string projectile, Action<ProjectileBase> act)
{
if (!dic_projectile.ContainsKey(projectile))
dic_projectile.Add(projectile, new List<ProjectileBase>());
for (int i = 0; i < dic_projectile[projectile].Count; i++)
{
if (!dic_projectile[projectile][i].gameObject.activeInHierarchy)
{
act?.Invoke(dic_projectile[projectile][i]);
return;
}
}
Load_Projectile(true, projectile, act);
}
// Pool에서 ProjectileData 가져오기
private ProjectileData GetProjectileData()
{
return projectileDataPool.Count > 0 ? projectileDataPool.Pop() : new ProjectileData();
}
// Pool에 ProjectileData 반환하기
private void ReturnProjectileData(ProjectileData pd)
{
pd.Reset(); // 데이터 초기화
projectileDataPool.Push(pd);
}
public void Shoot_Projectile(string projectile, Actor shooter, Actor target, float adddmgrate = 1f, Vector3? startpos = null, float liftTime = 4f, float skilldmg = 1f, Action<ProjectileBase> act = null)
{
Get_Projectile(projectile, proj =>
{
var pd = GetProjectileData(); // 풀에서 가져오기
pd.shooter = shooter;
pd.target = target;
pd.AddDmgPercent = adddmgrate;
pd.LifeTime = liftTime;
if (startpos.HasValue)
{
pd.eStartPos = eProjectileStartPos.Custom;
pd.v3_StartPos = startpos.Value;
pd.LookTarget = true;
}
else
{
pd.eStartPos = eProjectileStartPos.ShooterCenter;
}
proj.Set(pd);
act?.Invoke(proj);
ReturnProjectileData(pd); // 사용 후 풀에 반환
});
}
public void Shoot_Projectile(string projectile, ProjectileData pd, Action<ProjectileBase> act = null)
{
Get_Projectile(projectile, proj =>
{
proj.Set(pd);
act?.Invoke(proj);
});
}
}