2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-11-09 16:31:09 +00:00
|
|
|
var ArcadeSprite = require('./ArcadeSprite');
|
|
|
|
var Class = require('../../utils/Class');
|
|
|
|
var CONST = require('./const');
|
|
|
|
var Group = require('../../gameobjects/group/Group');
|
2018-08-01 17:01:36 +00:00
|
|
|
var IsPlainObject = require('../../utils/object/IsPlainObject');
|
2017-11-09 16:31:09 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
2018-06-13 16:41:50 +00:00
|
|
|
* An Arcade Physics Static Group object.
|
|
|
|
*
|
|
|
|
* All Game Objects created by this Group will automatically be given static Arcade Physics bodies.
|
|
|
|
*
|
|
|
|
* Its dynamic counterpart is {@link Phaser.Physics.Arcade.Group}.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @class StaticGroup
|
|
|
|
* @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-02-13 12:35:12 +00:00
|
|
|
* @param {(Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group.Types.GroupConfig|Phaser.GameObjects.Group.Types.GroupCreateConfig)} [children] - Game Objects to add to this group; or the `config` argument.
|
|
|
|
* @param {Phaser.GameObjects.Group.Types.GroupConfig|Phaser.GameObjects.Group.Types.GroupCreateConfig} [config] - Settings for this group.
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2017-11-09 16:31:09 +00:00
|
|
|
var StaticPhysicsGroup = new Class({
|
|
|
|
|
|
|
|
Extends: Group,
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function StaticPhysicsGroup (world, scene, children, config)
|
|
|
|
{
|
2018-08-01 17:01:36 +00:00
|
|
|
if (!children && !config)
|
|
|
|
{
|
|
|
|
config = {
|
|
|
|
createCallback: this.createCallbackHandler,
|
|
|
|
removeCallback: this.removeCallbackHandler,
|
|
|
|
createMultipleCallback: this.createMultipleCallbackHandler,
|
|
|
|
classType: ArcadeSprite
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else if (IsPlainObject(children))
|
2017-11-09 16:31:09 +00:00
|
|
|
{
|
2018-08-01 17:01:36 +00:00
|
|
|
// children is a plain object, so swizzle them:
|
2017-11-09 16:31:09 +00:00
|
|
|
config = children;
|
|
|
|
children = null;
|
2018-08-01 17:01:36 +00:00
|
|
|
|
|
|
|
config.createCallback = this.createCallbackHandler;
|
|
|
|
config.removeCallback = this.removeCallbackHandler;
|
|
|
|
config.createMultipleCallback = this.createMultipleCallbackHandler;
|
|
|
|
config.classType = ArcadeSprite;
|
2017-11-09 16:31:09 +00:00
|
|
|
}
|
2018-08-01 17:01:36 +00:00
|
|
|
else if (Array.isArray(children) && IsPlainObject(children[0]))
|
2017-11-09 16:31:09 +00:00
|
|
|
{
|
2018-08-01 17:01:36 +00:00
|
|
|
// children is an array of plain objects
|
|
|
|
config = children;
|
|
|
|
children = null;
|
|
|
|
|
|
|
|
config.forEach(function (singleConfig)
|
|
|
|
{
|
|
|
|
singleConfig.createCallback = this.createCallbackHandler;
|
|
|
|
singleConfig.removeCallback = this.removeCallbackHandler;
|
|
|
|
singleConfig.createMultipleCallback = this.createMultipleCallbackHandler;
|
|
|
|
singleConfig.classType = ArcadeSprite;
|
|
|
|
});
|
2017-11-09 16:31:09 +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.StaticGroup#world
|
|
|
|
* @type {Phaser.Physics.Arcade.World}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-09 16:31:09 +00:00
|
|
|
this.world = world;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-09-26 19:25:09 +00:00
|
|
|
* The scene this group belongs to.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.StaticGroup#physicsType
|
|
|
|
* @type {integer}
|
2018-11-07 17:43:43 +00:00
|
|
|
* @default Phaser.Physics.Arcade.STATIC_BODY
|
2018-02-09 03:44:23 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-09 16:31:09 +00:00
|
|
|
this.physicsType = CONST.STATIC_BODY;
|
|
|
|
|
|
|
|
Group.call(this, scene, children, config);
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-09-26 19:25:09 +00:00
|
|
|
* Adds a static physics body to the new group member (if it lacks one) and adds it to the simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-04-18 12:29:22 +00:00
|
|
|
* @method Phaser.Physics.Arcade.StaticGroup#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 new group member.
|
|
|
|
*
|
|
|
|
* @see Phaser.Physics.Arcade.World#enableBody
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-04-18 12:29:22 +00:00
|
|
|
createCallbackHandler: function (child)
|
2017-11-09 16:31:09 +00:00
|
|
|
{
|
|
|
|
if (!child.body)
|
|
|
|
{
|
|
|
|
this.world.enableBody(child, CONST.STATIC_BODY);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-09-26 19:25:09 +00:00
|
|
|
* Disables the group member's physics body, removing it from the simulation.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-04-18 12:29:22 +00:00
|
|
|
* @method Phaser.Physics.Arcade.StaticGroup#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 group member being removed.
|
|
|
|
*
|
|
|
|
* @see Phaser.Physics.Arcade.World#disableBody
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-04-18 12:29:22 +00:00
|
|
|
removeCallbackHandler: function (child)
|
2017-11-09 16:31:09 +00:00
|
|
|
{
|
|
|
|
if (child.body)
|
|
|
|
{
|
|
|
|
this.world.disableBody(child);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-09-26 19:25:09 +00:00
|
|
|
* Refreshes the group.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
2018-04-18 12:29:22 +00:00
|
|
|
* @method Phaser.Physics.Arcade.StaticGroup#createMultipleCallbackHandler
|
2018-02-09 03:44:23 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject[]} entries - The newly created group members.
|
|
|
|
*
|
|
|
|
* @see Phaser.Physics.Arcade.StaticGroup#refresh
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2018-04-18 12:29:22 +00:00
|
|
|
createMultipleCallbackHandler: function ()
|
2017-12-02 04:03:57 +00:00
|
|
|
{
|
|
|
|
this.refresh();
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-06-13 16:41:50 +00:00
|
|
|
* Resets each Body to the position of its parent Game Object.
|
|
|
|
* Body sizes aren't changed (use {@link Phaser.Physics.Arcade.Components.Enable#refreshBody} for that).
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.StaticGroup#refresh
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @return {Phaser.Physics.Arcade.StaticGroup} This group.
|
2018-06-13 16:41:50 +00:00
|
|
|
*
|
|
|
|
* @see Phaser.Physics.Arcade.StaticBody#reset
|
2018-02-09 03:44:23 +00:00
|
|
|
*/
|
2017-11-09 16:31:09 +00:00
|
|
|
refresh: function ()
|
|
|
|
{
|
|
|
|
var children = this.children.entries;
|
|
|
|
|
|
|
|
for (var i = 0; i < children.length; i++)
|
|
|
|
{
|
|
|
|
children[i].body.reset();
|
|
|
|
}
|
2017-12-02 04:03:57 +00:00
|
|
|
|
|
|
|
return this;
|
2017-11-09 16:31:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = StaticPhysicsGroup;
|