feat: initial commit

This commit is contained in:
2025-12-01 12:36:01 +08:00
commit ee78bf2cb5
221 changed files with 56853 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
using System;
namespace Common.Timer.Runtime
{
public interface IStopwatch : ITimer
{
public float Duration { get; set; }
public bool DeactivateOnTimeReached { get; set; }
public float RemainingTime { get; set; }
public float Progress { get; set; }
public bool IsTimeReached => CurrentTime >= Duration;
public event Action OnTimeReached;
public void SetDuration(float value);
public void SetRemainingTime(float value);
public void SetProgress(float value);
public void EndTime();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7d400d4cccd55e94088df52dfd4cbb96

View File

@@ -0,0 +1,23 @@
using System;
namespace Common.Timer.Runtime
{
public interface ITimer
{
public bool IsActive { get; }
public float CurrentTime { get; set; }
public float DeltaTime { get; }
public void Tick();
public void Activate(bool resetTime = false);
public void Deactivate();
public void ResetTime();
public void SetTime(float value);
public event Action OnTick;
public event Action OnActivate;
public event Action OnDeactivate;
public event Action OnTimeReset;
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 166ad9d07eff7ce409885714f6cddd55

View File

@@ -0,0 +1,20 @@
{
"name": "Common.Timer.Runtime",
"rootNamespace": "JoeSiu",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:6055be8ebefd69e48b49212b09b47b2f",
"GUID:324caed91501a9c47a04ebfd87b68794",
"GUID:d7f6647327e7fa54faec2c8e2a49621d",
"GUID:70e478b7eb804446bc79813af716d369"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 4867374f8b89f5b4587d9170bed9e856
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,104 @@
using System;
using TriInspector;
using UnityEngine;
namespace Common.Timer.Runtime
{
public class MonoStopwatch : MonoTimer, IStopwatch
{
public override float CurrentTime
{
get
{
currentTime = Mathf.Clamp(currentTime, 0f, Duration);
return currentTime;
}
set => currentTime = Mathf.Clamp(value, 0f, Duration);
}
[Title("Settings (Stopwatch)")]
[SerializeField]
protected float duration;
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float Duration
{
get
{
duration = Mathf.Max(0.001f, duration);
return duration;
}
set => duration = Mathf.Max(0.001f, value);
}
[SerializeField]
protected bool deactivateOnTimeReached;
public bool DeactivateOnTimeReached
{
get => deactivateOnTimeReached;
set => deactivateOnTimeReached = value;
}
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float RemainingTime
{
get => Mathf.Clamp(Duration - CurrentTime, 0f, Duration);
set => CurrentTime = Mathf.Clamp(Duration - value, 0f, Duration);
}
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float Progress
{
get => Duration == 0f ? 0f : Mathf.Clamp(CurrentTime / Duration, 0f, 1f);
set => CurrentTime = Mathf.Clamp(Duration * value, 0f, Duration);
}
[Group("Internal"), ReadOnly, ShowInInspector, DisableInEditMode]
public virtual bool IsTimeReached => CurrentTime >= Duration;
public event Action OnTimeReached;
public override void Tick()
{
if (IsTimeReached) return;
base.Tick();
if (!IsActive) return;
if (CurrentTime >= Duration) EndTime();
}
public virtual void SetDuration(float value)
{
Duration = value;
if (IsTimeReached) EndTime();
}
public virtual void SetRemainingTime(float value)
{
RemainingTime = value;
if (IsTimeReached) EndTime();
}
public virtual void SetProgress(float value)
{
Progress = value;
if (IsTimeReached) EndTime();
}
public virtual void EndTime()
{
currentTime = Duration;
InvokeOnTimeReached();
if (DeactivateOnTimeReached) Deactivate();
}
protected virtual void InvokeOnTimeReached()
{
OnTimeReached?.Invoke();
}
#if UNITY_EDITOR
[Group("Debug"), Button("End Time"), HideInEditMode]
private void EndTimer_Editor() => EndTime();
#endif
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 7152de60fabb33c41aebca0a58b2096b

View File

@@ -0,0 +1,109 @@
using System;
using TriInspector;
using UnityEngine;
namespace Common.Timer.Runtime
{
[DeclareBoxGroup("Debug")]
[DeclareBoxGroup("Internal")]
// TODO: Create a MonoTimerBase similar to TimerBaseSO instead
public class MonoTimer : MonoBehaviour, ITimer
{
[Title("Settings")]
[SerializeField]
protected bool isActive;
public virtual bool IsActive
{
get => isActive;
protected set => isActive = value;
}
protected float currentTime;
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float CurrentTime
{
get
{
currentTime = Mathf.Max(currentTime, 0f);
return currentTime;
}
set => currentTime = Mathf.Max(value, 0f);
}
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float DeltaTime => Time.deltaTime;
public event Action OnTick;
public event Action OnActivate;
public event Action OnDeactivate;
public event Action OnTimeReset;
protected virtual void Update()
{
Tick();
}
#region Public Methods
public virtual void Tick()
{
if (!IsActive) return;
CurrentTime += DeltaTime;
InvokeOnTick();
}
public virtual void Activate(bool resetTime = false)
{
IsActive = true;
InvokeOnActivate();
if (resetTime) ResetTime();
}
public virtual void Deactivate()
{
IsActive = false;
InvokeOnDeactivate();
}
public virtual void ResetTime()
{
CurrentTime = 0f;
InvokeOnTimeReset();
}
public virtual void SetTime(float value)
{
CurrentTime = value;
}
#endregion
#region Protected Methods
protected virtual void InvokeOnTick() => OnTick?.Invoke();
protected virtual void InvokeOnActivate() => OnActivate?.Invoke();
protected virtual void InvokeOnDeactivate() => OnDeactivate?.Invoke();
protected virtual void InvokeOnTimeReset() => OnTimeReset?.Invoke();
#endregion
#if UNITY_EDITOR
[Group("Debug"), Button("Activate"), HideInEditMode]
private void Activate_Editor() => Activate(false);
[Group("Debug"), Button("Activate (Reset Time)"), HideInEditMode]
private void ActivateResetTime_Editor() => Activate(true);
[Group("Debug"), Button("Deactivate"), HideInEditMode]
private void Deactivate_Editor() => Deactivate();
[Group("Debug"), Button("Tick"), HideInEditMode]
private void Tick_Editor() => Tick();
[Group("Debug"), Button("Reset Time"), HideInEditMode]
private void ResetTime_Editor() => ResetTime();
#endif
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 34fac720fb4287843aea355b0594eda1

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 84762aa781ebe084290160322bc621e3
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,62 @@
using System;
using TriInspector;
using UnityEngine;
namespace Common.Timer.Runtime.ScriptableObjects
{
[CreateAssetMenu(fileName = "Stopwatch", menuName = "JoeSiu/Common/Stopwatch")]
public class StopwatchSO : TimerBaseSO, IStopwatch
{
protected override ITimer Timer => _stopwatch;
[SerializeField, InlineProperty, HideLabel]
[InfoBox("The stopwatch in ScriptableObject will be deactivated by default")]
private Stopwatch _stopwatch = new(false, 10f);
public float Duration
{
get => _stopwatch.Duration;
set => _stopwatch.Duration = value;
}
public float RemainingTime
{
get => _stopwatch.RemainingTime;
set => _stopwatch.RemainingTime = value;
}
public float Progress
{
get => _stopwatch.Progress;
set => _stopwatch.Progress = value;
}
public bool IsTimeReached => _stopwatch.IsTimeReached;
public bool DeactivateOnTimeReached
{
get => _stopwatch.DeactivateOnTimeReached;
set => _stopwatch.DeactivateOnTimeReached = value;
}
public event Action OnTimeReached
{
add => _stopwatch.OnTimeReached += value;
remove => _stopwatch.OnTimeReached -= value;
}
protected override void Activate_Internal(bool resetTime = false)
{
_stopwatch.Activate(resetTime);
}
public virtual void SetDuration(float value) => _stopwatch.SetDuration(value);
public virtual void SetRemainingTime(float value) => _stopwatch.SetRemainingTime(value);
public virtual void SetProgress(float value) => _stopwatch.SetProgress(value);
public void EndTime() => _stopwatch.EndTime();
public override void Reinitialize()
{
_stopwatch = new(false, _stopwatch.Duration);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: bef3fff7f3888464aab61a599e9a460a

View File

@@ -0,0 +1,113 @@
using System;
using TriInspector;
using UnityEditor;
using UnityEngine;
namespace Common.Timer.Runtime.ScriptableObjects
{
public abstract class TimerBaseSO : ScriptableObject, ITimer
{
protected abstract ITimer Timer { get; }
public bool IsActive => Timer.IsActive;
public float CurrentTime
{
get => Timer.CurrentTime;
set => Timer.CurrentTime = value;
}
public float DeltaTime => Timer.DeltaTime;
public void Tick() => Timer.Tick();
public void Activate(bool resetTime = false)
{
AddToManager();
Activate_Internal(resetTime);
}
public void Deactivate() => Timer.Deactivate();
public void ResetTime() => Timer.ResetTime();
public virtual void SetTime(float value)
{
CurrentTime = value;
}
public event Action OnTick
{
add => Timer.OnTick += value;
remove => Timer.OnTick -= value;
}
public event Action OnActivate
{
add => Timer.OnActivate += value;
remove => Timer.OnActivate -= value;
}
public event Action OnDeactivate
{
add => Timer.OnDeactivate += value;
remove => Timer.OnDeactivate -= value;
}
public event Action OnTimeReset
{
add => Timer.OnTimeReset += value;
remove => Timer.OnTimeReset -= value;
}
protected abstract void Activate_Internal(bool resetTime = false);
private void AddToManager()
{
if (!TimerManager.GetInstance()) TimerManager.CreateInstance();
TimerManager.Instance.Add(this);
}
private void RemoveFromManager()
{
if (!TimerManager.GetInstance()) return;
TimerManager.Instance.Remove(this);
}
#if UNITY_EDITOR
protected virtual void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.ExitingPlayMode)
{
Reinitialize();
}
}
#endif
#if UNITY_EDITOR
protected virtual void OnValidate()
{
if (!EditorApplication.isPlaying) Timer.Deactivate();
}
#endif
protected virtual void OnEnable()
{
#if UNITY_EDITOR
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
#endif
}
protected virtual void OnDisable()
{
#if UNITY_EDITOR
EditorApplication.playModeStateChanged -= OnPlayModeStateChanged;
#endif
}
protected virtual void OnDestroy()
{
#if UNITY_EDITOR
Reinitialize();
#endif
}
[Button]
public abstract void Reinitialize();
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 312c8c00ee578df428063b28e6322aed

View File

@@ -0,0 +1,25 @@
using TriInspector;
using UnityEngine;
namespace Common.Timer.Runtime.ScriptableObjects
{
[CreateAssetMenu(fileName = "Timer", menuName = "JoeSiu/Common/Timer")]
public class TimerSO : TimerBaseSO
{
protected override ITimer Timer => _timer;
[SerializeField, InlineProperty, HideLabel]
[InfoBox("The timer in ScriptableObject will be deactivated by default")]
private Timer _timer = new(false);
protected override void Activate_Internal(bool resetTime = false)
{
_timer.Activate(resetTime);
}
public override void Reinitialize()
{
_timer = new(false);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: cc4e8df518cd7244dbdfc3464c604bed

View File

@@ -0,0 +1,120 @@
using System;
using TriInspector;
using UnityEngine;
using UnityEngine.Serialization;
namespace Common.Timer.Runtime
{
[Serializable]
public class Stopwatch : Timer, IStopwatch
{
public override float CurrentTime
{
get
{
currentTime = Mathf.Clamp(currentTime, 0f, Duration);
return currentTime;
}
set => currentTime = Mathf.Clamp(value, 0f, Duration);
}
[Title("Settings (Stopwatch)")]
[SerializeField]
[FormerlySerializedAs("_duration")]
protected float duration;
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float Duration
{
get
{
duration = Mathf.Max(0.001f, duration);
return duration;
}
set => duration = Mathf.Max(0.001f, value);
}
[SerializeField]
[FormerlySerializedAs("_deactivateOnTimeReached")]
protected bool deactivateOnTimeReached;
public bool DeactivateOnTimeReached
{
get => deactivateOnTimeReached;
set => deactivateOnTimeReached = value;
}
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float RemainingTime
{
get => Mathf.Clamp(Duration - CurrentTime, 0f, Duration);
set => CurrentTime = Mathf.Clamp(Duration - value, 0f, Duration);
}
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float Progress
{
get => Duration == 0f ? 0f : Mathf.Clamp(CurrentTime / Duration, 0f, 1f);
set => CurrentTime = Mathf.Clamp(Duration * value, 0f, Duration);
}
[Group("Internal"), ReadOnly, ShowInInspector, DisableInEditMode]
public virtual bool IsTimeReached => CurrentTime >= Duration;
public event Action OnTimeReached;
public Stopwatch(bool isActive, float duration, bool deactivateOnTimeReached = false) : base(isActive)
{
this.duration = duration;
this.deactivateOnTimeReached = deactivateOnTimeReached;
}
public override void Tick()
{
if (IsTimeReached) return;
base.Tick();
if (!IsActive) return;
if (IsTimeReached) EndTime();
}
public override void SetTime(float value)
{
CurrentTime = value;
if (IsTimeReached) EndTime();
}
public virtual void SetDuration(float value)
{
Duration = value;
if (IsTimeReached) EndTime();
}
public virtual void SetRemainingTime(float value)
{
RemainingTime = value;
if (IsTimeReached) EndTime();
}
public virtual void SetProgress(float value)
{
Progress = value;
if (IsTimeReached) EndTime();
}
public virtual void EndTime()
{
currentTime = Duration;
InvokeOnTimeReached();
if (DeactivateOnTimeReached) Deactivate();
}
protected virtual void InvokeOnTimeReached()
{
OnTimeReached?.Invoke();
}
#if UNITY_EDITOR
[Group("Debug"), Button("End Time"), HideInEditMode]
private void EndTimer_Editor() => EndTime();
#endif
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: deafc70d1d6a826429baf2f506837603

View File

@@ -0,0 +1,114 @@
using System;
using TriInspector;
using UnityEngine;
using UnityEngine.Serialization;
namespace Common.Timer.Runtime
{
/// <summary>
/// C# implementation of a timer. Remember to call Tick() on this Timer object via script!
/// </summary>
[Serializable]
[DeclareBoxGroup("Debug")]
[DeclareBoxGroup("Internal")]
public class Timer : ITimer
{
[Title("Settings")]
[SerializeField]
[FormerlySerializedAs("_isActive")]
protected bool isActive;
public virtual bool IsActive
{
get => isActive;
protected set => isActive = value;
}
protected float currentTime;
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float CurrentTime
{
get
{
currentTime = Mathf.Max(currentTime, 0f);
return currentTime;
}
set => currentTime = Mathf.Max(value, 0f);
}
[Group("Internal"), ShowInInspector, DisableInEditMode]
public virtual float DeltaTime => Time.deltaTime;
public event Action OnTick;
public event Action OnActivate;
public event Action OnDeactivate;
public event Action OnTimeReset;
public Timer(bool isActive)
{
this.isActive = isActive;
}
#region Public Methods
public virtual void Tick()
{
if (!IsActive) return;
CurrentTime += DeltaTime;
InvokeOnTick();
}
public virtual void Activate(bool resetTime = false)
{
IsActive = true;
InvokeOnActivate();
if (resetTime) ResetTime();
}
public virtual void Deactivate()
{
IsActive = false;
InvokeOnDeactivate();
}
public virtual void ResetTime()
{
CurrentTime = 0f;
InvokeOnTimeReset();
}
public virtual void SetTime(float value)
{
CurrentTime = value;
}
#endregion
#region Protected Methods
protected virtual void InvokeOnTick() => OnTick?.Invoke();
protected virtual void InvokeOnActivate() => OnActivate?.Invoke();
protected virtual void InvokeOnDeactivate() => OnDeactivate?.Invoke();
protected virtual void InvokeOnTimeReset() => OnTimeReset?.Invoke();
#endregion
#if UNITY_EDITOR
[Group("Debug"), Button("Activate"), HideInEditMode]
private void Activate_Editor() => Activate(false);
[Group("Debug"), Button("Activate (Reset Time)"), HideInEditMode]
private void ActivateResetTime_Editor() => Activate(true);
[Group("Debug"), Button("Deactivate"), HideInEditMode]
private void Deactivate_Editor() => Deactivate();
[Group("Debug"), Button("Tick"), HideInEditMode]
private void Tick_Editor() => Tick();
[Group("Debug"), Button("Reset Time"), HideInEditMode]
private void ResetTime_Editor() => ResetTime();
#endif
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 64b5ad6c3a3b3934892167377d2ff419

View File

@@ -0,0 +1,41 @@
using System.Collections.Generic;
using Common.Timer.Runtime.ScriptableObjects;
using JoeSiu.Common.Infrastructure.Runtime;
using TriInspector;
using UnityEngine;
namespace Common.Timer.Runtime
{
public class TimerManager : MonoSingleton<TimerManager>
{
[InfoBox("As ScriptableObject doesn't have Update() function, use this class to control the ScriptableObject timer's tick")]
[SerializeField]
private List<TimerBaseSO> _timerSOs = new();
public IReadOnlyList<TimerBaseSO> TimerSOs => _timerSOs.AsReadOnly();
protected override void Awake()
{
base.Awake();
DontDestroyOnLoad(this);
}
private void Update()
{
_timerSOs.ForEach(so => so?.Tick());
}
public void Add(TimerBaseSO timerBaseSO)
{
if (_timerSOs.Contains(timerBaseSO)) return;
_timerSOs.Add(timerBaseSO);
Debug.Log($"Added TimerBaseSO '{timerBaseSO.name}' to {nameof(TimerManager)}", this);
}
public void Remove(TimerBaseSO timerBaseSO)
{
if (!_timerSOs.Contains(timerBaseSO)) return;
_timerSOs.Remove(timerBaseSO);
Debug.Log( $"Removed TimerBaseSO '{timerBaseSO.name}' from {nameof(TimerManager)}", this);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 477f0034741527640a767bbcb0c42a01

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 6346ab71a8da1784c84e99bf10a88384
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,68 @@
using System;
using JoeSiu.Common.Serialization.Runtime;
using TMPro;
using UnityEditor;
using UnityEngine;
namespace Common.Timer.Runtime.UI
{
public class TimerDebugText : MonoBehaviour
{
[SerializeField]
private SerializableInterface<ITimer> _timer = new();
public TMP_Text text;
public string prefix;
public int decimalPlaces = 2;
public ITimer Timer
{
get => _timer.Value;
set => _timer.Value = value;
}
private void Reset()
{
text = GetComponent<TMP_Text>();
}
private void OnValidate()
{
#if UNITY_EDITOR
if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer);
#endif
}
private void Update()
{
UpdateText(Timer);
}
public void UpdateText(ITimer timer)
{
if (!text) return;
if (timer == null) return;
var format = $"F{decimalPlaces}";
var info = string.IsNullOrEmpty(prefix) ? "" : $"{prefix}\n";
if (timer is Behaviour behaviour) info += $"enabled: {behaviour.enabled}\n";
info += $"active: {timer.IsActive}\n";
switch (timer)
{
// Check if timer implements IStopwatch
case IStopwatch stopwatch:
info += $"isTimeReached: {stopwatch.IsTimeReached}\n";
info += $"{Math.Round(stopwatch.CurrentTime, decimalPlaces).ToString(format)}s / " +
$"{Math.Round(stopwatch.Duration, decimalPlaces).ToString(format)}s " +
$"(remain: {Math.Round(stopwatch.RemainingTime, decimalPlaces).ToString(format)}s)\n";
break;
// Fallback for base Timer class
default:
info += $"{Math.Round(timer.CurrentTime, decimalPlaces).ToString(format)}s\n";
break;
}
text.text = info;
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 9449292013eda4945bf3f8bc3ba3b52d

View File

@@ -0,0 +1,79 @@
using System;
using JoeSiu.Common.Serialization.Runtime;
using TMPro;
using TriInspector;
using UnityEditor;
using UnityEngine;
namespace Common.Timer.Runtime.UI
{
[ExecuteAlways]
public class TimerDurationText : MonoBehaviour
{
public TMP_Text text;
[SerializeField]
private SerializableInterface<ITimer> _timer = new();
#if UNITY_EDITOR
[ShowIf(nameof(IsStopwatch))]
#endif
public bool useRemainingTimeForStopwatch = true;
[Range(0, 15)]
public int precision = 0;
[Range(0, 15)]
public int padLeft = 0;
[Range(0, 15)]
public int padRight = 0;
#if UNITY_EDITOR
private bool IsStopwatch => Timer is IStopwatch;
#endif
public ITimer Timer
{
get => _timer?.Value;
set => _timer.Value = value;
}
private void Reset()
{
text = GetComponent<TMP_Text>();
}
private void OnValidate()
{
#if UNITY_EDITOR
if (!EditorApplication.isPlaying && Selection.Contains(gameObject)) UpdateText(Timer);
#endif
}
private void Update()
{
UpdateText(Timer);
}
public void UpdateText(ITimer timer)
{
if (!text) return;
if (timer == null) return;
float time;
if (useRemainingTimeForStopwatch && timer is IStopwatch stopwatch)
{
time = stopwatch.RemainingTime;
}
else
{
time = timer.CurrentTime;
}
time = (float)Math.Round(time, precision);
var timeString = time.ToString();
// TODO: PadLeft / PadRight doesn't work in all cases (e.g., given time of 1, it can't turn into 01.00)
// TODO: Use a custom user defined formatter instead?
if (padLeft > 0) timeString = timeString.PadLeft(padLeft, '0');
if (padRight > 0) timeString = timeString.PadRight(padRight, '0');
text.SetText(timeString);
}
}
}

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: a872f72d4d5d8e94895cff23a747e3f0