2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-09 04:00:56 +00:00
|
|
|
var ArcadeSprite = require('./ArcadeSprite');
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var CONST = require('./const');
|
|
|
|
var GetFastValue = require('../../utils/object/GetFastValue');
|
|
|
|
var Group = require('../../gameobjects/group/Group');
|
2018-08-01 17:01:36 +00:00
|
|
|
var IsPlainObject = require('../../utils/object/IsPlainObject');
|
2017-11-09 04:00:56 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* An Arcade Physics Group object.
|
2018-03-21 13:15:25 +00:00
|
|
|
*
|
2020-09-07 15:39:57 +00:00
|
|
|
* The primary use of a Physics Group is a way to collect together physics enable objects
|
|
|
|
* that share the same intrinsic structure into a single pool. They can they be easily
|
|
|
|
* compared against other Groups, or Game Objects.
|
2018-06-13 16:41:50 +00:00
|
|
|
*
|
2020-09-07 15:39:57 +00:00
|
|
|
* All Game Objects created by, or added to this Group will automatically be given **dynamic**
|
|
|
|
* Arcade Physics bodies (if they have no body already) and the bodies will receive the
|
|
|
|
* Groups {@link Phaser.Physics.Arcade.Group#defaults default values}.
|
|
|
|
*
|
|
|
|
* You should not pass objects into this Group that should not receive a body. For example,
|
|
|
|
* do not add basic Geometry or Tilemap Layers into a Group, as they will not behave in the
|
|
|
|
* way you may expect. Groups should all ideally have objects of the same type in them.
|
|
|
|
*
|
|
|
|
* If you wish to create a Group filled with Static Bodies, please see {@link Phaser.Physics.Arcade.StaticGroup}.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @class Group
|
|
|
|
* @extends Phaser.GameObjects.Group
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.Physics.Arcade
|
2018-02-09 03:44:23 +00:00
|
|
|
* @constructor
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {Phaser.Physics.Arcade.World} world - The physics simulation.
|
|
|
|
* @param {Phaser.Scene} scene - The scene this group belongs to.
|
2019-05-09 11:33:37 +00:00
|
|
|
* @param {(Phaser.GameObjects.GameObject[]|Phaser.Types.Physics.Arcade.PhysicsGroupConfig|Phaser.Types.GameObjects.Group.GroupCreateConfig)} [children] - Game Objects to add to this group; or the `config` argument.
|
|
|
|
* @param {Phaser.Types.Physics.Arcade.PhysicsGroupConfig|Phaser.Types.GameObjects.Group.GroupCreateConfig} [config] - Settings for this group.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2017-11-09 04:00:56 +00:00
|
|
|
var PhysicsGroup = new Class({
|
|
|
|
|
|
|
|
Extends: Group,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function PhysicsGroup (world, scene, children, config)
|
|
|
|
{
|
2020-01-15 12:45:59 +00:00
|
|
|
if (!children && !config)
|
|
|
|
{
|
|
|
|
config = {
|
|
|
|
internalCreateCallback: this.createCallbackHandler,
|
|
|
|
internalRemoveCallback: this.removeCallbackHandler
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (IsPlainObject(children))
|
2018-08-01 17:01:36 +00:00
|
|
|
{
|
|
|
|
// children is a plain object, so swizzle them:
|
2017-11-09 04:00:56 +00:00
|
|
|
config = children;
|
|
|
|
children = null;
|
2020-01-15 12:45:59 +00:00
|
|
|
|
|
|
|
config.internalCreateCallback = this.createCallbackHandler;
|
|
|
|
config.internalRemoveCallback = this.removeCallbackHandler;
|
2017-11-09 04:00:56 +00:00
|
|
|
}
|
2018-08-01 17:01:36 +00:00
|
|
|
else if (Array.isArray(children) && IsPlainObject(children[0]))
|
2017-11-09 04:00:56 +00:00
|
|
|
{
|
2020-05-11 17:22:13 +00:00
|
|
|
// children is an array of plain objects (i.e., configs)
|
2019-05-07 15:17:28 +00:00
|
|
|
config = children[0];
|
2020-01-15 12:45:59 +00:00
|
|
|
|
|
|
|
var _this = this;
|
|
|
|
|
|
|
|
children.forEach(function (singleConfig)
|
|
|
|
{
|
|
|
|
singleConfig.internalCreateCallback = _this.createCallbackHandler;
|
|
|
|
singleConfig.internalRemoveCallback = _this.removeCallbackHandler;
|
|
|
|
});
|
2020-05-11 17:22:13 +00:00
|
|
|
|
|
|
|
children = null;
|
2017-11-09 04:00:56 +00:00
|
|
|
}
|
2020-01-15 12:45:59 +00:00
|
|
|
else
|
2018-10-16 23:13:18 +00:00
|
|
|
{
|
2020-01-15 12:45:59 +00:00
|
|
|
// config is not defined and children is not a plain object nor an array of plain objects
|
|
|
|
config = {
|
|
|
|
internalCreateCallback: this.createCallbackHandler,
|
|
|
|
internalRemoveCallback: this.removeCallbackHandler
|
|
|
|
};
|
2018-10-16 23:13:18 +00:00
|
|
|
}
|
2017-11-09 04:00:56 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-09-26 19:25:09 +00:00
|
|
|
* The physics simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Group#world
|
|
|
|
* @type {Phaser.Physics.Arcade.World}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-09 04:00:56 +00:00
|
|
|
this.world = world;
|
|
|
|
|
2018-04-24 00:48:15 +00:00
|
|
|
/**
|
2019-02-04 17:16:08 +00:00
|
|
|
* The class to create new Group members from.
|
2019-11-16 22:50:06 +00:00
|
|
|
*
|
2019-02-04 17:16:08 +00:00
|
|
|
* This should be either `Phaser.Physics.Arcade.Image`, `Phaser.Physics.Arcade.Sprite`, or a class extending one of those.
|
2018-04-24 00:48:15 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Group#classType
|
2021-10-13 20:22:54 +00:00
|
|
|
* @type {Phaser.Types.GameObjects.Group.GroupClassTypeConstructor}
|
2018-04-24 00:48:15 +00:00
|
|
|
* @default ArcadeSprite
|
2019-02-12 12:22:25 +00:00
|
|
|
* @since 3.0.0
|
2018-04-24 00:48:15 +00:00
|
|
|
*/
|
2017-11-16 00:42:03 +00:00
|
|
|
config.classType = GetFastValue(config, 'classType', ArcadeSprite);
|
2017-11-09 04:00:56 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-09-26 19:25:09 +00:00
|
|
|
* The physics type of the Group's members.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Group#physicsType
|
2020-11-23 10:22:13 +00:00
|
|
|
* @type {number}
|
2018-11-07 17:43:43 +00:00
|
|
|
* @default Phaser.Physics.Arcade.DYNAMIC_BODY
|
2018-02-09 03:44:23 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-09 16:31:57 +00:00
|
|
|
this.physicsType = CONST.DYNAMIC_BODY;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Default physics properties applied to Game Objects added to the Group or created by the Group. Derived from the `config` argument.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2020-07-23 16:48:53 +00:00
|
|
|
* You can remove the default values by setting this property to `{}`.
|
|
|
|
*
|
2018-02-09 03:44:23 +00:00
|
|
|
* @name Phaser.Physics.Arcade.Group#defaults
|
2019-05-09 11:33:37 +00:00
|
|
|
* @type {Phaser.Types.Physics.Arcade.PhysicsGroupDefaults}
|
2018-02-09 03:44:23 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-09 04:00:56 +00:00
|
|
|
this.defaults = {
|
|
|
|
setCollideWorldBounds: GetFastValue(config, 'collideWorldBounds', false),
|
2018-12-15 21:52:12 +00:00
|
|
|
setBoundsRectangle: GetFastValue(config, 'customBoundsRectangle', null),
|
2017-11-09 04:00:56 +00:00
|
|
|
setAccelerationX: GetFastValue(config, 'accelerationX', 0),
|
|
|
|
setAccelerationY: GetFastValue(config, 'accelerationY', 0),
|
2018-05-15 14:59:02 +00:00
|
|
|
setAllowDrag: GetFastValue(config, 'allowDrag', true),
|
|
|
|
setAllowGravity: GetFastValue(config, 'allowGravity', true),
|
|
|
|
setAllowRotation: GetFastValue(config, 'allowRotation', true),
|
2017-11-09 04:00:56 +00:00
|
|
|
setBounceX: GetFastValue(config, 'bounceX', 0),
|
|
|
|
setBounceY: GetFastValue(config, 'bounceY', 0),
|
|
|
|
setDragX: GetFastValue(config, 'dragX', 0),
|
|
|
|
setDragY: GetFastValue(config, 'dragY', 0),
|
2018-09-26 17:25:45 +00:00
|
|
|
setEnable: GetFastValue(config, 'enable', true),
|
2017-11-09 04:00:56 +00:00
|
|
|
setGravityX: GetFastValue(config, 'gravityX', 0),
|
|
|
|
setGravityY: GetFastValue(config, 'gravityY', 0),
|
|
|
|
setFrictionX: GetFastValue(config, 'frictionX', 0),
|
|
|
|
setFrictionY: GetFastValue(config, 'frictionY', 0),
|
2020-09-22 23:05:21 +00:00
|
|
|
setMaxVelocityX: GetFastValue(config, 'maxVelocityX', 10000),
|
|
|
|
setMaxVelocityY: GetFastValue(config, 'maxVelocityY', 10000),
|
2017-11-09 04:00:56 +00:00
|
|
|
setVelocityX: GetFastValue(config, 'velocityX', 0),
|
|
|
|
setVelocityY: GetFastValue(config, 'velocityY', 0),
|
|
|
|
setAngularVelocity: GetFastValue(config, 'angularVelocity', 0),
|
|
|
|
setAngularAcceleration: GetFastValue(config, 'angularAcceleration', 0),
|
|
|
|
setAngularDrag: GetFastValue(config, 'angularDrag', 0),
|
|
|
|
setMass: GetFastValue(config, 'mass', 1),
|
2017-11-09 16:31:57 +00:00
|
|
|
setImmovable: GetFastValue(config, 'immovable', false)
|
2017-11-09 04:00:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Group.call(this, scene, children, config);
|
2019-11-16 22:50:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A textual representation of this Game Object.
|
|
|
|
* Used internally by Phaser but is available for your own custom classes to populate.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Group#type
|
|
|
|
* @type {string}
|
|
|
|
* @default 'PhysicsGroup'
|
|
|
|
* @since 3.21.0
|
|
|
|
*/
|
|
|
|
this.type = 'PhysicsGroup';
|
2017-11-09 04:00:56 +00:00
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Enables a Game Object's Body and assigns `defaults`. Called when a Group member is added or created.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-04-18 12:29:22 +00:00
|
|
|
* @method Phaser.Physics.Arcade.Group#createCallbackHandler
|
2018-02-09 03:44:23 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} child - The Game Object being added.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-04-18 12:29:22 +00:00
|
|
|
createCallbackHandler: function (child)
|
2017-11-09 04:00:56 +00:00
|
|
|
{
|
|
|
|
if (!child.body)
|
|
|
|
{
|
2017-11-09 16:31:57 +00:00
|
|
|
this.world.enableBody(child, CONST.DYNAMIC_BODY);
|
2017-11-09 04:00:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var body = child.body;
|
|
|
|
|
|
|
|
for (var key in this.defaults)
|
|
|
|
{
|
|
|
|
body[key](this.defaults[key]);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Disables a Game Object's Body. Called when a Group member is removed.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-04-18 12:29:22 +00:00
|
|
|
* @method Phaser.Physics.Arcade.Group#removeCallbackHandler
|
2018-02-09 03:44:23 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} child - The Game Object being removed.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-04-18 12:29:22 +00:00
|
|
|
removeCallbackHandler: function (child)
|
2017-11-09 04:00:56 +00:00
|
|
|
{
|
|
|
|
if (child.body)
|
|
|
|
{
|
|
|
|
this.world.disableBody(child);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Sets the velocity of each Group member.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Group#setVelocity
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {number} x - The horizontal velocity.
|
|
|
|
* @param {number} y - The vertical velocity.
|
|
|
|
* @param {number} [step=0] - The velocity increment. When set, the first member receives velocity (x, y), the second (x + step, y + step), and so on.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Group} This Physics Group object.
|
|
|
|
*/
|
2017-11-09 04:00:56 +00:00
|
|
|
setVelocity: function (x, y, step)
|
|
|
|
{
|
|
|
|
if (step === undefined) { step = 0; }
|
|
|
|
|
|
|
|
var items = this.getChildren();
|
|
|
|
|
|
|
|
for (var i = 0; i < items.length; i++)
|
|
|
|
{
|
|
|
|
items[i].body.velocity.set(x + (i * step), y + (i * step));
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Sets the horizontal velocity of each Group member.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Group#setVelocityX
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {number} value - The velocity value.
|
|
|
|
* @param {number} [step=0] - The velocity increment. When set, the first member receives velocity (x), the second (x + step), and so on.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Group} This Physics Group object.
|
|
|
|
*/
|
2017-11-09 04:00:56 +00:00
|
|
|
setVelocityX: function (value, step)
|
|
|
|
{
|
|
|
|
if (step === undefined) { step = 0; }
|
|
|
|
|
|
|
|
var items = this.getChildren();
|
|
|
|
|
|
|
|
for (var i = 0; i < items.length; i++)
|
|
|
|
{
|
|
|
|
items[i].body.velocity.x = value + (i * step);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Sets the vertical velocity of each Group member.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Group#setVelocityY
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {number} value - The velocity value.
|
|
|
|
* @param {number} [step=0] - The velocity increment. When set, the first member receives velocity (y), the second (y + step), and so on.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Group} This Physics Group object.
|
|
|
|
*/
|
2017-11-09 04:00:56 +00:00
|
|
|
setVelocityY: function (value, step)
|
|
|
|
{
|
|
|
|
if (step === undefined) { step = 0; }
|
|
|
|
|
|
|
|
var items = this.getChildren();
|
|
|
|
|
|
|
|
for (var i = 0; i < items.length; i++)
|
|
|
|
{
|
|
|
|
items[i].body.velocity.y = value + (i * step);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = PhysicsGroup;
|