2018-03-17 18:07:05 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
|
|
|
* @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');
|
|
|
|
|
|
|
|
/**
|
2018-03-21 03:16:36 +00:00
|
|
|
* [description]
|
2018-03-17 18:07:05 +00:00
|
|
|
*
|
2018-03-21 03:16:36 +00:00
|
|
|
* @function hasGetterOrSetter
|
|
|
|
* @private
|
|
|
|
*
|
|
|
|
* @param {object} def - The object to check.
|
2018-03-17 18:07:05 +00:00
|
|
|
*
|
2018-03-21 03:16:36 +00:00
|
|
|
* @return {boolean} True if it has a getter or setter, otherwise false.
|
|
|
|
*/
|
|
|
|
function hasGetterOrSetter (def)
|
|
|
|
{
|
|
|
|
return (!!def.get && typeof def.get === 'function') || (!!def.set && typeof def.set === 'function');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @function Phaser.Physics.Matter.MatterGameObject
|
|
|
|
* @since 3.3.0
|
2018-03-17 18:07:05 +00:00
|
|
|
*
|
|
|
|
* @param {Phaser.Physics.Matter.World} world - [description]
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - [description]
|
|
|
|
* @param {object} options - [description]
|
2018-03-21 03:16:36 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} [description]
|
2018-03-17 18:07:05 +00:00
|
|
|
*/
|
2018-03-21 03:16:36 +00:00
|
|
|
var MatterGameObject = function (world, gameObject, options)
|
|
|
|
{
|
|
|
|
if (options === undefined) { options = {}; }
|
|
|
|
|
|
|
|
var x = gameObject.x;
|
|
|
|
var y = gameObject.y;
|
|
|
|
|
|
|
|
// Temp body pos to avoid body null checks
|
|
|
|
gameObject.body = {
|
|
|
|
position: {
|
|
|
|
x: x,
|
|
|
|
y: y
|
|
|
|
}
|
|
|
|
};
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
var mixins = [
|
2018-03-17 18:07:05 +00:00
|
|
|
Components.Bounce,
|
|
|
|
Components.Collision,
|
|
|
|
Components.Force,
|
|
|
|
Components.Friction,
|
|
|
|
Components.Gravity,
|
|
|
|
Components.Mass,
|
|
|
|
Components.Sensor,
|
|
|
|
Components.SetBody,
|
|
|
|
Components.Sleep,
|
|
|
|
Components.Static,
|
|
|
|
Components.Transform,
|
|
|
|
Components.Velocity
|
2018-03-21 03:16:36 +00:00
|
|
|
];
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
// First let's inject all of the components into the Game Object
|
|
|
|
mixins.forEach(function (mixin) {
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
for (var key in mixin)
|
|
|
|
{
|
|
|
|
if (hasGetterOrSetter(mixin[key]))
|
|
|
|
{
|
|
|
|
Object.defineProperty(gameObject, key, {
|
|
|
|
get: mixin[key].get,
|
|
|
|
set: mixin[key].set
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Object.defineProperty(gameObject, key, {
|
|
|
|
value: mixin[key]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
});
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
gameObject.world = world;
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
gameObject._tempVec2 = new Vector2(x, y);
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
var shape = GetFastValue(options, 'shape', null);
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
if (!shape)
|
|
|
|
{
|
|
|
|
shape = 'rectangle';
|
2018-03-17 18:07:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 03:16:36 +00:00
|
|
|
gameObject.setBody(shape, options);
|
|
|
|
|
|
|
|
return gameObject;
|
|
|
|
};
|
2018-03-17 18:07:05 +00:00
|
|
|
|
|
|
|
module.exports = MatterGameObject;
|