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 taskDict = new Dictionary(); List delayedList = new List(); 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); } } } } }