61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using System;
|
|
using Intrepid.Utilities;
|
|
using Sirenix.OdinInspector;
|
|
using UnityEngine;
|
|
|
|
namespace Intrepid.Gameplay.Entities.Player.Sentinel.Cadence
|
|
{
|
|
[Serializable]
|
|
[CreateAssetMenu(fileName = "Cadence Service", menuName = "GON/Services/Cadence Service")]
|
|
public class CadenceService : SingletonScriptableObject<CadenceService>
|
|
{
|
|
[GroupDebug, SerializeField, ReadOnly] private float currentCadence;
|
|
|
|
private Timer CadenceTimer { get; set; } = new();
|
|
|
|
public float CurrentCadence
|
|
{
|
|
get => currentCadence;
|
|
private set => currentCadence = value;
|
|
}
|
|
public float CadenceElapsedTime => (float)CadenceTimer.ElapsedTime;
|
|
public bool IsCadenceRunning => CadenceTotalTime > 0f && CadenceTimer.IsRunning;
|
|
|
|
private float CadenceTotalTime => CadenceContext is null ? 0f : 0.001f * CadenceContext.TotalTimeMS;
|
|
private CadenceContext CadenceContext { get; set; }
|
|
public float WarmCadencePercent => CadenceContext?.WarmTime / CadenceTotalTime ?? 0f;
|
|
public float OptimalCadenceMinPercent => CadenceContext?.OptimalMinTime / CadenceTotalTime ?? 0f;
|
|
public float OptimalCadenceMaxPercent => CadenceContext?.OptimalMaxTime / CadenceTotalTime ?? 0f;
|
|
|
|
protected override void WhenInstantiate(CadenceService s)
|
|
{
|
|
base.WhenInstantiate(s);
|
|
StopCadence();
|
|
}
|
|
|
|
public void StartCadence(CadenceContext ctx)
|
|
{
|
|
if (ctx is null || ctx.TotalTimeMS <= 0f) return;
|
|
CadenceTimer.Start();
|
|
CadenceContext = ctx;
|
|
}
|
|
|
|
public void StopCadence()
|
|
{
|
|
CadenceTimer.Stop();
|
|
CadenceContext = null;
|
|
}
|
|
|
|
public void UpdateCadence()
|
|
{
|
|
if (CadenceTimer.IsNotRunning) return;
|
|
CadenceTimer.Update(Timer.ScaledTime);
|
|
var totalTime = CadenceTotalTime;
|
|
CurrentCadence = CadenceElapsedTime / totalTime;
|
|
if (CadenceTimer.Check(totalTime))
|
|
{
|
|
StopCadence();
|
|
}
|
|
}
|
|
}
|
|
} |