forked from RevenantX/LiteEntitySystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyncTimer.cs
More file actions
96 lines (81 loc) · 2.03 KB
/
SyncTimer.cs
File metadata and controls
96 lines (81 loc) · 2.03 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using LiteEntitySystem.Internal;
namespace LiteEntitySystem.Extensions
{
public class SyncTimer : SyncableField
{
public float MaxTime => _maxTime;
public float ElapsedTime => _time;
public bool IsTimeElapsed => _time >= _maxTime;
private SyncVar<float> _time;
private SyncVar<float> _maxTime;
public SyncTimer(float maxTime)
{
_maxTime = maxTime;
Finish();
}
public SyncTimer()
{
}
public float Progress
{
get
{
float p = _time/_maxTime;
return p > 1f ? 1f : p;
}
}
public void Reset()
{
_time = 0f;
}
public void Reset(float maxTime)
{
_maxTime = maxTime;
_time = 0f;
}
public void Finish()
{
_time = _maxTime;
}
public float LerpByProgress(float a, float b)
{
return Utils.Lerp(a, b, Progress);
}
public float LerpByProgress(float a, float b, bool inverse)
{
return inverse
? Utils.Lerp(a, b, Progress)
: Utils.Lerp(b, a, Progress);
}
public bool UpdateAndCheck(float delta)
{
if (IsTimeElapsed)
return false;
return Update(delta);
}
public bool Update(float delta)
{
if (_time < _maxTime)
_time += delta;
return IsTimeElapsed;
}
public bool CheckAndSubtractMaxTime()
{
if (_time >= _maxTime)
{
_time -= _maxTime;
return true;
}
return false;
}
public bool UpdateAndReset(float delta)
{
if (Update(delta))
{
_time -= _maxTime;
return true;
}
return false;
}
}
}