-
Notifications
You must be signed in to change notification settings - Fork 720
/
runtimescenetools.ts
361 lines (330 loc) · 11.2 KB
/
runtimescenetools.ts
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
namespace gdjs {
export namespace evtTools {
export namespace runtimeScene {
export const sceneJustBegins = function (
runtimeScene: gdjs.RuntimeScene
) {
return runtimeScene.getScene().getTimeManager().isFirstFrame();
};
export const sceneJustResumed = function (
runtimeScene: gdjs.RuntimeScene
) {
return runtimeScene.getScene().sceneJustResumed();
};
export const getSceneName = function (runtimeScene: gdjs.RuntimeScene) {
return runtimeScene.getScene().getName();
};
export const setBackgroundColor = function (
runtimeScene: gdjs.RuntimeScene,
rgbColor: string
) {
const colors = rgbColor.split(';');
if (colors.length < 3) {
return;
}
runtimeScene
.getScene()
.setBackgroundColor(
parseInt(colors[0]),
parseInt(colors[1]),
parseInt(colors[2])
);
};
export const getElapsedTimeInSeconds = function (
runtimeScene: gdjs.RuntimeScene
) {
return runtimeScene.getScene().getTimeManager().getElapsedTime() / 1000;
};
export const setTimeScale = function (
runtimeScene: gdjs.RuntimeScene,
timeScale: float
) {
return runtimeScene.getScene().getTimeManager().setTimeScale(timeScale);
};
export const getTimeScale = function (runtimeScene: gdjs.RuntimeScene) {
return runtimeScene.getScene().getTimeManager().getTimeScale();
};
/**
* Test a timer elapsed time, if the timer doesn't exist it is created.
*
* @deprecated prefer using getTimerElapsedTimeInSecondsOrNaN
*
* @param runtimeScene The scene owning the timer
* @param timeInSeconds The time value to check in seconds
* @param timerName The timer name
* @return True if the timer exists and its value is greater than or equal than the given time, false otherwise
*/
export const timerElapsedTime = function (
runtimeScene: gdjs.RuntimeScene,
timeInSeconds: float,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
timeManager.addTimer(timerName);
return false;
}
return (
timeManager.getTimer(timerName).getTime() / 1000 >= timeInSeconds
);
};
export const timerPaused = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
return false;
}
return timeManager.getTimer(timerName).isPaused();
};
export const resetTimer = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
timeManager.addTimer(timerName);
} else {
timeManager.getTimer(timerName).reset();
}
};
export const pauseTimer = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
timeManager.addTimer(timerName);
}
timeManager.getTimer(timerName).setPaused(true);
};
export const unpauseTimer = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
timeManager.addTimer(timerName);
}
return timeManager.getTimer(timerName).setPaused(false);
};
export const removeTimer = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
timeManager.removeTimer(timerName);
};
export class WaitTask extends gdjs.AsyncTask {
private duration: float;
private timeElapsedOnScene = 0;
constructor(durationInMilliseconds: float) {
super();
this.duration = durationInMilliseconds;
}
update(runtimeScene: RuntimeScene): boolean {
this.timeElapsedOnScene += runtimeScene
.getScene()
.getTimeManager()
.getElapsedTime();
return this.timeElapsedOnScene >= this.duration;
}
}
export const wait = (durationInSeconds: float): AsyncTask =>
new WaitTask(
durationInSeconds * 1000 /* Convert from seconds to milliseconds */
);
/**
* This is used by expressions to return 0 when a timer doesn't exist,
* because numeric expressions must always return a number.
*
* @param runtimeScene The scene owning the timer.
* @param timerName The timer name.
* @returns The timer elapsed time in seconds or 0 if the timer doesn't exist.
*/
export const getTimerElapsedTimeInSeconds = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
return 0;
}
return timeManager.getTimer(timerName).getTime() / 1000;
};
/**
* This is used by conditions to return false when a timer doesn't exist,
* no matter the relational operator.
*
* @param runtimeScene The scene owning the timer.
* @param timerName The timer name.
* @returns The timer elapsed time in seconds or NaN if the timer doesn't exist.
*/
export const getTimerElapsedTimeInSecondsOrNaN = function (
runtimeScene: gdjs.RuntimeScene,
timerName: string
) {
const timeManager = runtimeScene.getScene().getTimeManager();
if (!timeManager.hasTimer(timerName)) {
return Number.NaN;
}
return timeManager.getTimer(timerName).getTime() / 1000;
};
export const getTimeFromStartInSeconds = function (
runtimeScene: gdjs.RuntimeScene
) {
return (
runtimeScene.getScene().getTimeManager().getTimeFromStart() / 1000
);
};
export const getTime = function (
runtimeScene: gdjs.RuntimeScene,
what: string
) {
if (what === 'timestamp') {
return Date.now();
}
const now = new Date();
if (what === 'hour') {
return now.getHours();
} else if (what === 'min') {
return now.getMinutes();
} else if (what === 'sec') {
return now.getSeconds();
} else if (what === 'mday') {
return now.getDate();
} else if (what === 'mon') {
return now.getMonth();
} else if (what === 'year') {
//Conform to the C way of returning years.
return now.getFullYear() - 1900;
} else if (what === 'wday') {
return now.getDay();
} else if (what === 'yday') {
const start = new Date(now.getFullYear(), 0, 0);
const diff = now.getTime() - start.getTime();
const oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
return 0;
};
export const replaceScene = function (
runtimeScene: gdjs.RuntimeScene,
newSceneName: string,
clearOthers: boolean
) {
if (!runtimeScene.getGame().getSceneData(newSceneName)) {
return;
}
runtimeScene
.getScene()
.requestChange(
clearOthers
? gdjs.SceneChangeRequest.CLEAR_SCENES
: gdjs.SceneChangeRequest.REPLACE_SCENE,
newSceneName
);
};
export const pushScene = function (
runtimeScene: gdjs.RuntimeScene,
newSceneName: string
) {
if (!runtimeScene.getGame().getSceneData(newSceneName)) {
return;
}
runtimeScene
.getScene()
.requestChange(gdjs.SceneChangeRequest.PUSH_SCENE, newSceneName);
};
export const popScene = function (runtimeScene: gdjs.RuntimeScene) {
runtimeScene
.getScene()
.requestChange(gdjs.SceneChangeRequest.POP_SCENE);
};
export const stopGame = function (runtimeScene: gdjs.RuntimeScene) {
runtimeScene
.getScene()
.requestChange(gdjs.SceneChangeRequest.STOP_GAME);
};
export const createObjectsFromExternalLayout = function (
scene: gdjs.RuntimeInstanceContainer,
externalLayout: string,
xPos: float,
yPos: float,
zPos: float
) {
const externalLayoutData = scene
.getGame()
.getExternalLayoutData(externalLayout);
if (externalLayoutData === null) {
return;
}
// trackByPersistentUuid is set to false as we don't want external layouts
// instantiated at runtime to be hot-reloaded.
scene.getScene().createObjectsFrom(
externalLayoutData.instances,
xPos,
yPos,
/**
* When 3D was introduced, zPos argument was added to the signature.
* Existing calls (in JS events) to createObjectsFromExternalLayout will
* have zPos undefined. So it is set to 0 in that case.
*/
zPos || 0,
/*trackByPersistentUuid=*/
false
);
};
/**
* Check if the game has just resumed from being hidden
*/
export const hasGameJustResumed = (
instanceContainer: gdjs.RuntimeInstanceContainer
): boolean => {
return instanceContainer.getGame().hasJustResumed();
};
/**
* Check if a scene exists.
*/
export const doesSceneExist = (
runtimeScene: gdjs.RuntimeScene,
sceneName: string
): boolean => {
return runtimeScene.getGame().hasScene(sceneName);
};
/**
* Preload a scene assets as soon as possible in background.
*/
export const prioritizeLoadingOfScene = (
runtimeScene: gdjs.RuntimeScene,
sceneName: string
): void => {
runtimeScene.getGame().prioritizeLoadingOfScene(sceneName);
};
/**
* @return The progress of assets loading in background for a scene (between 0 and 1).
*/
export const getSceneLoadingProgress = (
runtimeScene: gdjs.RuntimeScene,
sceneName: string
): float => {
return runtimeScene.getGame().getSceneLoadingProgress(sceneName);
};
/**
* Check if scene assets have finished to load in background.
*/
export const areSceneAssetsLoaded = (
runtimeScene: gdjs.RuntimeScene,
sceneName: string
): boolean => {
return runtimeScene.getGame().areSceneAssetsLoaded(sceneName);
};
}
}
}