97 lines
1.7 KiB
C#
97 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using System;
|
|
|
|
namespace BF
|
|
{
|
|
public struct Delayed
|
|
{
|
|
public float time;
|
|
public Action action;
|
|
}
|
|
|
|
public class TaskManager : ManagerBase
|
|
{
|
|
int seqId = 1;
|
|
Dictionary<int, Task> taskDict = new Dictionary<int, Task>();
|
|
List<Delayed> delayedList = new List<Delayed>();
|
|
|
|
public int TaskCount { get; private set; }
|
|
|
|
static TaskManager instance;
|
|
|
|
TaskManager() { }
|
|
|
|
public static TaskManager Create()
|
|
{
|
|
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
|
|
instance = new TaskManager();
|
|
return instance;
|
|
}
|
|
|
|
public override void SetMono(MonoBehaviour mono)
|
|
{
|
|
base.SetMono(mono);
|
|
}
|
|
|
|
public override void Init()
|
|
{
|
|
}
|
|
|
|
public int AddTask(IEnumerator c)
|
|
{
|
|
var taskID = seqId++;
|
|
var task = new Task(taskID, c);
|
|
StartCoroutine(task.CallWrapper());
|
|
taskDict.Add(taskID, task);
|
|
TaskCount++;
|
|
return taskID;
|
|
}
|
|
|
|
void StartCoroutine(IEnumerator c)
|
|
{
|
|
mono.StartCoroutine(c);
|
|
}
|
|
|
|
public void StopTask(int taskID)
|
|
{
|
|
Task task = null;
|
|
if (taskDict.TryGetValue(taskID, out task))
|
|
{
|
|
task.Stop();
|
|
}
|
|
}
|
|
|
|
public void RemoveTask(int taskID)
|
|
{
|
|
if (taskDict.ContainsKey(taskID))
|
|
{
|
|
taskDict.Remove(taskID);
|
|
TaskCount--;
|
|
}
|
|
}
|
|
|
|
public void EasyTimer(float time, Action action)
|
|
{
|
|
delayedList.Add(new Delayed
|
|
{
|
|
time = Time.time + time,
|
|
action = action
|
|
});
|
|
}
|
|
|
|
public override void Update()
|
|
{
|
|
for (int i = delayedList.Count - 1; i >= 0; i--)
|
|
{
|
|
var delay = delayedList[i];
|
|
if (delay.time <= Time.time)
|
|
{
|
|
delay.action();
|
|
delayedList.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |