phaser/src/gameobjects/GameObjectFactory.js

182 lines
4.9 KiB
JavaScript
Raw Normal View History

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}
*/
2018-01-16 19:49:13 +00:00
var Class = require('../utils/Class');
var PluginCache = require('../plugins/PluginCache');
2018-02-01 05:48:56 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-02-06 15:04:20 +00:00
* The Game Object Factory is a Scene plugin that allows you to quickly create many common
* types of Game Objects and have them automatically registered with the Scene.
*
* Game Objects directly register themselves with the Factory and inject their own creation
* methods into the class.
2018-02-01 05:48:56 +00:00
*
* @class GameObjectFactory
* @memberOf Phaser.GameObjects
* @constructor
* @since 3.0.0
*
2018-02-06 15:04:20 +00:00
* @param {Phaser.Scene} scene - The Scene to which this Game Object Factory belongs.
2018-02-01 05:48:56 +00:00
*/
var GameObjectFactory = new Class({
initialize:
function GameObjectFactory (scene)
{
2018-02-01 05:48:56 +00:00
/**
2018-02-06 15:04:20 +00:00
* The Scene to which this Game Object Factory belongs.
2018-02-01 05:48:56 +00:00
*
* @name Phaser.GameObjects.GameObjectFactory#scene
* @type {Phaser.Scene}
* @protected
* @since 3.0.0
*/
this.scene = scene;
2018-02-01 05:48:56 +00:00
/**
* A reference to the Scene.Systems.
*
* @name Phaser.GameObjects.GameObjectFactory#systems
* @type {Phaser.Scenes.Systems}
* @protected
* @since 3.0.0
*/
2018-01-16 02:08:22 +00:00
this.systems = scene.sys;
2018-02-06 15:04:20 +00:00
/**
* A reference to the Scene Display List.
*
* @name Phaser.GameObjects.GameObjectFactory#displayList
* @type {Phaser.GameObjects.DisplayList}
* @protected
* @since 3.0.0
*/
this.displayList;
2018-02-01 05:48:56 +00:00
2018-02-06 15:04:20 +00:00
/**
* A reference to the Scene Update List.
*
* @name Phaser.GameObjects.GameObjectFactory#updateList;
* @type {Phaser.GameObjects.UpdateList}
* @protected
* @since 3.0.0
*/
this.updateList;
scene.sys.events.once('boot', this.boot, this);
scene.sys.events.on('start', this.start, this);
},
/**
* This method is called automatically, only once, when the Scene is first created.
* Do not invoke it directly.
*
* @method Phaser.GameObjects.GameObjectFactory#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.displayList = this.systems.displayList;
this.updateList = this.systems.updateList;
this.systems.events.once('destroy', this.destroy, this);
},
2018-02-01 05:48:56 +00:00
/**
* This method is called automatically by the Scene when it is starting up.
* It is responsible for creating local systems, properties and listening for Scene events.
* Do not invoke it directly.
2018-02-01 05:48:56 +00:00
*
* @method Phaser.GameObjects.GameObjectFactory#start
2018-02-01 05:48:56 +00:00
* @private
* @since 3.5.0
2018-02-01 05:48:56 +00:00
*/
start: function ()
{
this.systems.events.once('shutdown', this.shutdown, this);
},
2018-02-01 05:48:56 +00:00
/**
* Adds an existing Game Object to this Scene.
*
* If the Game Object renders, it will be added to the Display List.
* If it has a `preUpdate` method, it will be added to the Update List.
*
* @method Phaser.GameObjects.GameObjectFactory#existing
* @since 3.0.0
*
* @param {Phaser.GameObjects.GameObject} child - The child to be added to this Scene.
*
* @return {Phaser.GameObjects.GameObject} The Game Object that was added.
*/
existing: function (child)
{
if (child.renderCanvas || child.renderWebGL)
{
this.displayList.add(child);
}
if (child.preUpdate)
{
this.updateList.add(child);
}
return child;
},
2018-02-01 05:48:56 +00:00
/**
* The Scene that owns this plugin is shutting down.
* We need to kill and reset all internal properties as well as stop listening to Scene events.
2018-02-01 05:48:56 +00:00
*
* @method Phaser.GameObjects.GameObjectFactory#shutdown
* @private
2018-02-01 05:48:56 +00:00
* @since 3.0.0
*/
2018-01-16 02:08:22 +00:00
shutdown: function ()
{
this.systems.events.off('shutdown', this.shutdown, this);
2018-01-16 02:08:22 +00:00
},
2018-02-01 05:48:56 +00:00
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
2018-02-01 05:48:56 +00:00
*
* @method Phaser.GameObjects.GameObjectFactory#destroy
* @private
2018-02-01 05:48:56 +00:00
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
this.scene.sys.events.off('start', this.start, this);
this.scene = null;
this.systems = null;
this.displayList = null;
this.updateList = null;
}
});
// Static method called directly by the Game Object factory functions
2017-08-03 03:06:13 +00:00
2018-03-19 13:45:00 +00:00
GameObjectFactory.register = function (factoryType, factoryFunction)
{
2018-03-19 13:45:00 +00:00
if (!GameObjectFactory.prototype.hasOwnProperty(factoryType))
{
2018-03-19 13:45:00 +00:00
GameObjectFactory.prototype[factoryType] = factoryFunction;
}
};
PluginCache.register('GameObjectFactory', GameObjectFactory, 'add');
2018-01-16 02:08:22 +00:00
module.exports = GameObjectFactory;