728x90
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
public struct BuffData
{
public Soul owner;
public float lifeTime;
public Action onEnd;
public BuffData(Soul owner, float lifeTime, Action onEnd)
{
this.owner = owner;
this.lifeTime = lifeTime;
this.onEnd = onEnd;
}
}
public class Buff : MonoBehaviour
{
private Dictionary<int, BuffData> m_Dic_Buff = new Dictionary<int, BuffData>();
private Dictionary<int, GameObject> m_Dic_Effect = new Dictionary<int, GameObject>();
public void OnUpdate(float deltaTime)
{
foreach (var key in m_Dic_Buff.Keys.ToList())
{
var data = m_Dic_Buff[key];
if (!data.owner.IsLive())
data.lifeTime = 0.0f;
data.lifeTime -= deltaTime;
if (data.lifeTime <= 0)
Del(key);
else
m_Dic_Buff[key] = data;
}
}
public void Add(Soul owner, int index, float lifeTime, Action startAction, Action endAction)
{
if (m_Dic_Buff.ContainsKey(index))
m_Dic_Buff[index] = new BuffData(owner, lifeTime, endAction);
else
{
AttachEffect(index);
m_Dic_Buff.Add(index, new BuffData(owner, lifeTime, endAction));
startAction();
}
}
public void Del(int index)
{
if (!m_Dic_Buff.ContainsKey(index)) return;
if (m_Dic_Effect.ContainsKey(index))
{
if (m_Dic_Effect[index] != null)
Destroy(m_Dic_Effect[index]);
m_Dic_Effect.Remove(index);
}
var action = m_Dic_Buff[index].onEnd;
m_Dic_Buff.Remove(index);
action?.Invoke();
}
public void DelAll()
{
foreach (var key in m_Dic_Buff.Keys.ToList())
Del(key);
}
public List<int> GetBuffAll()
{
return m_Dic_Buff.Keys.ToList();
}
private void AttachEffect(int index)
{
var buffInfo = DataTableMgr.instance.GetData<BuffInfo>(eDataTableType.BUFF_INFO, index);
if (buffInfo.m_EffectName.ToLower() == "none")
return;
Addressables.LoadAssetAsync<GameObject>(buffInfo.m_EffectName).Completed
+= delegate(AsyncOperationHandle<GameObject> handle)
{
if (handle.IsDone)
{
if (!m_Dic_Effect.ContainsKey(index))
m_Dic_Effect.Add(index, Instantiate(handle.Result, gameObject.transform.position,
gameObject.transform.rotation,
gameObject.transform));
}
};
}
}
간-_-단
728x90
'프로그래밍 > C#' 카테고리의 다른 글
구글 RealTimeDataBase GetValue 개수와 크기 (0) | 2023.02.24 |
---|---|
C# Generic return T (0) | 2022.10.02 |
ObscuredString / String 을 숫자로 변환 했을 때의 성능 (0) | 2022.03.04 |
제트브레인 라이더(Rider) 코파일럿(Copilot) 사용법 (0) | 2022.02.21 |
C# 가상함수(Virtual)와 오버라이드(override) (0) | 2022.01.14 |
댓글