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.
|
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:01:13 +00:00
|
|
|
var ArcadeImage = require('./ArcadeImage');
|
|
|
|
var ArcadeSprite = require('./ArcadeSprite');
|
2017-11-08 17:18:41 +00:00
|
|
|
var Class = require('../../utils/Class');
|
2017-11-09 15:32:08 +00:00
|
|
|
var CONST = require('./const');
|
2017-11-09 16:31:09 +00:00
|
|
|
var PhysicsGroup = require('./PhysicsGroup');
|
|
|
|
var StaticPhysicsGroup = require('./StaticPhysicsGroup');
|
2017-11-08 17:18:41 +00:00
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* @classdesc
|
|
|
|
* The Arcade Physics Factory allows you to easily create Arcade Physics enabled Game Objects.
|
|
|
|
* Objects that are created by this Factory are automatically added to the physics world.
|
|
|
|
*
|
|
|
|
* @class Factory
|
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
|
|
|
|
*
|
|
|
|
* @param {Phaser.Physics.Arcade.World} world - The Arcade Physics World instance.
|
|
|
|
*/
|
2017-11-08 17:18:41 +00:00
|
|
|
var Factory = new Class({
|
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
|
|
|
function Factory (world)
|
|
|
|
{
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* A reference to the Arcade Physics World.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Factory#world
|
|
|
|
* @type {Phaser.Physics.Arcade.World}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-08 17:18:41 +00:00
|
|
|
this.world = world;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* A reference to the Scene this Arcade Physics instance belongs to.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Factory#scene
|
|
|
|
* @type {Phaser.Scene}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-09 04:01:13 +00:00
|
|
|
this.scene = world.scene;
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* A reference to the Scene.Systems this Arcade Physics instance belongs to.
|
|
|
|
*
|
|
|
|
* @name Phaser.Physics.Arcade.Factory#sys
|
|
|
|
* @type {Phaser.Scenes.Systems}
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-11-08 17:18:41 +00:00
|
|
|
this.sys = world.scene.sys;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-11-07 17:43:43 +00:00
|
|
|
* Creates a new Arcade Physics Collider object.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#collider
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-05-22 02:11:15 +00:00
|
|
|
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Group[])} object1 - The first object to check for collision.
|
|
|
|
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Group[])} object2 - The second object to check for collision.
|
2018-04-29 15:33:01 +00:00
|
|
|
* @param {ArcadePhysicsCallback} [collideCallback] - The callback to invoke when the two objects collide.
|
|
|
|
* @param {ArcadePhysicsCallback} [processCallback] - The callback to invoke when the two objects collide. Must return a boolean.
|
|
|
|
* @param {*} [callbackContext] - The scope in which to call the callbacks.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Collider} The Collider that was created.
|
|
|
|
*/
|
2017-11-09 17:03:58 +00:00
|
|
|
collider: function (object1, object2, collideCallback, processCallback, callbackContext)
|
|
|
|
{
|
|
|
|
return this.world.addCollider(object1, object2, collideCallback, processCallback, callbackContext);
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
2018-11-07 17:43:43 +00:00
|
|
|
* Creates a new Arcade Physics Collider Overlap object.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#overlap
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-05-22 02:11:15 +00:00
|
|
|
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Group[])} object1 - The first object to check for overlap.
|
|
|
|
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.GameObject[]|Phaser.GameObjects.Group|Phaser.GameObjects.Group[])} object2 - The second object to check for overlap.
|
2018-04-29 15:35:19 +00:00
|
|
|
* @param {ArcadePhysicsCallback} [collideCallback] - The callback to invoke when the two objects collide.
|
|
|
|
* @param {ArcadePhysicsCallback} [processCallback] - The callback to invoke when the two objects collide. Must return a boolean.
|
|
|
|
* @param {*} [callbackContext] - The scope in which to call the callbacks.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Collider} The Collider that was created.
|
|
|
|
*/
|
2017-11-09 17:03:58 +00:00
|
|
|
overlap: function (object1, object2, collideCallback, processCallback, callbackContext)
|
|
|
|
{
|
|
|
|
return this.world.addOverlap(object1, object2, collideCallback, processCallback, callbackContext);
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Adds an Arcade Physics Body to the given Game Object.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#existing
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-09-26 19:25:09 +00:00
|
|
|
* @param {Phaser.GameObjects.GameObject} gameObject - A Game Object.
|
|
|
|
* @param {boolean} [isStatic=false] - Create a Static body (true) or Dynamic body (false).
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} The Game Object.
|
|
|
|
*/
|
2018-01-08 22:11:19 +00:00
|
|
|
existing: function (gameObject, isStatic)
|
|
|
|
{
|
|
|
|
var type = (isStatic) ? CONST.STATIC_BODY : CONST.DYNAMIC_BODY;
|
|
|
|
|
|
|
|
this.world.enableBody(gameObject, type);
|
|
|
|
|
|
|
|
return gameObject;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Arcade Image object with a Static body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#staticImage
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Image} The Image object that was created.
|
|
|
|
*/
|
2017-11-09 15:32:08 +00:00
|
|
|
staticImage: function (x, y, key, frame)
|
|
|
|
{
|
2017-11-09 16:31:09 +00:00
|
|
|
var image = new ArcadeImage(this.scene, x, y, key, frame);
|
2017-11-09 15:32:08 +00:00
|
|
|
|
|
|
|
this.sys.displayList.add(image);
|
|
|
|
|
2017-11-09 16:31:09 +00:00
|
|
|
this.world.enableBody(image, CONST.STATIC_BODY);
|
|
|
|
|
2017-11-09 15:32:08 +00:00
|
|
|
return image;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Arcade Image object with a Dynamic body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#image
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Image} The Image object that was created.
|
|
|
|
*/
|
2017-11-08 17:18:41 +00:00
|
|
|
image: function (x, y, key, frame)
|
|
|
|
{
|
2017-11-09 16:31:09 +00:00
|
|
|
var image = new ArcadeImage(this.scene, x, y, key, frame);
|
2017-11-08 17:18:41 +00:00
|
|
|
|
|
|
|
this.sys.displayList.add(image);
|
|
|
|
|
2017-11-09 16:31:09 +00:00
|
|
|
this.world.enableBody(image, CONST.DYNAMIC_BODY);
|
|
|
|
|
2017-11-08 17:18:41 +00:00
|
|
|
return image;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Arcade Sprite object with a Static body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#staticSprite
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
|
|
|
* @param {string} texture - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Sprite} The Sprite object that was created.
|
|
|
|
*/
|
2017-11-09 15:32:08 +00:00
|
|
|
staticSprite: function (x, y, key, frame)
|
|
|
|
{
|
2017-11-09 16:31:09 +00:00
|
|
|
var sprite = new ArcadeSprite(this.scene, x, y, key, frame);
|
2017-11-09 15:32:08 +00:00
|
|
|
|
|
|
|
this.sys.displayList.add(sprite);
|
|
|
|
this.sys.updateList.add(sprite);
|
|
|
|
|
2017-11-09 16:31:09 +00:00
|
|
|
this.world.enableBody(sprite, CONST.STATIC_BODY);
|
|
|
|
|
2017-11-09 15:32:08 +00:00
|
|
|
return sprite;
|
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a new Arcade Sprite object with a Dynamic body.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#sprite
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {number} x - The horizontal position of this Game Object in the world.
|
|
|
|
* @param {number} y - The vertical position of this Game Object in the world.
|
2018-03-21 13:15:25 +00:00
|
|
|
* @param {string} key - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
|
2018-03-20 15:10:19 +00:00
|
|
|
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Sprite} The Sprite object that was created.
|
|
|
|
*/
|
2017-11-08 17:18:41 +00:00
|
|
|
sprite: function (x, y, key, frame)
|
|
|
|
{
|
2017-11-09 16:31:09 +00:00
|
|
|
var sprite = new ArcadeSprite(this.scene, x, y, key, frame);
|
2017-11-08 17:18:41 +00:00
|
|
|
|
|
|
|
this.sys.displayList.add(sprite);
|
|
|
|
this.sys.updateList.add(sprite);
|
|
|
|
|
2017-11-09 16:31:09 +00:00
|
|
|
this.world.enableBody(sprite, CONST.DYNAMIC_BODY);
|
|
|
|
|
2017-11-08 17:18:41 +00:00
|
|
|
return sprite;
|
2017-11-09 04:01:13 +00:00
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a Static Physics Group object.
|
|
|
|
* All Game Objects created by this Group will automatically be static Arcade Physics objects.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#staticGroup
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2019-05-09 10:56:16 +00:00
|
|
|
* @param {(Phaser.GameObjects.GameObject[]|Phaser.Types.GameObjects.Group.GroupConfig|Phaser.Types.GameObjects.Group.GroupCreateConfig)} [children] - Game Objects to add to this group; or the `config` argument.
|
|
|
|
* @param {Phaser.Types.GameObjects.Group.GroupConfig|Phaser.Types.GameObjects.Group.GroupCreateConfig} [config] - Settings for this group.
|
2018-02-09 03:44:23 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.StaticGroup} The Static Group object that was created.
|
|
|
|
*/
|
2017-11-09 16:31:09 +00:00
|
|
|
staticGroup: function (children, config)
|
|
|
|
{
|
2017-11-16 00:42:03 +00:00
|
|
|
return this.sys.updateList.add(new StaticPhysicsGroup(this.world, this.world.scene, children, config));
|
2017-11-09 16:31:09 +00:00
|
|
|
},
|
|
|
|
|
2018-02-09 03:44:23 +00:00
|
|
|
/**
|
|
|
|
* Creates a Physics Group object.
|
|
|
|
* All Game Objects created by this Group will automatically be dynamic Arcade Physics objects.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#group
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
* @return {Phaser.Physics.Arcade.Group} The Group object that was created.
|
|
|
|
*/
|
2017-11-09 04:01:13 +00:00
|
|
|
group: function (children, config)
|
|
|
|
{
|
2017-11-16 00:42:03 +00:00
|
|
|
return this.sys.updateList.add(new PhysicsGroup(this.world, this.world.scene, children, config));
|
2018-04-13 16:12:17 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys this Factory.
|
|
|
|
*
|
|
|
|
* @method Phaser.Physics.Arcade.Factory#destroy
|
2018-04-15 11:44:47 +00:00
|
|
|
* @since 3.5.0
|
2018-04-13 16:12:17 +00:00
|
|
|
*/
|
|
|
|
destroy: function ()
|
|
|
|
{
|
|
|
|
this.world = null;
|
|
|
|
this.scene = null;
|
|
|
|
this.sys = null;
|
2017-11-08 17:18:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Factory;
|