feat: initial commit
This commit is contained in:
62
Assets/Common/Timer/Runtime/ScriptableObjects/StopwatchSO.cs
Normal file
62
Assets/Common/Timer/Runtime/ScriptableObjects/StopwatchSO.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bef3fff7f3888464aab61a599e9a460a
|
||||
113
Assets/Common/Timer/Runtime/ScriptableObjects/TimerBaseSO.cs
Normal file
113
Assets/Common/Timer/Runtime/ScriptableObjects/TimerBaseSO.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 312c8c00ee578df428063b28e6322aed
|
||||
25
Assets/Common/Timer/Runtime/ScriptableObjects/TimerSO.cs
Normal file
25
Assets/Common/Timer/Runtime/ScriptableObjects/TimerSO.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc4e8df518cd7244dbdfc3464c604bed
|
||||
Reference in New Issue
Block a user