From e64981c51d98488b15f697cba2d6f39d44200516 Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Wed, 16 Aug 2017 00:30:12 +0100 Subject: [PATCH] Added Gravity component and new ImpactBody helper class --- v3/src/checksum.js | 2 +- v3/src/physics/impact/Body.js | 27 +++++++++++++++++ v3/src/physics/impact/Factory.js | 5 ++-- v3/src/physics/impact/ImpactBody.js | 32 +++++++++++++++++++++ v3/src/physics/impact/ImpactImage.js | 1 + v3/src/physics/impact/ImpactSprite.js | 1 + v3/src/physics/impact/components/Bounce.js | 9 +++++- v3/src/physics/impact/components/Gravity.js | 26 +++++++++++++++++ v3/src/physics/impact/components/index.js | 1 + 9 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 v3/src/physics/impact/ImpactBody.js create mode 100644 v3/src/physics/impact/components/Gravity.js diff --git a/v3/src/checksum.js b/v3/src/checksum.js index c2c4dc04c..7a23f2041 100644 --- a/v3/src/checksum.js +++ b/v3/src/checksum.js @@ -1,4 +1,4 @@ var CHECKSUM = { -build: '3cc5e4b0-820c-11e7-8573-1960fd3f13b3' +build: 'c897f920-8210-11e7-91c0-5f772e8c4462' }; module.exports = CHECKSUM; \ No newline at end of file diff --git a/v3/src/physics/impact/Body.js b/v3/src/physics/impact/Body.js index d0d632fbd..d22ffde9b 100644 --- a/v3/src/physics/impact/Body.js +++ b/v3/src/physics/impact/Body.js @@ -1,6 +1,7 @@ // Phaser.Physics.Impact.Body var Class = require('../../utils/Class'); +var Components = require('./Components'); var COLLIDES = require('./COLLIDES'); var GetVelocity = require('./GetVelocity'); var TYPE = require('./TYPE'); @@ -114,6 +115,32 @@ var Body = new Class({ return this; }, + toJSON: function () + { + var output = { + name: this.name, + size: { x: this.size.x, y: this.size.y }, + pos: { x: this.pos.x, y: this.pos.y }, + vel: { x: this.vel.x, y: this.vel.y }, + accel: { x: this.accel.x, y: this.accel.y }, + friction: { x: this.friction.x, y: this.friction.y }, + maxVel: { x: this.maxVel.x, y: this.maxVel.y }, + gravityFactor: this.gravityFactor, + bounciness: this.bounciness, + minBounceVelocity: this.minBounceVelocity, + type: this.type, + checkAgainst: this.checkAgainst, + collides: this.collides + }; + + return output; + }, + + fromJSON: function (config) + { + // TODO + }, + check: function (other) { // Overridden by user code diff --git a/v3/src/physics/impact/Factory.js b/v3/src/physics/impact/Factory.js index 469310f38..28c781dda 100644 --- a/v3/src/physics/impact/Factory.js +++ b/v3/src/physics/impact/Factory.js @@ -1,4 +1,5 @@ var Class = require('../../utils/Class'); +var ImpactBody = require('./ImpactBody'); var ImpactImage = require('./ImpactImage'); var ImpactSprite = require('./ImpactSprite'); @@ -13,9 +14,9 @@ var Factory = new Class({ this.sys = world.scene.sys; }, - body: function (x, y, sizeX, sizeY) + body: function (x, y, width, height) { - return this.world.create(x, y, sizeX, sizeY); + return new ImpactBody(this.world, x, y, width, height); }, image: function (x, y, key, frame) diff --git a/v3/src/physics/impact/ImpactBody.js b/v3/src/physics/impact/ImpactBody.js new file mode 100644 index 000000000..c5c6e0618 --- /dev/null +++ b/v3/src/physics/impact/ImpactBody.js @@ -0,0 +1,32 @@ + +var Class = require('../../utils/Class'); +var Components = require('./Components'); + +var ImpactBody = new Class({ + + Mixins: [ + Components.Acceleration, + Components.BodyType, + Components.Bounce, + Components.CheckAgainst, + Components.Collides, + Components.Gravity, + Components.Velocity + ], + + initialize: + + function ImpactBody (world, x, y, width, height) + { + this.body = world.create(x, y, width, height); + + // Local references to the Body properties + this.vel = this.body.vel; + this.accel = this.body.accel; + this.friction = this.body.friction; + this.maxVel = this.body.maxVel; + } + +}); + +module.exports = ImpactBody; diff --git a/v3/src/physics/impact/ImpactImage.js b/v3/src/physics/impact/ImpactImage.js index e498cbd67..39d27f830 100644 --- a/v3/src/physics/impact/ImpactImage.js +++ b/v3/src/physics/impact/ImpactImage.js @@ -13,6 +13,7 @@ var ImpactImage = new Class({ Components.Bounce, Components.CheckAgainst, Components.Collides, + Components.Gravity, Components.Velocity ], diff --git a/v3/src/physics/impact/ImpactSprite.js b/v3/src/physics/impact/ImpactSprite.js index 4dc7a2af6..852d423b0 100644 --- a/v3/src/physics/impact/ImpactSprite.js +++ b/v3/src/physics/impact/ImpactSprite.js @@ -13,6 +13,7 @@ var ImpactSprite = new Class({ Components.Bounce, Components.CheckAgainst, Components.Collides, + Components.Gravity, Components.Velocity ], diff --git a/v3/src/physics/impact/components/Bounce.js b/v3/src/physics/impact/components/Bounce.js index 7a9886529..63bee9b2c 100644 --- a/v3/src/physics/impact/components/Bounce.js +++ b/v3/src/physics/impact/components/Bounce.js @@ -7,7 +7,14 @@ var Bounce = { return this; }, - bounciness: { + setMinBounceVelocity: function (value) + { + this.body.minBounceVelocity = value; + + return this; + }, + + bounce: { get: function () { diff --git a/v3/src/physics/impact/components/Gravity.js b/v3/src/physics/impact/components/Gravity.js new file mode 100644 index 000000000..8332006fc --- /dev/null +++ b/v3/src/physics/impact/components/Gravity.js @@ -0,0 +1,26 @@ +var Gravity = { + + setGravity: function (value) + { + this.body.gravityFactor = value; + + return this; + }, + + gravity: { + + get: function () + { + return this.body.gravityFactor; + }, + + set: function (value) + { + this.body.gravityFactor = value; + } + + } + +}; + +module.exports = Gravity; diff --git a/v3/src/physics/impact/components/index.js b/v3/src/physics/impact/components/index.js index db2d9e11e..09793f0a4 100644 --- a/v3/src/physics/impact/components/index.js +++ b/v3/src/physics/impact/components/index.js @@ -7,6 +7,7 @@ module.exports = { Bounce: require('./Bounce'), CheckAgainst: require('./CheckAgainst'), Collides: require('./Collides'), + Gravity: require('./Gravity'), Velocity: require('./Velocity') };