76 lines
1.8 KiB
C#
76 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using ZXKFramework;
|
|
|
|
public class TaskSequence
|
|
{
|
|
//任务序列开始回调
|
|
public Action onStart;
|
|
//任务序列完成回调
|
|
public Action onFinish;
|
|
//任务步骤列表
|
|
private List<TaskProcedure> taskList;
|
|
public TaskSequence(Action onStart,Action onFinish)
|
|
{
|
|
taskList = new List<TaskProcedure>();
|
|
this.onFinish = onFinish;
|
|
this.onStart = onStart;
|
|
}
|
|
public TaskSequence Add(float delayTime, Action<TaskProcedure> taskFunc)
|
|
{
|
|
TaskProcedure task = new();
|
|
task.onTaskFunc = (taskParam) =>
|
|
{
|
|
Game.Instance.IEnumeratorManager.Run(delayTime, () =>
|
|
{
|
|
taskFunc(task);
|
|
taskParam.onComplete?.Invoke();
|
|
});
|
|
};
|
|
taskList.Add(task);
|
|
return this;
|
|
}
|
|
public TaskSequence Run()
|
|
{
|
|
onStart?.Invoke();
|
|
for (int i = 0; i < taskList.Count - 1; i++)
|
|
{
|
|
int index = i;
|
|
int nextIndex = index + 1;
|
|
taskList[index].onComplete = () => taskList[nextIndex].onTaskFunc(taskList[nextIndex]);
|
|
}
|
|
if (taskList.Count > 0)
|
|
{
|
|
taskList[taskList.Count - 1].onComplete = onFinish;
|
|
taskList[0].onTaskFunc(taskList[0]);
|
|
}
|
|
return this;
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
onStart = null;
|
|
onFinish = null;
|
|
foreach (TaskProcedure task in taskList)
|
|
{
|
|
task.Dispose();
|
|
}
|
|
taskList.Clear();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 任务步骤
|
|
/// </summary>
|
|
public class TaskProcedure
|
|
{
|
|
//任务步骤具体内容
|
|
public Action<TaskProcedure> onTaskFunc;
|
|
//该步骤完成回调
|
|
public Action onComplete;
|
|
public void Dispose()
|
|
{
|
|
onTaskFunc = null;
|
|
onComplete = null;
|
|
}
|
|
} |