From 3d4be64331333613c80a1a17438ff19fd1747dbb Mon Sep 17 00:00:00 2001 From: Richard Davey Date: Sat, 17 Mar 2018 18:07:05 +0000 Subject: [PATCH] Testing MatterGameObject --- src/physics/matter-js/Factory.js | 6 ++ src/physics/matter-js/MatterGameObject.js | 87 +++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 src/physics/matter-js/MatterGameObject.js diff --git a/src/physics/matter-js/Factory.js b/src/physics/matter-js/Factory.js index f604db342..67dce02fb 100644 --- a/src/physics/matter-js/Factory.js +++ b/src/physics/matter-js/Factory.js @@ -8,6 +8,7 @@ var Bodies = require('./lib/factory/Bodies'); var Class = require('../../utils/Class'); var Composites = require('./lib/factory/Composites'); var Constraint = require('./lib/constraint/Constraint'); +var MatterGameObject = require('./MatterGameObject'); var MatterImage = require('./MatterImage'); var MatterSprite = require('./MatterSprite'); var MatterTileBody = require('./MatterTileBody'); @@ -540,6 +541,11 @@ var Factory = new Class({ return image; }, + gameObject: function (gameObject, options) + { + return new MatterGameObject(this.world, gameObject, options); + }, + /** * [description] * diff --git a/src/physics/matter-js/MatterGameObject.js b/src/physics/matter-js/MatterGameObject.js new file mode 100644 index 000000000..a57637600 --- /dev/null +++ b/src/physics/matter-js/MatterGameObject.js @@ -0,0 +1,87 @@ +/** + * @author Richard Davey + * @copyright 2018 Photon Storm Ltd. + * @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License} + */ + +var Bodies = require('./lib/factory/Bodies'); +var Class = require('../../utils/Class'); +var Components = require('./components'); +var GetFastValue = require('../../utils/object/GetFastValue'); +var Vector2 = require('../../math/Vector2'); + +/** + * @classdesc + * A Matter Physics Body applied to a Game Object. + * + * @class MatterGameObject + * @memberOf Phaser.Physics.Matter + * @constructor + * @since 3.3.0 + * + * @extends Phaser.Physics.Matter.Components.Bounce + * @extends Phaser.Physics.Matter.Components.Collision + * @extends Phaser.Physics.Matter.Components.Force + * @extends Phaser.Physics.Matter.Components.Friction + * @extends Phaser.Physics.Matter.Components.Gravity + * @extends Phaser.Physics.Matter.Components.Mass + * @extends Phaser.Physics.Matter.Components.Sensor + * @extends Phaser.Physics.Matter.Components.SetBody + * @extends Phaser.Physics.Matter.Components.Sleep + * @extends Phaser.Physics.Matter.Components.Static + * @extends Phaser.Physics.Matter.Components.Transform + * @extends Phaser.Physics.Matter.Components.Velocity + * + * @param {Phaser.Physics.Matter.World} world - [description] + * @param {Phaser.GameObjects.GameObject} gameObject - [description] + * @param {object} options - [description] + */ +var MatterGameObject = new Class({ + + Mixins: [ + Components.Bounce, + Components.Collision, + Components.Force, + Components.Friction, + Components.Gravity, + Components.Mass, + Components.Sensor, + Components.SetBody, + Components.Sleep, + Components.Static, + Components.Transform, + Components.Velocity + ], + + initialize: + + function MatterGameObject (world, gameObject, options) + { + this.gameObject = gameObject; + + this.world = world; + + this._tempVec2 = new Vector2(x, y); + + var shape = GetFastValue(options, 'shape', null); + + if (!shape) + { + this.body = Bodies.rectangle(gameObject.x, gameObject.y, gameObject.width, gameObject.height, options); + + this.body.gameObject = this.gameObject; + + if (GetFastValue(options, 'addToWorld', true)) + { + world.add(this.body); + } + } + else + { + this.setBody(shape, options); + } + } + +}); + +module.exports = MatterGameObject;