2018-02-12 16:01:20 +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}
|
|
|
|
*/
|
|
|
|
|
2017-11-21 02:04:05 +00:00
|
|
|
// Phaser.Physics.Matter.World
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2017-11-21 16:54:54 +00:00
|
|
|
var Bodies = require('./lib/factory/Bodies');
|
2017-11-20 16:54:26 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-11-27 03:45:46 +00:00
|
|
|
var Composite = require('./lib/body/Composite');
|
2017-11-21 16:54:54 +00:00
|
|
|
var Engine = require('./lib/core/Engine');
|
2018-01-12 17:09:09 +00:00
|
|
|
var EventEmitter = require('eventemitter3');
|
2017-11-21 16:54:54 +00:00
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
2017-11-22 02:25:30 +00:00
|
|
|
var GetValue = require('../../utils/object/GetValue');
|
|
|
|
var MatterBody = require('./lib/body/Body');
|
|
|
|
var MatterEvents = require('./lib/core/Events');
|
2017-11-21 16:54:54 +00:00
|
|
|
var MatterWorld = require('./lib/body/World');
|
2018-01-21 18:53:48 +00:00
|
|
|
var MatterTileBody = require('./MatterTileBody');
|
2017-11-20 16:54:26 +00:00
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @class World
|
2018-02-13 01:39:22 +00:00
|
|
|
* @extends EventEmitter
|
2018-02-12 13:48:38 +00:00
|
|
|
* @memberOf Phaser.Physics.Matter
|
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.Scene} scene - [description]
|
|
|
|
* @param {object} config - [description]
|
|
|
|
*/
|
2017-11-20 16:54:26 +00:00
|
|
|
var World = new Class({
|
|
|
|
|
2018-01-12 18:59:11 +00:00
|
|
|
Extends: EventEmitter,
|
|
|
|
|
2017-11-20 16:54:26 +00:00
|
|
|
initialize:
|
|
|
|
|
|
|
|
function World (scene, config)
|
|
|
|
{
|
2018-01-12 18:59:11 +00:00
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-20 16:54:26 +00:00
|
|
|
this.scene = scene;
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#engine
|
2018-03-19 15:01:14 +00:00
|
|
|
* @type {Matter.Engine}
|
2018-02-12 13:48:38 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
this.engine = Engine.create(config);
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
2018-03-19 15:01:14 +00:00
|
|
|
* A `World` composite object that will contain all simulated bodies and constraints.
|
2018-02-12 13:48:38 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#localWorld
|
2018-03-19 15:01:14 +00:00
|
|
|
* @type {Matter.World}
|
2018-02-12 13:48:38 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
this.localWorld = this.engine.world;
|
|
|
|
|
|
|
|
var gravity = GetValue(config, 'gravity', null);
|
|
|
|
|
|
|
|
if (gravity)
|
|
|
|
{
|
|
|
|
this.setGravity(gravity.x, gravity.y, gravity.scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-12 13:48:38 +00:00
|
|
|
* An object containing the 4 wall bodies that bound the physics world.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#walls
|
2018-03-19 15:01:14 +00:00
|
|
|
* @type {object}
|
2018-02-12 13:48:38 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
this.walls = { left: null, right: null, top: null, bottom: null };
|
2018-01-21 18:53:48 +00:00
|
|
|
|
2017-11-21 16:54:54 +00:00
|
|
|
if (GetFastValue(config, 'setBounds', false))
|
|
|
|
{
|
|
|
|
var boundsConfig = config['setBounds'];
|
|
|
|
|
|
|
|
if (typeof boundsConfig === 'boolean')
|
|
|
|
{
|
|
|
|
this.setBounds();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var x = GetFastValue(boundsConfig, 'x', 0);
|
|
|
|
var y = GetFastValue(boundsConfig, 'y', 0);
|
|
|
|
var width = GetFastValue(boundsConfig, 'width', scene.sys.game.config.width);
|
|
|
|
var height = GetFastValue(boundsConfig, 'height', scene.sys.game.config.height);
|
|
|
|
var thickness = GetFastValue(boundsConfig, 'thickness', 64);
|
|
|
|
var left = GetFastValue(boundsConfig, 'left', true);
|
|
|
|
var right = GetFastValue(boundsConfig, 'right', true);
|
|
|
|
var top = GetFastValue(boundsConfig, 'top', true);
|
|
|
|
var bottom = GetFastValue(boundsConfig, 'bottom', true);
|
|
|
|
|
|
|
|
this.setBounds(x, y, width, height, thickness, left, right, top, bottom);
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 02:25:30 +00:00
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#enabled
|
|
|
|
* @type {boolean}
|
|
|
|
* @default true
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
|
|
|
this.enabled = GetValue(config, 'enabled', true);
|
2017-11-27 03:45:46 +00:00
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#drawDebug
|
|
|
|
* @type {boolean}
|
|
|
|
* @default false
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
this.drawDebug = GetValue(config, 'debug', false);
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#debugGraphic
|
|
|
|
* @type {Phaser.GameObjects.Graphics}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
this.debugGraphic;
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Matter.World#defaults
|
|
|
|
* @type {object}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
this.defaults = {
|
|
|
|
debugShowBody: GetValue(config, 'debugShowBody', true),
|
|
|
|
debugShowStaticBody: GetValue(config, 'debugShowStaticBody', true),
|
|
|
|
debugShowVelocity: GetValue(config, 'debugShowVelocity', true),
|
|
|
|
bodyDebugColor: GetValue(config, 'debugBodyColor', 0xff00ff),
|
|
|
|
staticBodyDebugColor: GetValue(config, 'debugBodyColor', 0x0000ff),
|
|
|
|
velocityDebugColor: GetValue(config, 'debugVelocityColor', 0x00ff00)
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.drawDebug)
|
|
|
|
{
|
|
|
|
this.createDebugGraphic();
|
|
|
|
}
|
|
|
|
|
2017-11-22 02:25:30 +00:00
|
|
|
this.setEventsProxy();
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#setEventsProxy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-22 02:25:30 +00:00
|
|
|
setEventsProxy: function ()
|
|
|
|
{
|
2018-01-12 18:59:11 +00:00
|
|
|
var _this = this;
|
|
|
|
var engine = this.engine;
|
2017-11-22 02:25:30 +00:00
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
MatterEvents.on(engine, 'beforeUpdate', function (event)
|
|
|
|
{
|
2017-11-23 01:45:38 +00:00
|
|
|
|
2018-01-12 18:59:11 +00:00
|
|
|
_this.emit('beforeupdate', event);
|
2017-11-23 01:45:38 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
MatterEvents.on(engine, 'afterUpdate', function (event)
|
|
|
|
{
|
2017-11-23 01:45:38 +00:00
|
|
|
|
2018-01-12 18:59:11 +00:00
|
|
|
_this.emit('afterupdate', event);
|
2017-11-23 01:45:38 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
MatterEvents.on(engine, 'collisionStart', function (event)
|
|
|
|
{
|
2018-01-12 18:59:11 +00:00
|
|
|
|
|
|
|
var pairs = event.pairs;
|
|
|
|
var bodyA;
|
|
|
|
var bodyB;
|
2017-11-22 02:25:30 +00:00
|
|
|
|
2018-01-12 18:59:11 +00:00
|
|
|
if (pairs.length > 0)
|
|
|
|
{
|
|
|
|
bodyA = pairs[0].bodyA;
|
|
|
|
bodyB = pairs[0].bodyB;
|
|
|
|
}
|
|
|
|
|
|
|
|
_this.emit('collisionstart', event, bodyA, bodyB);
|
2017-11-22 02:25:30 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
MatterEvents.on(engine, 'collisionActive', function (event)
|
|
|
|
{
|
2017-11-22 02:25:30 +00:00
|
|
|
|
2018-01-12 18:59:11 +00:00
|
|
|
var pairs = event.pairs;
|
|
|
|
var bodyA;
|
|
|
|
var bodyB;
|
|
|
|
|
|
|
|
if (pairs.length > 0)
|
|
|
|
{
|
|
|
|
bodyA = pairs[0].bodyA;
|
|
|
|
bodyB = pairs[0].bodyB;
|
|
|
|
}
|
|
|
|
|
|
|
|
_this.emit('collisionactive', event, bodyA, bodyB);
|
2017-11-22 02:25:30 +00:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
MatterEvents.on(engine, 'collisionEnd', function (event)
|
|
|
|
{
|
2018-01-12 18:59:11 +00:00
|
|
|
|
|
|
|
var pairs = event.pairs;
|
|
|
|
var bodyA;
|
|
|
|
var bodyB;
|
|
|
|
|
|
|
|
if (pairs.length > 0)
|
|
|
|
{
|
|
|
|
bodyA = pairs[0].bodyA;
|
|
|
|
bodyB = pairs[0].bodyB;
|
|
|
|
}
|
2017-11-22 02:25:30 +00:00
|
|
|
|
2018-01-12 18:59:11 +00:00
|
|
|
_this.emit('collisionend', event, bodyA, bodyB);
|
2017-11-22 02:25:30 +00:00
|
|
|
|
|
|
|
});
|
2017-11-21 16:54:54 +00:00
|
|
|
},
|
2017-11-21 02:04:05 +00:00
|
|
|
|
2017-11-21 16:54:54 +00:00
|
|
|
/**
|
2018-02-12 13:48:38 +00:00
|
|
|
* Sets the bounds of the Physics world to match the given world pixel dimensions.
|
|
|
|
* You can optionally set which 'walls' to create: left, right, top or bottom.
|
|
|
|
* If none of the walls are given it will default to use the walls settings it had previously.
|
|
|
|
* I.e. if you previously told it to not have the left or right walls, and you then adjust the world size
|
|
|
|
* the newly created bounds will also not have the left and right walls.
|
|
|
|
* Explicitly state them in the parameters to override this.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#setBounds
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {number} [x=0] - The x coordinate of the top-left corner of the bounds.
|
|
|
|
* @param {number} [y=0] - The y coordinate of the top-left corner of the bounds.
|
|
|
|
* @param {number} [width] - The width of the bounds.
|
|
|
|
* @param {number} [height] - The height of the bounds.
|
2018-02-12 13:48:38 +00:00
|
|
|
* @param {number} [thickness=128] - The thickness of each wall, in pixels.
|
|
|
|
* @param {boolean} [left=true] - If true will create the left bounds wall.
|
|
|
|
* @param {boolean} [right=true] - If true will create the right bounds wall.
|
|
|
|
* @param {boolean} [top=true] - If true will create the top bounds wall.
|
|
|
|
* @param {boolean} [bottom=true] - If true will create the bottom bounds wall.
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
setBounds: function (x, y, width, height, thickness, left, right, top, bottom)
|
|
|
|
{
|
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 0; }
|
|
|
|
if (width === undefined) { width = this.scene.sys.game.config.width; }
|
|
|
|
if (height === undefined) { height = this.scene.sys.game.config.height; }
|
2017-11-23 14:59:15 +00:00
|
|
|
if (thickness === undefined) { thickness = 128; }
|
2017-11-21 16:54:54 +00:00
|
|
|
if (left === undefined) { left = true; }
|
|
|
|
if (right === undefined) { right = true; }
|
|
|
|
if (top === undefined) { top = true; }
|
|
|
|
if (bottom === undefined) { bottom = true; }
|
|
|
|
|
|
|
|
this.updateWall(left, 'left', x - thickness, y, thickness, height);
|
|
|
|
this.updateWall(right, 'right', x + width, y, thickness, height);
|
|
|
|
this.updateWall(top, 'top', x, y - thickness, width, thickness);
|
|
|
|
this.updateWall(bottom, 'bottom', x, y + height, width, thickness);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
2017-11-21 02:04:05 +00:00
|
|
|
|
2017-11-21 16:54:54 +00:00
|
|
|
// position = 'left', 'right', 'top' or 'bottom'
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#updateWall
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {boolean} add - [description]
|
|
|
|
* @param {string} position - [description]
|
|
|
|
* @param {number} x - [description]
|
|
|
|
* @param {number} y - [description]
|
|
|
|
* @param {number} width - [description]
|
|
|
|
* @param {number} height - [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
updateWall: function (add, position, x, y, width, height)
|
|
|
|
{
|
|
|
|
var wall = this.walls[position];
|
|
|
|
|
|
|
|
if (add)
|
|
|
|
{
|
|
|
|
if (wall)
|
|
|
|
{
|
2017-11-29 23:36:35 +00:00
|
|
|
MatterWorld.remove(this.localWorld, wall);
|
2017-11-21 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// adjust center
|
|
|
|
x += (width / 2);
|
|
|
|
y += (height / 2);
|
|
|
|
|
2017-11-27 03:45:46 +00:00
|
|
|
this.walls[position] = this.create(x, y, width, height, { isStatic: true, friction: 0, frictionStatic: 0 });
|
2017-11-21 16:54:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (wall)
|
|
|
|
{
|
2017-11-29 23:36:35 +00:00
|
|
|
MatterWorld.remove(this.localWorld, wall);
|
2017-11-21 16:54:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.walls[position] = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#createDebugGraphic
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.Graphics} [description]
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
createDebugGraphic: function ()
|
|
|
|
{
|
|
|
|
var graphic = this.scene.sys.add.graphics({ x: 0, y: 0 });
|
|
|
|
|
2018-01-18 14:59:32 +00:00
|
|
|
graphic.setZ(Number.MAX_VALUE);
|
2017-11-27 03:45:46 +00:00
|
|
|
|
|
|
|
this.debugGraphic = graphic;
|
|
|
|
|
|
|
|
this.drawDebug = true;
|
|
|
|
|
|
|
|
return graphic;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#disableGravity
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
disableGravity: function ()
|
|
|
|
{
|
|
|
|
this.localWorld.gravity.x = 0;
|
|
|
|
this.localWorld.gravity.y = 0;
|
|
|
|
this.localWorld.gravity.scale = 0;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#setGravity
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} [x=0] - [description]
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {number} [y=1] - [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
* @param {number} [scale] - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
setGravity: function (x, y, scale)
|
|
|
|
{
|
2017-11-27 16:29:33 +00:00
|
|
|
if (x === undefined) { x = 0; }
|
|
|
|
if (y === undefined) { y = 1; }
|
|
|
|
|
2017-11-21 16:54:54 +00:00
|
|
|
this.localWorld.gravity.x = x;
|
|
|
|
this.localWorld.gravity.y = y;
|
|
|
|
|
|
|
|
if (scale !== undefined)
|
|
|
|
{
|
|
|
|
this.localWorld.gravity.scale = scale;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#create
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {number} x - [description]
|
|
|
|
* @param {number} y - [description]
|
|
|
|
* @param {number} width - [description]
|
|
|
|
* @param {number} height - [description]
|
|
|
|
* @param {object} options - [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*
|
2018-03-19 15:01:14 +00:00
|
|
|
* @return {Matter.Body} [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*/
|
2017-11-21 16:54:54 +00:00
|
|
|
create: function (x, y, width, height, options)
|
|
|
|
{
|
|
|
|
var body = Bodies.rectangle(x, y, width, height, options);
|
|
|
|
|
|
|
|
MatterWorld.add(this.localWorld, body);
|
|
|
|
|
|
|
|
return body;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#add
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:01:14 +00:00
|
|
|
* @param {object|object[]} object - Can be single or an array, and can be a body, composite or constraint
|
2018-02-12 13:48:38 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
add: function (object)
|
2017-11-21 16:54:54 +00:00
|
|
|
{
|
2017-11-27 03:45:46 +00:00
|
|
|
MatterWorld.add(this.localWorld, object);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#remove
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:01:14 +00:00
|
|
|
* @param {object} object - The object to be removed from the world.
|
2018-02-12 13:48:38 +00:00
|
|
|
* @param {boolean} deep - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
remove: function (object, deep)
|
|
|
|
{
|
2017-12-02 04:05:27 +00:00
|
|
|
var body = (object.body) ? object.body : object;
|
|
|
|
|
|
|
|
Composite.removeBody(this.localWorld, body, deep);
|
2017-11-21 02:04:05 +00:00
|
|
|
|
2017-11-21 16:54:54 +00:00
|
|
|
return this;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#removeConstraint
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:01:14 +00:00
|
|
|
* @param {Matter.Constraint} constraint - [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
* @param {boolean} deep - [description]
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
2018-02-12 13:37:29 +00:00
|
|
|
removeConstraint: function (constraint, deep)
|
|
|
|
{
|
|
|
|
Composite.remove(this.localWorld, constraint, deep);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-01-21 18:53:48 +00:00
|
|
|
/**
|
2018-01-25 20:04:58 +00:00
|
|
|
* Adds MatterTileBody instances for all the colliding tiles within the given tilemap layer. Set
|
|
|
|
* the appropriate tiles in your layer to collide before calling this method!
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#convertTilemapLayer
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {Phaser.GameObjects.StaticTilemapLayer|Phaser.GameObjects.DynamicTilemapLayer} tilemapLayer -
|
2018-01-25 20:04:58 +00:00
|
|
|
* An array of tiles.
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {object} [options] - Options to be passed to the MatterTileBody constructor. {@ee Phaser.Physics.Matter.TileBody}
|
|
|
|
*
|
2018-02-12 13:48:38 +00:00
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
2018-01-21 18:53:48 +00:00
|
|
|
*/
|
|
|
|
convertTilemapLayer: function (tilemapLayer, options)
|
|
|
|
{
|
|
|
|
var layerData = tilemapLayer.layer;
|
2018-02-16 18:17:51 +00:00
|
|
|
var tiles = tilemapLayer.getTilesWithin(0, 0, layerData.width, layerData.height, {isColliding: true});
|
2018-01-21 18:53:48 +00:00
|
|
|
|
|
|
|
this.convertTiles(tiles, options);
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2018-01-25 20:04:58 +00:00
|
|
|
* Adds MatterTileBody instances for the given tiles. This adds bodies regardless of whether the
|
|
|
|
* tiles are set to collide or not.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#convertTiles
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {Phaser.GameObjects.Tile[]} tiles - An array of tiles.
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {object} [options] - Options to be passed to the MatterTileBody constructor. {@see Phaser.Physics.Matter.TileBody}
|
|
|
|
*
|
2018-02-12 13:48:38 +00:00
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
2018-01-21 18:53:48 +00:00
|
|
|
*/
|
|
|
|
convertTiles: function (tiles, options)
|
|
|
|
{
|
|
|
|
if (tiles.length === 0)
|
|
|
|
{
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < tiles.length; i++)
|
|
|
|
{
|
|
|
|
new MatterTileBody(this, tiles[i], options);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#nextGroup
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {boolean} isNonColliding - [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @return {number} [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*/
|
2017-11-22 02:25:30 +00:00
|
|
|
nextGroup: function (isNonColliding)
|
|
|
|
{
|
2017-11-27 16:29:33 +00:00
|
|
|
return MatterBody.nextGroup(isNonColliding);
|
2017-11-22 02:25:30 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#nextCategory
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @return {number} [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*/
|
2017-11-22 02:25:30 +00:00
|
|
|
nextCategory: function ()
|
|
|
|
{
|
|
|
|
return MatterBody.nextCategory();
|
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#pause
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
|
|
|
pause: function ()
|
|
|
|
{
|
|
|
|
this.enabled = false;
|
|
|
|
|
|
|
|
this.emit('pause');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#resume
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Matter.World} This Matter World object.
|
|
|
|
*/
|
|
|
|
resume: function ()
|
|
|
|
{
|
|
|
|
this.enabled = true;
|
|
|
|
|
|
|
|
this.emit('resume');
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#update
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} time - [description]
|
|
|
|
* @param {number} delta - [description]
|
|
|
|
*/
|
2017-11-20 16:54:26 +00:00
|
|
|
update: function (time, delta)
|
|
|
|
{
|
2018-02-12 13:48:38 +00:00
|
|
|
if (this.enabled)
|
2017-11-27 03:45:46 +00:00
|
|
|
{
|
2018-02-12 13:48:38 +00:00
|
|
|
var correction = 1;
|
2017-11-21 02:04:05 +00:00
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
Engine.update(this.engine, delta, correction);
|
|
|
|
}
|
2017-11-21 02:04:05 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#postUpdate
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-21 02:04:05 +00:00
|
|
|
postUpdate: function ()
|
|
|
|
{
|
2018-01-17 15:22:16 +00:00
|
|
|
if (!this.drawDebug)
|
2017-11-27 03:45:46 +00:00
|
|
|
{
|
2018-01-17 15:22:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-11-27 03:45:46 +00:00
|
|
|
|
2018-01-17 15:22:16 +00:00
|
|
|
var graphics = this.debugGraphic;
|
|
|
|
var bodies = Composite.allBodies(this.localWorld);
|
2017-11-27 17:01:27 +00:00
|
|
|
|
2018-01-17 15:22:16 +00:00
|
|
|
graphics.clear();
|
|
|
|
graphics.lineStyle(1, this.defaults.bodyDebugColor);
|
2018-03-08 10:16:04 +00:00
|
|
|
graphics.beginPath();
|
2017-11-27 03:45:46 +00:00
|
|
|
|
2018-01-17 15:22:16 +00:00
|
|
|
for (var i = 0; i < bodies.length; i++)
|
|
|
|
{
|
2018-01-27 03:44:51 +00:00
|
|
|
if (!bodies[i].render.visible)
|
2018-01-17 15:22:16 +00:00
|
|
|
{
|
2018-01-27 03:44:51 +00:00
|
|
|
return;
|
2018-01-17 15:22:16 +00:00
|
|
|
}
|
2017-11-27 03:45:46 +00:00
|
|
|
|
2018-01-27 03:44:51 +00:00
|
|
|
// Handle drawing both single bodies and compound bodies. If compound, draw both the
|
|
|
|
// convex hull (first part) and the rest of the bodies.
|
|
|
|
for (var j = 0; j < bodies[i].parts.length; j++)
|
|
|
|
{
|
|
|
|
var body = bodies[i].parts[j];
|
2017-11-27 03:45:46 +00:00
|
|
|
|
2018-01-27 03:44:51 +00:00
|
|
|
var vertices = body.vertices;
|
2017-11-27 03:45:46 +00:00
|
|
|
|
2018-01-27 03:44:51 +00:00
|
|
|
graphics.moveTo(vertices[0].x, vertices[0].y);
|
|
|
|
|
|
|
|
for (var k = 1; k < vertices.length; k++)
|
|
|
|
{
|
|
|
|
graphics.lineTo(vertices[k].x, vertices[k].y);
|
|
|
|
}
|
2018-01-17 15:22:16 +00:00
|
|
|
|
2018-01-27 03:44:51 +00:00
|
|
|
graphics.lineTo(vertices[0].x, vertices[0].y);
|
2018-01-17 15:22:16 +00:00
|
|
|
|
2018-01-27 03:44:51 +00:00
|
|
|
graphics.strokePath();
|
|
|
|
}
|
2017-11-27 03:45:46 +00:00
|
|
|
}
|
2018-03-08 10:46:01 +00:00
|
|
|
graphics.closePath();
|
2017-11-27 03:45:46 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#fromPath
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:01:14 +00:00
|
|
|
* @param {string} path - [description]
|
2018-03-19 00:10:32 +00:00
|
|
|
* @param {array} points - [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*
|
2018-03-19 00:10:32 +00:00
|
|
|
* @return {array} [description]
|
2018-02-12 13:48:38 +00:00
|
|
|
*/
|
2017-11-27 03:45:46 +00:00
|
|
|
fromPath: function (path, points)
|
|
|
|
{
|
|
|
|
if (points === undefined) { points = []; }
|
|
|
|
|
2018-02-16 19:17:49 +00:00
|
|
|
// var pathPattern = /L?\s*([-\d.e]+)[\s,]*([-\d.e]+)*/ig;
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-useless-escape
|
2017-11-27 03:45:46 +00:00
|
|
|
var pathPattern = /L?\s*([\-\d\.e]+)[\s,]*([\-\d\.e]+)*/ig;
|
|
|
|
|
2018-02-16 18:17:51 +00:00
|
|
|
path.replace(pathPattern, function (match, x, y)
|
2017-11-27 03:45:46 +00:00
|
|
|
{
|
|
|
|
points.push({ x: parseFloat(x), y: parseFloat(y) });
|
|
|
|
});
|
|
|
|
|
|
|
|
return points;
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#shutdown
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-20 16:54:26 +00:00
|
|
|
shutdown: function ()
|
|
|
|
{
|
2017-11-29 23:36:35 +00:00
|
|
|
MatterWorld.clear(this.localWorld, false);
|
2018-02-12 13:48:38 +00:00
|
|
|
|
2017-11-29 23:36:35 +00:00
|
|
|
Engine.clear(this.engine);
|
2017-11-20 16:54:26 +00:00
|
|
|
},
|
|
|
|
|
2018-02-12 13:48:38 +00:00
|
|
|
/**
|
|
|
|
* [description]
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Matter.World#destroy
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-20 16:54:26 +00:00
|
|
|
destroy: function ()
|
|
|
|
{
|
2017-11-29 23:36:35 +00:00
|
|
|
this.shutdown();
|
2017-11-20 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = World;
|