This repository has been archived by the owner on May 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 165
/
World.js
1306 lines (1067 loc) · 58.5 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
/**
* IsoArcade Physics constructor.
*
* @class Phaser.Plugin.Isometric.Arcade
* @classdesc IsoArcade Physics Constructor
* @constructor
* @param {Phaser.Game} game reference to the current game instance.
*/
Phaser.Plugin.Isometric.Arcade = function (game) {
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = game;
/**
* @property {Phaser.Plugin.Isometric.Point3} gravity - The World gravity setting. Defaults to x: 0, y: 0, z: 0 or no gravity.
*/
this.gravity = new Phaser.Plugin.Isometric.Point3();
/**
* @property {Phaser.Plugin.Isometric.Cube} bounds - The bounds inside of which the physics world exists. Defaults to match the world bounds relatively closely given the isometric projection.
*/
this.bounds = new Phaser.Plugin.Isometric.Cube(0, 0, 0, game.world.width * 0.5, game.world.width * 0.5, game.world.height);
/**
* Set the checkCollision properties to control for which bounds collision is processed.
* For example checkCollision.down = false means Bodies cannot collide with the World.bounds.bottom.
* @property {object} checkCollision - An object containing allowed collision flags.
*/
this.checkCollision = {
up: true,
down: true,
frontX: true,
frontY: true,
backX: true,
backY: true
};
/**
* @property {number} maxObjects - Used by the QuadTree/Octree to set the maximum number of objects per quad.
*/
this.maxObjects = 10;
/**
* @property {number} maxLevels - Used by the QuadTree/Octree to set the maximum number of iteration levels.
*/
this.maxLevels = 4;
/**
* @property {number} OVERLAP_BIAS - A value added to the delta values during collision checks.
*/
this.OVERLAP_BIAS = 4;
/**
* @property {boolean} forceX - If true World.separate will always separate on the X and Y axes before Z. Otherwise it will check gravity totals first.
*/
this.forceXY = false;
/**
* @property {boolean} skipTree - If true an Octree/QuadTree will never be used for any collision. Handy for tightly packed games. See also Body.skipTree.
*/
this.skipTree = false;
/**
* @property {boolean} useQuadTree - If true, the collision/overlap routines will use a QuadTree, which will ignore the z position of objects when determining potential collisions. This will be faster if you don't do a lot of stuff on the z-axis.
*/
this.useQuadTree = false;
/**
* @property {Phaser.QuadTree} quadTree - The world QuadTree.
*/
this.quadTree = new Phaser.QuadTree(this.bounds.x, this.bounds.y, this.bounds.widthX, this.bounds.widthY, this.maxObjects, this.maxLevels);
/**
* @property {Phaser.Plugin.Isometric.Octree} octree - The world Octree.
*/
this.octree = new Phaser.Plugin.Isometric.Octree(this.bounds.x, this.bounds.y, this.bounds.z, this.bounds.widthX, this.bounds.widthY, this.bounds.height, this.maxObjects, this.maxLevels);
// Avoid gc spikes by caching these values for re-use
/**
* @property {number} _overlap - Internal cache var.
* @private
*/
this._overlap = 0;
/**
* @property {number} _maxOverlap - Internal cache var.
* @private
*/
this._maxOverlap = 0;
/**
* @property {number} _velocity1 - Internal cache var.
* @private
*/
this._velocity1 = 0;
/**
* @property {number} _velocity2 - Internal cache var.
* @private
*/
this._velocity2 = 0;
/**
* @property {number} _newVelocity1 - Internal cache var.
* @private
*/
this._newVelocity1 = 0;
/**
* @property {number} _newVelocity2 - Internal cache var.
* @private
*/
this._newVelocity2 = 0;
/**
* @property {number} _average - Internal cache var.
* @private
*/
this._average = 0;
/**
* @property {Array} _mapData - Internal cache var.
* @private
*/
this._mapData = [];
/**
* @property {boolean} _result - Internal cache var.
* @private
*/
this._result = false;
/**
* @property {number} _total - Internal cache var.
* @private
*/
this._total = 0;
/**
* @property {number} _angle - Internal cache var.
* @private
*/
this._angle = 0;
/**
* @property {number} _dx - Internal cache var.
* @private
*/
this._dx = 0;
/**
* @property {number} _dy - Internal cache var.
* @private
*/
this._dy = 0;
/**
* @property {number} _dz - Internal cache var.
* @private
*/
this._dz = 0;
};
Phaser.Plugin.Isometric.Arcade.prototype.constructor = Phaser.Plugin.Isometric.Arcade;
Phaser.Plugin.Isometric.Arcade.prototype = {
/**
* Updates the size of this physics world.
*
* @method Phaser.Plugin.Isometric.Arcade#setBounds
* @param {number} x - Bottom rear most corner of the world.
* @param {number} y - Bottom rear most corner of the world.
* @param {number} z - Bottom rear most corner of the world.
* @param {number} widthX - New X width (breadth) of the world. Can never be smaller than the Game.width.
* @param {number} widthY - New Y width (depth) of the world. Can never be smaller than the Game.width.
* @param {number} height - New height of the world. Can never be smaller than the Game.height.
*/
setBounds: function (x, y, z, widthX, widthY, height) {
this.bounds.setTo(x, y, z, widthX, widthY, height);
},
/**
* Updates the size of this physics world to match the size of the game world.
*
* @method Phaser.Plugin.Isometric.Arcade#setBoundsToWorld
*/
setBoundsToWorld: function () {
this.bounds.setTo(0, 0, 0, this.game.world.width * 0.5, this.game.world.width * 0.5, this.game.world.height);
},
/**
* This will create an IsoArcade Physics body on the given game object or array of game objects.
* A game object can only have 1 physics body active at any one time, and it can't be changed until the object is destroyed.
*
* @method Phaser.Plugin.Isometric.Arcade#enable
* @param {object|array|Phaser.Group} object - The game object to create the physics body on. Can also be an array or Group of objects, a body will be created on every child that has a `body` property.
* @param {boolean} [children=true] - Should a body be created on all children of this object? If true it will recurse down the display list as far as it can go.
*/
enable: function (object, children) {
if (typeof children === 'undefined') {
children = true;
}
var i = 1;
if (Array.isArray(object)) {
i = object.length;
while (i--) {
if (object[i] instanceof Phaser.Group) {
// If it's a Group then we do it on the children regardless
this.enable(object[i].children, children);
} else {
this.enableBody(object[i]);
if (children && object[i].hasOwnProperty('children') && object[i].children.length > 0) {
this.enable(object[i], true);
}
}
}
} else {
if (object instanceof Phaser.Group) {
// If it's a Group then we do it on the children regardless
this.enable(object.children, children);
} else {
this.enableBody(object);
if (children && object.hasOwnProperty('children') && object.children.length > 0) {
this.enable(object.children, true);
}
}
}
},
/**
* Creates an IsoArcade Physics body on the given game object.
* A game object can only have 1 physics body active at any one time, and it can't be changed until the body is nulled.
*
* @method Phaser.Plugin.Isometric.Arcade#enableBody
* @param {object} object - The game object to create the physics body on. A body will only be created if this object has a null `body` property.
*/
enableBody: function (object) {
if (object.hasOwnProperty('body') && object.body === null) {
object.body = new Phaser.Plugin.Isometric.Body(object);
}
},
/**
* Called automatically by a Physics body, it updates all motion related values on the Body.
*
* @method Phaser.Plugin.Isometric.Arcade#updateMotion
* @param {Phaser.Plugin.Isometric.Body} body - The Body object to be updated.
*/
updateMotion: function (body) {
this._velocityDelta = this.computeVelocity(0, body, body.angularVelocity, body.angularAcceleration, body.angularDrag, body.maxAngular) - body.angularVelocity;
body.angularVelocity += this._velocityDelta;
body.rotation += (body.angularVelocity * this.game.time.physicsElapsed);
body.velocity.x = this.computeVelocity(1, body, body.velocity.x, body.acceleration.x, body.drag.x, body.maxVelocity.x);
body.velocity.y = this.computeVelocity(2, body, body.velocity.y, body.acceleration.y, body.drag.y, body.maxVelocity.y);
body.velocity.z = this.computeVelocity(3, body, body.velocity.z, body.acceleration.z, body.drag.z, body.maxVelocity.z);
},
/**
* A tween-like function that takes a starting velocity and some other factors and returns an altered velocity.
* Based on a function in Flixel by @ADAMATOMIC
*
* @method Phaser.Plugin.Isometric.Arcade#computeVelocity
* @param {number} axis - 0 for nothing, 1 for X-axis, 2 for Y-axis, 3 for vertical (Z-axis).
* @param {Phaser.Plugin.Isometric.Body} body - The Body object to be updated.
* @param {number} velocity - Any component of velocity (e.g. 20).
* @param {number} acceleration - Rate at which the velocity is changing.
* @param {number} drag - Really kind of a deceleration, this is how much the velocity changes if Acceleration is not set.
* @param {number} [max=10000] - An absolute value cap for the velocity.
* @return {number} The altered Velocity value.
*/
computeVelocity: function (axis, body, velocity, acceleration, drag, max) {
max = max || 10000;
if (axis === 1 && body.allowGravity) {
velocity += (this.gravity.x + body.gravity.x) * this.game.time.physicsElapsed;
} else if (axis === 2 && body.allowGravity) {
velocity += (this.gravity.y + body.gravity.y) * this.game.time.physicsElapsed;
} else if (axis === 3 && body.allowGravity) {
velocity += (this.gravity.z + body.gravity.z) * this.game.time.physicsElapsed;
}
if (acceleration) {
velocity += acceleration * this.game.time.physicsElapsed;
} else if (drag) {
this._drag = drag * this.game.time.physicsElapsed;
if (velocity - this._drag > 0) {
velocity -= this._drag;
} else if (velocity + this._drag < 0) {
velocity += this._drag;
} else {
velocity = 0;
}
}
if (velocity > max) {
velocity = max;
} else if (velocity < -max) {
velocity = -max;
}
return velocity;
},
/**
* Checks for overlaps between two game objects. The objects can be IsoSprites or Groups.
* You can perform IsoSprite vs. IsoSprite, IsoSprite vs. Group and Group vs. Group overlap checks.
* Unlike collide the objects are NOT automatically separated or have any physics applied, they merely test for overlap results.
* The second parameter can be an array of objects, of differing types.
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups within Groups).
*
* @method Phaser.Plugin.Isometric.Arcade#overlap
* @param {Phaser.Plugin.Isometric.IsoSprite|Phaser.Group} object1 - The first object to check. Can be an instance of Phaser.Plugin.Isometric.IsoSprite or Phaser.Group.
* @param {Phaser.Plugin.Isometric.IsoSprite|Phaser.Group|array} object2 - The second object or array of objects to check. Can be Phaser.Plugin.Isometric.IsoSprite or Phaser.Group.
* @param {function} [overlapCallback=null] - An optional callback function that is called if the objects overlap. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then overlapCallback will only be called if processCallback returns true.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @return {boolean} True if an overlap occured otherwise false.
*/
overlap: function (object1, object2, overlapCallback, processCallback, callbackContext) {
overlapCallback = overlapCallback || null;
processCallback = processCallback || null;
callbackContext = callbackContext || overlapCallback;
this._result = false;
this._total = 0;
if (Array.isArray(object2)) {
for (var i = 0, len = object2.length; i < len; i++) {
this.collideHandler(object1, object2[i], overlapCallback, processCallback, callbackContext, true);
}
} else {
this.collideHandler(object1, object2, overlapCallback, processCallback, callbackContext, true);
}
return (this._total > 0);
},
/**
* Checks for collision between two game objects. You can perform IsoSprite vs. IsoSprite, IsoSprite vs. Group or Group vs. Group collisions.
* The second parameter can be an array of objects, of differing types.
* The objects are also automatically separated. If you don't require separation then use IsoArcade.overlap instead.
* An optional processCallback can be provided. If given this function will be called when two sprites are found to be colliding. It is called before any separation takes place,
* giving you the chance to perform additional checks. If the function returns true then the collision and separation is carried out. If it returns false it is skipped.
* The collideCallback is an optional function that is only called if two sprites collide. If a processCallback has been set then it needs to return true for collideCallback to be called.
* NOTE: This function is not recursive, and will not test against children of objects passed (i.e. Groups within Groups).
*
* @method Phaser.Plugin.Isometric.Arcade#collide
* @param {Phaser.Plugin.Isometric.IsoSprite|Phaser.Group} object1 - The first object to check. Can be an instance of Phaser.Plugin.Isometric.IsoSprite or Phaser.Group.
* @param {Phaser.Plugin.Isometric.IsoSprite|Phaser.Group|array} object2 - The second object or array of objects to check. Can be Phaser.Plugin.Isometric.IsoSprite or Phaser.Group.
* @param {function} [collideCallback=null] - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them, unless you are colliding Group vs. Sprite, in which case Sprite will always be the first parameter.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} [callbackContext] - The context in which to run the callbacks.
* @return {boolean} True if a collision occured otherwise false.
*/
collide: function (object1, object2, collideCallback, processCallback, callbackContext) {
collideCallback = collideCallback || null;
processCallback = processCallback || null;
callbackContext = callbackContext || collideCallback;
this._result = false;
this._total = 0;
if (Array.isArray(object2)) {
for (var i = 0, len = object2.length; i < len; i++) {
this.collideHandler(object1, object2[i], collideCallback, processCallback, callbackContext, false);
}
}
else {
this.collideHandler(object1, object2, collideCallback, processCallback, callbackContext, false);
}
return (this._total > 0);
},
/**
* Internal collision handler.
*
* @method Phaser.Plugin.Isometric.Arcade#collideHandler
* @private
* @param {Phaser.Plugin.Isometric.IsoSprite|Phaser.Group} object1 - The first object to check. Can be an instance of Phaser.Plugin.Isometric.IsoSprite or Phaser.Group.
* @param {Phaser.Plugin.Isometric.IsoSprite|Phaser.Group} object2 - The second object to check. Can be an instance of Phaser.Plugin.Isometric.IsoSprite or Phaser.Group. Can also be an array of objects to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideHandler: function (object1, object2, collideCallback, processCallback, callbackContext, overlapOnly) {
// Only collide valid objects
if (!object2 && object1.type === Phaser.GROUP) {
this.collideGroupVsSelf(object1, collideCallback, processCallback, callbackContext, overlapOnly);
return;
}
if (object1 && object2 && object1.exists && object2.exists) {
// ISOSPRITES
if (object1.type === Phaser.Plugin.Isometric.ISOSPRITE) {
if (object2.type === Phaser.Plugin.Isometric.ISOSPRITE) {
this.collideSpriteVsSprite(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
} else if (object2.type === Phaser.GROUP) {
this.collideSpriteVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
// GROUPS
else if (object1.type === Phaser.GROUP) {
if (object2.type === Phaser.Plugin.Isometric.ISOSPRITE) {
this.collideSpriteVsGroup(object2, object1, collideCallback, processCallback, callbackContext, overlapOnly);
} else if (object2.type === Phaser.GROUP) {
this.collideGroupVsGroup(object1, object2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
}
},
/**
* An internal function. Use Phaser.Plugin.Isometric.Arcade.collide instead.
*
* @method Phaser.Plugin.Isometric.Arcade#collideSpriteVsSprite
* @private
* @param {Phaser.Plugin.Isometric.IsoSprite} sprite1 - The first sprite to check.
* @param {Phaser.Plugin.Isometric.IsoSprite} sprite2 - The second sprite to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
* @return {boolean} True if there was a collision, otherwise false.
*/
collideSpriteVsSprite: function (sprite1, sprite2, collideCallback, processCallback, callbackContext, overlapOnly) {
if (!sprite1.body || !sprite2.body) {
return false;
}
if (this.separate(sprite1.body, sprite2.body, processCallback, callbackContext, overlapOnly)) {
if (collideCallback) {
collideCallback.call(callbackContext, sprite1, sprite2);
}
this._total++;
}
return true;
},
/**
* An internal function. Use Phaser.Plugin.Isometric.Arcade.collide instead.
*
* @method Phaser.Plugin.Isometric.Arcade#collideSpriteVsGroup
* @private
* @param {Phaser.Plugin.Isometric.IsoSprite} sprite - The sprite to check.
* @param {Phaser.Group} group - The Group to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideSpriteVsGroup: function (sprite, group, collideCallback, processCallback, callbackContext, overlapOnly) {
var i, len;
if (group.length === 0 || !sprite.body) {
return;
}
if (sprite.body.skipTree || this.skipTree) {
for (i = 0, len = group.children.length; i < len; i++) {
if (group.children[i] && group.children[i].exists) {
this.collideSpriteVsSprite(sprite, group.children[i], collideCallback, processCallback, callbackContext, overlapOnly);
}
}
} else {
if (this.useQuadTree) {
// What is the sprite colliding with in the quadTree?
this.quadTree.clear();
this.quadTree.reset(this.bounds.x, this.bounds.y, this.bounds.widthX, this.bounds.widthY, this.maxObjects, this.maxLevels);
this.quadTree.populate(group);
this._potentials = this.quadTree.retrieve(sprite);
} else {
// What is the sprite colliding with in the octree?
this.octree.clear();
this.octree.reset(this.bounds.x, this.bounds.y, this.bounds.z, this.bounds.widthX, this.bounds.widthY, this.bounds.height, this.maxObjects, this.maxLevels);
this.octree.populate(group);
this._potentials = this.octree.retrieve(sprite);
}
for (i = 0, len = this._potentials.length; i < len; i++) {
// We have our potential suspects, are they in this group?
if (this.separate(sprite.body, this._potentials[i], processCallback, callbackContext, overlapOnly)) {
if (collideCallback) {
collideCallback.call(callbackContext, sprite, this._potentials[i].sprite);
}
this._total++;
}
}
}
},
/**
* An internal function. Use Phaser.Plugin.Isometric.Arcade.collide instead.
*
* @method Phaser.Plugin.Isometric.Arcade#collideGroupVsSelf
* @private
* @param {Phaser.Group} group - The Group to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
* @return {boolean} True if there was a collision, otherwise false.
*/
collideGroupVsSelf: function (group, collideCallback, processCallback, callbackContext, overlapOnly) {
if (group.length === 0) {
return;
}
var len = group.children.length;
for (var i = 0; i < len; i++) {
for (var j = i + 1; j <= len; j++) {
if (group.children[i] && group.children[j] && group.children[i].exists && group.children[j].exists) {
this.collideSpriteVsSprite(group.children[i], group.children[j], collideCallback, processCallback, callbackContext, overlapOnly);
}
}
}
},
/**
* An internal function. Use Phaser.Plugin.Isometric.Arcade.collide instead.
*
* @method Phaser.Plugin.Isometric.Arcade#collideGroupVsGroup
* @private
* @param {Phaser.Group} group1 - The first Group to check.
* @param {Phaser.Group} group2 - The second Group to check.
* @param {function} collideCallback - An optional callback function that is called if the objects collide. The two objects will be passed to this function in the same order in which you specified them.
* @param {function} processCallback - A callback function that lets you perform additional checks against the two objects if they overlap. If this is set then collision will only happen if processCallback returns true. The two objects will be passed to this function in the same order in which you specified them.
* @param {object} callbackContext - The context in which to run the callbacks.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
*/
collideGroupVsGroup: function (group1, group2, collideCallback, processCallback, callbackContext, overlapOnly) {
if (group1.length === 0 || group2.length === 0) {
return;
}
for (var i = 0, len = group1.children.length; i < len; i++) {
if (group1.children[i].exists) {
this.collideSpriteVsGroup(group1.children[i], group2, collideCallback, processCallback, callbackContext, overlapOnly);
}
}
},
/**
* The core separation function to separate two physics bodies.
*
* @private
* @method Phaser.Plugin.Isometric.Arcade#separate
* @param {Phaser.Plugin.Isometric.Body} body1 - The first Body object to separate.
* @param {Phaser.Plugin.Isometric.Body} body2 - The second Body object to separate.
* @param {function} [processCallback=null] - A callback function that lets you perform additional checks against the two objects if they overlap. If this function is set then the sprites will only be collided if it returns true.
* @param {object} [callbackContext] - The context in which to run the process callback.
* @param {boolean} overlapOnly - Just run an overlap or a full collision.
* @return {boolean} Returns true if the bodies collided, otherwise false.
*/
separate: function (body1, body2, processCallback, callbackContext, overlapOnly) {
if (!body1.enable || !body2.enable || !this.intersects(body1, body2)) {
return false;
}
// They overlap. Is there a custom process callback? If it returns true then we can carry on, otherwise we should abort.
if (processCallback && processCallback.call(callbackContext, body1.sprite, body2.sprite) === false) {
return false;
}
if (overlapOnly) {
// We already know they intersect from the check above, and we don't need separation, so ...
return true;
}
// Do we separate on X and Y first?
// If we weren't having to carry around so much legacy baggage with us, we could do this properly. But alas ...
if (this.forceXY || Math.abs(this.gravity.z + body1.gravity.z) < Math.abs(this.gravity.x + body1.gravity.x) || Math.abs(this.gravity.z + body1.gravity.z) < Math.abs(this.gravity.y + body1.gravity.y)) {
this._result = (this.separateX(body1, body2, overlapOnly) || this.separateY(body1, body2, overlapOnly) || this.separateZ(body1, body2, overlapOnly));
} else {
this._result = (this.separateZ(body1, body2, overlapOnly) || this.separateX(body1, body2, overlapOnly) || this.separateY(body1, body2, overlapOnly));
}
return this._result;
},
/**
* Check for intersection against two bodies.
*
* @method Phaser.Plugin.Isometric.Arcade#intersects
* @param {Phaser.Plugin.Isometric.Body} body1 - The Body object to check.
* @param {Phaser.Plugin.Isometric.Body} body2 - The Body object to check.
* @return {boolean} True if they intersect, otherwise false.
*/
intersects: function (body1, body2) {
if (body1.frontX <= body2.x) {
return false;
}
if (body1.frontY <= body2.y) {
return false;
}
if (body1.x >= body2.frontX) {
return false;
}
if (body1.y >= body2.frontY) {
return false;
}
if (body1.top <= body2.z) {
return false;
}
if (body1.z >= body2.top) {
return false;
}
return true;
},
/**
* The core separation function to separate two physics bodies on the x axis.
*
* @private
* @method Phaser.Plugin.Isometric.Arcade#separateX
* @param {Phaser.Plugin.Isometric.Body} body1 - The Body object to separate.
* @param {Phaser.Plugin.Isometric.Body} body2 - The Body object to separate.
* @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place.
* @return {boolean} Returns true if the bodies were separated, otherwise false.
*/
separateX: function (body1, body2, overlapOnly) {
// Can't separate two immovable bodies
if (body1.immovable && body2.immovable) {
return false;
}
this._overlap = 0;
// Check if the hulls actually overlap
if (this.intersects(body1, body2)) {
this._maxOverlap = body1.deltaAbsX() + body2.deltaAbsX() + this.OVERLAP_BIAS;
if (body1.deltaX() === 0 && body2.deltaX() === 0) {
// They overlap but neither of them are moving
body1.embedded = true;
body2.embedded = true;
} else if (body1.deltaX() > body2.deltaX()) {
// Body1 is moving forward and/or Body2 is moving back
this._overlap = body1.frontX - body2.x;
if ((this._overlap > this._maxOverlap) || body1.checkCollision.frontX === false || body2.checkCollision.backX === false) {
this._overlap = 0;
} else {
body1.touching.none = false;
body1.touching.frontX = true;
body2.touching.none = false;
body2.touching.backX = true;
}
} else if (body1.deltaX() < body2.deltaX()) {
// Body1 is moving back and/or Body2 is moving forward
this._overlap = body1.x - body2.widthX - body2.x;
if ((-this._overlap > this._maxOverlap) || body1.checkCollision.backX === false || body2.checkCollision.frontX === false) {
this._overlap = 0;
} else {
body1.touching.none = false;
body1.touching.backX = true;
body2.touching.none = false;
body2.touching.frontX = true;
}
}
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap !== 0) {
body1.overlapX = this._overlap;
body2.overlapX = this._overlap;
if (overlapOnly || body1.customSeparateX || body2.customSeparateX) {
return true;
}
this._velocity1 = body1.velocity.x;
this._velocity2 = body2.velocity.x;
if (!body1.immovable && !body2.immovable) {
this._overlap *= 0.5;
body1.x = body1.x - this._overlap;
body2.x += this._overlap;
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
this._newVelocity1 -= this._average;
this._newVelocity2 -= this._average;
body1.velocity.x = this._average + this._newVelocity1 * body1.bounce.x;
body2.velocity.x = this._average + this._newVelocity2 * body2.bounce.x;
} else if (!body1.immovable) {
body1.x = body1.x - this._overlap;
body1.velocity.x = this._velocity2 - this._velocity1 * body1.bounce.x;
} else if (!body2.immovable) {
body2.x += this._overlap;
body2.velocity.x = this._velocity1 - this._velocity2 * body2.bounce.x;
}
return true;
}
}
return false;
},
/**
* The core separation function to separate two physics bodies on the x axis.
*
* @private
* @method Phaser.Plugin.Isometric.Arcade#separateY
* @param {Phaser.Plugin.Isometric.Body} body1 - The Body object to separate.
* @param {Phaser.Plugin.Isometric.Body} body2 - The Body object to separate.
* @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place.
* @return {boolean} Returns true if the bodies were separated, otherwise false.
*/
separateY: function (body1, body2, overlapOnly) {
// Can't separate two immovable bodies
if (body1.immovable && body2.immovable) {
return false;
}
this._overlap = 0;
// Check if the hulls actually overlap
if (this.intersects(body1, body2)) {
this._maxOverlap = body1.deltaAbsY() + body2.deltaAbsY() + this.OVERLAP_BIAS;
if (body1.deltaY() === 0 && body2.deltaY() === 0) {
// They overlap but neither of them are moving
body1.embedded = true;
body2.embedded = true;
} else if (body1.deltaY() > body2.deltaY()) {
// Body1 is moving forward and/or Body2 is moving back
this._overlap = body1.frontY - body2.y;
if ((this._overlap > this._maxOverlap) || body1.checkCollision.frontY === false || body2.checkCollision.backY === false) {
this._overlap = 0;
} else {
body1.touching.none = false;
body1.touching.frontY = true;
body2.touching.none = false;
body2.touching.backY = true;
}
} else if (body1.deltaY() < body2.deltaY()) {
// Body1 is moving back and/or Body2 is moving forward
this._overlap = body1.y - body2.widthY - body2.y;
if ((-this._overlap > this._maxOverlap) || body1.checkCollision.backY === false || body2.checkCollision.frontY === false) {
this._overlap = 0;
} else {
body1.touching.none = false;
body1.touching.backY = true;
body2.touching.none = false;
body2.touching.frontY = true;
}
}
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap !== 0) {
body1.overlapY = this._overlap;
body2.overlapY = this._overlap;
if (overlapOnly || body1.customSeparateY || body2.customSeparateY) {
return true;
}
this._velocity1 = body1.velocity.y;
this._velocity2 = body2.velocity.y;
if (!body1.immovable && !body2.immovable) {
this._overlap *= 0.5;
body1.y = body1.y - this._overlap;
body2.y += this._overlap;
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
this._newVelocity1 -= this._average;
this._newVelocity2 -= this._average;
body1.velocity.y = this._average + this._newVelocity1 * body1.bounce.y;
body2.velocity.y = this._average + this._newVelocity2 * body2.bounce.y;
} else if (!body1.immovable) {
body1.y = body1.y - this._overlap;
body1.velocity.y = this._velocity2 - this._velocity1 * body1.bounce.y;
} else if (!body2.immovable) {
body2.y += this._overlap;
body2.velocity.y = this._velocity1 - this._velocity2 * body2.bounce.y;
}
return true;
}
}
return false;
},
/**
* The core separation function to separate two physics bodies on the z axis.
*
* @private
* @method Phaser.Plugin.Isometric.Arcade#separateZ
* @param {Phaser.Plugin.Isometric.Body} body1 - The Body object to separate.
* @param {Phaser.Plugin.Isometric.Body} body2 - The Body object to separate.
* @param {boolean} overlapOnly - If true the bodies will only have their overlap data set, no separation or exchange of velocity will take place.
* @return {boolean} Returns true if the bodies were separated, otherwise false.
*/
separateZ: function (body1, body2, overlapOnly) {
// Can't separate two immovable or non-existing bodys
if (body1.immovable && body2.immovable) {
return false;
}
this._overlap = 0;
// Check if the hulls actually overlap
if (this.intersects(body1, body2)) {
this._maxOverlap = body1.deltaAbsZ() + body2.deltaAbsZ() + this.OVERLAP_BIAS;
if (body1.deltaZ() === 0 && body2.deltaZ() === 0) {
// They overlap but neither of them are moving
body1.embedded = true;
body2.embedded = true;
} else if (body1.deltaZ() > body2.deltaZ()) {
// Body1 is moving down and/or Body2 is moving up
this._overlap = body1.top - body2.z;
if ((this._overlap > this._maxOverlap) || body1.checkCollision.down === false || body2.checkCollision.up === false) {
this._overlap = 0;
} else {
body1.touching.none = false;
body1.touching.down = true;
body2.touching.none = false;
body2.touching.up = true;
}
} else if (body1.deltaZ() < body2.deltaZ()) {
// Body1 is moving up and/or Body2 is moving down
this._overlap = body1.z - body2.top;
if ((-this._overlap > this._maxOverlap) || body1.checkCollision.up === false || body2.checkCollision.down === false) {
this._overlap = 0;
} else {
body1.touching.none = false;
body1.touching.up = true;
body2.touching.none = false;
body2.touching.down = true;
}
}
// Then adjust their positions and velocities accordingly (if there was any overlap)
if (this._overlap !== 0) {
body1.overlapZ = this._overlap;
body2.overlapZ = this._overlap;
if (overlapOnly || body1.customSeparateY || body2.customSeparateZ) {
return true;
}
this._velocity1 = body1.velocity.z;
this._velocity2 = body2.velocity.z;
if (!body1.immovable && !body2.immovable) {
this._overlap *= 0.5;
body1.z = body1.z - this._overlap;
body2.z += this._overlap;
this._newVelocity1 = Math.sqrt((this._velocity2 * this._velocity2 * body2.mass) / body1.mass) * ((this._velocity2 > 0) ? 1 : -1);
this._newVelocity2 = Math.sqrt((this._velocity1 * this._velocity1 * body1.mass) / body2.mass) * ((this._velocity1 > 0) ? 1 : -1);
this._average = (this._newVelocity1 + this._newVelocity2) * 0.5;
this._newVelocity1 -= this._average;
this._newVelocity2 -= this._average;
body1.velocity.z = this._average + this._newVelocity1 * body1.bounce.z;
body2.velocity.z = this._average + this._newVelocity2 * body2.bounce.z;
} else if (!body1.immovable) {
body1.z = body1.z - this._overlap;
body1.velocity.z = this._velocity2 - this._velocity1 * body1.bounce.z;
// This is special case code that handles things like moving platforms you can ride
if (body2.moves) {
body1.x += body2.x - body2.prev.x;
body1.y += body2.y - body2.prev.y;
}
} else if (!body2.immovable) {
body2.z += this._overlap;
body2.velocity.z = this._velocity1 - this._velocity2 * body2.bounce.z;
// This is special case code that handles things like moving platforms you can ride
if (body1.moves) {
body2.x += body1.x - body1.prev.x;
body2.y += body1.y - body1.prev.y;
}
}
return true;
}
}
return false;
},
/**
* Find the distance between two display objects (like Sprites).
*
* @method Phaser.Plugin.Isometric.Isometric.Arcade#distanceBetween
* @param {any} source - The Display Object to test from.
* @param {any} target - The Display Object to test to.
* @return {number} The distance between the source and target objects.
*/
distanceBetween: function (source, target) {
this._dx = source.x - target.x;
this._dy = source.y - target.y;
this._dz = source.z - target.z;
return Math.sqrt(this._dx * this._dx + this._dy * this._dy + this._dz * this._dz);
},
/**
* Find the distance between a display object (like a Sprite) and the given x/y coordinates only (ignore z).
* The calculation is made from the display objects x/y coordinate. This may be the top-left if its anchor hasn't been changed.
* If you need to calculate from the center of a display object instead use the method distanceBetweenCenters()
*
* @method Phaser.Plugin.Isometric.Arcade#distanceToXY
* @param {any} displayObject - The Display Object to test from.
* @param {number} x - The x coordinate to test to.
* @param {number} y - The y coordinate to test to.
* @return {number} The distance between the object and the x/y coordinates.
*/
distanceToXY: function (displayObject, x, y) {
this._dx = displayObject.x - x;
this._dy = displayObject.y - y;
return Math.sqrt(this._dx * this._dx + this._dy * this._dy);