Skip to content

Commit 952b5c0

Browse files
committed
The start of Arcade Physics circle bodies.
1 parent b9f62e7 commit 952b5c0

1 file changed

Lines changed: 62 additions & 7 deletions

File tree

src/physics/arcade/Body.js

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ Phaser.Physics.Arcade.Body = function (sprite) {
3535
*/
3636
this.enable = true;
3737

38+
/**
39+
* @property {boolean} isCircle - Testing.
40+
* @default
41+
*/
42+
this.isCircle = false;
43+
44+
this.radius = 0;
45+
3846
/**
3947
* @property {Phaser.Point} offset - The offset of the Physics Body from the Sprite x/y position.
4048
*/
@@ -569,7 +577,10 @@ Phaser.Physics.Arcade.Body.prototype = {
569577
},
570578

571579
/**
572-
* Removes this bodys reference to its parent sprite, freeing it up for gc.
580+
* Destroys this Body.
581+
*
582+
* First it calls Group.removeFromHash if the Game Object this Body belongs to is part of a Group.
583+
* Then it nulls the Game Objects body reference, and nulls this Body.sprite reference.
573584
*
574585
* @method Phaser.Physics.Arcade.Body#destroy
575586
*/
@@ -694,7 +705,9 @@ Phaser.Physics.Arcade.Body.prototype = {
694705
* @return {boolean} True if the given coordinates are inside this Body, otherwise false.
695706
*/
696707
hitTest: function (x, y) {
697-
return Phaser.Rectangle.contains(this, x, y);
708+
709+
return (this.isCircle) ? Phaser.Circle.contains(this, x, y) : Phaser.Rectangle.contains(this, x, y);
710+
698711
},
699712

700713
/**
@@ -704,7 +717,9 @@ Phaser.Physics.Arcade.Body.prototype = {
704717
* @return {boolean} True if in contact with either the world bounds or a tile.
705718
*/
706719
onFloor: function () {
720+
707721
return this.blocked.down;
722+
708723
},
709724

710725
/**
@@ -714,7 +729,9 @@ Phaser.Physics.Arcade.Body.prototype = {
714729
* @return {boolean} True if in contact with either the world bounds or a tile.
715730
*/
716731
onCeiling: function(){
732+
717733
return this.blocked.up;
734+
718735
},
719736

720737
/**
@@ -724,7 +741,9 @@ Phaser.Physics.Arcade.Body.prototype = {
724741
* @return {boolean} True if in contact with either the world bounds or a tile.
725742
*/
726743
onWall: function () {
744+
727745
return (this.blocked.left || this.blocked.right);
746+
728747
},
729748

730749
/**
@@ -734,7 +753,9 @@ Phaser.Physics.Arcade.Body.prototype = {
734753
* @return {number} The absolute delta value.
735754
*/
736755
deltaAbsX: function () {
756+
737757
return (this.deltaX() > 0 ? this.deltaX() : -this.deltaX());
758+
738759
},
739760

740761
/**
@@ -744,7 +765,9 @@ Phaser.Physics.Arcade.Body.prototype = {
744765
* @return {number} The absolute delta value.
745766
*/
746767
deltaAbsY: function () {
768+
747769
return (this.deltaY() > 0 ? this.deltaY() : -this.deltaY());
770+
748771
},
749772

750773
/**
@@ -754,7 +777,9 @@ Phaser.Physics.Arcade.Body.prototype = {
754777
* @return {number} The delta value. Positive if the motion was to the right, negative if to the left.
755778
*/
756779
deltaX: function () {
780+
757781
return this.position.x - this.prev.x;
782+
758783
},
759784

760785
/**
@@ -764,7 +789,9 @@ Phaser.Physics.Arcade.Body.prototype = {
764789
* @return {number} The delta value. Positive if the motion was downwards, negative if upwards.
765790
*/
766791
deltaY: function () {
792+
767793
return this.position.y - this.prev.y;
794+
768795
},
769796

770797
/**
@@ -774,7 +801,9 @@ Phaser.Physics.Arcade.Body.prototype = {
774801
* @return {number} The delta value. Positive if the motion was clockwise, negative if anti-clockwise.
775802
*/
776803
deltaZ: function () {
804+
777805
return this.rotation - this.preRotation;
806+
778807
}
779808

780809
};
@@ -787,7 +816,9 @@ Phaser.Physics.Arcade.Body.prototype = {
787816
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
788817

789818
get: function () {
819+
790820
return this.position.y + this.height;
821+
791822
}
792823

793824
});
@@ -800,7 +831,9 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "bottom", {
800831
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
801832

802833
get: function () {
834+
803835
return this.position.x + this.width;
836+
804837
}
805838

806839
});
@@ -812,7 +845,9 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "right", {
812845
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "x", {
813846

814847
get: function () {
848+
815849
return this.position.x;
850+
816851
},
817852

818853
set: function (value) {
@@ -829,7 +864,9 @@ Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "x", {
829864
Object.defineProperty(Phaser.Physics.Arcade.Body.prototype, "y", {
830865

831866
get: function () {
867+
832868
return this.position.y;
869+
833870
},
834871

835872
set: function (value) {
@@ -855,15 +892,33 @@ Phaser.Physics.Arcade.Body.render = function (context, body, color, filled) {
855892

856893
color = color || 'rgba(0,255,0,0.4)';
857894

858-
if (filled)
895+
if (this.isCircle)
859896
{
860-
context.fillStyle = color;
861-
context.fillRect(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.width, body.height);
897+
context.beginPath();
898+
context.arc(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.radius, 0, 6.283185307179586, false);
899+
context.closePath();
900+
901+
if (filled)
902+
{
903+
context.fillStyle = color;
904+
}
905+
else
906+
{
907+
context.strokeStyle = color;
908+
}
862909
}
863910
else
864911
{
865-
context.strokeStyle = color;
866-
context.strokeRect(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.width, body.height);
912+
if (filled)
913+
{
914+
context.fillStyle = color;
915+
context.fillRect(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.width, body.height);
916+
}
917+
else
918+
{
919+
context.strokeStyle = color;
920+
context.strokeRect(body.position.x - body.game.camera.x, body.position.y - body.game.camera.y, body.width, body.height);
921+
}
867922
}
868923

869924
};

0 commit comments

Comments
 (0)