-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimerStruct.cs
56 lines (54 loc) · 1.96 KB
/
TimerStruct.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using Fusion;
using UnityEngine;
namespace JazzApps.Utils
{
public enum State
{
INACTIVE,
SIGNALLING,
ACTIVE,
}
/// <summary>
/// A deterministic approach to a timer system.
/// </summary>
public struct TimerStruct : INetworkStruct
{
public State State { get; private set; }
public float TimeLeft { get; private set; }
public float TimeLeftPercent => TimeLeft / length;
public float TimeLeftOverallPercent => (TimeLeft + length * intervalsLeft) / length * (intervals+1);
public string TimeLeftFormatted => TimeLeft < 0 ? "0" : TimeLeft.ToString("G2");
public string TimeLeftPercentFormatted => TimeLeftPercent < 0 ? "0" : TimeLeftPercent.ToString("G2");
public string TimeLeftOverallPercentFormatted => TimeLeftOverallPercent < 0 ? "0" : TimeLeftOverallPercent.ToString("G2");
private float intervalsLeft;
private readonly float length;
private readonly float intervals;
public TimerStruct(float length, float repeatedTimes = 0)
{
this.State = State.ACTIVE;
this.length = length;
this.TimeLeft = length;
this.intervals = repeatedTimes < 0 ? Mathf.Infinity : repeatedTimes;
this.intervalsLeft = intervals;
}
public static TimerStruct Process(TimerStruct timer, float deltaTime, out State state)
{
timer.TimeLeft -= timer.TimeLeft > 0 ? deltaTime : 0;
if (timer.TimeLeft <= 0)
{
if (timer.intervalsLeft > 0)
{
timer.TimeLeft = timer.length;
timer.intervalsLeft--;
timer.State = State.SIGNALLING;
}
else
timer.State = State.INACTIVE;
}
else
timer.State = State.ACTIVE;
state = timer.State;
return timer;
}
}
}