phaser/plugins/impact/Factory.js

152 lines
4.7 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +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-02-12 16:01:20 +00:00
*/
var Class = require('../../utils/Class');
var ImpactBody = require('./ImpactBody');
var ImpactImage = require('./ImpactImage');
var ImpactSprite = require('./ImpactSprite');
2018-02-09 16:04:43 +00:00
/**
* @classdesc
* The Impact Physics Factory allows you to easily create Impact 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.Impact
2018-02-09 16:04:43 +00:00
* @constructor
* @since 3.0.0
*
2018-09-28 11:19:21 +00:00
* @param {Phaser.Physics.Impact.World} world - A reference to the Impact Physics world.
2018-02-09 16:04:43 +00:00
*/
var Factory = new Class({
initialize:
function Factory (world)
{
2018-02-09 16:04:43 +00:00
/**
2018-09-28 11:19:21 +00:00
* A reference to the Impact Physics world.
2018-02-09 16:04:43 +00:00
*
* @name Phaser.Physics.Impact.Factory#world
* @type {Phaser.Physics.Impact.World}
* @since 3.0.0
*/
this.world = world;
2018-02-09 16:04:43 +00:00
/**
* A reference to the Scene.Systems this Impact Physics instance belongs to.
*
* @name Phaser.Physics.Impact.Factory#sys
* @type {Phaser.Scenes.Systems}
* @since 3.0.0
*/
this.sys = world.scene.sys;
},
2018-02-09 16:04:43 +00:00
/**
2018-09-28 11:19:21 +00:00
* Creates a new ImpactBody object and adds it to the physics simulation.
2018-02-09 16:04:43 +00:00
*
* @method Phaser.Physics.Impact.Factory#body
* @since 3.0.0
*
2018-09-28 11:19:21 +00:00
* @param {number} x - The horizontal position of the body in the physics world.
* @param {number} y - The vertical position of the body in the physics world.
* @param {number} width - The width of the body.
* @param {number} height - The height of the body.
2018-02-09 16:04:43 +00:00
*
* @return {Phaser.Physics.Impact.ImpactBody} The ImpactBody object that was created.
*/
body: function (x, y, width, height)
{
return new ImpactBody(this.world, x, y, width, height);
},
2018-02-09 16:04:43 +00:00
/**
* Adds an Impact Physics Body to the given Game Object.
*
* @method Phaser.Physics.Impact.Factory#existing
* @since 3.0.0
*
2018-09-28 11:19:21 +00:00
* @param {Phaser.GameObjects.GameObject} gameObject - The Game Object to receive the physics body.
2018-02-09 16:04:43 +00:00
*
* @return {Phaser.GameObjects.GameObject} The Game Object.
*/
existing: function (gameObject)
{
var x = gameObject.x - gameObject.frame.centerX;
var y = gameObject.y - gameObject.frame.centerY;
var w = gameObject.width;
var h = gameObject.height;
gameObject.body = this.world.create(x, y, w, h);
gameObject.body.parent = gameObject;
gameObject.body.gameObject = gameObject;
return gameObject;
},
2018-02-09 16:04:43 +00:00
/**
2018-09-28 11:19:21 +00:00
* Creates a new ImpactImage object and adds it to the physics world.
2018-02-09 16:04:43 +00:00
*
* @method Phaser.Physics.Impact.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.
2018-03-18 23:42:09 +00:00
* @param {string} key - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
2018-02-09 16:04:43 +00:00
*
* @return {Phaser.Physics.Impact.ImpactImage} The ImpactImage object that was created.
*/
image: function (x, y, key, frame)
{
var image = new ImpactImage(this.world, x, y, key, frame);
this.sys.displayList.add(image);
return image;
},
2018-02-09 16:04:43 +00:00
/**
2018-09-28 11:19:21 +00:00
* Creates a new ImpactSprite object and adds it to the physics world.
2018-02-09 16:04:43 +00:00
*
* @method Phaser.Physics.Impact.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-18 23:42:09 +00:00
* @param {string} key - The key of the Texture this Game Object will use to render with, as stored in the Texture Manager.
* @param {(string|integer)} [frame] - An optional frame from the Texture this Game Object is rendering with.
2018-02-09 16:04:43 +00:00
*
* @return {Phaser.Physics.Impact.ImpactSprite} The ImpactSprite object that was created.
*/
sprite: function (x, y, key, frame)
{
var sprite = new ImpactSprite(this.world, x, y, key, frame);
this.sys.displayList.add(sprite);
this.sys.updateList.add(sprite);
return sprite;
},
/**
* Destroys this Factory.
*
* @method Phaser.Physics.Impact.Factory#destroy
* @since 3.5.0
*/
destroy: function ()
{
this.world = null;
this.sys = null;
}
});
module.exports = Factory;