forked from Anuken/Arc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.java
More file actions
160 lines (130 loc) · 4.4 KB
/
Time.java
File metadata and controls
160 lines (130 loc) · 4.4 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package arc.util;
import arc.*;
import arc.struct.*;
import arc.func.*;
import arc.util.Timer.*;
import arc.util.pooling.Pool.*;
import arc.util.pooling.*;
public class Time{
/** Conversion factors for ticks to other unit values. */
public static final float toSeconds = 60f, toMinutes = 60f * 60f, toHours = 60f * 60f * 60f;
/** Global delta value. Do not change. */
public static float delta = 1f;
/** Global time values. Do not change. */
public static float time, globalTime;
public static final long nanosPerMilli = 1000000;
private static double timeRaw, globalTimeRaw;
private static Seq<DelayRun> runs = new Seq<>();
private static Seq<DelayRun> removal = new Seq<>();
private static LongSeq marks = new LongSeq();
private static Floatp deltaimpl = () -> Math.min(Core.app.getDeltaTime() * 60f, 3f);
/** Runs a task with a delay of several ticks. If Time.clear() is called, this task will be cancelled. */
public static void run(float delay, Runnable r){
DelayRun run = Pools.obtain(DelayRun.class, DelayRun::new);
run.finish = r;
run.delay = delay;
runs.add(run);
}
/** Runs a task with a delay of several ticks. Unless the application is closed, this task will always complete. */
public static Task runTask(float delay, Runnable r){
return Timer.schedule(r, delay / 60f);
}
public static void mark(){
marks.add(nanos());
}
/** A value of -1 means mark() wasn't called beforehand. */
public static float elapsed(){
if(marks.size == 0){
return -1;
}else{
return timeSinceNanos(marks.pop()) / 1000000f;
}
}
public static void updateGlobal(){
globalTimeRaw += Core.app.getDeltaTime()*60f;
delta = deltaimpl.get();
if(Double.isInfinite(timeRaw) || Double.isNaN(timeRaw)){
timeRaw = 0;
}
time = (float)timeRaw;
globalTime = (float)globalTimeRaw;
}
/** Use normal delta time (e. g. delta * 60) */
public static void update(){
timeRaw += delta;
removal.clear();
if(Double.isInfinite(timeRaw) || Double.isNaN(timeRaw)){
timeRaw = 0;
}
time = (float)timeRaw;
globalTime = (float)globalTimeRaw;
for(DelayRun run : runs){
run.delay -= delta;
if(run.delay <= 0){
run.finish.run();
removal.add(run);
Pools.free(run);
}
}
runs.removeAll(removal);
}
public static void clear(){
runs.clear();
}
public static void setDeltaProvider(Floatp impl){
deltaimpl = impl;
delta = impl.get();
}
/** @return The current value of the system timer, in nanoseconds. */
public static long nanos(){
return System.nanoTime();
}
/** @return the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. */
public static long millis(){
return System.currentTimeMillis();
}
/**
* Convert nanoseconds time to milliseconds
* @param nanos must be nanoseconds
* @return time value in milliseconds
*/
public static long nanosToMillis(long nanos){
return nanos / nanosPerMilli;
}
/**
* Convert milliseconds time to nanoseconds
* @param millis must be milliseconds
* @return time value in nanoseconds
*/
public static long millisToNanos(long millis){
return millis * nanosPerMilli;
}
/**
* Get the time in nanos passed since a previous time
* @param prevTime - must be nanoseconds
* @return - time passed since prevTime in nanoseconds
*/
public static long timeSinceNanos(long prevTime){
return nanos() - prevTime;
}
public static float millisSinceNanos(long prevTime){
return (nanos() - prevTime) / (float)nanosPerMilli;
}
/**
* Get the time in millis passed since a previous time
* @param prevTime - must be milliseconds
* @return - time passed since prevTime in milliseconds
*/
public static long timeSinceMillis(long prevTime){
return millis() - prevTime;
}
public static class DelayRun implements Poolable{
float delay;
Runnable finish;
@Override
public void reset(){
delay = 0;
finish = null;
}
}
}