using Cysharp.Threading.Tasks;
using Framework;
using Gameplay.Area;
using Gameplay.Bullet;
using Gameplay.Effect;
using Gameplay.PlayerSkill.Cmp;
using UnityEngine;
using UnityEngine.Playables;
namespace Gameplay.PlayerSkill.Logic
{
///
/// 逻辑:
/// 在指定格子位置创建一个带timeline的特效
/// 等待timeline触发事件,接收到该事件后像该位置发射一发子弹
/// 逻辑参数 [特效id, 子弹id]
///
public class PlayerSkillLogic03 : BasePlayerSkillLogic
{
private int _cacheBulletId;
private int _cacheReleaseCellIndex;
private EffectPlayingData _cacheEffectData;
protected override void _OnInit()
{
base._OnInit();
var effectId = owner.ReadParamInt(0);
var bulletId = owner.ReadParamInt(1);
_cacheBulletId = bulletId;
var releaseCellIndex = owner.selectedCellIndex;
_cacheReleaseCellIndex = releaseCellIndex;
var releasePos = AreaManager.instance.GetWorldPosByIndex(releaseCellIndex);
_InitEffect(effectId, releasePos);
EventManager.Instance.Register(EventManager.EventName.INFIGHT_PLAYER_SKILL_VIEW_TRIGGER, _OnEffectEvent);
}
protected override void _OnDispose()
{
base._OnDispose();
_cacheEffectData.onLoaded = null;
EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_PLAYER_SKILL_VIEW_TRIGGER, _OnEffectEvent);
}
private void _InitEffect(int effectId, Vector3 releasePos)
{
_cacheEffectData = EffectManager.instance.PlayEffectById(effectId, releasePos);
_cacheEffectData.onLoaded = _OnEffectLoaded;
}
private void _OnEffectLoaded()
{
var effectObj = _cacheEffectData.rootObj;
var timeLine = effectObj.GetComponentInChildren();
if (timeLine == null)
{
DebugUtil.LogError("该模式下的特效必须包含timeline");
isFinished = true;
return;
}
// 设置timeline的signal receiver
// 获取bindings
var isSet = false;
var bindings = timeLine.playableAsset.outputs;
foreach (var binding in bindings)
{
if (binding.streamName == "Signal Track")
{
timeLine.SetGenericBinding(binding.sourceObject, CmpPlayerSkillEventWrapper.instance.m_SignalReceiver);
isSet = true;
break;
}
}
if (!isSet)
{
DebugUtil.LogError("该模式下的timeline必须包含signal track");
isFinished = true;
return;
}
timeLine.Play();
}
private void _OnEffectEvent()
{
BulletManager.instance.EmitBullet(_cacheBulletId, _cacheReleaseCellIndex, owner.playerUnit);
isFinished = true;
}
}
}