phaser/src/gameobjects/GameObjectCreator.js

163 lines
4.5 KiB
JavaScript
Raw Normal View History

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
*/
2018-01-16 19:49:13 +00:00
var Class = require('../utils/Class');
var PluginCache = require('../plugins/PluginCache');
2019-01-18 13:41:43 +00:00
var SceneEvents = require('../scene/events');
2018-02-06 15:04:20 +00:00
/**
2018-02-07 15:27:21 +00:00
* @classdesc
2018-02-06 15:04:20 +00:00
* The Game Object Creator is a Scene plugin that allows you to quickly create many common
* types of Game Objects and return them. Unlike the Game Object Factory, they are not automatically
* added to the Scene.
*
* Game Objects directly register themselves with the Creator and inject their own creation
* methods into the class.
*
* @class GameObjectCreator
2018-10-10 09:49:13 +00:00
* @memberof Phaser.GameObjects
2018-02-06 15:04:20 +00:00
* @constructor
* @since 3.0.0
*
* @param {Phaser.Scene} scene - The Scene to which this Game Object Factory belongs.
*/
var GameObjectCreator = new Class({
initialize:
function GameObjectCreator (scene)
{
2018-02-06 15:04:20 +00:00
/**
* The Scene to which this Game Object Creator belongs.
*
* @name Phaser.GameObjects.GameObjectCreator#scene
* @type {Phaser.Scene}
* @protected
* @since 3.0.0
*/
this.scene = scene;
2018-02-06 15:04:20 +00:00
/**
* A reference to the Scene.Systems.
*
* @name Phaser.GameObjects.GameObjectCreator#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.GameObjectCreator#displayList
* @type {Phaser.GameObjects.DisplayList}
* @protected
* @since 3.0.0
*/
this.displayList;
2018-02-06 15:04:20 +00:00
/**
* A reference to the Scene Update List.
*
2019-12-18 09:38:44 +00:00
* @name Phaser.GameObjects.GameObjectCreator#updateList
2018-02-06 15:04:20 +00:00
* @type {Phaser.GameObjects.UpdateList}
* @protected
* @since 3.0.0
*/
this.updateList;
2019-01-18 13:41:43 +00:00
scene.sys.events.once(SceneEvents.BOOT, this.boot, this);
scene.sys.events.on(SceneEvents.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.GameObjectCreator#boot
* @private
* @since 3.5.1
*/
boot: function ()
{
this.displayList = this.systems.displayList;
this.updateList = this.systems.updateList;
2019-01-18 13:41:43 +00:00
this.systems.events.once(SceneEvents.DESTROY, this.destroy, this);
},
2018-02-06 15:04:20 +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-06 15:04:20 +00:00
*
* @method Phaser.GameObjects.GameObjectCreator#start
2018-02-06 15:04:20 +00:00
* @private
* @since 3.5.0
2018-02-06 15:04:20 +00:00
*/
start: function ()
2018-01-16 02:08:22 +00:00
{
2019-01-18 13:41:43 +00:00
this.systems.events.once(SceneEvents.SHUTDOWN, this.shutdown, this);
2018-01-16 02:08:22 +00:00
},
2018-02-06 15:04:20 +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-06 15:04:20 +00:00
*
* @method Phaser.GameObjects.GameObjectCreator#shutdown
* @private
2018-02-06 15:04:20 +00:00
* @since 3.0.0
*/
2018-01-16 02:08:22 +00:00
shutdown: function ()
{
2019-01-18 13:41:43 +00:00
this.systems.events.off(SceneEvents.SHUTDOWN, this.shutdown, this);
},
2018-02-06 15:04:20 +00:00
/**
* The Scene that owns this plugin is being destroyed.
* We need to shutdown and then kill off all external references.
2018-02-06 15:04:20 +00:00
*
* @method Phaser.GameObjects.GameObjectCreator#destroy
* @private
2018-02-06 15:04:20 +00:00
* @since 3.0.0
*/
destroy: function ()
{
this.shutdown();
2019-01-18 13:41:43 +00:00
this.scene.sys.events.off(SceneEvents.START, this.start, this);
this.scene = null;
this.systems = null;
this.displayList = null;
this.updateList = null;
}
});
// Static method called directly by the Game Object creator functions
2018-03-19 13:45:00 +00:00
GameObjectCreator.register = function (factoryType, factoryFunction)
{
2018-03-19 13:45:00 +00:00
if (!GameObjectCreator.prototype.hasOwnProperty(factoryType))
{
2018-03-19 13:45:00 +00:00
GameObjectCreator.prototype[factoryType] = factoryFunction;
}
};
GameObjectCreator.remove = function (factoryType)
{
if (GameObjectCreator.prototype.hasOwnProperty(factoryType))
{
delete GameObjectCreator.prototype[factoryType];
}
};
PluginCache.register('GameObjectCreator', GameObjectCreator, 'make');
2018-01-16 02:08:22 +00:00
module.exports = GameObjectCreator;