2018-03-17 18:07:05 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2020-01-15 12:07:09 +00:00
|
|
|
* @copyright 2020 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-03-17 18:07:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
var Components = require('./components');
|
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
|
|
|
var Vector2 = require('../../math/Vector2');
|
|
|
|
|
|
|
|
/**
|
2018-09-28 11:19:21 +00:00
|
|
|
* Internal function to check if the object has a getter or setter.
|
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');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-05 23:55:45 +00:00
|
|
|
* A Matter Game Object is a generic object that allows you to combine any Phaser Game Object,
|
|
|
|
* including those you have extended or created yourself, with all of the Matter Components.
|
|
|
|
*
|
|
|
|
* This enables you to use component methods such as `setVelocity` or `isSensor` directly from
|
|
|
|
* this Game Object.
|
2018-03-21 03:16:36 +00:00
|
|
|
*
|
|
|
|
* @function Phaser.Physics.Matter.MatterGameObject
|
|
|
|
* @since 3.3.0
|
2018-03-17 18:07:05 +00:00
|
|
|
*
|
2018-09-28 11:19:21 +00:00
|
|
|
* @param {Phaser.Physics.Matter.World} world - The Matter world to add the body to.
|
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object that will have the Matter body applied to it.
|
2020-01-08 11:14:40 +00:00
|
|
|
* @param {(Phaser.Types.Physics.Matter.MatterBodyConfig|MatterJS.Body)} [options] - A Matter Body configuration object, or an instance of a Matter Body.
|
2019-12-17 13:08:32 +00:00
|
|
|
* @param {boolean} [addToWorld=true] - Should the newly created body be immediately added to the World?
|
2018-03-21 03:16:36 +00:00
|
|
|
*
|
2018-09-28 11:19:21 +00:00
|
|
|
* @return {Phaser.GameObjects.GameObject} The Game Object that was created with the Matter body.
|
2018-03-17 18:07:05 +00:00
|
|
|
*/
|
2019-12-12 17:15:00 +00:00
|
|
|
var MatterGameObject = function (world, gameObject, options, addToWorld)
|
2018-03-21 03:16:36 +00:00
|
|
|
{
|
|
|
|
if (options === undefined) { options = {}; }
|
2019-12-12 17:15:00 +00:00
|
|
|
if (addToWorld === undefined) { addToWorld = true; }
|
2018-03-21 03:16:36 +00:00
|
|
|
|
|
|
|
var x = gameObject.x;
|
|
|
|
var y = gameObject.y;
|
|
|
|
|
|
|
|
// Temp body pos to avoid body null checks
|
|
|
|
gameObject.body = {
|
2018-09-26 13:15:38 +00:00
|
|
|
temp: true,
|
2018-03-21 03:16:36 +00:00
|
|
|
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
|
2018-03-22 13:22:23 +00:00
|
|
|
mixins.forEach(function (mixin)
|
|
|
|
{
|
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
|
|
|
|
{
|
2018-03-22 13:22:23 +00:00
|
|
|
Object.defineProperty(gameObject, key, {value: mixin[key]});
|
2018-03-21 03:16:36 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2019-06-01 09:53:05 +00:00
|
|
|
if (options.hasOwnProperty('type') && options.type === 'body')
|
2018-03-21 03:16:36 +00:00
|
|
|
{
|
2019-12-12 17:15:00 +00:00
|
|
|
gameObject.setExistingBody(options, addToWorld);
|
2018-03-17 18:07:05 +00:00
|
|
|
}
|
2019-06-01 09:53:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
var shape = GetFastValue(options, 'shape', null);
|
2018-03-17 18:07:05 +00:00
|
|
|
|
2019-06-01 09:53:05 +00:00
|
|
|
if (!shape)
|
|
|
|
{
|
|
|
|
shape = 'rectangle';
|
|
|
|
}
|
2019-12-17 13:08:32 +00:00
|
|
|
|
|
|
|
options.addToWorld = addToWorld;
|
2019-06-01 09:53:05 +00:00
|
|
|
|
|
|
|
gameObject.setBody(shape, options);
|
|
|
|
}
|
2018-03-21 03:16:36 +00:00
|
|
|
|
|
|
|
return gameObject;
|
|
|
|
};
|
2018-03-17 18:07:05 +00:00
|
|
|
|
|
|
|
module.exports = MatterGameObject;
|