Skip to content

Commit 3d4be64

Browse files
committed
Testing MatterGameObject
1 parent 7e26fdb commit 3d4be64

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

src/physics/matter-js/Factory.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var Bodies = require('./lib/factory/Bodies');
88
var Class = require('../../utils/Class');
99
var Composites = require('./lib/factory/Composites');
1010
var Constraint = require('./lib/constraint/Constraint');
11+
var MatterGameObject = require('./MatterGameObject');
1112
var MatterImage = require('./MatterImage');
1213
var MatterSprite = require('./MatterSprite');
1314
var MatterTileBody = require('./MatterTileBody');
@@ -540,6 +541,11 @@ var Factory = new Class({
540541
return image;
541542
},
542543

544+
gameObject: function (gameObject, options)
545+
{
546+
return new MatterGameObject(this.world, gameObject, options);
547+
},
548+
543549
/**
544550
* [description]
545551
*
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
5+
*/
6+
7+
var Bodies = require('./lib/factory/Bodies');
8+
var Class = require('../../utils/Class');
9+
var Components = require('./components');
10+
var GetFastValue = require('../../utils/object/GetFastValue');
11+
var Vector2 = require('../../math/Vector2');
12+
13+
/**
14+
* @classdesc
15+
* A Matter Physics Body applied to a Game Object.
16+
*
17+
* @class MatterGameObject
18+
* @memberOf Phaser.Physics.Matter
19+
* @constructor
20+
* @since 3.3.0
21+
*
22+
* @extends Phaser.Physics.Matter.Components.Bounce
23+
* @extends Phaser.Physics.Matter.Components.Collision
24+
* @extends Phaser.Physics.Matter.Components.Force
25+
* @extends Phaser.Physics.Matter.Components.Friction
26+
* @extends Phaser.Physics.Matter.Components.Gravity
27+
* @extends Phaser.Physics.Matter.Components.Mass
28+
* @extends Phaser.Physics.Matter.Components.Sensor
29+
* @extends Phaser.Physics.Matter.Components.SetBody
30+
* @extends Phaser.Physics.Matter.Components.Sleep
31+
* @extends Phaser.Physics.Matter.Components.Static
32+
* @extends Phaser.Physics.Matter.Components.Transform
33+
* @extends Phaser.Physics.Matter.Components.Velocity
34+
*
35+
* @param {Phaser.Physics.Matter.World} world - [description]
36+
* @param {Phaser.GameObjects.GameObject} gameObject - [description]
37+
* @param {object} options - [description]
38+
*/
39+
var MatterGameObject = new Class({
40+
41+
Mixins: [
42+
Components.Bounce,
43+
Components.Collision,
44+
Components.Force,
45+
Components.Friction,
46+
Components.Gravity,
47+
Components.Mass,
48+
Components.Sensor,
49+
Components.SetBody,
50+
Components.Sleep,
51+
Components.Static,
52+
Components.Transform,
53+
Components.Velocity
54+
],
55+
56+
initialize:
57+
58+
function MatterGameObject (world, gameObject, options)
59+
{
60+
this.gameObject = gameObject;
61+
62+
this.world = world;
63+
64+
this._tempVec2 = new Vector2(x, y);
65+
66+
var shape = GetFastValue(options, 'shape', null);
67+
68+
if (!shape)
69+
{
70+
this.body = Bodies.rectangle(gameObject.x, gameObject.y, gameObject.width, gameObject.height, options);
71+
72+
this.body.gameObject = this.gameObject;
73+
74+
if (GetFastValue(options, 'addToWorld', true))
75+
{
76+
world.add(this.body);
77+
}
78+
}
79+
else
80+
{
81+
this.setBody(shape, options);
82+
}
83+
}
84+
85+
});
86+
87+
module.exports = MatterGameObject;

0 commit comments

Comments
 (0)