Project_WL/Assets/Script/Info/ProjectileInfo.cs

124 lines
3.7 KiB
C#
Raw Normal View History

2024-12-15 01:39:39 +00:00
using System;
2024-12-27 23:19:08 +00:00
using System.Collections;
2024-12-15 01:39:39 +00:00
using System.Collections.Generic;
using UnityEngine;
public class ProjectileInfo : MonoBehaviour
{
public static bool isIns = false;
2024-12-15 01:39:39 +00:00
public static ProjectileInfo Ins;
public Material[] arr_materials; // 0 얼음, 1 히트
Dictionary<string, List<ProjectileBase>> dic_projectile = new Dictionary<string, List<ProjectileBase>>();
2025-02-09 06:50:27 +00:00
// ProjectileData Pool
private Stack<ProjectileData> projectileDataPool = new Stack<ProjectileData>();
private void Awake()
{
Ins = this;
isIns = true;
}
public void AllOff()
{
foreach (var projectileList in dic_projectile.Values)
foreach (var projectile in projectileList)
projectile.gameObject.SetActive(false);
2025-02-09 06:50:27 +00:00
}
2024-12-27 23:19:08 +00:00
IEnumerator Start()
2024-12-15 01:39:39 +00:00
{
2025-02-09 06:50:27 +00:00
while (!TableChecker.Ins.CheckAllLoad())
yield return new WaitForSeconds(0.1f);
2024-12-27 23:19:08 +00:00
2024-12-15 01:39:39 +00:00
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);
}
2025-02-09 06:50:27 +00:00
// 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);
}
2025-02-20 00:40:21 +00:00
public void Shoot_Projectile(string projectile, Actor shooter, Actor target, float adddmgrate = 1f, Vector3? startpos = null, float liftTime = 4f, Action<ProjectileBase> act = null)
2024-12-15 01:39:39 +00:00
{
Get_Projectile(projectile, proj =>
{
2025-02-09 06:50:27 +00:00
var pd = GetProjectileData(); // 풀에서 가져오기
pd.shooter = shooter;
pd.target = target;
2025-02-19 04:14:09 +00:00
pd.AddDmgPercent = adddmgrate;
2025-02-09 06:50:27 +00:00
pd.LifeTime = liftTime;
if (startpos.HasValue)
{
pd.eStartPos = eProjectileStartPos.Custom;
pd.v3_StartPos = startpos.Value;
pd.LookTarget = true;
}
2024-12-15 01:39:39 +00:00
else
2025-02-09 06:50:27 +00:00
{
pd.eStartPos = eProjectileStartPos.ShooterCenter;
}
proj.Set(pd);
2025-01-20 06:47:44 +00:00
act?.Invoke(proj);
2025-02-09 06:50:27 +00:00
ReturnProjectileData(pd); // 사용 후 풀에 반환
2024-12-15 01:39:39 +00:00
});
}
2025-02-09 06:50:27 +00:00
2024-12-15 01:39:39 +00:00
public void Shoot_Projectile(string projectile, ProjectileData pd, Action<ProjectileBase> act = null)
{
Get_Projectile(projectile, proj =>
{
2025-02-09 06:50:27 +00:00
proj.Set(pd);
act?.Invoke(proj);
2024-12-15 01:39:39 +00:00
});
}
}