/
Body.js
1505 lines (1223 loc) · 48.4 KB
/
Body.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 <rich@photonstorm.com>
* @copyright 2016 Photon Storm Ltd.
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
*/
/**
* The Physics Body is linked to a single Sprite. All physics operations should be performed against the body rather than
* the Sprite itself. For example you can set the velocity, acceleration, bounce values etc all on the Body.
*
* @class Phaser.Physics.Arcade.Body
* @constructor
* @param {Phaser.Sprite} sprite - The Sprite object this physics body belongs to.
*/
Phaser.Physics.Arcade.Body = function (sprite) {
/**
* @property {Phaser.Sprite} sprite - Reference to the parent Sprite.
*/
this.sprite = sprite;
/**
* @property {Phaser.Game} game - Local reference to game.
*/
this.game = sprite.game;
/**
* @property {number} type - The type of physics system this body belongs to.
*/
this.type = Phaser.Physics.ARCADE;
/**
* @property {boolean} enable - A disabled body won't be checked for any form of collision or overlap or have its pre/post updates run.
* @default
*/
this.enable = true;
/**
* If `true` this Body is using circular collision detection. If `false` it is using rectangular.
* Use `Body.setCircle` to control the collision shape this Body uses.
* @property {boolean} isCircle
* @default
* @readOnly
*/
this.isCircle = false;
/**
* The radius of the circular collision shape this Body is using if Body.setCircle has been enabled, relative to the Sprite's _texture_.
* If you wish to change the radius then call {@link #setCircle} again with the new value.
* If you wish to stop the Body using a circle then call {@link #setCircle} with a radius of zero (or undefined).
* The actual radius of the Body (at any Sprite scale) is equal to {@link #halfWidth} and the diameter is equal to {@link #width}.
* @property {number} radius
* @default
* @readOnly
*/
this.radius = 0;
/**
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite's texture.
*/
this.offset = new Phaser.Point();
/**
* @property {Phaser.Point} position - The position of the physics body, equivalent to ({@link #left}, {@link #top}).
* @readonly
*/
this.position = new Phaser.Point(sprite.x, sprite.y);
/**
* @property {Phaser.Point} prev - The previous position of the physics body.
* @readonly
*/
this.prev = new Phaser.Point(this.position.x, this.position.y);
/**
* @property {boolean} allowRotation - Allow this Body to be rotated? (via angularVelocity, etc)
* @default
*/
this.allowRotation = true;
/**
* The Body's rotation in degrees, as calculated by its angularVelocity and angularAcceleration. Please understand that the collision Body
* itself never rotates, it is always axis-aligned. However these values are passed up to the parent Sprite and updates its rotation.
* @property {number} rotation
*/
this.rotation = sprite.angle;
/**
* @property {number} preRotation - The previous rotation of the physics body, in degrees.
* @readonly
*/
this.preRotation = sprite.angle;
/**
* @property {number} width - The calculated width of the physics body.
* @readonly
*/
this.width = sprite.width;
/**
* @property {number} height - The calculated height of the physics body.
* @readonly
*/
this.height = sprite.height;
/**
* @property {number} sourceWidth - The un-scaled original size.
* @readonly
*/
this.sourceWidth = sprite.width;
/**
* @property {number} sourceHeight - The un-scaled original size.
* @readonly
*/
this.sourceHeight = sprite.height;
if (sprite.texture)
{
this.sourceWidth = sprite.texture.frame.width;
this.sourceHeight = sprite.texture.frame.height;
}
/**
* @property {number} halfWidth - The calculated width / 2 of the physics body.
* @readonly
*/
this.halfWidth = Math.abs(sprite.width / 2);
/**
* @property {number} halfHeight - The calculated height / 2 of the physics body.
* @readonly
*/
this.halfHeight = Math.abs(sprite.height / 2);
/**
* @property {Phaser.Point} center - The center coordinate of the Physics Body.
* @readonly
*/
this.center = new Phaser.Point(sprite.x + this.halfWidth, sprite.y + this.halfHeight);
/**
* @property {Phaser.Point} velocity - The velocity, or rate of change the Body's position. Measured in pixels per second.
*/
this.velocity = new Phaser.Point();
/**
* @property {Phaser.Point} newVelocity - The distanced traveled during the last update, equal to `velocity * physicsElapsed`. Calculated during the Body.preUpdate and applied to its position.
* @readonly
*/
this.newVelocity = new Phaser.Point();
/**
* @property {Phaser.Point} deltaMax - The Sprite position is updated based on the delta x/y values. You can set a cap on those (both +-) using deltaMax.
*/
this.deltaMax = new Phaser.Point();
/**
* @property {Phaser.Point} acceleration - The acceleration is the rate of change of the velocity. Measured in pixels per second squared.
*/
this.acceleration = new Phaser.Point();
/**
* @property {boolean} allowDrag - Allow this Body to be influenced by {@link #drag}?
* @default
*/
this.allowDrag = true;
/**
* @property {Phaser.Point} drag - The drag applied to the motion of the Body (when {@link #allowDrag} is enabled). Measured in pixels per second squared.
*/
this.drag = new Phaser.Point();
/**
* @property {boolean} allowGravity - Allow this Body to be influenced by gravity? Either world or local.
* @default
*/
this.allowGravity = true;
/**
* @property {Phaser.Point} gravity - This Body's local gravity, **added** to any world gravity, unless Body.allowGravity is set to false.
*/
this.gravity = new Phaser.Point();
/**
* @property {Phaser.Point} bounce - The elasticity of the Body when colliding. bounce.x/y = 1 means full rebound, bounce.x/y = 0.5 means 50% rebound velocity.
*/
this.bounce = new Phaser.Point();
/**
* The elasticity of the Body when colliding with the World bounds.
* By default this property is `null`, in which case `Body.bounce` is used instead. Set this property
* to a Phaser.Point object in order to enable a World bounds specific bounce value.
* @property {Phaser.Point} worldBounce
*/
this.worldBounce = null;
/**
* A Signal that is dispatched when this Body collides with the world bounds.
* Due to the potentially high volume of signals this could create it is disabled by default.
* To use this feature set this property to a Phaser.Signal: `sprite.body.onWorldBounds = new Phaser.Signal()`
* and it will be called when a collision happens, passing five arguments:
* `onWorldBounds(sprite, up, down, left, right)`
* where the Sprite is a reference to the Sprite that owns this Body, and the other arguments are booleans
* indicating on which side of the world the Body collided.
* @property {Phaser.Signal} onWorldBounds
*/
this.onWorldBounds = null;
/**
* A Signal that is dispatched when this Body collides with another Body.
*
* You still need to call `game.physics.arcade.collide` in your `update` method in order
* for this signal to be dispatched.
*
* Usually you'd pass a callback to the `collide` method, but this signal provides for
* a different level of notification.
*
* Due to the potentially high volume of signals this could create it is disabled by default.
*
* To use this feature set this property to a Phaser.Signal: `sprite.body.onCollide = new Phaser.Signal()`
* and it will be called when a collision happens, passing two arguments: the sprites which collided.
* The first sprite in the argument is always the owner of this Body.
*
* If two Bodies with this Signal set collide, both will dispatch the Signal.
* @property {Phaser.Signal} onCollide
*/
this.onCollide = null;
/**
* A Signal that is dispatched when this Body overlaps with another Body.
*
* You still need to call `game.physics.arcade.overlap` in your `update` method in order
* for this signal to be dispatched.
*
* Usually you'd pass a callback to the `overlap` method, but this signal provides for
* a different level of notification.
*
* Due to the potentially high volume of signals this could create it is disabled by default.
*
* To use this feature set this property to a Phaser.Signal: `sprite.body.onOverlap = new Phaser.Signal()`
* and it will be called when a collision happens, passing two arguments: the sprites which collided.
* The first sprite in the argument is always the owner of this Body.
*
* If two Bodies with this Signal set collide, both will dispatch the Signal.
* @property {Phaser.Signal} onOverlap
*/
this.onOverlap = null;
/**
* @property {Phaser.Point} maxVelocity - The maximum velocity (in pixels per second squared) that the Body can reach.
* @default
*/
this.maxVelocity = new Phaser.Point(10000, 10000);
/**
* @property {Phaser.Point} friction - If this Body is {@link #immovable} and moving, and another Body is 'riding' this one, this is the amount of motion the riding Body receives on each axis.
*/
this.friction = new Phaser.Point(1, 0);
/**
* @property {number} angularVelocity - The angular velocity is the rate of change of the Body's rotation. It is measured in degrees per second.
* @default
*/
this.angularVelocity = 0;
/**
* @property {number} angularAcceleration - The angular acceleration is the rate of change of the angular velocity. Measured in degrees per second squared.
* @default
*/
this.angularAcceleration = 0;
/**
* @property {number} angularDrag - The drag applied during the rotation of the Body. Measured in degrees per second squared.
* @default
*/
this.angularDrag = 0;
/**
* @property {number} maxAngular - The maximum angular velocity in degrees per second that the Body can reach.
* @default
*/
this.maxAngular = 1000;
/**
* @property {number} mass - The mass of the Body. When two bodies collide their mass is used in the calculation to determine the exchange of velocity.
* @default
*/
this.mass = 1;
/**
* @property {number} angle - The angle of the Body's **velocity** in radians.
* @readonly
*/
this.angle = 0;
/**
* @property {number} speed - The speed of the Body in pixels per second, equal to the magnitude of the velocity.
* @readonly
*/
this.speed = 0;
/**
* @property {number} facing - A const reference to the direction the Body is traveling or facing: Phaser.NONE, Phaser.LEFT, Phaser.RIGHT, Phaser.UP, or Phaser.DOWN. If the Body is moving on both axes, UP and DOWN take precedence.
* @default
*/
this.facing = Phaser.NONE;
/**
* @property {boolean} immovable - An immovable Body will not receive any impacts from other bodies. **Two** immovable Bodies can't separate or exchange momentum and will pass through each other.
* @default
*/
this.immovable = false;
/**
* Whether the physics system should update the Body's position and rotation based on its velocity, acceleration, drag, and gravity.
*
* If you have a Body that is being moved around the world via a tween or a Group motion, but its local x/y position never
* actually changes, then you should set Body.moves = false. Otherwise it will most likely fly off the screen.
* If you want the physics system to move the body around, then set moves to true.
*
* A Body with moves = false can still be moved slightly (but not accelerated) during collision separation unless you set {@link #immovable} as well.
*
* @property {boolean} moves - Set to true to allow the Physics system to move this Body, otherwise false to move it manually.
* @default
*/
this.moves = true;
/**
* This flag allows you to disable the custom x separation that takes place by Physics.Arcade.separate.
* Used in combination with your own collision processHandler you can create whatever type of collision response you need.
* @property {boolean} customSeparateX - Use a custom separation system or the built-in one?
* @default
*/
this.customSeparateX = false;
/**
* This flag allows you to disable the custom y separation that takes place by Physics.Arcade.separate.
* Used in combination with your own collision processHandler you can create whatever type of collision response you need.
* @property {boolean} customSeparateY - Use a custom separation system or the built-in one?
* @default
*/
this.customSeparateY = false;
/**
* When this body collides with another, the amount of overlap is stored here.
* @property {number} overlapX - The amount of horizontal overlap during the collision.
*/
this.overlapX = 0;
/**
* When this body collides with another, the amount of overlap is stored here.
* @property {number} overlapY - The amount of vertical overlap during the collision.
*/
this.overlapY = 0;
/**
* If `Body.isCircle` is true, and this body collides with another circular body, the amount of overlap is stored here.
* @property {number} overlapR - The amount of overlap during the collision.
*/
this.overlapR = 0;
/**
* If a body is overlapping with another body, but neither of them are moving (maybe they spawned on-top of each other?) this is set to true.
* @property {boolean} embedded - Body embed value.
*/
this.embedded = false;
/**
* A Body can be set to collide against the World bounds automatically and rebound back into the World if this is set to true. Otherwise it will leave the World.
* @property {boolean} collideWorldBounds - Should the Body collide with the World bounds?
*/
this.collideWorldBounds = false;
/**
* Set the checkCollision properties to control which directions collision is processed for this Body.
* For example checkCollision.up = false means it won't collide when the collision happened while moving up.
* If you need to disable a Body entirely, use `body.enable = false`, this will also disable motion.
* If you need to disable just collision and/or overlap checks, but retain motion, set `checkCollision.none = true`.
* @property {object} checkCollision - An object containing allowed collision (none, up, down, left, right).
*/
this.checkCollision = { none: false, up: true, down: true, left: true, right: true };
/**
* This object is populated with boolean values when the Body collides with another.
* touching.up = true means the collision happened to the top of this Body for example.
* @property {object} touching - An object containing touching results (none, up, down, left, right).
*/
this.touching = { none: true, up: false, down: false, left: false, right: false };
/**
* This object is populated with previous touching values from the bodies previous collision.
* @property {object} wasTouching - An object containing previous touching results (none, up, down, left, right).
*/
this.wasTouching = { none: true, up: false, down: false, left: false, right: false };
/**
* This object is populated with boolean values when the Body collides with the World bounds or a Tile.
* For example if blocked.up is true then the Body cannot move up.
* @property {object} blocked - An object containing on which faces this Body is blocked from moving, if any (none, up, down, left, right).
*/
this.blocked = { none: true, up: false, down: false, left: false, right: false };
/**
* If this is an especially small or fast moving object then it can sometimes skip over tilemap collisions if it moves through a tile in a step.
* Set this padding value to add extra padding to its bounds. tilePadding.x applied to its width, y to its height.
* @property {Phaser.Point} tilePadding - Extra padding to be added to this sprite's dimensions when checking for tile collision.
*/
this.tilePadding = new Phaser.Point();
/**
* @property {boolean} dirty - If this Body in a preUpdate (true) or postUpdate (false) state?
*/
this.dirty = false;
/**
* @property {boolean} skipQuadTree - If true and you collide this Sprite against a Group, it will disable the collision check from using a QuadTree.
*/
this.skipQuadTree = false;
/**
* If true the Body will check itself against the Sprite.getBounds() dimensions and adjust its width and height accordingly.
* If false it will compare its dimensions against the Sprite scale instead, and adjust its width height if the scale has changed.
* Typically you would need to enable syncBounds if your sprite is the child of a responsive display object such as a FlexLayer,
* or in any situation where the Sprite scale doesn't change, but its parents scale is effecting the dimensions regardless.
* @property {boolean} syncBounds
* @default
*/
this.syncBounds = false;
/**
* @property {boolean} isMoving - Set by the `moveTo` and `moveFrom` methods.
*/
this.isMoving = false;
/**
* @property {boolean} stopVelocityOnCollide - Set by the `moveTo` and `moveFrom` methods.
*/
this.stopVelocityOnCollide = true;
/**
* @property {integer} moveTimer - Internal time used by the `moveTo` and `moveFrom` methods.
* @private
*/
this.moveTimer = 0;
/**
* @property {integer} moveDistance - Internal distance value, used by the `moveTo` and `moveFrom` methods.
* @private
*/
this.moveDistance = 0;
/**
* @property {integer} moveDuration - Internal duration value, used by the `moveTo` and `moveFrom` methods.
* @private
*/
this.moveDuration = 0;
/**
* @property {Phaser.Line} moveTarget - Set by the `moveTo` method, and updated each frame.
* @private
*/
this.moveTarget = null;
/**
* @property {Phaser.Point} moveEnd - Set by the `moveTo` method, and updated each frame.
* @private
*/
this.moveEnd = null;
/**
* @property {Phaser.Signal} onMoveComplete - Listen for the completion of `moveTo` or `moveFrom` events.
*/
this.onMoveComplete = new Phaser.Signal();
/**
* @property {function} movementCallback - Optional callback. If set, invoked during the running of `moveTo` or `moveFrom` events.
*/
this.movementCallback = null;
/**
* @property {object} movementCallbackContext - Context in which to call the movementCallback.
*/
this.movementCallbackContext = null;
/**
* @property {boolean} _reset - Internal cache var.
* @private
*/
this._reset = true;
/**
* @property {number} _sx - Internal cache var.
* @private
*/
this._sx = sprite.scale.x;
/**
* @property {number} _sy - Internal cache var.
* @private
*/
this._sy = sprite.scale.y;
/**
* @property {number} _dx - Internal cache var.
* @private
*/
this._dx = 0;
/**
* @property {number} _dy - Internal cache var.
* @private
*/
this._dy = 0;
};
Phaser.Physics.Arcade.Body.prototype = {
/**
* Internal method.
*
* @method Phaser.Physics.Arcade.Body#updateBounds
* @protected
*/
updateBounds: function () {
if (this.syncBounds)
{
var b = this.sprite.getBounds();
b.ceilAll();
if (b.width !== this.width || b.height !== this.height)
{
this.width = b.width;
this.height = b.height;
this._reset = true;
}
}
else
{
var asx = Math.abs(this.sprite.scale.x);
var asy = Math.abs(this.sprite.scale.y);
if (asx !== this._sx || asy !== this._sy)
{
this.width = this.sourceWidth * asx;
this.height = this.sourceHeight * asy;
this._sx = asx;
this._sy = asy;
this._reset = true;
}
}
if (this._reset)
{
this.halfWidth = Math.floor(this.width / 2);
this.halfHeight = Math.floor(this.height / 2);
this.updateCenter();
}
},
/**
* Update the Body's center from its position.
*
* @method Phaser.Physics.Arcade.Body#updateCenter
* @protected
*/
updateCenter: function () {
this.center.setTo(this.position.x + this.halfWidth, this.position.y + this.halfHeight);
},
/**
* Internal method.
*
* @method Phaser.Physics.Arcade.Body#preUpdate
* @protected
*/
preUpdate: function () {
if (!this.enable || this.game.physics.arcade.isPaused)
{
return;
}
this.dirty = true;
// Store and reset collision flags
this.wasTouching.none = this.touching.none;
this.wasTouching.up = this.touching.up;
this.wasTouching.down = this.touching.down;
this.wasTouching.left = this.touching.left;
this.wasTouching.right = this.touching.right;
this.touching.none = true;
this.touching.up = false;
this.touching.down = false;
this.touching.left = false;
this.touching.right = false;
this.blocked.none = true;
this.blocked.up = false;
this.blocked.down = false;
this.blocked.left = false;
this.blocked.right = false;
this.overlapR = 0;
this.overlapX = 0;
this.overlapY = 0;
this.embedded = false;
this.updateBounds();
this.position.x = (this.sprite.world.x - (this.sprite.anchor.x * this.sprite.width)) + this.sprite.scale.x * this.offset.x;
this.position.x -= this.sprite.scale.x < 0 ? this.width : 0;
this.position.y = (this.sprite.world.y - (this.sprite.anchor.y * this.sprite.height)) + this.sprite.scale.y * this.offset.y;
this.position.y -= this.sprite.scale.y < 0 ? this.height : 0;
this.updateCenter();
this.rotation = this.sprite.angle;
this.preRotation = this.rotation;
if (this._reset || this.sprite.fresh)
{
this.prev.x = this.position.x;
this.prev.y = this.position.y;
}
if (this.moves)
{
this.game.physics.arcade.updateMotion(this);
this.newVelocity.set(this.velocity.x * this.game.time.physicsElapsed, this.velocity.y * this.game.time.physicsElapsed);
this.position.x += this.newVelocity.x;
this.position.y += this.newVelocity.y;
this.updateCenter();
if (this.position.x !== this.prev.x || this.position.y !== this.prev.y)
{
this.angle = Math.atan2(this.velocity.y, this.velocity.x);
}
this.speed = Math.sqrt(this.velocity.x * this.velocity.x + this.velocity.y * this.velocity.y);
// Now the State update will throw collision checks at the Body
// And finally we'll integrate the new position back to the Sprite in postUpdate
if (this.collideWorldBounds)
{
if (this.checkWorldBounds() && this.onWorldBounds)
{
this.onWorldBounds.dispatch(this.sprite, this.blocked.up, this.blocked.down, this.blocked.left, this.blocked.right);
}
}
}
this._dx = this.deltaX();
this._dy = this.deltaY();
this._reset = false;
},
/**
* Internal method.
*
* @method Phaser.Physics.Arcade.Body#updateMovement
* @protected
*/
updateMovement: function () {
var percent = 0;
var collided = (this.overlapX !== 0 || this.overlapY !== 0);
// Duration or Distance based?
if (this.moveDuration > 0)
{
this.moveTimer += this.game.time.elapsedMS;
percent = this.moveTimer / this.moveDuration;
}
else
{
this.moveTarget.end.set(this.position.x, this.position.y);
percent = this.moveTarget.length / this.moveDistance;
}
if (this.movementCallback)
{
var result = this.movementCallback.call(this.movementCallbackContext, this, this.velocity, percent);
}
if (collided || percent >= 1 || (result !== undefined && result !== true))
{
this.stopMovement((percent >= 1) || (this.stopVelocityOnCollide && collided));
return false;
}
return true;
},
/**
* If this Body is moving as a result of a call to `moveTo` or `moveFrom` (i.e. it
* has Body.isMoving true), then calling this method will stop the movement before
* either the duration or distance counters expire.
*
* The `onMoveComplete` signal is dispatched.
*
* @method Phaser.Physics.Arcade.Body#stopMovement
* @param {boolean} [stopVelocity] - Should the Body.velocity be set to zero?
*/
stopMovement: function (stopVelocity) {
if (this.isMoving)
{
this.isMoving = false;
if (stopVelocity)
{
this.velocity.set(0);
}
// Send the Sprite this Body belongs to
// and a boolean indicating if it stopped because of a collision or not
this.onMoveComplete.dispatch(this.sprite, (this.overlapX !== 0 || this.overlapY !== 0));
}
},
/**
* Internal method.
*
* @method Phaser.Physics.Arcade.Body#postUpdate
* @protected
*/
postUpdate: function () {
// Only allow postUpdate to be called once per frame
if (!this.enable || !this.dirty)
{
return;
}
// Moving?
if (this.isMoving)
{
this.updateMovement();
}
this.dirty = false;
if (this.deltaX() < 0)
{
this.facing = Phaser.LEFT;
}
else if (this.deltaX() > 0)
{
this.facing = Phaser.RIGHT;
}
if (this.deltaY() < 0)
{
this.facing = Phaser.UP;
}
else if (this.deltaY() > 0)
{
this.facing = Phaser.DOWN;
}
if (this.moves)
{
this._dx = this.deltaX();
this._dy = this.deltaY();
if (this.deltaMax.x !== 0 && this._dx !== 0)
{
if (this._dx < 0 && this._dx < -this.deltaMax.x)
{
this._dx = -this.deltaMax.x;
}
else if (this._dx > 0 && this._dx > this.deltaMax.x)
{
this._dx = this.deltaMax.x;
}
}
if (this.deltaMax.y !== 0 && this._dy !== 0)
{
if (this._dy < 0 && this._dy < -this.deltaMax.y)
{
this._dy = -this.deltaMax.y;
}
else if (this._dy > 0 && this._dy > this.deltaMax.y)
{
this._dy = this.deltaMax.y;
}
}
this.sprite.position.x += this._dx;
this.sprite.position.y += this._dy;
this._reset = true;
}
this.updateCenter();
if (this.allowRotation)
{
this.sprite.angle += this.deltaZ();
}
this.prev.x = this.position.x;
this.prev.y = this.position.y;
},
/**
* Internal method.
*
* @method Phaser.Physics.Arcade.Body#checkWorldBounds
* @protected
* @return {boolean} True if the Body collided with the world bounds, otherwise false.
*/
checkWorldBounds: function () {
var pos = this.position;
var bounds = this.game.physics.arcade.bounds;
var check = this.game.physics.arcade.checkCollision;
var bx = (this.worldBounce) ? -this.worldBounce.x : -this.bounce.x;
var by = (this.worldBounce) ? -this.worldBounce.y : -this.bounce.y;
if (pos.x < bounds.x && check.left)
{
pos.x = bounds.x;
this.velocity.x *= bx;
this.blocked.left = true;
this.blocked.none = false;
}
else if (this.right > bounds.right && check.right)
{
pos.x = bounds.right - this.width;
this.velocity.x *= bx;
this.blocked.right = true;
this.blocked.none = false;
}
if (pos.y < bounds.y && check.up)
{
pos.y = bounds.y;
this.velocity.y *= by;
this.blocked.up = true;
this.blocked.none = false;
}
else if (this.bottom > bounds.bottom && check.down)
{
pos.y = bounds.bottom - this.height;
this.velocity.y *= by;
this.blocked.down = true;
this.blocked.none = false;
}
return !this.blocked.none;
},
/**
* Note: This method is experimental, and may be changed or removed in a future release.
*
* This method moves the Body in the given direction, for the duration specified.
* It works by setting the velocity on the Body, and an internal timer, and then
* monitoring the duration each frame. When the duration is up the movement is
* stopped and the `Body.onMoveComplete` signal is dispatched.
*
* Movement also stops if the Body collides or overlaps with any other Body.
*
* You can control if the velocity should be reset to zero on collision, by using
* the property `Body.stopVelocityOnCollide`.
*
* Stop the movement at any time by calling `Body.stopMovement`.
*
* You can optionally set a speed in pixels per second. If not specified it
* will use the current `Body.speed` value. If this is zero, the function will return false.
*
* Please note that due to browser timings you should allow for a variance in
* when the duration will actually expire. Depending on system it may be as much as
* +- 50ms. Also this method doesn't take into consideration any other forces acting
* on the Body, such as Gravity, drag or maxVelocity, all of which may impact the
* movement.
*
* @method Phaser.Physics.Arcade.Body#moveFrom
* @param {integer} duration - The duration of the movement, in ms.
* @param {integer} [speed] - The speed of the movement, in pixels per second. If not provided `Body.speed` is used.
* @param {integer} [direction] - The angle of movement. If not provided `Body.angle` is used.
* @return {boolean} True if the movement successfully started, otherwise false.
*/
moveFrom: function (duration, speed, direction) {
if (speed === undefined) { speed = this.speed; }
if (speed === 0)
{
return false;
}
var angle;
if (direction === undefined)
{
angle = this.angle;
direction = this.game.math.radToDeg(angle);
}
else
{
angle = this.game.math.degToRad(direction);
}
this.moveTimer = 0;
this.moveDuration = duration;
// Avoid sin/cos
if (direction === 0 || direction === 180)
{
this.velocity.set(Math.cos(angle) * speed, 0);
}
else if (direction === 90 || direction === 270)
{
this.velocity.set(0, Math.sin(angle) * speed);
}
else
{
this.velocity.setToPolar(angle, speed);
}
this.isMoving = true;
return true;
},
/**
* Note: This method is experimental, and may be changed or removed in a future release.
*
* This method moves the Body in the given direction, for the duration specified.
* It works by setting the velocity on the Body, and an internal distance counter.
* The distance is monitored each frame. When the distance equals the distance
* specified in this call, the movement is stopped, and the `Body.onMoveComplete`
* signal is dispatched.
*
* Movement also stops if the Body collides or overlaps with any other Body.
*
* You can control if the velocity should be reset to zero on collision, by using
* the property `Body.stopVelocityOnCollide`.
*
* Stop the movement at any time by calling `Body.stopMovement`.
*
* Please note that due to browser timings you should allow for a variance in
* when the distance will actually expire.
*
* Note: This method doesn't take into consideration any other forces acting
* on the Body, such as Gravity, drag or maxVelocity, all of which may impact the
* movement.
*
* @method Phaser.Physics.Arcade.Body#moveTo
* @param {integer} duration - The duration of the movement, in ms.
* @param {integer} distance - The distance, in pixels, the Body will move.
* @param {integer} [direction] - The angle of movement. If not provided `Body.angle` is used.
* @return {boolean} True if the movement successfully started, otherwise false.
*/
moveTo: function (duration, distance, direction) {
var speed = distance / (duration / 1000);
if (speed === 0)
{
return false;
}
var angle;
if (direction === undefined)
{
angle = this.angle;
direction = this.game.math.radToDeg(angle);
}
else
{
angle = this.game.math.degToRad(direction);
}