NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Effect/EffectPlayingData.cs

211 lines
5.1 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
using Cysharp.Threading.Tasks;
2023-08-22 19:08:45 +08:00
using Gameplay.Level;
2023-10-19 15:00:19 +08:00
using Gameplay.Pause;
using Gameplay.Pool;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
2023-08-22 19:08:45 +08:00
namespace Gameplay.Effect
{
using Framework;
using UnityEngine;
2023-12-28 19:01:07 +08:00
using Skill;
2023-08-22 19:08:45 +08:00
public class EffectPlayingData
{
2023-10-19 15:00:19 +08:00
/// <summary>
/// pauser信息直接放这里
/// </summary>
public DataEffectPauser pauserData;
2023-08-22 19:08:45 +08:00
public GameUnit owner;
public GameObject rootObj;
2024-01-02 15:11:37 +08:00
public Action onLoaded;
2023-08-22 19:08:45 +08:00
2023-12-29 19:40:20 +08:00
private TrailRenderer[] _trails;
2023-08-22 19:08:45 +08:00
public Transform transform
{
get
{
2023-10-19 15:00:19 +08:00
if (rootObj == null)
{
return null;
}
2023-08-22 19:08:45 +08:00
return rootObj.transform;
}
}
2023-12-28 19:01:07 +08:00
public float remainTime { get; private set; }
2023-08-22 19:08:45 +08:00
2023-12-28 19:01:07 +08:00
public bool autoDestroy;
2023-08-22 19:08:45 +08:00
2023-12-28 19:01:07 +08:00
public bool isLoaded { get; private set; }
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
public bool needReturn
2023-08-22 19:08:45 +08:00
{
get
{
2023-12-28 19:01:07 +08:00
return (autoDestroy && remainTime <= 0) || _forceDestroy;
2023-08-22 19:08:45 +08:00
}
}
private BaseEffectLogic _logic;
2023-12-28 19:01:07 +08:00
private bool _forceDestroy;
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
public readonly int effectId;
2024-01-02 15:11:37 +08:00
public bool isReturned;
2023-10-19 15:00:19 +08:00
public EffectPlayingData(int effectId)
{
this.effectId = effectId;
2023-12-28 19:01:07 +08:00
isLoaded = false;
2023-10-19 15:00:19 +08:00
}
2023-12-28 19:01:07 +08:00
public void Reset()
2023-08-22 19:08:45 +08:00
{
2023-12-28 19:01:07 +08:00
_forceDestroy = false;
2024-01-02 15:11:37 +08:00
autoDestroy = false;
isReturned = false;
2023-12-29 19:40:20 +08:00
if (_trails != null)
{
for (int i = 0; i < _trails.Length; i++)
{
var trail = _trails[i];
2024-01-02 15:11:37 +08:00
trail.enabled = true;
2023-12-29 19:40:20 +08:00
}
}
2023-12-28 19:01:07 +08:00
}
2023-10-19 15:00:19 +08:00
2023-12-28 19:01:07 +08:00
public async void Load(string prefabPath)
{
if (isLoaded) return;
isLoaded = true;
2023-10-19 15:00:19 +08:00
2023-12-28 13:07:11 +08:00
var sampleObj = await EffectManager.instance.orderCacher._LoadEffect(prefabPath);
2023-08-22 19:08:45 +08:00
if (sampleObj == null)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("特效不存在:{0}", prefabPath);
2023-08-22 19:08:45 +08:00
return;
}
if (rootObj == null)
{
// 说明已经被销毁了
return;
}
var obj = Object.Instantiate(sampleObj, rootObj.transform);
2023-08-22 19:08:45 +08:00
obj.transform.ResetLocals();
obj.SetActive(false);
await UniTask.DelayFrame(1);
2024-01-19 14:03:50 +08:00
if (obj == null)
{
// 这里如果被销毁了,说明游戏都结束了
return;
}
obj.SetActive(true);
2023-12-29 19:40:20 +08:00
_trails = obj.GetComponentsInChildren<TrailRenderer>();
2023-08-22 19:08:45 +08:00
rootObj.SetLayerEx(rootObj.layer);
2024-01-02 15:11:37 +08:00
if (onLoaded != null)
{
onLoaded();
}
2023-08-22 19:08:45 +08:00
EventManager.Instance.Send(EventManager.EventName.EFFECT_INST_FINISHED, this);
}
2023-12-28 19:01:07 +08:00
public void SetRemainTime(float setTime)
{
remainTime = setTime;
autoDestroy = remainTime > 0;
}
2024-01-02 15:11:37 +08:00
2023-08-22 19:08:45 +08:00
public void AddLogic(BaseEffectLogic logic)
{
2023-10-19 15:00:19 +08:00
if (_logic != null)
{
ObjPoolManager.instance.Return(logic);
}
2023-12-29 19:40:20 +08:00
autoDestroy = false;
2023-08-22 19:08:45 +08:00
_logic = logic;
_logic.effect = this;
}
public void LogicUpdate(float dt)
{
2023-10-19 15:00:19 +08:00
if (LevelManager.Instance.CurrentLevel?.isInBattle ?? false)
2023-08-22 19:08:45 +08:00
{
if (SkillManager.instance != null)
{
if (SkillManager.instance.inSkillingUnit != owner)
2023-08-22 19:08:45 +08:00
{
dt *= SkillManager.instance.needSkillPaused ? 0 : 1;
}
}
}
2023-10-19 15:00:19 +08:00
2023-08-22 19:08:45 +08:00
if (autoDestroy)
{
remainTime -= dt;
}
if (_logic != null)
{
_logic.LogicUpdate(dt);
}
}
public void Destroy()
{
2023-12-28 19:01:07 +08:00
_forceDestroy = true;
2024-01-02 15:11:37 +08:00
if (isReturned)
{
return;
}
isReturned = true;
2023-10-19 15:00:19 +08:00
EventManager.Instance.Send(EventManager.EventName.EFFECT_INST_RETURN, this);
2023-11-02 19:53:39 +08:00
if (_logic != null)
{
ObjPoolManager.instance.Return(_logic);
_logic = null;
}
2024-01-02 15:11:37 +08:00
if (_trails != null)
{
for (int i = 0; i < _trails.Length; i++)
{
var t = _trails[i];
t.Clear();
t.enabled = false;
}
}
2023-10-19 15:00:19 +08:00
EffectManager.instance.ReturnEffect(this);
}
public void RealDestroy()
{
2024-02-20 15:17:22 +08:00
_logic = null;
2023-12-28 19:01:07 +08:00
_forceDestroy = true;
2023-08-22 19:08:45 +08:00
if (rootObj != null)
{
rootObj.Destroy();
rootObj = null;
}
}
public void SetActive(bool active)
{
if (rootObj == null) return;
rootObj.SetActive(active);
}
}
}