-
Notifications
You must be signed in to change notification settings - Fork 0
/
gd.js
424 lines (381 loc) · 12.6 KB
/
gd.js
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// @ts-check
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
/**
* The `gdjs` namespace contains all classes and objects of the game engine.
* @namespace gdjs
*/
// @ts-ignore - creating the global object acting as a namespace
window.gdjs = {};
/**
* Contains functions used by events (this is a convention only, functions can actually
* be anywhere).
* @namespace
* @memberOf gdjs
*/
gdjs.evtTools = {};
gdjs.objectsTypes = new Hashtable();
gdjs.behaviorsTypes = new Hashtable();
/** @type {Function[]} */ gdjs.callbacksFirstRuntimeSceneLoaded = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeSceneLoaded = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeScenePreEvents = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeScenePostEvents = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeScenePaused = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeSceneResumed = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeSceneUnloading = [];
/** @type {Function[]} */ gdjs.callbacksRuntimeSceneUnloaded = [];
/** @type {Function[]} */ gdjs.callbacksObjectDeletedFromScene = [];
/**
* Convert a rgb color value to a hex string.
*
* No "#" or "0x" are added.
* @param {number} r Red
* @param {number} g Green
* @param {number} b Blue
* @returns {string}
*/
gdjs.rgbToHex = function (r, g, b) {
return '' + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};
/**
* Convert a rgb color value to a hex value.
* @param {number} r Red
* @param {number} g Green
* @param {number} b Blue
* @returns {number}
*/
gdjs.rgbToHexNumber = function (r, g, b) {
return (r << 16) + (g << 8) + b;
};
/**
* Get a random integer between 0 and max.
* @param {number} max The maximum value (inclusive).
* @returns {number}
*/
gdjs.random = function (max) {
if (max <= 0) return 0;
return Math.floor(Math.random() * (max + 1));
};
/**
* Get a random integer between min and max.
* @param {number} min The minimum value (inclusive).
* @param {number} max The maximum value (inclusive).
* @returns {number}
*/
gdjs.randomInRange = function (min, max) {
return min + gdjs.random(max - min); // return min if min >= max
};
/**
* Get a random float in the range 0 to less than max (inclusive of 0, but not max).
* @param {number} max The maximum value (exclusive).
* @returns {number}
*/
gdjs.randomFloat = function (max) {
if (max <= 0) return 0;
return Math.random() * max;
};
/**
* Get a random float between min and max
* @param {number} min The minimum value (inclusive).
* @param {number} max The maximum value (exclusive).
* @returns {number}
*/
gdjs.randomFloatInRange = function (min, max) {
return min + gdjs.randomFloat(max - min); // return min if min >= max
};
/**
* Get a random number between min and max in steps
* @param {number} min The minimum value (inclusive).
* @param {number} max The maximum value (inclusive).
* @param {number} step The interval between each value.
* @returns {number}
*/
gdjs.randomWithStep = function (min, max, step) {
if (step <= 0) return min + gdjs.random(max - min);
return min + gdjs.random(Math.floor((max - min) / step)) * step; // return min if min >= max
};
/**
* Convert an angle in degrees to radians.
* @param {number} angleInDegrees The angle in degrees.
* @returns {number}
*/
gdjs.toRad = function (angleInDegrees) {
return (angleInDegrees / 180) * 3.14159;
};
/**
* Convert an angle in radians to degrees.
* @param {number} angleInRadians The angle in radians.
* @returns {number}
*/
gdjs.toDegrees = function (angleInRadians) {
return (angleInRadians * 180) / 3.14159;
};
/**
* Register a runtime object (class extending {@link gdjs.RuntimeObject}) that can be used in a scene.
*
* The name of the type of the object must be complete, with the namespace if any. For
* example, if you are providing a Text object in the TextObject extension, the full name
* of the type of the object is "TextObject::Text".
*
* @param {string} objectTypeName The name of the type of the Object.
* @param {typeof gdjs.RuntimeObject} Ctor The constructor of the Object.
*/
gdjs.registerObject = function (objectTypeName, Ctor) {
gdjs.objectsTypes.put(objectTypeName, Ctor);
};
/**
* Register a runtime behavior (class extending {@link gdjs.RuntimeBehavior}) that can be used by a
* {@link gdjs.RuntimeObject}.
*
* The type of the behavior must be complete, with the namespace of the extension. For
* example, if you are providing a Draggable behavior in the DraggableBehavior extension,
* the full name of the type of the behavior is "DraggableBehavior::Draggable".
*
* @param {string} behaviorTypeName The name of the type of the behavior.
* @param {typeof gdjs.RuntimeBehavior} Ctor The constructor of the Object.
*/
gdjs.registerBehavior = function (behaviorTypeName, Ctor) {
gdjs.behaviorsTypes.put(behaviorTypeName, Ctor);
};
/**
* Register a function to be called when the first {@link gdjs.RuntimeScene} is loaded, after
* resources loading is done. This can be considered as the "start of the game".
*
* @param {Function} callback The function to be called.
*/
gdjs.registerFirstRuntimeSceneLoadedCallback = function (callback) {
gdjs.callbacksFirstRuntimeSceneLoaded.push(callback);
};
/**
* Register a function to be called when a scene is loaded.
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeSceneLoadedCallback = function (callback) {
gdjs.callbacksRuntimeSceneLoaded.push(callback);
};
/**
* Register a function to be called each time a scene is stepped (i.e: at every frame),
* before events are run.
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeScenePreEventsCallback = function (callback) {
gdjs.callbacksRuntimeScenePreEvents.push(callback);
};
/**
* Register a function to be called each time a scene is stepped (i.e: at every frame),
* after events are run and before rendering.
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeScenePostEventsCallback = function (callback) {
gdjs.callbacksRuntimeScenePostEvents.push(callback);
};
/**
* Register a function to be called when a scene is paused.
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeScenePausedCallback = function (callback) {
gdjs.callbacksRuntimeScenePaused.push(callback);
};
/**
* Register a function to be called when a scene is resumed.
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeSceneResumedCallback = function (callback) {
gdjs.callbacksRuntimeSceneResumed.push(callback);
};
/**
* Register a function to be called when a scene unload started. This is
* before the object deletion and renderer destruction. It is safe to
* manipulate these. It is **not** be safe to release resources as other
* callbacks might do operations on objects or the scene.
*
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeSceneUnloadingCallback = function (callback) {
gdjs.callbacksRuntimeSceneUnloading.push(callback);
};
/**
* Register a function to be called when a scene is unloaded. The objects
* and renderer are now destroyed - it is **not** safe to do anything apart
* from releasing resources.
*
* @param {Function} callback The function to be called.
*/
gdjs.registerRuntimeSceneUnloadedCallback = function (callback) {
gdjs.callbacksRuntimeSceneUnloaded.push(callback);
};
/**
* Register a function to be called when an object is deleted from a scene.
* @param {Function} callback The function to be called.
*/
gdjs.registerObjectDeletedFromSceneCallback = function (callback) {
gdjs.callbacksObjectDeletedFromScene.push(callback);
};
/**
* Keep this function until we're sure now client is using it anymore.
* @deprecated
* @private
*/
gdjs.registerGlobalCallbacks = function () {
console.warn(
"You're calling gdjs.registerGlobalCallbacks. This method is now useless and you must not call it anymore."
);
};
/**
* Remove all the global callbacks that were registered previously.
*
* Should only be used for testing - this should never be used at runtime.
*/
gdjs.clearGlobalCallbacks = function () {
gdjs.callbacksFirstRuntimeSceneLoaded = [];
gdjs.callbacksRuntimeSceneLoaded = [];
gdjs.callbacksRuntimeScenePreEvents = [];
gdjs.callbacksRuntimeScenePostEvents = [];
gdjs.callbacksRuntimeScenePaused = [];
gdjs.callbacksRuntimeSceneResumed = [];
gdjs.callbacksRuntimeSceneUnloading = [];
gdjs.callbacksRuntimeSceneUnloaded = [];
gdjs.callbacksObjectDeletedFromScene = [];
};
/**
* Get the constructor of an object.
*
* @param {string} name The name of the type of the object.
* @returns {typeof gdjs.RuntimeObject}
*/
gdjs.getObjectConstructor = function (name) {
if (name !== undefined && gdjs.objectsTypes.containsKey(name))
return gdjs.objectsTypes.get(name);
console.warn('Object type "' + name + '" was not found.');
return gdjs.objectsTypes.get(''); //Create a base empty runtime object.
};
/**
* Get the constructor of a behavior.
*
* @param {string} name The name of the type of the behavior.
* @returns {typeof gdjs.RuntimeBehavior}
*/
gdjs.getBehaviorConstructor = function (name) {
if (name !== undefined && gdjs.behaviorsTypes.containsKey(name))
return gdjs.behaviorsTypes.get(name);
console.warn('Behavior type "' + name + '" was not found.');
return gdjs.behaviorsTypes.get(''); //Create a base empty runtime behavior.
};
/**
* Create a static array that won't need a new allocation each time it's used.
* @param {any} owner The owner of the Array.
* @returns {Array<any>}
*/
gdjs.staticArray = function (owner) {
owner._staticArray = owner._staticArray || [];
return owner._staticArray;
};
/**
* Create a second static array that won't need a new allocation each time it's used.
* @param {any} owner The owner of the Array.
* @returns {Array<any>}
*/
gdjs.staticArray2 = function (owner) {
owner._staticArray2 = owner._staticArray2 || [];
return owner._staticArray2;
};
/**
* Create a static object that won't need a new allocation each time it's used.
* @param {any} owner The owner of the Array.
* @returns {Object}
*/
gdjs.staticObject = function (owner) {
owner._staticObject = owner._staticObject || {};
return owner._staticObject;
};
/**
* Return a new array of objects that is the concatenation of all the objects passed
* as parameters.
* @param objectsLists
* @returns {Array}
*/
gdjs.objectsListsToArray = function (objectsLists) {
var lists = gdjs.staticArray(gdjs.objectsListsToArray);
objectsLists.values(lists);
var result = [];
for (var i = 0; i < lists.length; ++i) {
var arr = lists[i];
for (var k = 0; k < arr.length; ++k) {
result.push(arr[k]);
}
}
return result;
};
/**
* Copy the element for the first array into the second array, so that
* both array contains the same elements.
* @param {Array<any>} src The source array
* @param {Array<any>} dst The destination array
*/
gdjs.copyArray = function (src, dst) {
var len = src.length;
for (var i = 0; i < len; ++i) {
dst[i] = src[i];
}
dst.length = len;
};
/**
* Generate a UUID v4.
* @returns {string} The generated UUID.
*/
gdjs.makeUuid = function () {
// Fallback to non cryptographically secure UUIDs if not supported
if (typeof crypto === 'undefined' || !crypto.getRandomValues) {
const makeMathRandomUuid = (a) => {
return a
? (a ^ ((Math.random() * 16) >> (a / 4))).toString(16)
: (""+ 1e7 + -1e3 + -4e3 + -8e3 + -1e11).replace(
/[018]/g,
makeMathRandomUuid
);
};
return makeMathRandomUuid();
}
// @ts-ignore - TS does not like properties added on functions
if (!gdjs.makeUuid.hex) {
// @ts-ignore - TS does not like properties added on functions
gdjs.makeUuid.hex = [];
for (var i = 0; i < 256; i++) {
// @ts-ignore - TS does not like properties added on functions
gdjs.makeUuid.hex[i] = (i < 16 ? '0' : '') + i.toString(16);
}
}
// @ts-ignore - TS does not like properties added on functions
const hex = gdjs.makeUuid.hex;
var r = crypto.getRandomValues(new Uint8Array(16));
r[6] = (r[6] & 0x0f) | 0x40;
r[8] = (r[8] & 0x3f) | 0x80;
return (
hex[r[0]] +
hex[r[1]] +
hex[r[2]] +
hex[r[3]] +
'-' +
hex[r[4]] +
hex[r[5]] +
'-' +
hex[r[6]] +
hex[r[7]] +
'-' +
hex[r[8]] +
hex[r[9]] +
'-' +
hex[r[10]] +
hex[r[11]] +
hex[r[12]] +
hex[r[13]] +
hex[r[14]] +
hex[r[15]]
);
};
//Make sure console.warn and console.error are available.
console.warn = console.warn || console.log;
console.error = console.error || console.log;