2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 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');
|
2018-05-15 11:51:50 +00:00
|
|
|
var PluginCache = require('../plugins/PluginCache');
|
2019-01-18 13:41:43 +00:00
|
|
|
var SceneEvents = require('../scene/events');
|
2017-07-04 00:59:31 +00:00
|
|
|
|
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
|
2018-10-10 09:49:13 +00:00
|
|
|
* @memberof Phaser.GameObjects
|
2018-02-01 05:48:56 +00:00
|
|
|
* @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
|
|
|
*/
|
2017-09-14 00:32:10 +00:00
|
|
|
var GameObjectFactory = new Class({
|
2017-07-04 00:59:31 +00:00
|
|
|
|
|
|
|
initialize:
|
|
|
|
|
2017-07-14 13:30:20 +00:00
|
|
|
function GameObjectFactory (scene)
|
2017-07-04 00:59:31 +00:00
|
|
|
{
|
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
|
|
|
|
*/
|
2017-07-14 13:30:20 +00:00
|
|
|
this.scene = scene;
|
2017-07-04 00:59:31 +00:00
|
|
|
|
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;
|
|
|
|
|
2020-08-24 18:20:20 +00:00
|
|
|
/**
|
|
|
|
* A reference to the Scene Event Emitter.
|
|
|
|
*
|
|
|
|
* @name Phaser.GameObjects.GameObjectFactory#events
|
|
|
|
* @type {Phaser.Events.EventEmitter}
|
|
|
|
* @protected
|
|
|
|
* @since 3.50.0
|
|
|
|
*/
|
|
|
|
this.events = scene.sys.events;
|
|
|
|
|
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
|
|
|
|
*/
|
2017-08-15 22:37:38 +00:00
|
|
|
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.
|
|
|
|
*
|
2019-12-18 09:38:44 +00:00
|
|
|
* @name Phaser.GameObjects.GameObjectFactory#updateList
|
2018-02-06 15:04:20 +00:00
|
|
|
* @type {Phaser.GameObjects.UpdateList}
|
|
|
|
* @protected
|
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-08-15 22:37:38 +00:00
|
|
|
this.updateList;
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2020-08-24 18:20:20 +00:00
|
|
|
this.events.once(SceneEvents.BOOT, this.boot, this);
|
|
|
|
this.events.on(SceneEvents.START, this.start, this);
|
2017-08-15 22:37:38 +00:00
|
|
|
},
|
|
|
|
|
2018-04-17 01:34:07 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
2018-04-17 11:25:45 +00:00
|
|
|
|
2020-08-24 18:20:20 +00:00
|
|
|
this.events.once(SceneEvents.DESTROY, this.destroy, this);
|
2018-04-17 01:34:07 +00:00
|
|
|
},
|
|
|
|
|
2018-02-01 05:48:56 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +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
|
|
|
*
|
2018-04-13 16:12:17 +00:00
|
|
|
* @method Phaser.GameObjects.GameObjectFactory#start
|
2018-02-01 05:48:56 +00:00
|
|
|
* @private
|
2018-04-15 11:44:47 +00:00
|
|
|
* @since 3.5.0
|
2018-02-01 05:48:56 +00:00
|
|
|
*/
|
2018-04-13 16:12:17 +00:00
|
|
|
start: function ()
|
2017-08-15 22:37:38 +00:00
|
|
|
{
|
2020-08-24 18:20:20 +00:00
|
|
|
this.events.once(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2017-07-04 00:59:31 +00:00
|
|
|
},
|
|
|
|
|
2018-02-01 05:48:56 +00:00
|
|
|
/**
|
|
|
|
* Adds an existing Game Object to this Scene.
|
2019-12-18 09:38:44 +00:00
|
|
|
*
|
2018-02-01 05:48:56 +00:00
|
|
|
* 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
|
|
|
|
*
|
2021-04-13 12:42:33 +00:00
|
|
|
* @generic {(Phaser.GameObjects.GameObject|Phaser.GameObjects.Group|Phaser.GameObjects.Layer)} G - [child,$return]
|
2020-07-29 19:19:09 +00:00
|
|
|
*
|
2021-04-13 12:42:33 +00:00
|
|
|
* @param {(Phaser.GameObjects.GameObject|Phaser.GameObjects.Group|Phaser.GameObjects.Layer)} child - The child to be added to this Scene.
|
2018-02-01 05:48:56 +00:00
|
|
|
*
|
|
|
|
* @return {Phaser.GameObjects.GameObject} The Game Object that was added.
|
|
|
|
*/
|
2017-07-05 00:21:47 +00:00
|
|
|
existing: function (child)
|
|
|
|
{
|
|
|
|
if (child.renderCanvas || child.renderWebGL)
|
|
|
|
{
|
|
|
|
this.displayList.add(child);
|
|
|
|
}
|
2020-09-30 15:30:12 +00:00
|
|
|
|
|
|
|
// For when custom objects have overridden `preUpdate` but don't hook into the ADDED_TO_SCENE event:
|
|
|
|
// Adding to the list multiple times is safe, as it won't add duplicates into the list anyway.
|
|
|
|
if (child.preUpdate)
|
2020-09-13 09:05:55 +00:00
|
|
|
{
|
|
|
|
this.updateList.add(child);
|
|
|
|
}
|
2017-07-05 00:21:47 +00:00
|
|
|
|
|
|
|
return child;
|
|
|
|
},
|
|
|
|
|
2018-02-01 05:48:56 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +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
|
2018-04-13 16:12:17 +00:00
|
|
|
* @private
|
2018-02-01 05:48:56 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2018-01-16 02:08:22 +00:00
|
|
|
shutdown: function ()
|
|
|
|
{
|
2020-08-24 18:20:20 +00:00
|
|
|
this.events.off(SceneEvents.SHUTDOWN, this.shutdown, this);
|
2018-01-16 02:08:22 +00:00
|
|
|
},
|
|
|
|
|
2018-02-01 05:48:56 +00:00
|
|
|
/**
|
2018-04-13 16:12:17 +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
|
2018-04-13 16:12:17 +00:00
|
|
|
* @private
|
2018-02-01 05:48:56 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*/
|
2017-09-14 00:32:10 +00:00
|
|
|
destroy: function ()
|
2017-07-04 00:59:31 +00:00
|
|
|
{
|
2018-04-13 16:12:17 +00:00
|
|
|
this.shutdown();
|
|
|
|
|
2020-08-24 18:20:20 +00:00
|
|
|
this.events.off(SceneEvents.START, this.start, this);
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2017-09-14 00:32:10 +00:00
|
|
|
this.scene = null;
|
2018-04-13 16:12:17 +00:00
|
|
|
this.systems = null;
|
2020-08-24 18:20:20 +00:00
|
|
|
this.events = null;
|
2018-04-13 16:12:17 +00:00
|
|
|
|
2017-09-14 00:32:10 +00:00
|
|
|
this.displayList = null;
|
|
|
|
this.updateList = null;
|
|
|
|
}
|
2017-07-04 00:59:31 +00:00
|
|
|
|
2017-09-14 00:32:10 +00:00
|
|
|
});
|
2017-07-27 16:40:15 +00:00
|
|
|
|
2019-12-18 10:32:29 +00:00
|
|
|
/**
|
|
|
|
* Static method called directly by the Game Object factory functions.
|
|
|
|
* With this method you can register a custom GameObject factory in the GameObjectFactory,
|
|
|
|
* providing a name (`factoryType`) and the constructor (`factoryFunction`) in order
|
|
|
|
* to be called when you call to Phaser.Scene.add[ factoryType ] method.
|
|
|
|
*
|
2019-12-18 10:48:30 +00:00
|
|
|
* @method Phaser.GameObjects.GameObjectFactory.register
|
2019-12-18 10:32:29 +00:00
|
|
|
* @static
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} factoryType - The key of the factory that you will use to call to Phaser.Scene.add[ factoryType ] method.
|
|
|
|
* @param {function} factoryFunction - The constructor function to be called when you invoke to the Phaser.Scene.add method.
|
|
|
|
*/
|
2018-03-19 13:45:00 +00:00
|
|
|
GameObjectFactory.register = function (factoryType, factoryFunction)
|
2017-09-14 00:32:10 +00:00
|
|
|
{
|
2018-03-19 13:45:00 +00:00
|
|
|
if (!GameObjectFactory.prototype.hasOwnProperty(factoryType))
|
2017-08-11 12:22:41 +00:00
|
|
|
{
|
2018-03-19 13:45:00 +00:00
|
|
|
GameObjectFactory.prototype[factoryType] = factoryFunction;
|
2017-09-13 15:06:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-12-18 10:32:29 +00:00
|
|
|
/**
|
|
|
|
* Static method called directly by the Game Object factory functions.
|
|
|
|
* With this method you can remove a custom GameObject factory registered in the GameObjectFactory,
|
|
|
|
* providing a its `factoryType`.
|
|
|
|
*
|
2019-12-18 10:48:30 +00:00
|
|
|
* @method Phaser.GameObjects.GameObjectFactory.remove
|
2019-12-18 10:32:29 +00:00
|
|
|
* @static
|
|
|
|
* @since 3.0.0
|
|
|
|
*
|
|
|
|
* @param {string} factoryType - The key of the factory that you want to remove from the GameObjectFactory.
|
|
|
|
*/
|
2019-07-17 13:36:02 +00:00
|
|
|
GameObjectFactory.remove = function (factoryType)
|
|
|
|
{
|
|
|
|
if (GameObjectFactory.prototype.hasOwnProperty(factoryType))
|
|
|
|
{
|
|
|
|
delete GameObjectFactory.prototype[factoryType];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-05-15 11:51:50 +00:00
|
|
|
PluginCache.register('GameObjectFactory', GameObjectFactory, 'add');
|
2018-01-16 02:08:22 +00:00
|
|
|
|
2017-07-04 00:59:31 +00:00
|
|
|
module.exports = GameObjectFactory;
|