-
Notifications
You must be signed in to change notification settings - Fork 7.1k
/
Copy pathWorld.js
2535 lines (2239 loc) · 91.4 KB
/
World.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
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
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Richard Davey <[email protected]>
* @copyright 2013-2025 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var AngleBetweenPoints = require('../../math/angle/BetweenPoints');
var Body = require('./Body');
var Clamp = require('../../math/Clamp');
var Class = require('../../utils/Class');
var Collider = require('./Collider');
var CONST = require('./const');
var DistanceBetween = require('../../math/distance/DistanceBetween');
var DistanceBetweenPoints = require('../../math/distance/DistanceBetweenPoints');
var EventEmitter = require('eventemitter3');
var Events = require('./events');
var FuzzyEqual = require('../../math/fuzzy/Equal');
var FuzzyGreaterThan = require('../../math/fuzzy/GreaterThan');
var FuzzyLessThan = require('../../math/fuzzy/LessThan');
var GetOverlapX = require('./GetOverlapX');
var GetOverlapY = require('./GetOverlapY');
var GetTilesWithinWorldXY = require('../../tilemaps/components/GetTilesWithinWorldXY');
var GetValue = require('../../utils/object/GetValue');
var MATH_CONST = require('../../math/const');
var ProcessQueue = require('../../structs/ProcessQueue');
var ProcessTileCallbacks = require('./tilemap/ProcessTileCallbacks');
var Rectangle = require('../../geom/rectangle/Rectangle');
var RTree = require('../../structs/RTree');
var SeparateTile = require('./tilemap/SeparateTile');
var SeparateX = require('./SeparateX');
var SeparateY = require('./SeparateY');
var Set = require('../../structs/Set');
var StaticBody = require('./StaticBody');
var TileIntersectsBody = require('./tilemap/TileIntersectsBody');
var TransformMatrix = require('../../gameobjects/components/TransformMatrix');
var Vector2 = require('../../math/Vector2');
var Wrap = require('../../math/Wrap');
/**
* @classdesc
* The Arcade Physics World.
*
* The World is responsible for creating, managing, colliding and updating all of the bodies within it.
*
* An instance of the World belongs to a Phaser.Scene and is accessed via the property `physics.world`.
*
* @class World
* @extends Phaser.Events.EventEmitter
* @memberof Phaser.Physics.Arcade
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene to which this World instance belongs.
* @param {Phaser.Types.Physics.Arcade.ArcadeWorldConfig} config - An Arcade Physics Configuration object.
*/
var World = new Class({
Extends: EventEmitter,
initialize:
function World (scene, config)
{
EventEmitter.call(this);
/**
* The Scene this simulation belongs to.
*
* @name Phaser.Physics.Arcade.World#scene
* @type {Phaser.Scene}
* @since 3.0.0
*/
this.scene = scene;
/**
* Dynamic Bodies in this simulation.
*
* @name Phaser.Physics.Arcade.World#bodies
* @type {Phaser.Structs.Set.<Phaser.Physics.Arcade.Body>}
* @since 3.0.0
*/
this.bodies = new Set();
/**
* Static Bodies in this simulation.
*
* @name Phaser.Physics.Arcade.World#staticBodies
* @type {Phaser.Structs.Set.<Phaser.Physics.Arcade.StaticBody>}
* @since 3.0.0
*/
this.staticBodies = new Set();
/**
* Static Bodies marked for deletion.
*
* @name Phaser.Physics.Arcade.World#pendingDestroy
* @type {Phaser.Structs.Set.<(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody)>}
* @since 3.1.0
*/
this.pendingDestroy = new Set();
/**
* This simulation's collision processors.
*
* @name Phaser.Physics.Arcade.World#colliders
* @type {Phaser.Structs.ProcessQueue.<Phaser.Physics.Arcade.Collider>}
* @since 3.0.0
*/
this.colliders = new ProcessQueue();
/**
* Acceleration of Bodies due to gravity, in pixels per second.
*
* @name Phaser.Physics.Arcade.World#gravity
* @type {Phaser.Math.Vector2}
* @since 3.0.0
*/
this.gravity = new Vector2(GetValue(config, 'gravity.x', 0), GetValue(config, 'gravity.y', 0));
/**
* A boundary constraining Bodies.
*
* @name Phaser.Physics.Arcade.World#bounds
* @type {Phaser.Geom.Rectangle}
* @since 3.0.0
*/
this.bounds = new Rectangle(
GetValue(config, 'x', 0),
GetValue(config, 'y', 0),
GetValue(config, 'width', scene.sys.scale.width),
GetValue(config, 'height', scene.sys.scale.height)
);
/**
* The boundary edges that Bodies can collide with.
*
* @name Phaser.Physics.Arcade.World#checkCollision
* @type {Phaser.Types.Physics.Arcade.CheckCollisionObject}
* @since 3.0.0
*/
this.checkCollision = {
up: GetValue(config, 'checkCollision.up', true),
down: GetValue(config, 'checkCollision.down', true),
left: GetValue(config, 'checkCollision.left', true),
right: GetValue(config, 'checkCollision.right', true)
};
/**
* The number of physics steps to be taken per second.
*
* This property is read-only. Use the `setFPS` method to modify it at run-time.
*
* @name Phaser.Physics.Arcade.World#fps
* @readonly
* @type {number}
* @default 60
* @since 3.10.0
*/
this.fps = GetValue(config, 'fps', 60);
/**
* Should Physics use a fixed update time-step (true) or sync to the render fps (false)?.
* False value of this property disables fps and timeScale properties.
*
* @name Phaser.Physics.Arcade.World#fixedStep
* @type {boolean}
* @default true
* @since 3.23.0
*/
this.fixedStep = GetValue(config, 'fixedStep', true);
/**
* The amount of elapsed ms since the last frame.
*
* @name Phaser.Physics.Arcade.World#_elapsed
* @private
* @type {number}
* @since 3.10.0
*/
this._elapsed = 0;
/**
* Internal frame time value.
*
* @name Phaser.Physics.Arcade.World#_frameTime
* @private
* @type {number}
* @since 3.10.0
*/
this._frameTime = 1 / this.fps;
/**
* Internal frame time ms value.
*
* @name Phaser.Physics.Arcade.World#_frameTimeMS
* @private
* @type {number}
* @since 3.10.0
*/
this._frameTimeMS = 1000 * this._frameTime;
/**
* The number of steps that took place in the last frame.
*
* @name Phaser.Physics.Arcade.World#stepsLastFrame
* @readonly
* @type {number}
* @since 3.10.0
*/
this.stepsLastFrame = 0;
/**
* Scaling factor applied to the frame rate.
*
* - 1.0 = normal speed
* - 2.0 = half speed
* - 0.5 = double speed
*
* @name Phaser.Physics.Arcade.World#timeScale
* @type {number}
* @default 1
* @since 3.10.0
*/
this.timeScale = GetValue(config, 'timeScale', 1);
/**
* The maximum absolute difference of a Body's per-step velocity and its overlap with another Body that will result in separation on *each axis*.
* Larger values favor separation.
* Smaller values favor no separation.
*
* @name Phaser.Physics.Arcade.World#OVERLAP_BIAS
* @type {number}
* @default 4
* @since 3.0.0
*/
this.OVERLAP_BIAS = GetValue(config, 'overlapBias', 4);
/**
* The maximum absolute value of a Body's overlap with a tile that will result in separation on *each axis*.
* Larger values favor separation.
* Smaller values favor no separation.
* The optimum value may be similar to the tile size.
*
* @name Phaser.Physics.Arcade.World#TILE_BIAS
* @type {number}
* @default 16
* @since 3.0.0
*/
this.TILE_BIAS = GetValue(config, 'tileBias', 16);
/**
* Always separate overlapping Bodies horizontally before vertically.
* False (the default) means Bodies are first separated on the axis of greater gravity, or the vertical axis if neither is greater.
*
* @name Phaser.Physics.Arcade.World#forceX
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.forceX = GetValue(config, 'forceX', false);
/**
* Whether the simulation advances with the game loop.
*
* @name Phaser.Physics.Arcade.World#isPaused
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.isPaused = GetValue(config, 'isPaused', false);
/**
* Temporary total of colliding Bodies.
*
* @name Phaser.Physics.Arcade.World#_total
* @type {number}
* @private
* @default 0
* @since 3.0.0
*/
this._total = 0;
/**
* Enables the debug display.
*
* @name Phaser.Physics.Arcade.World#drawDebug
* @type {boolean}
* @default false
* @since 3.0.0
*/
this.drawDebug = GetValue(config, 'debug', false);
/**
* The graphics object drawing the debug display.
*
* @name Phaser.Physics.Arcade.World#debugGraphic
* @type {Phaser.GameObjects.Graphics}
* @since 3.0.0
*/
this.debugGraphic;
/**
* Default debug display settings for new Bodies.
*
* @name Phaser.Physics.Arcade.World#defaults
* @type {Phaser.Types.Physics.Arcade.ArcadeWorldDefaults}
* @since 3.0.0
*/
this.defaults = {
debugShowBody: GetValue(config, 'debugShowBody', true),
debugShowStaticBody: GetValue(config, 'debugShowStaticBody', true),
debugShowVelocity: GetValue(config, 'debugShowVelocity', true),
bodyDebugColor: GetValue(config, 'debugBodyColor', 0xff00ff),
staticBodyDebugColor: GetValue(config, 'debugStaticBodyColor', 0x0000ff),
velocityDebugColor: GetValue(config, 'debugVelocityColor', 0x00ff00)
};
/**
* The maximum number of items per node on the RTree.
*
* This is ignored if `useTree` is `false`. If you have a large number of bodies in
* your world then you may find search performance improves by increasing this value,
* to allow more items per node and less node division.
*
* @name Phaser.Physics.Arcade.World#maxEntries
* @type {number}
* @default 16
* @since 3.0.0
*/
this.maxEntries = GetValue(config, 'maxEntries', 16);
/**
* Should this Arcade Physics World use an RTree for Dynamic bodies?
*
* An RTree is a fast way of spatially sorting of all the bodies in the world.
* However, at certain limits, the cost of clearing and inserting the bodies into the
* tree every frame becomes more expensive than the search speed gains it provides.
*
* If you have a large number of dynamic bodies in your world then it may be best to
* disable the use of the RTree by setting this property to `false` in the physics config.
*
* The number it can cope with depends on browser and device, but a conservative estimate
* of around 5,000 bodies should be considered the max before disabling it.
*
* This only applies to dynamic bodies. Static bodies are always kept in an RTree,
* because they don't have to be cleared every frame, so you benefit from the
* massive search speeds all the time.
*
* @name Phaser.Physics.Arcade.World#useTree
* @type {boolean}
* @default true
* @since 3.10.0
*/
this.useTree = GetValue(config, 'useTree', true);
/**
* The spatial index of Dynamic Bodies.
*
* @name Phaser.Physics.Arcade.World#tree
* @type {Phaser.Structs.RTree}
* @since 3.0.0
*/
this.tree = new RTree(this.maxEntries);
/**
* The spatial index of Static Bodies.
*
* @name Phaser.Physics.Arcade.World#staticTree
* @type {Phaser.Structs.RTree}
* @since 3.0.0
*/
this.staticTree = new RTree(this.maxEntries);
/**
* Recycled input for tree searches.
*
* @name Phaser.Physics.Arcade.World#treeMinMax
* @type {Phaser.Types.Physics.Arcade.ArcadeWorldTreeMinMax}
* @since 3.0.0
*/
this.treeMinMax = { minX: 0, minY: 0, maxX: 0, maxY: 0 };
/**
* A temporary Transform Matrix used by bodies for calculations without them needing their own local copy.
*
* @name Phaser.Physics.Arcade.World#_tempMatrix
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.12.0
*/
this._tempMatrix = new TransformMatrix();
/**
* A temporary Transform Matrix used by bodies for calculations without them needing their own local copy.
*
* @name Phaser.Physics.Arcade.World#_tempMatrix2
* @type {Phaser.GameObjects.Components.TransformMatrix}
* @private
* @since 3.12.0
*/
this._tempMatrix2 = new TransformMatrix();
/**
* The Filtering Options passed to `GetTilesWithinWorldXY` as part of the `collideSpriteVsTilemapLayer` check.
*
* @name Phaser.Physics.Arcade.World#tileFilterOptions
* @type {Phaser.Types.Tilemaps.FilteringOptions}
* @since 3.60.0
*/
this.tileFilterOptions = { isColliding: true, isNotEmpty: true, hasInterestingFace: true };
if (this.drawDebug)
{
this.createDebugGraphic();
}
},
/**
* Adds an Arcade Physics Body to a Game Object, an array of Game Objects, or the children of a Group.
*
* The difference between this and the `enableBody` method is that you can pass arrays or Groups
* to this method.
*
* You can specify if the bodies are to be Dynamic or Static. A dynamic body can move via velocity and
* acceleration. A static body remains fixed in place and as such is able to use an optimized search
* tree, making it ideal for static elements such as level objects. You can still collide and overlap
* with static bodies.
*
* Normally, rather than calling this method directly, you'd use the helper methods available in the
* Arcade Physics Factory, such as:
*
* ```javascript
* this.physics.add.image(x, y, textureKey);
* this.physics.add.sprite(x, y, textureKey);
* ```
*
* Calling factory methods encapsulates the creation of a Game Object and the creation of its
* body at the same time. If you are creating custom classes then you can pass them to this
* method to have their bodies created.
*
* @method Phaser.Physics.Arcade.World#enable
* @since 3.0.0
*
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Group[])} object - The object, or objects, on which to create the bodies.
* @param {number} [bodyType] - The type of Body to create. Either `DYNAMIC_BODY` or `STATIC_BODY`.
*/
enable: function (object, bodyType)
{
if (bodyType === undefined) { bodyType = CONST.DYNAMIC_BODY; }
if (!Array.isArray(object))
{
object = [ object ];
}
for (var i = 0; i < object.length; i++)
{
var entry = object[i];
if (entry.isParent)
{
var children = entry.getChildren();
for (var c = 0; c < children.length; c++)
{
var child = children[c];
if (child.isParent)
{
// Handle Groups nested inside of Groups
this.enable(child, bodyType);
}
else
{
this.enableBody(child, bodyType);
}
}
}
else
{
this.enableBody(entry, bodyType);
}
}
},
/**
* Creates an Arcade Physics Body on a single Game Object.
*
* If the Game Object already has a body, this method will simply add it back into the simulation.
*
* You can specify if the body is Dynamic or Static. A dynamic body can move via velocity and
* acceleration. A static body remains fixed in place and as such is able to use an optimized search
* tree, making it ideal for static elements such as level objects. You can still collide and overlap
* with static bodies.
*
* Normally, rather than calling this method directly, you'd use the helper methods available in the
* Arcade Physics Factory, such as:
*
* ```javascript
* this.physics.add.image(x, y, textureKey);
* this.physics.add.sprite(x, y, textureKey);
* ```
*
* Calling factory methods encapsulates the creation of a Game Object and the creation of its
* body at the same time. If you are creating custom classes then you can pass them to this
* method to have their bodies created.
*
* @method Phaser.Physics.Arcade.World#enableBody
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} object - The Game Object on which to create the body.
* @param {number} [bodyType] - The type of Body to create. Either `DYNAMIC_BODY` or `STATIC_BODY`.
*
* @return {Phaser.GameObjects.GameObject} The Game Object on which the body was created.
*/
enableBody: function (object, bodyType)
{
if (bodyType === undefined) { bodyType = CONST.DYNAMIC_BODY; }
if (object.hasTransformComponent)
{
if (!object.body)
{
if (bodyType === CONST.DYNAMIC_BODY)
{
object.body = new Body(this, object);
}
else if (bodyType === CONST.STATIC_BODY)
{
object.body = new StaticBody(this, object);
}
}
this.add(object.body);
}
return object;
},
/**
* Adds an existing Arcade Physics Body or StaticBody to the simulation.
*
* The body is enabled and added to the local search trees.
*
* @method Phaser.Physics.Arcade.World#add
* @since 3.10.0
*
* @param {(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody)} body - The Body to be added to the simulation.
*
* @return {(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody)} The Body that was added to the simulation.
*/
add: function (body)
{
if (body.physicsType === CONST.DYNAMIC_BODY)
{
this.bodies.set(body);
}
else if (body.physicsType === CONST.STATIC_BODY)
{
this.staticBodies.set(body);
this.staticTree.insert(body);
}
body.enable = true;
return body;
},
/**
* Disables the Arcade Physics Body of a Game Object, an array of Game Objects, or the children of a Group.
*
* The difference between this and the `disableBody` method is that you can pass arrays or Groups
* to this method.
*
* The body itself is not deleted, it just has its `enable` property set to false, which
* means you can re-enable it again at any point by passing it to enable `World.enable` or `World.add`.
*
* @method Phaser.Physics.Arcade.World#disable
* @since 3.0.0
*
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Group[])} object - The object, or objects, on which to disable the bodies.
*/
disable: function (object)
{
if (!Array.isArray(object))
{
object = [ object ];
}
for (var i = 0; i < object.length; i++)
{
var entry = object[i];
if (entry.isParent)
{
var children = entry.getChildren();
for (var c = 0; c < children.length; c++)
{
var child = children[c];
if (child.isParent)
{
// Handle Groups nested inside of Groups
this.disable(child);
}
else
{
this.disableBody(child.body);
}
}
}
else
{
this.disableBody(entry.body);
}
}
},
/**
* Disables an existing Arcade Physics Body or StaticBody and removes it from the simulation.
*
* The body is disabled and removed from the local search trees.
*
* The body itself is not deleted, it just has its `enable` property set to false, which
* means you can re-enable it again at any point by passing it to enable `World.enable` or `World.add`.
*
* @method Phaser.Physics.Arcade.World#disableBody
* @since 3.0.0
*
* @param {(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody)} body - The Body to be disabled.
*/
disableBody: function (body)
{
this.remove(body);
body.enable = false;
},
/**
* Removes an existing Arcade Physics Body or StaticBody from the simulation.
*
* The body is disabled and removed from the local search trees.
*
* The body itself is not deleted, it just has its `enabled` property set to false, which
* means you can re-enable it again at any point by passing it to enable `enable` or `add`.
*
* @method Phaser.Physics.Arcade.World#remove
* @since 3.0.0
*
* @param {(Phaser.Physics.Arcade.Body|Phaser.Physics.Arcade.StaticBody)} body - The body to be removed from the simulation.
*/
remove: function (body)
{
if (body.physicsType === CONST.DYNAMIC_BODY)
{
this.tree.remove(body);
this.bodies.delete(body);
}
else if (body.physicsType === CONST.STATIC_BODY)
{
this.staticBodies.delete(body);
this.staticTree.remove(body);
}
},
/**
* Creates a Graphics Game Object that the world will use to render the debug display to.
*
* This is called automatically when the World is instantiated if the `debug` config property
* was set to `true`. However, you can call it at any point should you need to display the
* debug Graphic from a fixed point.
*
* You can control which objects are drawn to the Graphics object, and the colors they use,
* by setting the debug properties in the physics config.
*
* You should not typically use this in a production game. Use it to aid during debugging.
*
* @method Phaser.Physics.Arcade.World#createDebugGraphic
* @since 3.0.0
*
* @return {Phaser.GameObjects.Graphics} The Graphics object that was created for use by the World.
*/
createDebugGraphic: function ()
{
var graphic = this.scene.sys.add.graphics({ x: 0, y: 0 });
graphic.setDepth(Number.MAX_VALUE);
this.debugGraphic = graphic;
this.drawDebug = true;
return graphic;
},
/**
* Sets the position, size and properties of the World boundary.
*
* The World boundary is an invisible rectangle that defines the edges of the World.
* If a Body is set to collide with the world bounds then it will automatically stop
* when it reaches any of the edges. You can optionally set which edges of the boundary
* should be checked against.
*
* @method Phaser.Physics.Arcade.World#setBounds
* @since 3.0.0
*
* @param {number} x - The top-left x coordinate of the boundary.
* @param {number} y - The top-left y coordinate of the boundary.
* @param {number} width - The width of the boundary.
* @param {number} height - The height of the boundary.
* @param {boolean} [checkLeft] - Should bodies check against the left edge of the boundary?
* @param {boolean} [checkRight] - Should bodies check against the right edge of the boundary?
* @param {boolean} [checkUp] - Should bodies check against the top edge of the boundary?
* @param {boolean} [checkDown] - Should bodies check against the bottom edge of the boundary?
*
* @return {Phaser.Physics.Arcade.World} This World object.
*/
setBounds: function (x, y, width, height, checkLeft, checkRight, checkUp, checkDown)
{
this.bounds.setTo(x, y, width, height);
if (checkLeft !== undefined)
{
this.setBoundsCollision(checkLeft, checkRight, checkUp, checkDown);
}
return this;
},
/**
* Enables or disables collisions on each edge of the World boundary.
*
* @method Phaser.Physics.Arcade.World#setBoundsCollision
* @since 3.0.0
*
* @param {boolean} [left=true] - Should bodies check against the left edge of the boundary?
* @param {boolean} [right=true] - Should bodies check against the right edge of the boundary?
* @param {boolean} [up=true] - Should bodies check against the top edge of the boundary?
* @param {boolean} [down=true] - Should bodies check against the bottom edge of the boundary?
*
* @return {Phaser.Physics.Arcade.World} This World object.
*/
setBoundsCollision: function (left, right, up, down)
{
if (left === undefined) { left = true; }
if (right === undefined) { right = true; }
if (up === undefined) { up = true; }
if (down === undefined) { down = true; }
this.checkCollision.left = left;
this.checkCollision.right = right;
this.checkCollision.up = up;
this.checkCollision.down = down;
return this;
},
/**
* Pauses the simulation.
*
* A paused simulation does not update any existing bodies, or run any Colliders.
*
* However, you can still enable and disable bodies within it, or manually run collide or overlap
* checks.
*
* @method Phaser.Physics.Arcade.World#pause
* @fires Phaser.Physics.Arcade.Events#PAUSE
* @since 3.0.0
*
* @return {Phaser.Physics.Arcade.World} This World object.
*/
pause: function ()
{
this.isPaused = true;
this.emit(Events.PAUSE);
return this;
},
/**
* Resumes the simulation, if paused.
*
* @method Phaser.Physics.Arcade.World#resume
* @fires Phaser.Physics.Arcade.Events#RESUME
* @since 3.0.0
*
* @return {Phaser.Physics.Arcade.World} This World object.
*/
resume: function ()
{
this.isPaused = false;
this.emit(Events.RESUME);
return this;
},
/**
* Creates a new Collider object and adds it to the simulation.
*
* A Collider is a way to automatically perform collision checks between two objects,
* calling the collide and process callbacks if they occur.
*
* Colliders are run as part of the World update, after all of the Bodies have updated.
*
* By creating a Collider you don't need then call `World.collide` in your `update` loop,
* as it will be handled for you automatically.
*
* @method Phaser.Physics.Arcade.World#addCollider
* @since 3.0.0
* @see Phaser.Physics.Arcade.World#collide
*
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object1 - The first object to check for collision.
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object2 - The second object to check for collision.
* @param {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback} [collideCallback] - The callback to invoke when the two objects collide.
* @param {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback} [processCallback] - The callback to invoke when the two objects collide. Must return a boolean.
* @param {*} [callbackContext] - The scope in which to call the callbacks.
*
* @return {Phaser.Physics.Arcade.Collider} The Collider that was created.
*/
addCollider: function (object1, object2, collideCallback, processCallback, callbackContext)
{
if (collideCallback === undefined) { collideCallback = null; }
if (processCallback === undefined) { processCallback = null; }
if (callbackContext === undefined) { callbackContext = collideCallback; }
var collider = new Collider(this, false, object1, object2, collideCallback, processCallback, callbackContext);
this.colliders.add(collider);
return collider;
},
/**
* Creates a new Overlap Collider object and adds it to the simulation.
*
* A Collider is a way to automatically perform overlap checks between two objects,
* calling the collide and process callbacks if they occur.
*
* Colliders are run as part of the World update, after all of the Bodies have updated.
*
* By creating a Collider you don't need then call `World.overlap` in your `update` loop,
* as it will be handled for you automatically.
*
* @method Phaser.Physics.Arcade.World#addOverlap
* @since 3.0.0
*
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object1 - The first object to check for overlap.
* @param {Phaser.Types.Physics.Arcade.ArcadeColliderType} object2 - The second object to check for overlap.
* @param {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback} [collideCallback] - The callback to invoke when the two objects overlap.
* @param {Phaser.Types.Physics.Arcade.ArcadePhysicsCallback} [processCallback] - The callback to invoke when the two objects overlap. Must return a boolean.
* @param {*} [callbackContext] - The scope in which to call the callbacks.
*
* @return {Phaser.Physics.Arcade.Collider} The Collider that was created.
*/
addOverlap: function (object1, object2, collideCallback, processCallback, callbackContext)
{
if (collideCallback === undefined) { collideCallback = null; }
if (processCallback === undefined) { processCallback = null; }
if (callbackContext === undefined) { callbackContext = collideCallback; }
var collider = new Collider(this, true, object1, object2, collideCallback, processCallback, callbackContext);
this.colliders.add(collider);
return collider;
},
/**
* Removes a Collider from the simulation so it is no longer processed.
*
* This method does not destroy the Collider. If you wish to add it back at a later stage you can call
* `World.colliders.add(Collider)`.
*
* If you no longer need the Collider you can call the `Collider.destroy` method instead, which will
* automatically clear all of its references and then remove it from the World. If you call destroy on
* a Collider you _don't_ need to pass it to this method too.
*
* @method Phaser.Physics.Arcade.World#removeCollider
* @since 3.0.0
*
* @param {Phaser.Physics.Arcade.Collider} collider - The Collider to remove from the simulation.
*
* @return {Phaser.Physics.Arcade.World} This World object.
*/
removeCollider: function (collider)
{
this.colliders.remove(collider);
return this;
},
/**
* Sets the frame rate to run the simulation at.
*
* The frame rate value is used to simulate a fixed update time step. This fixed
* time step allows for a straightforward implementation of a deterministic game state.
*
* This frame rate is independent of the frequency at which the game is rendering. The
* higher you set the fps, the more physics simulation steps will occur per game step.
* Conversely, the lower you set it, the less will take place.
*
* You can optionally advance the simulation directly yourself by calling the `step` method.
*
* @method Phaser.Physics.Arcade.World#setFPS
* @since 3.10.0
*
* @param {number} framerate - The frame rate to advance the simulation at.
*
* @return {this} This World object.
*/
setFPS: function (framerate)
{
this.fps = framerate;
this._frameTime = 1 / this.fps;
this._frameTimeMS = 1000 * this._frameTime;
return this;
},
/**
* Advances the simulation based on the elapsed time and fps rate.
*
* This is called automatically by your Scene and does not need to be invoked directly.
*
* @method Phaser.Physics.Arcade.World#update
* @fires Phaser.Physics.Arcade.Events#WORLD_STEP
* @since 3.0.0
*
* @param {number} time - The current timestamp as generated by the Request Animation Frame or SetTimeout.
* @param {number} delta - The delta time, in ms, elapsed since the last frame.
*/
update: function (time, delta)
{
if (this.isPaused || this.bodies.size === 0)
{
return;
}
var i;
var fixedDelta = this._frameTime;
var msPerFrame = this._frameTimeMS * this.timeScale;
this._elapsed += delta;
// Update all active bodies
var body;
var bodies = this.bodies.entries;
// Will a step happen this frame?
var willStep = (this._elapsed >= msPerFrame);
if (!this.fixedStep)
{
fixedDelta = delta * 0.001;
willStep = true;
this._elapsed = 0;
}
for (i = 0; i < bodies.length; i++)
{
body = bodies[i];
if (body.enable)
{
body.preUpdate(willStep, fixedDelta);
}
}
// We know that a step will happen this frame, so let's bundle it all together to save branching and iteration costs
if (willStep)
{
this._elapsed -= msPerFrame;
this.stepsLastFrame = 1;
// Optionally populate our dynamic collision tree
if (this.useTree)
{
this.tree.clear();
this.tree.load(bodies);
}
// Process any colliders
var colliders = this.colliders.update();
for (i = 0; i < colliders.length; i++)
{
var collider = colliders[i];
if (collider.active)
{
collider.update();
}
}
this.emit(Events.WORLD_STEP, fixedDelta);
}