首次提交
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public static class TimerHelper
|
||||
{
|
||||
private static string name = "TimerHelper";
|
||||
|
||||
#region Time
|
||||
|
||||
private static YFsa _sYFsa;
|
||||
private static Psvsa _psvsa;
|
||||
private static UFdsa _sUFdsa;
|
||||
|
||||
public static YFsa mEasy
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sYFsa == null)
|
||||
{
|
||||
_sYFsa = TimerI.Instance.CreateSimpleTimer(name, TimerTimeType.Time);
|
||||
}
|
||||
|
||||
return _sYFsa;
|
||||
}
|
||||
}
|
||||
|
||||
public static Psvsa General
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_psvsa == null)
|
||||
{
|
||||
_psvsa = TimerI.Instance.CreateTimer(name, TimerTimeType.Time);
|
||||
}
|
||||
|
||||
return _psvsa;
|
||||
}
|
||||
}
|
||||
|
||||
public static UFdsa mNormal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sUFdsa == null)
|
||||
{
|
||||
_sUFdsa = TimerI.Instance.CreateHeavyTimer(name, TimerTimeType.Time);
|
||||
}
|
||||
|
||||
return _sUFdsa;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UnscaledTime
|
||||
|
||||
private static YFsa _sUnscaledYFsa;
|
||||
private static Psvsa _unscaledPsvsa;
|
||||
private static UFdsa _sUnscaledUFdsa;
|
||||
|
||||
public static YFsa mUnscaleEasy
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sUnscaledYFsa == null)
|
||||
{
|
||||
_sUnscaledYFsa = TimerI.Instance.CreateSimpleTimer(name, TimerTimeType.UnscaledTime);
|
||||
}
|
||||
|
||||
return _sUnscaledYFsa;
|
||||
}
|
||||
}
|
||||
|
||||
public static Psvsa UnscaleGeneral
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_unscaledPsvsa == null)
|
||||
{
|
||||
_unscaledPsvsa = TimerI.Instance.CreateTimer(name, TimerTimeType.UnscaledTime);
|
||||
}
|
||||
|
||||
return _unscaledPsvsa;
|
||||
}
|
||||
}
|
||||
|
||||
public static UFdsa mUnscaleNormal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sUnscaledUFdsa == null)
|
||||
{
|
||||
_sUnscaledUFdsa = TimerI.Instance.CreateHeavyTimer(name, TimerTimeType.UnscaledTime);
|
||||
}
|
||||
|
||||
return _sUnscaledUFdsa;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RealtimeSinceStartup
|
||||
|
||||
private static YFsa _sRealYFsa;
|
||||
private static Psvsa _realPsvsa;
|
||||
private static UFdsa _sRealUFdsa;
|
||||
|
||||
public static YFsa mRealEasy
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sRealYFsa == null)
|
||||
{
|
||||
_sRealYFsa = TimerI.Instance.CreateSimpleTimer(name, TimerTimeType.RealtimeSinceStartup);
|
||||
}
|
||||
|
||||
return _sRealYFsa;
|
||||
}
|
||||
}
|
||||
|
||||
public static Psvsa RealGeneral
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_realPsvsa == null)
|
||||
{
|
||||
_realPsvsa = TimerI.Instance.CreateTimer(name, TimerTimeType.RealtimeSinceStartup);
|
||||
}
|
||||
|
||||
return _realPsvsa;
|
||||
}
|
||||
}
|
||||
|
||||
public static UFdsa mRealNormal
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sRealUFdsa == null)
|
||||
{
|
||||
_sRealUFdsa = TimerI.Instance.CreateHeavyTimer(name, TimerTimeType.RealtimeSinceStartup);
|
||||
}
|
||||
|
||||
return _sRealUFdsa;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 889a363446c9d38489732b45d60696c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,247 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public class Psvsagh
|
||||
{
|
||||
private Psvsa _psvsa;
|
||||
|
||||
public int repeatCount;
|
||||
public float interval;
|
||||
private Action<Psvsagh> onCallBack;
|
||||
public object[] args;
|
||||
public float time;
|
||||
public bool isActive = true;
|
||||
public bool isFrameUpdate = false;
|
||||
|
||||
public Psvsagh(Psvsa psvsa, float interval, Action<Psvsagh> onCallBack, params object[] args)
|
||||
{
|
||||
this._psvsa = psvsa;
|
||||
|
||||
repeatCount = 1;
|
||||
this.interval = interval;
|
||||
this.onCallBack = onCallBack;
|
||||
this.args = args;
|
||||
time = psvsa.GetTriggerTime(interval);
|
||||
}
|
||||
|
||||
public Psvsagh(Psvsa psvsa, int repeatCount, float interval, Action<Psvsagh> onCallBack,
|
||||
params object[] args)
|
||||
{
|
||||
this._psvsa = psvsa;
|
||||
|
||||
this.repeatCount = repeatCount;
|
||||
this.interval = interval;
|
||||
this.onCallBack = onCallBack;
|
||||
this.args = args;
|
||||
time = psvsa.GetTriggerTime(interval);
|
||||
}
|
||||
|
||||
public Psvsagh(Psvsa psvsa, Action<Psvsagh> onCallBack, params object[] args)
|
||||
{
|
||||
this._psvsa = psvsa;
|
||||
|
||||
this.onCallBack = onCallBack;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public void SetActive(bool isActive)
|
||||
{
|
||||
if (this.isActive == isActive) return;
|
||||
|
||||
this.isActive = isActive;
|
||||
if (!isFrameUpdate)
|
||||
{
|
||||
if (this.isActive)
|
||||
{
|
||||
time = _psvsa.GetTriggerTime(interval);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void CallFunc()
|
||||
{
|
||||
onCallBack?.Invoke(this);
|
||||
}
|
||||
|
||||
public void Kill(bool needComplete = false)
|
||||
{
|
||||
if (needComplete)
|
||||
{
|
||||
CallFunc();
|
||||
}
|
||||
|
||||
Dispose();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_psvsa.RemoveTimer(this);
|
||||
}
|
||||
|
||||
public static implicit operator bool(Psvsagh psvsagh)
|
||||
{
|
||||
return null != psvsagh;
|
||||
}
|
||||
}
|
||||
|
||||
public class Psvsa : MonoBehaviour
|
||||
{
|
||||
public const int INFINITE_LOOP = -1;
|
||||
|
||||
private List<Psvsagh> timerTaskList = new();
|
||||
private List<Psvsagh> timerTaskTriggers = 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 (timerTaskList.Count > 0)
|
||||
{
|
||||
timerTaskTriggers.Clear();
|
||||
foreach (var timerTask in timerTaskList.Where(timerTask => timerTask.isActive))
|
||||
{
|
||||
if (timerTask.isFrameUpdate)
|
||||
{
|
||||
timerTaskTriggers.Add(timerTask);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (timerTask.time <= GetTime())
|
||||
{
|
||||
timerTaskTriggers.Add(timerTask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var triggerTimerTask in timerTaskTriggers)
|
||||
{
|
||||
if (triggerTimerTask.repeatCount != INFINITE_LOOP)
|
||||
{
|
||||
triggerTimerTask.repeatCount--;
|
||||
}
|
||||
|
||||
if (triggerTimerTask.repeatCount == 0)
|
||||
{
|
||||
timerTaskList.Remove(triggerTimerTask);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!triggerTimerTask.isFrameUpdate)
|
||||
{
|
||||
triggerTimerTask.time = GetTriggerTime(triggerTimerTask.interval);
|
||||
}
|
||||
}
|
||||
|
||||
triggerTimerTask.CallFunc();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Psvsagh AddTimer(float interval, Action<Psvsagh> onCallBack, params object[] args)
|
||||
{
|
||||
var timeTask = new Psvsagh(this, interval, onCallBack, args);
|
||||
AddTimer(timeTask);
|
||||
return timeTask;
|
||||
}
|
||||
|
||||
public Psvsagh AddRepeatTimer(int repeatCount, float interval, Action<Psvsagh> onCallBack,
|
||||
params object[] args)
|
||||
{
|
||||
var timeTask = new Psvsagh(this, repeatCount, interval, onCallBack, args);
|
||||
AddTimer(timeTask);
|
||||
return timeTask;
|
||||
}
|
||||
|
||||
public Psvsagh AddLoopTimer(float interval, Action<Psvsagh> onCallBack, params object[] args)
|
||||
{
|
||||
var timeTask = new Psvsagh(this, INFINITE_LOOP, interval, onCallBack, args);
|
||||
AddTimer(timeTask);
|
||||
return timeTask;
|
||||
}
|
||||
|
||||
public Psvsagh AddFrameUpdateTimer(Action<Psvsagh> onCallBack, params object[] args)
|
||||
{
|
||||
var timeTask = new Psvsagh(this, onCallBack, args)
|
||||
{
|
||||
repeatCount = INFINITE_LOOP,
|
||||
isFrameUpdate = true
|
||||
};
|
||||
AddTimer(timeTask);
|
||||
return timeTask;
|
||||
}
|
||||
|
||||
public void AddTimer(Psvsagh psvsagh)
|
||||
{
|
||||
if (null != psvsagh)
|
||||
{
|
||||
if (!psvsagh.isFrameUpdate)
|
||||
{
|
||||
if (psvsagh.interval <= 0)
|
||||
{
|
||||
psvsagh.CallFunc();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
timerTaskList.Add(psvsagh);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveTimer(Psvsagh psvsagh)
|
||||
{
|
||||
if (psvsagh == null) return;
|
||||
if (timerTaskList == null) return;
|
||||
if (timerTaskList.Count == 0) return;
|
||||
|
||||
if (timerTaskList.Contains(psvsagh))
|
||||
{
|
||||
timerTaskList.Remove(psvsagh);
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
timerTaskList.Clear();
|
||||
timerTaskTriggers.Clear();
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
public float GetTriggerTime(float interval)
|
||||
{
|
||||
return GetTime() + interval;
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ClearAll();
|
||||
timerTaskList = null;
|
||||
timerTaskTriggers = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1af09b65e6897dd4e8adcb89203638d1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,378 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BingoBrain.Core
|
||||
{
|
||||
public class UFdsa : MonoBehaviour
|
||||
{
|
||||
private class TimerTask
|
||||
{
|
||||
private UFdsa _mUFdsa;
|
||||
|
||||
private string name;
|
||||
private TimerMode mode;
|
||||
private float startTime;
|
||||
private float duration;
|
||||
|
||||
private bool isBreak = false;
|
||||
private float breakStart;
|
||||
private float breakDuration = 0;
|
||||
|
||||
private Action timerEvent;
|
||||
private Action<object[]> timerArgsEvent;
|
||||
private object[] args = null;
|
||||
|
||||
public float TimeLeft => Mathf.Max(0f, duration - (_mUFdsa.GetTime() - startTime) + breakDuration);
|
||||
|
||||
public TimerTask(UFdsa uFdsa, string name, TimerMode mode, float startTime, float duration,
|
||||
Action handler)
|
||||
{
|
||||
this._mUFdsa = uFdsa;
|
||||
|
||||
this.name = name;
|
||||
this.mode = mode;
|
||||
this.startTime = startTime;
|
||||
this.duration = duration;
|
||||
timerEvent = handler;
|
||||
}
|
||||
|
||||
public TimerTask(UFdsa uFdsa, string name, TimerMode mode, float startTime, float duration,
|
||||
Action<object[]> handler, params object[] args)
|
||||
{
|
||||
this._mUFdsa = uFdsa;
|
||||
|
||||
this.name = name;
|
||||
this.mode = mode;
|
||||
this.startTime = startTime;
|
||||
this.duration = duration;
|
||||
timerArgsEvent = handler;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public void Run()
|
||||
{
|
||||
if (isBreak)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (TimeLeft > 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == TimerMode.Normal)
|
||||
{
|
||||
_mUFdsa.RemoveTimer(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
startTime = _mUFdsa.GetTime();
|
||||
breakDuration = 0;
|
||||
}
|
||||
|
||||
timerEvent?.Invoke();
|
||||
timerArgsEvent?.Invoke(args);
|
||||
}
|
||||
|
||||
public void Break()
|
||||
{
|
||||
if (isBreak)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
isBreak = true;
|
||||
breakStart = _mUFdsa.GetTime();
|
||||
}
|
||||
|
||||
public void Resume()
|
||||
{
|
||||
if (!isBreak)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
breakDuration += (_mUFdsa.GetTime() - breakStart);
|
||||
isBreak = false;
|
||||
}
|
||||
|
||||
public static implicit operator bool(TimerTask timerTask)
|
||||
{
|
||||
return null != timerTask;
|
||||
}
|
||||
}
|
||||
|
||||
private enum TimerMode
|
||||
{
|
||||
Normal,
|
||||
Repeat,
|
||||
}
|
||||
|
||||
private Dictionary<string, TimerTask> addTimerList = new();
|
||||
private Dictionary<string, TimerTask> timerList = new();
|
||||
private List<string> destroyTimerList = new();
|
||||
|
||||
[SerializeField] private string timerName;
|
||||
private TimerTimeType type = TimerTimeType.Null;
|
||||
|
||||
public void SetTimer(string name, TimerTimeType type)
|
||||
{
|
||||
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 (destroyTimerList.Count > 0)
|
||||
{
|
||||
foreach (var i in destroyTimerList)
|
||||
{
|
||||
timerList.Remove(i);
|
||||
}
|
||||
|
||||
destroyTimerList.Clear();
|
||||
}
|
||||
|
||||
if (addTimerList.Count > 0)
|
||||
{
|
||||
foreach (var i in addTimerList.Where(i => i.Value != null))
|
||||
{
|
||||
timerList[i.Key] = i.Value;
|
||||
}
|
||||
|
||||
addTimerList.Clear();
|
||||
}
|
||||
|
||||
if (timerList.Count > 0)
|
||||
{
|
||||
foreach (var timer in timerList.Values)
|
||||
{
|
||||
if (timer == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
timer.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool AddTimer(string key, float duration, Action handler)
|
||||
{
|
||||
return InternalAddTimer(key, TimerMode.Normal, duration, handler);
|
||||
}
|
||||
|
||||
public bool AddTimer(string key, float duration, Action<object[]> handler, params object[] args)
|
||||
{
|
||||
return InternalAddTimer(key, TimerMode.Normal, duration, handler, args);
|
||||
}
|
||||
|
||||
public bool AddRepeatTimer(string key, float duration, Action handler)
|
||||
{
|
||||
return InternalAddTimer(key, TimerMode.Repeat, duration, handler);
|
||||
}
|
||||
|
||||
public bool AddRepeatTimer(string key, float duration, Action<object[]> handler, params object[] args)
|
||||
{
|
||||
return InternalAddTimer(key, TimerMode.Repeat, duration, handler, args);
|
||||
}
|
||||
|
||||
private bool InternalAddTimer(string key, TimerMode mode, float duration, Action handler)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (duration <= 0.0f)
|
||||
{
|
||||
if (mode == TimerMode.Normal)
|
||||
{
|
||||
handler();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("[HeavyTimer]Add Repeat Timer Error: Time is Not Greater Than Zero");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var timer = new TimerTask(this, key, mode, GetTime(), duration, handler);
|
||||
addTimerList[key] = timer;
|
||||
return true;
|
||||
}
|
||||
|
||||
private bool InternalAddTimer(string key, TimerMode mode, float duration, Action<object[]> handler,
|
||||
params object[] args)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (duration <= 0.0f)
|
||||
{
|
||||
if (mode == TimerMode.Normal)
|
||||
{
|
||||
handler(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("[HeavyTimer]Add Repeat Timer Error: Time is Not Greater Than Zero");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
var timer = new TimerTask(this, key, mode, GetTime(), duration, handler, args);
|
||||
addTimerList[key] = timer;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void BreakTimerWithPrefix(string prefix)
|
||||
{
|
||||
if (timerList is { Count: > 0 })
|
||||
{
|
||||
var arr = new string[timerList.Count];
|
||||
timerList.Keys.CopyTo(arr, 0);
|
||||
|
||||
foreach (var t in arr)
|
||||
{
|
||||
if (t.StartsWith(prefix))
|
||||
{
|
||||
BreakTimer(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void BreakTimer(string key)
|
||||
{
|
||||
if (!timerList.ContainsKey(key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var timer = timerList[key];
|
||||
timer.Break();
|
||||
}
|
||||
|
||||
public void ResumeTimerWithPrefix(string prefix)
|
||||
{
|
||||
if (timerList is { Count: > 0 })
|
||||
{
|
||||
var arr = new string[timerList.Count];
|
||||
timerList.Keys.CopyTo(arr, 0);
|
||||
|
||||
foreach (var t in arr)
|
||||
{
|
||||
if (t.StartsWith(prefix))
|
||||
{
|
||||
ResumeTimer(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void ResumeTimer(string key)
|
||||
{
|
||||
if (!timerList.ContainsKey(key))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var timer = timerList[key];
|
||||
timer.Resume();
|
||||
}
|
||||
|
||||
public void ClearTimerWithPrefix(string prefix)
|
||||
{
|
||||
if (timerList is { Count: > 0 })
|
||||
{
|
||||
foreach (var timerKey in timerList.Keys.Where(timerKey => timerKey.StartsWith(prefix)))
|
||||
{
|
||||
RemoveTimer(timerKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveTimer(string key)
|
||||
{
|
||||
if (!timerList.ContainsKey(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!destroyTimerList.Contains(key))
|
||||
{
|
||||
destroyTimerList.Add(key);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsRunning(string key)
|
||||
{
|
||||
return timerList.ContainsKey(key);
|
||||
}
|
||||
|
||||
public float GetTimerLeft(string key)
|
||||
{
|
||||
if (!timerList.ContainsKey(key))
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
var timer = timerList[key];
|
||||
return timer.TimeLeft;
|
||||
}
|
||||
|
||||
public float GetTimerLeftWithPrefix(string prefix)
|
||||
{
|
||||
if (timerList is { Count: > 0 })
|
||||
{
|
||||
var arr = new string[timerList.Count];
|
||||
timerList.Keys.CopyTo(arr, 0);
|
||||
|
||||
return (from t in arr where t.StartsWith(prefix) select GetTimerLeft(t)).FirstOrDefault();
|
||||
}
|
||||
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
addTimerList.Clear();
|
||||
timerList.Clear();
|
||||
destroyTimerList.Clear();
|
||||
}
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
Destroy(this);
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
ClearAll();
|
||||
addTimerList = null;
|
||||
timerList = null;
|
||||
destroyTimerList = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fefb9ed2ef0fffd409b6ea3e113cb153
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,94 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cccc8c2ef81a9c04c99cc97fdf07a8a2
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user