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

182 lines
4.4 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
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-10-19 15:00:19 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
namespace Gameplay.Effect
{
using Framework;
using UnityEngine;
using Gameplay.Skill;
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;
public Action onLoaded;
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;
}
}
public float remainTime;
public bool autoDestroy = false;
public EEffectState state = EEffectState.Error;
2023-10-19 15:00:19 +08:00
public bool needReturn
2023-08-22 19:08:45 +08:00
{
get
{
2023-10-19 15:00:19 +08:00
return (autoDestroy && remainTime <= 0) || state == EEffectState.Destroied || state == EEffectState.Returned;
2023-08-22 19:08:45 +08:00
}
}
private BaseEffectLogic _logic;
2023-10-19 15:00:19 +08:00
public readonly int effectId;
public EffectPlayingData(int effectId)
{
this.effectId = effectId;
}
2023-08-22 19:08:45 +08:00
public async void Load(string prefabPath)
{
2023-10-19 15:00:19 +08:00
if (state != EEffectState.Error) return;
2023-08-22 19:08:45 +08:00
state = EEffectState.Loading;
2023-10-19 15:00:19 +08:00
if (SROptions.Current != null)
{
if (!SROptions.Current.EnableEffect)
{
state = EEffectState.Destroied;
return;
}
}
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(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;
}
2023-10-19 15:00:19 +08:00
var obj = await AssetManager.Instance.InstanceAsync<GameObject>(prefabPath);
2023-08-22 19:08:45 +08:00
if (rootObj == null)
{
// 说明已经被销毁了
obj.Destroy();
return;
}
obj.transform.SetParent(rootObj.transform);
obj.transform.ResetLocals();
state = EEffectState.Playing;
rootObj.SetLayerEx(rootObj.layer);
if (onLoaded != null)
{
onLoaded();
}
2023-08-22 19:08:45 +08:00
EventManager.Instance.Send(EventManager.EventName.EFFECT_INST_FINISHED, this);
}
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-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-10-19 15:00:19 +08:00
if (state == EEffectState.Returned
|| state == EEffectState.Destroied) return;
state = EEffectState.Returned;
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;
}
2023-10-19 15:00:19 +08:00
EffectManager.instance.ReturnEffect(this);
}
public void RealDestroy()
{
2023-08-22 19:08:45 +08:00
state = EEffectState.Destroied;
if (rootObj != null)
{
rootObj.Destroy();
rootObj = null;
}
}
public void SetActive(bool active)
{
if (rootObj == null) return;
rootObj.SetActive(active);
}
}
}