Skip to content

Commit ed8a0d9

Browse files
committed
Renamed Plugin to BasePlugin to accurately reflect what it is. Added default methods.
1 parent e431cc3 commit ed8a0d9

2 files changed

Lines changed: 190 additions & 178 deletions

File tree

src/plugins/BasePlugin.js

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/**
2+
* @author Richard Davey <rich@photonstorm.com>
3+
* @copyright 2018 Photon Storm Ltd.
4+
* @license {@link https://github.com/photonstorm/phaser3-plugin-template/blob/master/LICENSE|MIT License}
5+
*/
6+
7+
var Class = require('../utils/Class');
8+
9+
// A Scene Level Plugin is installed into every Scene and belongs to that Scene.
10+
// It can listen for Scene events and respond to them.
11+
// It can map itself to a Scene property, or into the Scene Systems, or both.
12+
//
13+
// A Global Plugin is installed just once into the Game owned Plugin Manager.
14+
// It can listen for Game events and respond to them.
15+
16+
/**
17+
* @classdesc
18+
* [description]
19+
*
20+
* @class BasePlugin
21+
* @memberOf Phaser.Plugins
22+
* @constructor
23+
* @since 3.8.0
24+
*
25+
* @param {Phaser.Game} game - [description]
26+
*/
27+
var BasePlugin = new Class({
28+
29+
initialize:
30+
31+
function BasePlugin (key, pluginManager)
32+
{
33+
/**
34+
* The unique (within this game instance) name of this plugin within the Plugin Manager.
35+
* This is set by the developer in their game config, although you should provide a default value.
36+
*
37+
* @name Phaser.Plugins.BasePlugin#key
38+
* @type {string}
39+
* @protected
40+
* @since 3.8.0
41+
*/
42+
this.key = key;
43+
44+
/**
45+
* A handy reference to the Plugin Manager that is responsible for this plugin.
46+
* Can be used as a route to gain access to game systems and events.
47+
*
48+
* @name Phaser.Plugins.BasePlugin#pluginManager
49+
* @type {Phaser.Plugins.BasePluginManager}
50+
* @protected
51+
* @since 3.8.0
52+
*/
53+
this.pluginManager = pluginManager;
54+
55+
/**
56+
* A reference to the Game instance this plugin is running under.
57+
*
58+
* @name Phaser.Plugins.BasePlugin#game
59+
* @type {Phaser.Game}
60+
* @protected
61+
* @since 3.8.0
62+
*/
63+
this.game = pluginManager.game;
64+
65+
/**
66+
* A reference to the Scene that has installed this plugin.
67+
* Only set if it's a Scene Plugin, otherwise `null`.
68+
* This property is only set when the plugin is instantiated and added to the Scene, not before.
69+
* You cannot use it during the `init` method, but you can during the `boot` method.
70+
*
71+
* @name Phaser.Plugins.BasePlugin#scene
72+
* @type {?Phaser.Scene}
73+
* @protected
74+
* @since 3.8.0
75+
*/
76+
this.scene;
77+
78+
/**
79+
* A reference to the Scene Systems of the Scene that has installed this plugin.
80+
* Only set if it's a Scene Plugin, otherwise `null`.
81+
* This property is only set when the plugin is instantiated and added to the Scene, not before.
82+
* You cannot use it during the `init` method, but you can during the `boot` method.
83+
*
84+
* @name Phaser.Plugins.BasePlugin#systems
85+
* @type {?Phaser.Scene.Systems}
86+
* @protected
87+
* @since 3.8.0
88+
*/
89+
this.systems;
90+
},
91+
92+
/**
93+
* Called by the PluginManager when this plugin is first instantiated.
94+
* It will never be called again on this instance.
95+
* In here you can set-up whatever you need for this plugin to run.
96+
* If a plugin is set to automatically start then `BasePlugin.start` will be called immediately after this.
97+
*
98+
* @method Phaser.Plugins.BasePlugin#init
99+
* @since 3.8.0
100+
*/
101+
init: function ()
102+
{
103+
},
104+
105+
/**
106+
* Called by the PluginManager when this plugin is started.
107+
* If a plugin is stopped, and then started again, this will get called again.
108+
* Typically called immediately after `BasePlugin.init`.
109+
*
110+
* @method Phaser.Plugins.BasePlugin#start
111+
* @since 3.8.0
112+
*/
113+
start: function ()
114+
{
115+
// Here are the game-level events you can listen to.
116+
// At the very least you should offer a destroy handler for when the game closes down.
117+
118+
// var eventEmitter = this.game.events;
119+
120+
// eventEmitter.once('destroy', this.gameDestroy, this);
121+
// eventEmitter.on('pause', this.gamePause, this);
122+
// eventEmitter.on('resume', this.gameResume, this);
123+
// eventEmitter.on('resize', this.gameResize, this);
124+
// eventEmitter.on('prestep', this.gamePreStep, this);
125+
// eventEmitter.on('step', this.gameStep, this);
126+
// eventEmitter.on('poststep', this.gamePostStep, this);
127+
// eventEmitter.on('prerender', this.gamePreRender, this);
128+
// eventEmitter.on('postrender', this.gamePostRender, this);
129+
},
130+
131+
/**
132+
* Called by the PluginManager when this plugin is stopped.
133+
* The game code has requested that your plugin stop doing whatever it does.
134+
* It is now considered as 'inactive' by the PluginManager.
135+
* Handle that process here (i.e. stop listening for events, etc)
136+
* If the plugin is started again then `BasePlugin.start` will be called again.
137+
*
138+
* @method Phaser.Plugins.BasePlugin#stop
139+
* @since 3.8.0
140+
*/
141+
stop: function ()
142+
{
143+
},
144+
145+
/**
146+
* If this is a Scene Plugin (i.e. installed into a Scene) then this method is called when the Scene boots.
147+
* By this point the plugin properties `scene` and `systems` will have already been set.
148+
* In here you can listen for Scene events and set-up whatever you need for this plugin to run.
149+
*
150+
* @method Phaser.Plugins.BasePlugin#boot
151+
* @since 3.8.0
152+
*/
153+
boot: function ()
154+
{
155+
// Here are the Scene events you can listen to.
156+
// At the very least you should offer a destroy handler for when the Scene closes down.
157+
158+
// var eventEmitter = this.systems.events;
159+
160+
// eventEmitter.once('destroy', this.sceneDestroy, this);
161+
// eventEmitter.on('start', this.sceneStart, this);
162+
// eventEmitter.on('preupdate', this.scenePreUpdate, this);
163+
// eventEmitter.on('update', this.sceneUpdate, this);
164+
// eventEmitter.on('postupdate', this.scenePostUpdate, this);
165+
// eventEmitter.on('pause', this.scenePause, this);
166+
// eventEmitter.on('resume', this.sceneResume, this);
167+
// eventEmitter.on('sleep', this.sceneSleep, this);
168+
// eventEmitter.on('wake', this.sceneWake, this);
169+
// eventEmitter.on('shutdown', this.sceneShutdown, this);
170+
// eventEmitter.on('destroy', this.sceneDestroy, this);
171+
},
172+
173+
/**
174+
* Game instance has been destroyed.
175+
* You must release everything in here, all references, all objects, free it all up.
176+
*
177+
* @method Phaser.Plugins.BasePlugin#destroy
178+
* @since 3.8.0
179+
*/
180+
destroy: function ()
181+
{
182+
this.pluginManager = null;
183+
this.game = null;
184+
this.scene = null;
185+
this.systems = null;
186+
}
187+
188+
});
189+
190+
module.exports = BasePlugin;

src/plugins/Plugin.js

Lines changed: 0 additions & 178 deletions
This file was deleted.

0 commit comments

Comments
 (0)