using System.Collections.Generic;
using System.Threading;
using Cysharp.Threading.Tasks;
using UnityEngine;
namespace Stary.Evo.StoryEditor
{
public class GraphPlayer
{
///
/// 剧本名称
///
public string Name;
///
/// 节点
///
public List Nodes = new();
///
/// 起始节点
///
public BeginNodePlayer BeginNode;
///
/// 结束节点
///
public EndNodePlayer EndNode;
///
/// 当前进行中的节点
///
public List CurrentNodes = new();
///
/// 播放完成
///
public bool Finished;
///
/// CTS
///
public CancellationTokenSource Cts = new();
public GraphPlayer(GraphData data, IResource loader)
{
// 检查加载方式
if (loader.GetType().ToString() != data.resourceType)
{
Debug.LogError($"加载方式不匹配,剧本无法加载:{data.name}\n需要的加载方式: {data.resourceType}");
return;
}
Name = data.name;
data.nodes.ForEach(node =>
{
switch (node)
{
case BeginNodeData beginNode:
Nodes.Add(beginNode.GetPlayer(this));
break;
case EndNodeData endNode:
Nodes.Add(endNode.GetPlayer(this));
break;
case ScriptParagraphNodeData paraNode:
Nodes.Add(paraNode.GetPlayer(this));
break;
case FlowNodeData flowNode:
Nodes.Add(flowNode.GetPlayer(this));
break;
}
});
BeginNode = (BeginNodePlayer)Nodes[data.startNodeIndex];
BeginNode.Connect();
}
///
/// 执行剧本
///
public async UniTask Execute()
{
// 初始化结束标志
Finished = false;
// 执行剧本
_ = BeginNode.Execute();
// 等待剧本完成
await UniTask.WaitUntil(() => Finished);
}
///
/// 停止剧本
///
public void Stop()
{
// 取消并重置CancelToken
Cts.Cancel();
Cts = new();
// 标记剧本结束
Finished = true;
}
}
}