This repository was archived by the owner on Sep 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathTime.cs
More file actions
133 lines (111 loc) · 4.7 KB
/
Copy pathTime.cs
File metadata and controls
133 lines (111 loc) · 4.7 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
namespace Foster.Framework;
/// <summary>
/// Time values
/// </summary>
public static class Time
{
/// <summary>
/// The Target Framerate of a Fixed Timestep update
/// </summary>
public static int FixedStepTarget = 60;
/// <summary>
/// The Maximum elapsed time a fixed update can take before skipping update calls
/// </summary>
public static TimeSpan FixedMaxElapsedTime = TimeSpan.FromMilliseconds(500);
/// <summary>
/// The time since the start of the Application
/// </summary>
public static TimeSpan Duration { get; internal set; }
/// <summary>
/// The time since the start of the Application, as of the previous frame
/// </summary>
public static TimeSpan PreviousDuration { get; internal set; }
/// <summary>
/// The total fixed-update duration since the start of the Application
/// </summary>
public static TimeSpan FixedDuration { get; internal set; }
/// <summary>
/// The total fixed-update duration since the start of the Application, as of the previous frame
/// </summary>
public static TimeSpan PreviousFixedDuration { get; internal set; }
/// <summary>
/// Multiplies the Delta Time per frame by the scale value
/// </summary>
public static float DeltaScale = 1.0f;
/// <summary>
/// The Delta Time from the last frame. Fixed or Variable depending on the current Update method.
/// Note that outside of Update Methods, this will return the last Variable Delta Time
/// </summary>
public static float Delta { get; internal set; }
/// <summary>
/// The Delta Time from the last frame, not scaled by DeltaScale. Fixed or Variable depending on the current Update method.
/// Note that outside of Update Methods, this will return the last Raw Variable Delta Time
/// </summary>
public static float RawDelta { get; internal set; }
/// <summary>
/// The last Fixed Delta Time.
/// </summary>
public static float FixedDelta { get; internal set; }
/// <summary>
/// The last Fixed Delta Time, not scaled by DeltaScale
/// </summary>
public static float RawFixedDelta { get; internal set; }
/// <summary>
/// The last Variable Delta Time.
/// </summary>
public static float VariableDelta { get; internal set; }
/// <summary>
/// The last Variable Delta Time, not scaled by DeltaScale
/// </summary>
public static float RawVariableDelta { get; internal set; }
/// <summary>
/// A rough estimate of the current Frames Per Second
/// </summary>
public static int FPS { get; internal set; }
/// <summary>
/// Returns true when the elapsed time passes a given interval based on the delta time
/// </summary>
public static bool OnInterval(double time, double delta, double interval, double offset)
{
return Math.Floor((time - offset - delta) / interval) < Math.Floor((time - offset) / interval);
}
/// <summary>
/// Returns true when the elapsed time passes a given interval based on the delta time
/// </summary>
public static bool OnInterval(double delta, double interval, double offset)
{
return OnInterval(Duration.TotalSeconds, delta, interval, offset);
}
/// <summary>
/// Returns true when the elapsed time passes a given interval based on the delta time
/// </summary>
public static bool OnInterval(double interval, double offset = 0.0)
{
return OnInterval(Duration.TotalSeconds, Delta, interval, offset);
}
/// <summary>
/// Returns true when the elapsed time is between the given interval. Ex: an interval of 0.1 will be false for 0.1 seconds, then true for 0.1 seconds, and then repeat.
/// </summary>
public static bool BetweenInterval(double time, double interval, double offset)
{
return (time - offset) % (interval * 2) >= interval;
}
/// <summary>
/// Returns true when the elapsed time is between the given interval. Ex: an interval of 0.1 will be false for 0.1 seconds, then true for 0.1 seconds, and then repeat.
/// </summary>
public static bool BetweenInterval(double interval, double offset = 0.0)
{
return BetweenInterval(Duration.TotalSeconds, interval, offset);
}
/// <summary>
/// Sine-wave a value between `from` and `to` with a period of `duration`.
/// You can use `offsetPercent` to offset the sine wave.
/// </summary>
public static float SineWave(float from, float to, float duration, float offsetPercent)
{
float total = (float)Duration.TotalSeconds;
float range = (to - from) * 0.5f;
return from + range + MathF.Sin(((total + duration * offsetPercent) / duration) * MathF.Tau) * range;
}
}