UNPKG

phaser

Version:

A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers from the team at Phaser Studio Inc.

146 lines (132 loc) 5 kB
/** * @author Richard Davey <rich@phaser.io> * @copyright 2013-2026 Phaser Studio Inc. * @license {@link https://github.com/photonstorm/phaser3-plugin-template/blob/master/LICENSE|MIT License} */ var BasePlugin = require('./BasePlugin'); var Class = require('../utils/Class'); var SceneEvents = require('../scene/events'); /** * @classdesc * A Scene Level Plugin is a plugin that is installed into every Scene and belongs to that Scene for its lifetime. * Unlike a Global Plugin, which exists once for the entire game, a Scene Plugin is instantiated separately for * each Scene and is destroyed when its Scene is destroyed. * * You would extend this class when creating your own custom plugins that need per-Scene state or need to * respond to Scene lifecycle events such as `start`, `pause`, `sleep`, `wake`, `shutdown`, and `destroy`. * * The plugin can map itself to a property on the Scene (e.g. `this.myPlugin`) and/or into the Scene Systems, * making it accessible from anywhere within that Scene. Override the `boot` method to set up event listeners * and initialize any per-Scene resources your plugin requires. * * @class ScenePlugin * @memberof Phaser.Plugins * @extends Phaser.Plugins.BasePlugin * @constructor * @since 3.8.0 * * @param {Phaser.Scene} scene - A reference to the Scene that has installed this plugin. * @param {Phaser.Plugins.PluginManager} pluginManager - A reference to the Plugin Manager. * @param {string} pluginKey - The key under which this plugin has been installed into the Scene Systems. */ var ScenePlugin = new Class({ Extends: BasePlugin, initialize: function ScenePlugin (scene, pluginManager, pluginKey) { BasePlugin.call(this, pluginManager); /** * A reference to the Scene that has installed this plugin. * Only set if it's a Scene Plugin, otherwise `null`. * This property is only set when the plugin is instantiated and added to the Scene, not before. * You can use it during the `boot` method. * * @name Phaser.Plugins.ScenePlugin#scene * @type {?Phaser.Scene} * @protected * @since 3.8.0 */ this.scene = scene; /** * A reference to the Scene Systems of the Scene that has installed this plugin. * Only set if it's a Scene Plugin, otherwise `null`. * This property is only set when the plugin is instantiated and added to the Scene, not before. * You can use it during the `boot` method. * * @name Phaser.Plugins.ScenePlugin#systems * @type {?Phaser.Scenes.Systems} * @protected * @since 3.8.0 */ this.systems = scene.sys; /** * The key under which this plugin was installed into the Scene Systems. * * This property is only set when the plugin is instantiated and added to the Scene, not before. * You can use it during the `boot` method. * * @name Phaser.Plugins.ScenePlugin#pluginKey * @type {string} * @readonly * @since 3.54.0 */ this.pluginKey = pluginKey; scene.sys.events.once(SceneEvents.BOOT, this.boot, this); }, /** * This method is called when the Scene boots. It is only ever called once. * * By this point the plugin properties `scene` and `systems` will have already been set. * * In here you can listen for {@link Phaser.Scenes.Events Scene events} and set-up whatever you need for this plugin to run. * Here are the Scene events you can listen to: * * - start * - ready * - preupdate * - update * - postupdate * - resize * - pause * - resume * - sleep * - wake * - transitioninit * - transitionstart * - transitioncomplete * - transitionout * - shutdown * - destroy * * At the very least you should offer a destroy handler for when the Scene closes down, i.e: * * ```javascript * var eventEmitter = this.systems.events; * eventEmitter.once('destroy', this.sceneDestroy, this); * ``` * * @method Phaser.Plugins.ScenePlugin#boot * @since 3.8.0 */ boot: function () { }, /** * Destroys this plugin and releases all references it holds, including the Scene, Scene Systems, * and Plugin Manager. This method is called automatically when the Scene is destroyed. * * If you extend this class you should override this method, clean up any resources your plugin * has created, and then call `BasePlugin.destroy` via `super.destroy()` or the prototype chain. * * @method Phaser.Plugins.ScenePlugin#destroy * @since 3.8.0 */ destroy: function () { this.pluginManager = null; this.game = null; this.scene = null; this.systems = null; } }); module.exports = ScenePlugin;