NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Show/MainShowManager.cs

142 lines
4.0 KiB
C#

using cfg.PlayerSkillCfg;
using cfg.ShowCfg;
using Cysharp.Threading.Tasks;
using Gameplay;
using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
/// <summary>
/// 主场景演出管理
/// </summary>
public class MainShowManager : ShowManager
{
/// <summary>
/// 主场景演出点结果
/// </summary>
public class MainShowPointResult
{
/// <summary>
/// 演出点索引
/// </summary>
public int showPointIndex;
/// <summary>
/// 角色数据
/// </summary>
public CharacterDataInfo characterDataInfo;
}
/// <summary>
/// Timeline
/// </summary>
[LabelText("Timeline")]
[SerializeField]
private PlayableDirector playableDirector;
/// <summary>
/// 最终的主场景演出点数据
/// </summary>
private readonly List<MainShowPointResult> listMainShowPointResult = new();
/// <summary>
/// Timeline绑定
/// </summary>
private readonly Dictionary<int, Object> dicTimelineBinding = new();
protected override E_ShowSceneType ShowSceneType
{
get { return E_ShowSceneType.Main; }
}
protected override void Awake()
{
base.Awake();
if (!CommonUtils.IsInGame())
return;
if (playableDirector == null || playableDirector.playableAsset == null)
{
DebugUtil.LogError("空数据");
return;
}
int i = 0;
foreach (PlayableBinding playableBinding in playableDirector.playableAsset.outputs)
{
dicTimelineBinding.Add(i, playableBinding.sourceObject);
++i;
}
}
/// <summary>
/// 刷新演出
/// </summary>
/// <returns></returns>
public override async UniTask RefreshShow()
{
ExtractShowCharacter();
await ExecuteShowResult();
}
/// <summary>
/// 抽取演出角色
/// </summary>
/// <exception cref="System.NotImplementedException"></exception>
protected override void ExtractShowCharacter()
{
base.ExtractShowCharacter();
listMainShowPointResult.Clear();
CharacterDataInfo[] characterDataInfos = CommonUtils.GetRandomItems(listRemindCharacter, CharacterMaxCount);
int[] pointIndexs = (int[])CommonUtils.GetRandomList(0, listShowPoint.Count - 1);
for (int i = 0; i < characterDataInfos.Length; ++i)
{
MainShowPointResult mainShowPointResult = new()
{
showPointIndex = pointIndexs[i],
characterDataInfo = characterDataInfos[i],
};
listMainShowPointResult.Add(mainShowPointResult);
}
}
/// <summary>
/// 执行演出结果
/// </summary>
/// <exception cref="System.NotImplementedException"></exception>
protected override async UniTask ExecuteShowResult()
{
if (playableDirector == null || playableDirector.playableAsset == null)
{
DebugUtil.LogError("空数据");
return;
}
for (int i = 0; i < listMainShowPointResult.Count; ++i)
{
MainShowPointResult result = listMainShowPointResult[i];
Transform tsShowPoint = listShowPoint[result.showPointIndex].transform;
if (tsShowPoint == null)
return;
GameObject goModel = await GetModel(result.characterDataInfo);
if (goModel == null)
continue;
goModel.transform.SetPositionAndRotation(tsShowPoint.position, tsShowPoint.rotation);
if (goModel.TryGetComponent(out PlayableDirector director)) // 禁用角色自身的Timeline
director.enabled = false;
if (dicTimelineBinding.TryGetValue(result.showPointIndex, out Object sourceObject))
playableDirector.SetGenericBinding(sourceObject, goModel.GetComponentInChildren<Animator>());
}
}
}