Skip to content

Commit b5cc011

Browse files
committed
Body.move tests.
1 parent 834a8df commit b5cc011

1 file changed

Lines changed: 83 additions & 0 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ Phaser.Physics.Arcade.Body = function (sprite) {
337337
*/
338338
this.syncBounds = false;
339339

340+
this.isMoving = false;
341+
this.target = new Phaser.Line();
342+
this._v = { x: 0, y: 0 };
343+
this.moveDistance = 0;
344+
this.moveEnd = new Phaser.Point();
345+
this.onMoveComplete = new Phaser.Signal();
346+
340347
/**
341348
* @property {boolean} _reset - Internal cache var.
342349
* @private
@@ -500,6 +507,36 @@ Phaser.Physics.Arcade.Body.prototype = {
500507

501508
},
502509

510+
/**
511+
* Internal method.
512+
*
513+
* @method Phaser.Physics.Arcade.Body#updateMovement
514+
* @protected
515+
*/
516+
updateMovement: function () {
517+
518+
this.target.end.setTo(this.position.x, this.position.y);
519+
520+
if (this.target.length >= this.moveDistance || this.overlapX !== 0 || this.overlapY !== 0)
521+
{
522+
if (this.overlapX === 0 && this.overlapY === 0)
523+
{
524+
this.velocity.set(0);
525+
this.position.x = this.moveEnd.x;
526+
this.position.y = this.moveEnd.y;
527+
}
528+
529+
this.isMoving = false;
530+
531+
this.onMoveComplete.dispatch(this.sprite, this.moveDistance);
532+
533+
return true;
534+
}
535+
536+
return false;
537+
538+
},
539+
503540
/**
504541
* Internal method.
505542
*
@@ -514,6 +551,12 @@ Phaser.Physics.Arcade.Body.prototype = {
514551
return;
515552
}
516553

554+
// Moving?
555+
if (this.isMoving)
556+
{
557+
this.updateMovement();
558+
}
559+
517560
this.dirty = false;
518561

519562
if (this.deltaX() < 0)
@@ -623,6 +666,46 @@ Phaser.Physics.Arcade.Body.prototype = {
623666

624667
},
625668

669+
move: function (direction, distance, duration) {
670+
671+
// Apply constant velocity to get the Body to move the distance required
672+
// over the duration required (unless it hits something), then cancel
673+
// the velocity. Overrides any currently set velocity.
674+
675+
var angle = this.game.math.degToRad(direction);
676+
var speed = distance / (duration / 1000);
677+
678+
this.moveDistance = distance;
679+
680+
this.target.fromAngle(this.position.x, this.position.y, angle, distance);
681+
682+
this.moveEnd.set(this.target.end.x, this.target.end.y);
683+
684+
this.target.setTo(this.x, this.y, this.x, this.y);
685+
686+
console.log('distance', distance, 'duration', duration, 'speed', speed);
687+
688+
// Avoid sin/cos for right-angled shots
689+
if (direction === 0 || direction === 180)
690+
{
691+
this.velocity.set(Math.cos(angle) * speed, 0);
692+
}
693+
else if (direction === 90 || direction === 270)
694+
{
695+
this.velocity.set(0, Math.sin(angle) * speed);
696+
}
697+
else
698+
{
699+
this.velocity.set(Math.cos(angle) * speed, Math.sin(angle) * speed);
700+
}
701+
702+
this._v.x = this.velocity.x;
703+
this._v.y = this.velocity.y;
704+
705+
this.isMoving = true;
706+
707+
},
708+
626709
/**
627710
* You can modify the size of the physics Body to be any dimension you need.
628711
* This allows you to make it smaller, or larger, than the parent Sprite.

0 commit comments

Comments
 (0)