ball 项目提交
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class EasyTimer : 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 ClearAll()
|
||||
{
|
||||
mIntervalDic.Clear();
|
||||
triggers.Clear();
|
||||
}
|
||||
|
||||
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:
|
||||
@@ -0,0 +1,157 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class NormalTimer : MonoBehaviour
|
||||
{
|
||||
private class TimerTask
|
||||
{
|
||||
private NormalTimer mNormalTimer;
|
||||
|
||||
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 - (mNormalTimer.GetTime() - startTime) + breakDuration);
|
||||
|
||||
public void Run()
|
||||
{
|
||||
if (isBreak)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (TimeLeft > 0f)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (mode == TimerMode.Normal)
|
||||
{
|
||||
mNormalTimer.RemoveTimer(name);
|
||||
}
|
||||
else
|
||||
{
|
||||
startTime = mNormalTimer.GetTime();
|
||||
breakDuration = 0;
|
||||
}
|
||||
|
||||
timerEvent?.Invoke();
|
||||
timerArgsEvent?.Invoke(args);
|
||||
}
|
||||
|
||||
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 RemoveTimer(string key)
|
||||
{
|
||||
if (!timerList.ContainsKey(key))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!destroyTimerList.Contains(key))
|
||||
{
|
||||
destroyTimerList.Add(key);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
addTimerList.Clear();
|
||||
timerList.Clear();
|
||||
destroyTimerList.Clear();
|
||||
}
|
||||
|
||||
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,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public class TimerTask
|
||||
{
|
||||
private Timer timer;
|
||||
|
||||
public int repeatCount;
|
||||
public float interval;
|
||||
private Action<TimerTask> onCallBack;
|
||||
public object[] args;
|
||||
public float time;
|
||||
public bool isActive = true;
|
||||
public bool isFrameUpdate = false;
|
||||
|
||||
public TimerTask(Timer timer, float interval, Action<TimerTask> onCallBack, params object[] args)
|
||||
{
|
||||
this.timer = timer;
|
||||
|
||||
repeatCount = 1;
|
||||
this.interval = interval;
|
||||
this.onCallBack = onCallBack;
|
||||
this.args = args;
|
||||
time = timer.GetTriggerTime(interval);
|
||||
}
|
||||
|
||||
public TimerTask(Timer timer, int repeatCount, float interval, Action<TimerTask> onCallBack,
|
||||
params object[] args)
|
||||
{
|
||||
this.timer = timer;
|
||||
|
||||
this.repeatCount = repeatCount;
|
||||
this.interval = interval;
|
||||
this.onCallBack = onCallBack;
|
||||
this.args = args;
|
||||
time = timer.GetTriggerTime(interval);
|
||||
}
|
||||
|
||||
public TimerTask(Timer timer, Action<TimerTask> onCallBack, params object[] args)
|
||||
{
|
||||
this.timer = timer;
|
||||
|
||||
this.onCallBack = onCallBack;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
public void CallFunc()
|
||||
{
|
||||
onCallBack?.Invoke(this);
|
||||
}
|
||||
|
||||
public static implicit operator bool(TimerTask timerTask)
|
||||
{
|
||||
return null != timerTask;
|
||||
}
|
||||
}
|
||||
|
||||
public class Timer : MonoBehaviour
|
||||
{
|
||||
public const int INFINITE_LOOP = -1;
|
||||
|
||||
private List<TimerTask> timerTaskList = new();
|
||||
private List<TimerTask> 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 TimerTask AddLoopTimer(float interval, Action<TimerTask> onCallBack, params object[] args)
|
||||
{
|
||||
var timeTask = new TimerTask(this, INFINITE_LOOP, interval, onCallBack, args);
|
||||
AddTimer(timeTask);
|
||||
return timeTask;
|
||||
}
|
||||
|
||||
public void AddTimer(TimerTask timerTask)
|
||||
{
|
||||
if (null != timerTask)
|
||||
{
|
||||
if (!timerTask.isFrameUpdate)
|
||||
{
|
||||
if (timerTask.interval <= 0)
|
||||
{
|
||||
timerTask.CallFunc();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
timerTaskList.Add(timerTask);
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearAll()
|
||||
{
|
||||
timerTaskList.Clear();
|
||||
timerTaskTriggers.Clear();
|
||||
}
|
||||
|
||||
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,57 @@
|
||||
namespace BallKingdomCrush
|
||||
{
|
||||
public static class TimerHelper
|
||||
{
|
||||
private static string name = "TimerHelper";
|
||||
|
||||
#region Time
|
||||
|
||||
private static EasyTimer sEasyTimer;
|
||||
private static Timer _Timer;
|
||||
private static NormalTimer sNormalTimer;
|
||||
|
||||
public static EasyTimer mEasy
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sEasyTimer == null)
|
||||
{
|
||||
sEasyTimer = TimerIManager.Instance.CreateSimpleTimer(name, TimerTimeType.Time);
|
||||
}
|
||||
|
||||
return sEasyTimer;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region UnscaledTime
|
||||
|
||||
private static EasyTimer sUnscaledEasyTimer;
|
||||
private static Timer _UnscaledTimer;
|
||||
private static NormalTimer sUnscaledNormalTimer;
|
||||
|
||||
public static Timer UnscaleGeneral
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_UnscaledTimer == null)
|
||||
{
|
||||
_UnscaledTimer = TimerIManager.Instance.CreateTimer(name, TimerTimeType.UnscaledTime);
|
||||
}
|
||||
|
||||
return _UnscaledTimer;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region RealtimeSinceStartup
|
||||
|
||||
private static EasyTimer sRealEasyTimer;
|
||||
private static Timer _RealTimer;
|
||||
private static NormalTimer sRealNormalTimer;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 889a363446c9d38489732b45d60696c6
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user