using DefaultNamespace.ProcessMode;
using Framework.ProcessMode;
using MotionFramework;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using static InterfaceManager;
///
///
///
public class FlowStepController : MonoBehaviour
{
public static FlowStepController Instance;
///
/// 步骤集合
///
public List flowSteps = new List();
///
/// 子步骤集合
///
public List flowSubSteps = new List();
///
/// 步骤初始化
///
public List stepInits = new List();
///
/// 插件
///
private ProcessManager processManager;
private void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
//查找场景内所有
//Invoke(nameof(Init), 3f);
}
///
/// 初始化流程
///
public void Init()
{
processManager = MotionEngine.GetModule();
var steps = processManager.CurrentProcessCollection.Steps;
if (steps != null && steps.Count > 0)
{
//根据steps个数在本物体下生成挂有FlowStep脚本的物体,并为上边的脚本赋值
for (int i = 0; i < steps.Count; i++)
{
// 创建一个新的子物体
GameObject newStepObject = new GameObject("FlowStep_" + i);
newStepObject.transform.SetParent(this.transform); // 将新物体设为当前物体的子物体
// 为新物体添加FlowStep脚本
FlowStep flowStepScript = newStepObject.AddComponent();
// 为FlowStep脚本赋值
flowStepScript.stepNum = i + 1;
flowStepScript.currentStep = steps[i].StepDescription;
flowStepScript.isActivate = i == 0 ? true : false;
flowStepScript.isCompleted = false;
StepInit stepInit = FindObjectByName(steps[i].StepDescription);
if (stepInit!= null)
{
flowStepScript.stepInit = stepInit;
}
flowSteps.Add(flowStepScript);
if (i > 0 && flowSteps.Count > (i - 1))
{
flowSteps[i - 1].nextStep = flowStepScript;
}
LoadProcessStepDescription(steps[i], flowStepScript);
if (i == steps.Count - 1)
{
Invoke(nameof(LoadFlowSubSteps), 1f);
}
}
}
processManager.OnStepActionsCompleteEvent += OnStepCompleted;
processManager.OnCurrentActionCompleteEvent += OnCurrentComplete;
}
///
///
///
private void LoadFlowSubSteps()
{
for (int k = 0; k < flowSubSteps.Count - 1; k++)
{
flowSubSteps[k].nextStep = flowSubSteps[k + 1];
}
}
///
/// 加载子项
///
///
///
public void LoadProcessStepDescription(ProcessStep step, FlowStep flowStep)
{
List Actions= step.Actions;
for (int i = 0; i < Actions.Count; i++)
{
GameObject newStepObject = new GameObject(Actions[i].Title);
newStepObject.transform.SetParent(flowStep.transform);
newStepObject.SetActive(false);
// 为新物体添加FlowStep脚本
FlowSubStep flowSubStepScript = newStepObject.AddComponent();
flowSubStepScript.stepNum = i + 1;
flowSubStepScript.currentStep = Actions[i].Title;
flowSubStepScript.isCompleted = false;
StepInit stepInit = ReturnStepInit(Actions[i].Title);
if (stepInit != null)
{
flowSubStepScript.stepInit = stepInit;
Debug.Log(stepInit.name);
}
flowStep.flowSubSteps.Add(flowSubStepScript);
flowSubSteps.Add(flowSubStepScript);
newStepObject.SetActive(true);
flowSubStepScript.isActivate = (i == 0 && flowStep.stepNum == 1) ? true : false;
}
}
///
/// 根据名称查找StepInit脚本
///
///
///
public StepInit ReturnStepInit(string title)
{
StepInit stepInit = stepInits.Find(x => x.name == title && x.gameObject.activeSelf);
return stepInit;
}
///
/// 处理步骤完成事件
///
private void OnStepCompleted(ProcessStep step)
{
Debug.Log($"步骤{step.StepDescription}完成");
for (int i = 0; i < flowSteps.Count; i++)
{
if (flowSteps[i].currentStep == step.StepDescription)
{
flowSteps[i].isCompleted = true;
if (i + 1 < flowSteps.Count)
{
flowSteps[i + 1].isActivate = true;
}
}
}
// 在这里可以添加其他处理逻辑
}
///
/// 处理当前动作完成事件
///
///
public void OnCurrentComplete(ProcessStepDescription step)
{
Debug.Log($"动作 {step.Title}完成 ");
for (int i = 0; i < flowSubSteps.Count; i++)
{
if (flowSubSteps[i].currentStep == step.Title)
{
flowSubSteps[i].isCompleted = true;
if (i + 1 < flowSubSteps.Count)
{
flowSubSteps[i + 1].isActivate = true;
}
}
}
}
///
/// 需要加载的步骤
///
public string title;
///
///
///
[ContextMenu("LoadCurrentComplete")]
public void LoadCurrentComplete()
{
for (int i = 0; i < flowSubSteps.Count; i++)
{
if (flowSubSteps[i].currentStep == title)
{
flowSubSteps[i].isActivate = true;
}
}
}
}