-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathRuntimeInstanceContainer.ts
More file actions
905 lines (817 loc) 路 30.8 KB
/
Copy pathRuntimeInstanceContainer.ts
File metadata and controls
905 lines (817 loc) 路 30.8 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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
* 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 {
const logger = new gdjs.Logger('RuntimeInstanceContainer');
const unknownObjectData = {
name: '',
type: '',
variables: [],
behaviors: [],
effects: [],
content: {
width: 32,
height: 32,
depth: 32,
},
};
/**
* A container of object instances rendered on screen.
* @category Core Engine > Instance Container
*/
export abstract class RuntimeInstanceContainer {
_initialBehaviorSharedData: Hashtable<BehaviorSharedData | null>;
/** Contains the instances living on the container */
_instances: Hashtable<RuntimeObject[]>;
/**
* An array used to create a list of all instance when necessary.
* @see gdjs.RuntimeInstanceContainer#_constructListOfAllInstances}
*/
private _allInstancesList: gdjs.RuntimeObject[] = [];
_allInstancesListIsUpToDate = true;
/** Used to recycle destroyed instance instead of creating new ones. */
_instancesCache: Hashtable<RuntimeObject[]>;
/** The instances removed from the container and waiting to be sent to the cache. */
_instancesRemoved: gdjs.RuntimeObject[] = [];
/** Contains the objects data stored in the project */
_objects: Hashtable<ObjectData>;
_objectsCtor: Hashtable<typeof RuntimeObject>;
_layers: Hashtable<RuntimeLayer>;
_orderedLayers: RuntimeLayer[]; // TODO: should this be a single structure with _layers, to enforce its usage?
_layersCameraCoordinates: Record<string, [float, float, float, float]> = {};
// Options for the debug draw:
_debugDrawEnabled: boolean = false;
_debugDrawShowHiddenInstances: boolean = false;
_debugDrawShowPointsNames: boolean = false;
_debugDrawShowCustomPoints: boolean = false;
_debugDraw3DEnabled: boolean = false;
_debugDraw3DColorHex: integer = 0x00ff00;
_debugDraw3DDepthTest: boolean = true;
_onceTriggers: OnceTriggers;
/**
* @param runtimeGame The game associated to this scene.
*/
constructor(runtimeGame: gdjs.RuntimeGame) {
this._initialBehaviorSharedData = new Hashtable();
this._instances = new Hashtable();
this._instancesCache = new Hashtable();
this._objects = new Hashtable();
this._objectsCtor = new Hashtable();
this._layers = new Hashtable();
this._orderedLayers = [];
if (runtimeGame.isInGameEdition()) {
// Register an UnknownRuntimeObject to use when the object doesn't exist.
this.registerObject(unknownObjectData);
}
this._onceTriggers = new gdjs.OnceTriggers();
}
/**
* Return the time elapsed since the last frame,
* in milliseconds, for objects on the layer.
*/
abstract getElapsedTime(): float;
/**
* Get the renderer associated to the container.
*/
abstract getRenderer(): gdjs.RuntimeInstanceContainerRenderer;
/**
* Get the renderer for visual debugging associated to the container.
*/
abstract getDebuggerRenderer(): gdjs.DebuggerRenderer;
/**
* Get the {@link gdjs.RuntimeGame} associated to this.
*/
abstract getGame(): gdjs.RuntimeGame;
/**
* Get the {@link gdjs.RuntimeScene} associated to this.
*/
abstract getScene(): gdjs.RuntimeScene;
abstract getAsyncTasksManager(): gdjs.AsyncTasksManager;
/**
* Convert a point from the canvas coordinates (for example,
* the mouse position) to the container coordinates.
*
* @param x The x position, in container coordinates.
* @param y The y position, in container coordinates.
* @param result The point instance that is used to return the result.
*/
abstract convertCoords(x: float, y: float, result?: FloatPoint): FloatPoint;
/**
* Convert a point from the container coordinates (for example,
* an object position) to the canvas coordinates.
*
* @param sceneX The x position, in container coordinates.
* @param sceneY The y position, in container coordinates.
* @param result The point instance that is used to return the result.
*/
abstract convertInverseCoords(
sceneX: float,
sceneY: float,
result: FloatPoint
): FloatPoint;
/**
* @return the left bound of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getUnrotatedViewportMinX(): float;
/**
* @return the top bound of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getUnrotatedViewportMinY(): float;
/**
* @return the right bound of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getUnrotatedViewportMaxX(): float;
/**
* @return the bottom bound of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getUnrotatedViewportMaxY(): float;
/**
* @return the left bound of:
* - the initial game resolution for a {@link gdjs.RuntimeScene}
* - the initial default dimensions (inner area) set in the editor for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getInitialUnrotatedViewportMinX(): float;
/**
* @return the top bound of:
* - the initial game resolution for a {@link gdjs.RuntimeScene}
* - the initial default dimensions (inner area) set in the editor for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getInitialUnrotatedViewportMinY(): float;
/**
* @return the right bound of:
* - the initial game resolution for a {@link gdjs.RuntimeScene}
* - the initial default dimensions (inner area) set in the editor for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getInitialUnrotatedViewportMaxX(): float;
/**
* @return the bottom bound of:
* - the initial game resolution for a {@link gdjs.RuntimeScene}
* - the initial default dimensions (inner area) set in the editor for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getInitialUnrotatedViewportMaxY(): float;
/**
* @return the width of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getViewportWidth(): float;
/**
* @return the height of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getViewportHeight(): float;
/**
* @return the center X of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getViewportOriginX(): float;
/**
* @return the center Y of:
* - the game resolution for a {@link gdjs.RuntimeScene}
* - the default dimensions (the AABB of all its children) for a
* {@link gdjs.CustomRuntimeObject}.
*/
abstract getViewportOriginY(): float;
/**
* Triggered when the AABB of a child of the container could have changed.
*/
abstract onChildrenLocationChanged(): void;
/**
* Activate or deactivate the debug visualization for collisions and points.
*/
enableDebugDraw(
enableDebugDraw: boolean,
showHiddenInstances: boolean,
showPointsNames: boolean,
showCustomPoints: boolean
): void {
if (this._debugDrawEnabled && !enableDebugDraw) {
this.getDebuggerRenderer().clearDebugDraw();
}
this._debugDrawEnabled = enableDebugDraw;
this._debugDrawShowHiddenInstances = showHiddenInstances;
this._debugDrawShowPointsNames = showPointsNames;
this._debugDrawShowCustomPoints = showCustomPoints;
}
/**
* Activate or deactivate the 3D debug visualization for collision shapes
* of objects using the built-in 3D physics behavior.
*/
enableDebugDraw3D(
enableDebugDraw: boolean,
colorHex: integer,
depthTest: boolean
): void {
const settingsChanged =
this._debugDraw3DColorHex !== colorHex ||
this._debugDraw3DDepthTest !== depthTest;
if (
this._debugDraw3DEnabled &&
(!enableDebugDraw || settingsChanged)
) {
this.getDebuggerRenderer().clearDebugDraw3D(
this.getAdhocListOfAllInstances()
);
}
this._debugDraw3DEnabled = enableDebugDraw;
this._debugDraw3DColorHex = colorHex;
this._debugDraw3DDepthTest = depthTest;
}
/**
* Check if an object is registered, meaning that instances of it can be
* created and lives in the container.
* @see gdjs.RuntimeInstanceContainer#registerObject
*/
isObjectRegistered(objectName: string): boolean {
return (
this._objects.containsKey(objectName) &&
this._instances.containsKey(objectName) &&
this._objectsCtor.containsKey(objectName)
);
}
/**
* Register a {@link gdjs.RuntimeObject} so that instances of it can be
* used in the container.
* @param objectData The data for the object to register.
*/
registerObject(objectData: ObjectData) {
this._objects.put(objectData.name, objectData);
this._instances.put(objectData.name, []);
// Cache the constructor
const Ctor = gdjs.getObjectConstructor(objectData.type);
this._objectsCtor.put(objectData.name, Ctor);
// Also prepare a cache for recycled instances, if the object supports it.
if (Ctor.supportsReinitialization) {
this._instancesCache.put(objectData.name, []);
}
}
/**
* Update the data of a {@link gdjs.RuntimeObject} so that instances use
* this when constructed.
* @param objectData The data for the object to register.
*/
updateObject(objectData: ObjectData): void {
if (!this.isObjectRegistered(objectData.name)) {
logger.warn(
'Tried to call updateObject for an object that was not registered (' +
objectData.name +
'). Call registerObject first.'
);
}
this._objects.put(objectData.name, objectData);
}
// Don't erase instances, nor instances cache, or objectsCtor cache.
/**
* Unregister a {@link gdjs.RuntimeObject}. Instances will be destroyed.
* @param objectName The name of the object to unregister.
*/
unregisterObject(objectName: string) {
const instances = this._instances.get(objectName);
if (instances) {
// This is sub-optimal: markObjectForDeletion will search the instance to
// remove in instances, so cost is O(n^2), n being the number of instances.
// As we're unregistering an object which only happen during a hot-reloading,
// this is fine.
const instancesToRemove = instances.slice();
for (let i = 0; i < instancesToRemove.length; i++) {
this.markObjectForDeletion(instancesToRemove[i]);
}
this._cacheOrClearRemovedInstances();
}
this._objects.remove(objectName);
this._instances.remove(objectName);
this._instancesCache.remove(objectName);
this._objectsCtor.remove(objectName);
}
/**
* Create objects from initial instances data (for example, the initial instances
* of the scene or the instances of an external layout).
*
* @param data The instances data
* @param xPos The offset on X axis
* @param yPos The offset on Y axis
* @param zPos The offset on Z axis
* @param trackByPersistentUuid If true, objects are tracked by setting their `persistentUuid`
* to the same as the associated instance. Useful for hot-reloading when instances are changed.
*/
createObjectsFrom(
data: InstanceData[],
xPos: float,
yPos: float,
zPos: float,
trackByPersistentUuid: boolean,
options?: {
excludedObjectNames?: Set<string> | null;
}
): void {
let zOffset: number = zPos;
let shouldTrackByPersistentUuid: boolean = trackByPersistentUuid;
if (arguments.length <= 4) {
/**
* Support for the previous signature (before 3D was introduced):
* createObjectsFrom(data, xPos, yPos, trackByPersistentUuid)
*/
zOffset = 0;
shouldTrackByPersistentUuid = arguments[3];
}
for (let i = 0, len = data.length; i < len; ++i) {
const instanceData = data[i];
const objectName = instanceData.name;
if (options?.excludedObjectNames?.has(objectName)) {
continue;
}
const newObject = this.createObject(objectName, instanceData);
if (newObject !== null) {
if (shouldTrackByPersistentUuid) {
// Give the object the same persistentUuid as the instance, so that
// it can be hot-reloaded.
newObject.persistentUuid = instanceData.persistentUuid || null;
}
newObject.setPosition(instanceData.x + xPos, instanceData.y + yPos);
newObject.setAngle(instanceData.angle);
if (gdjs.Base3DHandler && gdjs.Base3DHandler.is3D(newObject)) {
newObject.setZ((instanceData.z || 0) + zOffset);
if (instanceData.rotationX !== undefined)
newObject.setRotationX(instanceData.rotationX);
if (instanceData.rotationY !== undefined)
newObject.setRotationY(instanceData.rotationY);
}
newObject.setZOrder(instanceData.zOrder);
newObject.setLayer(instanceData.layer);
newObject
.getVariables()
.initFrom(instanceData.initialVariables, true);
newObject.extraInitializationFromInitialInstance(instanceData);
}
}
}
/**
* Get the data representing the initial shared data of the scene for the specified behavior.
* @param name The name of the behavior
* @returns The shared data for the behavior, if any.
*/
getInitialSharedDataForBehavior(name: string): BehaviorSharedData | null {
return this._initialBehaviorSharedData.get(name);
}
/**
* Set the data representing the initial shared data of the scene for the specified behavior.
* @param name The name of the behavior
* @param sharedData The shared data for the behavior, or null to remove it.
*/
setInitialSharedDataForBehavior(
name: string,
sharedData: BehaviorSharedData | null
): void {
this._initialBehaviorSharedData.put(name, sharedData);
}
/**
* Set the default Z order for each layer, which is the highest Z order found on each layer.
* Useful as it ensures that instances created from events are, by default, shown in front
* of other instances.
*/
_setLayerDefaultZOrders() {
if (
this.getGame().getGameData().properties.useDeprecatedZeroAsDefaultZOrder
) {
// Deprecated option to still support games that were made considered 0 as the
// default Z order for all layers.
return;
}
const layerHighestZOrders: Record<string, number> = {};
const allInstances = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstances.length; i < len; ++i) {
const object = allInstances[i];
let layerName = object.getLayer();
const zOrder = object.getZOrder();
if (
layerHighestZOrders[layerName] === undefined ||
layerHighestZOrders[layerName] < zOrder
) {
layerHighestZOrders[layerName] = zOrder;
}
}
for (let layerName in layerHighestZOrders) {
this.getLayer(layerName).setDefaultZOrder(
layerHighestZOrders[layerName] + 1
);
}
}
_updateLayersCameraCoordinates(scale: float) {
this._layersCameraCoordinates = this._layersCameraCoordinates || {};
for (const name in this._layers.items) {
if (this._layers.items.hasOwnProperty(name)) {
const theLayer = this._layers.items[name];
this._layersCameraCoordinates[name] = this._layersCameraCoordinates[
name
] || [0, 0, 0, 0];
this._layersCameraCoordinates[name][0] =
theLayer.getCameraX() - (theLayer.getCameraWidth() / 2) * scale;
this._layersCameraCoordinates[name][1] =
theLayer.getCameraY() - (theLayer.getCameraHeight() / 2) * scale;
this._layersCameraCoordinates[name][2] =
theLayer.getCameraX() + (theLayer.getCameraWidth() / 2) * scale;
this._layersCameraCoordinates[name][3] =
theLayer.getCameraY() + (theLayer.getCameraHeight() / 2) * scale;
}
}
}
/**
* Called to update effects of layers before rendering.
*/
_updateLayersPreRender() {
for (const layer of this._orderedLayers) {
layer.updatePreRender(this);
}
}
/**
* Called to update visibility of the renderers of objects
* rendered on the scene ("culling"), update effects (of visible objects)
* and give a last chance for objects to update before rendering.
*
* Visibility is set to false if object is hidden, or if
* object is too far from the camera of its layer ("culling").
*/
_updateObjectsPreRender() {
const allInstancesList = this.getAdhocListOfAllInstances();
// TODO (3D) culling - add support for 3D object culling?
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const object = allInstancesList[i];
const rendererObject = object.getRendererObject();
if (rendererObject) {
rendererObject.visible = !object.isHidden();
// Update effects, only for visible objects.
if (rendererObject.visible) {
this.getGame()
.getEffectsManager()
.updatePreRender(object.getRendererEffects(), object);
}
}
// Perform pre-render update.
object.updatePreRender(this);
}
return;
}
/**
* Empty the list of the removed objects:
*
* When an object is removed from the container, it is still kept in
* {@link gdjs.RuntimeInstanceContainer#_instancesRemoved}.
*
* This method should be called regularly (after events or behaviors steps) so as to clear this list
* and allows the removed objects to be cached (or destroyed if the cache is full).
*
* The removed objects could not be sent directly to the cache, as events may still be using them after
* removing them from the scene for example.
*/
_cacheOrClearRemovedInstances() {
for (let k = 0, lenk = this._instancesRemoved.length; k < lenk; ++k) {
const instance = this._instancesRemoved[k];
// Cache the instance to recycle it into a new instance later.
// If the object does not support recycling, the cache won't be defined.
const cache = this._instancesCache.get(instance.getName());
if (cache) {
if (cache.length < 128) {
cache.push(instance);
}
}
instance.onDestroyed();
}
this._instancesRemoved.length = 0;
}
/**
* Tool function filling _allInstancesList member with all the living object instances.
*/
private _constructListOfAllInstances() {
let currentListSize = 0;
for (const name in this._instances.items) {
if (this._instances.items.hasOwnProperty(name)) {
const list = this._instances.items[name];
const oldSize = currentListSize;
currentListSize += list.length;
for (let j = 0, lenj = list.length; j < lenj; ++j) {
if (oldSize + j < this._allInstancesList.length) {
this._allInstancesList[oldSize + j] = list[j];
} else {
this._allInstancesList.push(list[j]);
}
}
}
}
this._allInstancesList.length = currentListSize;
this._allInstancesListIsUpToDate = true;
}
/**
* @param objectName The name of the object
* @returns the instances of a given object in the container.
*/
getInstancesOf(objectName: string): gdjs.RuntimeObject[] {
return this._instances.items[objectName];
}
/**
* Get a list of all {@link gdjs.RuntimeObject} living in the container.
* You should not, normally, need this method at all. It's only to be used
* in exceptional use cases where you need to loop through all objects,
* and it won't be performant.
*
* @returns The list of all runtime objects in the container
*/
getAdhocListOfAllInstances(): gdjs.RuntimeObject[] {
if (!this._allInstancesListIsUpToDate) {
this._constructListOfAllInstances();
}
return this._allInstancesList;
}
/**
* Update the objects before launching the events.
*/
_updateObjectsPreEvents() {
// It is *mandatory* to create and iterate on a external list of all objects, as the behaviors
// may delete the objects.
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const obj = allInstancesList[i];
const elapsedTime = obj.getElapsedTime();
if (!obj.hasNoForces()) {
const averageForce = obj.getAverageForce();
const elapsedTimeInSeconds = elapsedTime / 1000;
obj.setX(obj.getX() + averageForce.getX() * elapsedTimeInSeconds);
obj.setY(obj.getY() + averageForce.getY() * elapsedTimeInSeconds);
obj.update(this);
obj.updateForces(elapsedTimeInSeconds);
} else {
obj.update(this);
}
obj.updateTimers(elapsedTime);
allInstancesList[i].stepBehaviorsPreEvents(this);
}
// Some behaviors may have request objects to be deleted.
this._cacheOrClearRemovedInstances();
}
_updateObjectsForInGameEditor() {
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
const obj = allInstancesList[i];
obj.update(this);
}
}
/**
* Call each behavior stepPostEvents method.
*/
_stepBehaviorsPostEvents() {
this._cacheOrClearRemovedInstances();
// It is *mandatory* to create and iterate on a external list of all objects, as the behaviors
// may delete the objects.
const allInstancesList = this.getAdhocListOfAllInstances();
for (let i = 0, len = allInstancesList.length; i < len; ++i) {
allInstancesList[i].stepBehaviorsPostEvents(this);
}
// Some behaviors may have request objects to be deleted.
this._cacheOrClearRemovedInstances();
}
/**
* Add an object to the instances living in the container.
* @param obj The object to be added.
*/
addObject(obj: gdjs.RuntimeObject) {
if (!this._instances.containsKey(obj.name)) {
this._instances.put(obj.name, []);
}
this._instances.get(obj.name).push(obj);
this._allInstancesListIsUpToDate = false;
}
/**
* Get all the instances of the object called name.
* @param name Name of the object for which the instances must be returned.
* @return The list of objects with the given name
*/
getObjects(name: string): gdjs.RuntimeObject[] {
if (!this._instances.containsKey(name)) {
logger.info(
'RuntimeInstanceContainer.getObjects: No instances called "' +
name +
'"! Adding it.'
);
this._instances.put(name, []);
}
return this._instances.get(name);
}
/**
* Create a new object from its name. The object is also added to the instances
* living in the container (No need to call {@link addObject})
* @param objectName The name of the object to be created
* @return The created object
*/
createObject(
objectName: string,
instanceData?: InstanceData
): gdjs.RuntimeObject | null {
if (
!this._objectsCtor.containsKey(objectName) ||
!this._objects.containsKey(objectName)
) {
if (this.getGame().isInGameEdition()) {
logger.error(
`Object "${objectName}" not found - creating a placeholder object as a fallback.`
);
// Fallback on the UnknownRuntimeObject.
objectName = '';
} else {
// There is no such object in this container.
return null;
}
}
const objectData = this._objects.get(objectName);
// Create a new object using the object constructor (cached during loading)
// and the stored object's data:
const cache = this._instancesCache.get(objectName);
const ctor = this._objectsCtor.get(objectName);
let obj;
if (!cache || cache.length === 0) {
obj = new ctor(this, objectData, instanceData);
} else {
// Reuse an objet destroyed before. If there is an object in the cache,
// then it means it does support reinitialization.
obj = cache.pop();
obj.reinitialize(objectData);
}
this.addObject(obj);
return obj;
}
/**
* Must be called whenever an object must be removed from the container.
* @param obj The object to be removed.
*/
markObjectForDeletion(obj: gdjs.RuntimeObject) {
// Add to the objects removed list.
// The objects will be sent to the instances cache or really deleted from memory later.
if (this._instancesRemoved.indexOf(obj) === -1) {
this._instancesRemoved.push(obj);
}
// Delete from the living instances.
if (this._instances.containsKey(obj.getName())) {
const objId = obj.id;
const allInstances = this._instances.get(obj.getName());
for (let i = 0, len = allInstances.length; i < len; ++i) {
if (allInstances[i].id == objId) {
allInstances.splice(i, 1);
this._allInstancesListIsUpToDate = false;
break;
}
}
}
// Notify the object it was removed from the container
obj.onDeletedFromScene();
// Notify the global callbacks
for (let j = 0; j < gdjs.callbacksObjectDeletedFromScene.length; ++j) {
gdjs.callbacksObjectDeletedFromScene[j](this, obj);
}
return;
}
/**
* Get the layer with the given name
* @param name The name of the layer
* @returns The layer, or the base layer if not found
*/
getLayer(name: string): gdjs.RuntimeLayer {
if (this._layers.containsKey(name)) {
return this._layers.get(name);
}
return this._layers.get('');
}
/**
* Check if a layer exists
* @param name The name of the layer
*/
hasLayer(name: string): boolean {
return this._layers.containsKey(name);
}
/**
* Add a layer.
* @param layerData The data to construct the layer
*/
abstract addLayer(layerData: LayerData);
/**
* Remove a layer. All {@link gdjs.RuntimeObject} on this layer will
* be moved back to the base layer.
* @param layerName The name of the layer to remove
*/
removeLayer(layerName: string) {
const existingLayer = this._layers.get(layerName);
if (!existingLayer) return;
const allInstances = this.getAdhocListOfAllInstances();
for (let i = 0; i < allInstances.length; ++i) {
const runtimeObject = allInstances[i];
if (runtimeObject.getLayer() === layerName) {
runtimeObject.setLayer('');
}
}
this._layers.remove(layerName);
const layerIndex = this._orderedLayers.indexOf(existingLayer);
this._orderedLayers.splice(layerIndex, 1);
}
/**
* Change the position of a layer.
*
* @param layerName The name of the layer to reorder
* @param newIndex The new position in the list of layers
*/
setLayerIndex(layerName: string, newIndex: integer): void {
const layer: gdjs.RuntimeLayer = this._layers.get(layerName);
if (!layer) {
return;
}
const layerIndex = this._orderedLayers.indexOf(layer);
if (layerIndex === newIndex) return;
this._orderedLayers.splice(layerIndex, 1);
this._orderedLayers.splice(newIndex, 0, layer);
this.getRenderer().setLayerIndex(layer, newIndex);
}
/**
* Fill the array passed as argument with the names of all layers
* @param result The array where to put the layer names
*/
getAllLayerNames(result: string[]) {
this._layers.keys(result);
}
/**
* Return the number of instances of the specified object living in the container.
* @param objectName The object name for which instances must be counted.
*/
getInstancesCountOnScene(objectName: string): integer {
const instances = this._instances.get(objectName);
if (instances) {
return instances.length;
}
return 0;
}
/**
* Update the objects positions according to their forces
*/
updateObjectsForces(): void {
for (const name in this._instances.items) {
if (this._instances.items.hasOwnProperty(name)) {
const list = this._instances.items[name];
for (let j = 0, listLen = list.length; j < listLen; ++j) {
const obj = list[j];
if (!obj.hasNoForces()) {
const averageForce = obj.getAverageForce();
const elapsedTimeInSeconds = obj.getElapsedTime() / 1000;
obj.setX(obj.getX() + averageForce.getX() * elapsedTimeInSeconds);
obj.setY(obj.getY() + averageForce.getY() * elapsedTimeInSeconds);
obj.updateForces(elapsedTimeInSeconds);
}
}
}
}
}
/**
* Get the structure containing the triggers for "Trigger once" conditions.
*/
getOnceTriggers() {
return this._onceTriggers;
}
/**
* Clear any data structures to make sure memory is freed as soon as
* possible.
*/
_destroy() {
// It should not be necessary to reset these variables, but this help
// ensuring that all memory related to the container is released immediately.
this._layers = new Hashtable();
this._orderedLayers = [];
this._objects = new Hashtable();
this._instances = new Hashtable();
this._instancesCache = new Hashtable();
this._objectsCtor = new Hashtable();
this._allInstancesList = [];
this._instancesRemoved = [];
this._layersCameraCoordinates = {};
this._initialBehaviorSharedData = new Hashtable();
// @ts-ignore We are deleting the object
this._onceTriggers = null;
}
}
}