-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGameObjectTimer.cs
More file actions
221 lines (166 loc) · 5.64 KB
/
GameObjectTimer.cs
File metadata and controls
221 lines (166 loc) · 5.64 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Engine.Game.App;
using UnityEngine;
public class GameObjectTimerKeys {
public static string gameUpdateAll = "game-update-all";
public static string gameUpdateAlways = "game-update-always";
public static string gameUpdatePhysics = "game-update-physics";
public static string gameLateUpdateAll = "game-late-update-all";
public static string gameLateUpdateAlways = "game-late-update-always";
public static string gameLateUpdatePhysics = "game-late-update-physics";
public static string gameFixedUpdateAll = "game-fixed-update-all";
public static string gameFixedUpdateAlways = "game-fixed-update-always";
public static string gameFixedUpdatePhysics = "game-fixed-update-physics";
}
public class GameObjectTimerData : GameDataObject {
// key
// delta
// last_time
}
public class GameObjectTimer {
public Dictionary<string, GameObjectTimerData> timers;
public Dictionary<string, float> intervals;
private static volatile GameObjectTimer instance;
private static System.Object syncRoot = new System.Object();
public static GameObjectTimer Instance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = new GameObjectTimer();
}
}
return instance;
}
set {
instance = value;
}
}
public GameObjectTimer() {
Reset();
}
public void Reset() {
InitTimers(true);
InitIntervals(true);
}
public void InitIntervals(bool reset = false) {
if (intervals == null || reset) {
intervals = new Dictionary<string, float>();
SetInterval(GameObjectTimerKeys.gameUpdateAll, defaultInterval);
SetInterval(GameObjectTimerKeys.gameUpdateAlways, defaultInterval);
SetInterval(GameObjectTimerKeys.gameUpdatePhysics, defaultInterval);
SetInterval(GameObjectTimerKeys.gameLateUpdateAll, defaultInterval);
SetInterval(GameObjectTimerKeys.gameLateUpdateAlways, defaultInterval);
SetInterval(GameObjectTimerKeys.gameLateUpdatePhysics, defaultInterval);
SetInterval(GameObjectTimerKeys.gameFixedUpdateAll, defaultInterval);
SetInterval(GameObjectTimerKeys.gameFixedUpdateAlways, defaultInterval);
SetInterval(GameObjectTimerKeys.gameFixedUpdatePhysics, defaultInterval);
}
}
public void InitTimers(bool reset = false) {
if (timers == null || reset) {
timers = new Dictionary<string, GameObjectTimerData>();
}
}
public void SetInterval(string key, float increment) {
InitIntervals();
intervals.Set(key, increment);
}
public float GetInterval(string key) {
InitIntervals();
if (intervals.ContainsKey(key)) {
return intervals[key];
}
return defaultInterval;
}
public GameObjectTimerData SetTimer(string key, float delta = 0.033f, float modifier = 1f) {
InitTimers();
GameObjectTimerData obj = GetTimer(key, delta, modifier);
obj.key = key;
obj.delta = delta;
obj.last_time = Time.time;
obj.modifier = modifier;
timers.Set<string, GameObjectTimerData>(key, obj);
return obj;
}
public GameObjectTimerData GetTimer(string key, float delta = 0.033f, float modifier = 1f) {
InitTimers();
GameObjectTimerData obj = null;
if (timers.Has(key)) {
obj = timers.Get(key);
}
if (obj == null) {
obj = new GameObjectTimerData();
obj.key = key;
obj.delta = delta;
obj.last_time = Time.time;
obj.modifier = modifier;
timers.Set<string, GameObjectTimerData>(key, obj);
}
return obj;
}
public bool IsTimer(string key, float delta = .033f, float modifier = 1f) {
InitTimers();
GameObjectTimerData obj = GetTimer(key, delta, modifier);
if ((obj.last_time + (delta * modifier)) < Time.time) {
obj.last_time = Time.time;
timers.Set<GameObjectTimerData>(key, obj);
//Debug.Log("GameObjectTimer:" + key + " last_time:" + obj.last_time);
return true;
}
return false;
}
public bool IsTimerPerfRelative(string key, float delta = .033f) {
return IsTimer(key, delta, currentModifier);
}
public bool IsTimerPerfRelativeHalved(string key, float delta = .033f) {
return IsTimer(key, delta, currentModifier * 2);
}
public bool IsTimerPerf(string key, float modifier = 1f) {
return IsTimerPerfRelative(
key,
GetInterval(key) * modifier);
}
public bool IsFPSLessThan(float val) {
#if USE_GAME_LIB_GAMES
return FPSDisplay.IsFPSLessThan(val);
#else
return false;
#endif
}
public float GetFPSOffset(float desiredFPS = 30f) {
return desiredFPS / currentFPS;
}
public float currentFPS {
get {
#if USE_GAME_LIB_GAMES
return FPSDisplay.GetCurrentFPS();
#else
return 30;
#endif
}
}
public float currentModifier {
get {
return GetFPSOffset();
}
}
public float defaultInterval {
get {
return 1f / 30f;
}
}
public float defaultIntervalDouble {
get {
return 2f / 30f;
}
}
public float defaultIntervalQuarter {
get {
return 4f / 30f;
}
}
}