1
This commit is contained in:
121
Src/Unity/Scripts/Tick/GameTickService.cs
Normal file
121
Src/Unity/Scripts/Tick/GameTickService.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using Cysharp.Threading.Tasks;
|
||||
#if UNITY_EDITOR
|
||||
using UnityEditor;
|
||||
#endif
|
||||
using UnityEngine;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
[Serializable]
|
||||
public sealed class GameTick:ITicker
|
||||
{
|
||||
|
||||
public ulong TickCount => GameTickService.TickCount;
|
||||
|
||||
public void Add(Action action)=>GameTickService.Add(action);
|
||||
|
||||
public void Add(Action<float> action)=>GameTickService.Add(action);
|
||||
|
||||
public void Remove(Action<float> action)=>GameTickService.Remove(action);
|
||||
}
|
||||
public class GameTickService : MonoBehaviour,ITicker
|
||||
{
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
private static void Reload()
|
||||
{
|
||||
_ActionQueue.Clear();
|
||||
//_TickActions.Clear();
|
||||
TickCount = ulong.MinValue;
|
||||
}
|
||||
private static readonly Queue<Action> _ActionQueue = new();
|
||||
// private static readonly CacheList<Action<float>> _TickActions = new();
|
||||
// public static void Add(Action<float> action) => _TickActions.Add(action);
|
||||
// public static void Remove(Action<float> action) => _TickActions.Remove(action);
|
||||
private static event Action<float> _TickEvents;
|
||||
public static void Add( Action<float> action)=>_TickEvents += action;
|
||||
public static void Remove( Action<float> action)=>_TickEvents -= action;
|
||||
public static void Add(Action action)=>_ActionQueue.Enqueue(action);
|
||||
public static ulong TickCount { get; private set; }
|
||||
|
||||
ulong ITicker.TickCount => TickCount;
|
||||
|
||||
void ITicker.Add(Action action)=>Add(action);
|
||||
void ITicker.Add(Action<float> action)=>Add(action);
|
||||
void ITicker.Remove(Action<float> action)=>Remove(action);
|
||||
[SerializeField] private int tickRate = 32;
|
||||
[SerializeField] private bool isMainThread;
|
||||
[SerializeField] private bool isConcurrent;
|
||||
private readonly Timer _timer = new();
|
||||
private float _deltaTime;
|
||||
private double _lastTime;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
tickRate = Mathf.Clamp(tickRate, 1, 128);
|
||||
|
||||
_deltaTime = 1f / tickRate;
|
||||
|
||||
_timer.Elapsed += Tick;
|
||||
_timer.AutoReset = isConcurrent;
|
||||
_timer.Interval = TimeSpan.FromSeconds(_deltaTime).TotalMilliseconds;
|
||||
_timer.Start();
|
||||
destroyCancellationToken.Register(() =>
|
||||
{
|
||||
_timer.Stop();
|
||||
_timer.Dispose();
|
||||
});
|
||||
}
|
||||
private async void Tick(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
TickCount++;
|
||||
try
|
||||
{
|
||||
|
||||
var delta = (float)(BITApp.Time.TimeAsDouble - _lastTime);
|
||||
_lastTime = BITApp.Time.TimeAsDouble;
|
||||
if (isMainThread) await UniTask.SwitchToMainThread(destroyCancellationToken);
|
||||
#if UNITY_EDITOR
|
||||
if (EditorApplication.isPlaying is false)
|
||||
{
|
||||
Restart();
|
||||
}
|
||||
#endif
|
||||
while (_ActionQueue.TryDequeue(out var action))
|
||||
{
|
||||
action?.Invoke();
|
||||
}
|
||||
_TickEvents?.Invoke(delta);
|
||||
// using var xEnumerator = _TickActions.GetEnumerator();
|
||||
// while (xEnumerator.MoveNext())
|
||||
// {
|
||||
// xEnumerator.Current!.Invoke(delta);
|
||||
// }
|
||||
//_TickEvents?.Invoke((float)delta);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
BIT4Log.LogException(exception);
|
||||
}
|
||||
|
||||
Restart();
|
||||
return;
|
||||
void Restart()
|
||||
{
|
||||
if (isConcurrent is false && destroyCancellationToken.IsCancellationRequested is false)
|
||||
{
|
||||
_timer.Start();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user