94 lines
2.2 KiB
C#
94 lines
2.2 KiB
C#
using System;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
namespace BingoBrain.Core
|
|
{
|
|
public class YFsa : MonoBehaviour
|
|
{
|
|
private Dictionary<Action, float> mIntervalDic = new();
|
|
private List<Action> triggers = new();
|
|
|
|
[SerializeField] private string timerName;
|
|
private TimerTimeType type = TimerTimeType.Null;
|
|
|
|
public void SetTimer(string name, TimerTimeType type)
|
|
{
|
|
this.timerName = name + "_" + type;
|
|
this.type = type;
|
|
}
|
|
|
|
private float GetTime()
|
|
{
|
|
return type switch
|
|
{
|
|
TimerTimeType.Time => Time.time,
|
|
TimerTimeType.UnscaledTime => Time.unscaledTime,
|
|
TimerTimeType.RealtimeSinceStartup => Time.realtimeSinceStartup,
|
|
_ => 0
|
|
};
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (mIntervalDic.Count > 0)
|
|
{
|
|
triggers.Clear();
|
|
foreach (var keyValue in mIntervalDic.Where(keyValue => keyValue.Value <= GetTime()))
|
|
{
|
|
triggers.Add(keyValue.Key);
|
|
}
|
|
|
|
foreach (var func in triggers)
|
|
{
|
|
mIntervalDic.Remove(func);
|
|
func();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void AddTimer(float interval, Action func)
|
|
{
|
|
if (null != func)
|
|
{
|
|
if (interval <= 0)
|
|
{
|
|
func();
|
|
return;
|
|
}
|
|
|
|
mIntervalDic[func] = GetTime() + interval;
|
|
}
|
|
}
|
|
|
|
public void RemoveTimer(Action func)
|
|
{
|
|
if (null != func)
|
|
{
|
|
if (mIntervalDic.ContainsKey(func))
|
|
{
|
|
mIntervalDic.Remove(func);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void ClearAll()
|
|
{
|
|
mIntervalDic.Clear();
|
|
triggers.Clear();
|
|
}
|
|
|
|
public void Destroy()
|
|
{
|
|
Destroy(this);
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
ClearAll();
|
|
mIntervalDic = null;
|
|
triggers = null;
|
|
}
|
|
}
|
|
} |