├── .gitignore ├── LICENSE ├── README.md ├── dist ├── BasePlugin.js └── BasePlugin.min.js ├── package.json ├── src └── main.js └── webpack.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | # System and IDE files 2 | Thumbs.db 3 | .DS_Store 4 | .idea 5 | *.suo 6 | *.sublime-project 7 | *.sublime-workspace 8 | 9 | # Vendors 10 | node_modules/ 11 | 12 | # Build 13 | build/ 14 | /npm-debug.log 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Richard Davey 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Phaser3 Plugin Template 2 | 3 | A base plugin template for Phaser 3 to allow you to create your own plugins. 4 | 5 | Run `npm install` and then `npm run build` to build the plugin. 6 | 7 | ## Using Plugins in Phaser 3 8 | 9 | You can load plugins externally, or include them in your bundle. 10 | 11 | To load an external plugin: 12 | 13 | ``` 14 | function preload () 15 | { 16 | this.load.plugin('BasePlugin', 'path/to/BasePlugin.js'); 17 | } 18 | ``` 19 | 20 | Then to install it into a Scene: 21 | 22 | ``` 23 | this.sys.install('BasePlugin'); 24 | ``` 25 | 26 | If you load the plugins in a Preloader scene then you can add them to any other Scenes by specifying them in the plugins array: 27 | 28 | ``` 29 | var config = { 30 | scene: { 31 | create: create, 32 | plugins: [ 'BasePlugin' ], 33 | map: { 34 | 'base': 'base' 35 | } 36 | } 37 | }; 38 | ``` 39 | 40 | More examples and instructions will follow, but for now see the [Phaser 3 Examples](https://github.com/photonstorm/phaser3-examples) repo for details. 41 | -------------------------------------------------------------------------------- /dist/BasePlugin.js: -------------------------------------------------------------------------------- 1 | (function webpackUniversalModuleDefinition(root, factory) { 2 | if(typeof exports === 'object' && typeof module === 'object') 3 | module.exports = factory(); 4 | else if(typeof define === 'function' && define.amd) 5 | define("BasePlugin", [], factory); 6 | else if(typeof exports === 'object') 7 | exports["BasePlugin"] = factory(); 8 | else 9 | root["BasePlugin"] = factory(); 10 | })(typeof self !== 'undefined' ? self : this, function() { 11 | return /******/ (function(modules) { // webpackBootstrap 12 | /******/ // The module cache 13 | /******/ var installedModules = {}; 14 | /******/ 15 | /******/ // The require function 16 | /******/ function __webpack_require__(moduleId) { 17 | /******/ 18 | /******/ // Check if module is in cache 19 | /******/ if(installedModules[moduleId]) { 20 | /******/ return installedModules[moduleId].exports; 21 | /******/ } 22 | /******/ // Create a new module (and put it into the cache) 23 | /******/ var module = installedModules[moduleId] = { 24 | /******/ i: moduleId, 25 | /******/ l: false, 26 | /******/ exports: {} 27 | /******/ }; 28 | /******/ 29 | /******/ // Execute the module function 30 | /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); 31 | /******/ 32 | /******/ // Flag the module as loaded 33 | /******/ module.l = true; 34 | /******/ 35 | /******/ // Return the exports of the module 36 | /******/ return module.exports; 37 | /******/ } 38 | /******/ 39 | /******/ 40 | /******/ // expose the modules object (__webpack_modules__) 41 | /******/ __webpack_require__.m = modules; 42 | /******/ 43 | /******/ // expose the module cache 44 | /******/ __webpack_require__.c = installedModules; 45 | /******/ 46 | /******/ // define getter function for harmony exports 47 | /******/ __webpack_require__.d = function(exports, name, getter) { 48 | /******/ if(!__webpack_require__.o(exports, name)) { 49 | /******/ Object.defineProperty(exports, name, { 50 | /******/ configurable: false, 51 | /******/ enumerable: true, 52 | /******/ get: getter 53 | /******/ }); 54 | /******/ } 55 | /******/ }; 56 | /******/ 57 | /******/ // getDefaultExport function for compatibility with non-harmony modules 58 | /******/ __webpack_require__.n = function(module) { 59 | /******/ var getter = module && module.__esModule ? 60 | /******/ function getDefault() { return module['default']; } : 61 | /******/ function getModuleExports() { return module; }; 62 | /******/ __webpack_require__.d(getter, 'a', getter); 63 | /******/ return getter; 64 | /******/ }; 65 | /******/ 66 | /******/ // Object.prototype.hasOwnProperty.call 67 | /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; 68 | /******/ 69 | /******/ // __webpack_public_path__ 70 | /******/ __webpack_require__.p = ""; 71 | /******/ 72 | /******/ // Load entry module and return exports 73 | /******/ return __webpack_require__(__webpack_require__.s = 0); 74 | /******/ }) 75 | /************************************************************************/ 76 | /******/ ([ 77 | /* 0 */ 78 | /***/ (function(module, exports) { 79 | 80 | /** 81 | * @author Richard Davey 82 | * @copyright 2018 Photon Storm Ltd. 83 | * @license {@link https://github.com/photonstorm/phaser3-plugin-template/blob/master/LICENSE|MIT License} 84 | */ 85 | 86 | var BasePlugin = function (scene) 87 | { 88 | // The Scene that owns this plugin 89 | this.scene = scene; 90 | 91 | this.systems = scene.sys; 92 | 93 | if (!scene.sys.settings.isBooted) 94 | { 95 | scene.sys.events.once('boot', this.boot, this); 96 | } 97 | }; 98 | 99 | // Static function called by the PluginFile Loader. 100 | BasePlugin.register = function (PluginManager) 101 | { 102 | // Register this plugin with the PluginManager, so it can be added to Scenes. 103 | 104 | // The first argument is the name this plugin will be known as in the PluginManager. It should not conflict with already registered plugins. 105 | // The second argument is a reference to the plugin object, which will be instantiated by the PluginManager when the Scene boots. 106 | // The third argument is the local mapping. This will make the plugin available under `this.sys.base` and also `this.base` from a Scene if 107 | // it has an entry in the InjectionMap. 108 | PluginManager.register('BasePlugin', BasePlugin, 'base'); 109 | }; 110 | 111 | BasePlugin.prototype = { 112 | 113 | // Called when the Plugin is booted by the PluginManager. 114 | // If you need to reference other systems in the Scene (like the Loader or DisplayList) then set-up those references now, not in the constructor. 115 | boot: function () 116 | { 117 | var eventEmitter = this.systems.events; 118 | 119 | // Listening to the following events is entirely optional, although we would recommend cleanly shutting down and destroying at least. 120 | // If you don't need any of these events then remove the listeners and the relevant methods too. 121 | 122 | eventEmitter.on('start', this.start, this); 123 | 124 | eventEmitter.on('preupdate', this.preUpdate, this); 125 | eventEmitter.on('update', this.update, this); 126 | eventEmitter.on('postupdate', this.postUpdate, this); 127 | 128 | eventEmitter.on('pause', this.pause, this); 129 | eventEmitter.on('resume', this.resume, this); 130 | 131 | eventEmitter.on('sleep', this.sleep, this); 132 | eventEmitter.on('wake', this.wake, this); 133 | 134 | eventEmitter.on('shutdown', this.shutdown, this); 135 | eventEmitter.on('destroy', this.destroy, this); 136 | }, 137 | 138 | // A test method. 139 | test: function (name) 140 | { 141 | console.log('BasePlugin says hello ' + name + '!'); 142 | }, 143 | 144 | // Called when a Scene is started by the SceneManager. The Scene is now active, visible and running. 145 | start: function () 146 | { 147 | }, 148 | 149 | // Called every Scene step - phase 1 150 | preUpdate: function (time, delta) 151 | { 152 | }, 153 | 154 | // Called every Scene step - phase 2 155 | update: function (time, delta) 156 | { 157 | }, 158 | 159 | // Called every Scene step - phase 3 160 | postUpdate: function (time, delta) 161 | { 162 | }, 163 | 164 | // Called when a Scene is paused. A paused scene doesn't have its Step run, but still renders. 165 | pause: function () 166 | { 167 | }, 168 | 169 | // Called when a Scene is resumed from a paused state. 170 | resume: function () 171 | { 172 | }, 173 | 174 | // Called when a Scene is put to sleep. A sleeping scene doesn't update or render, but isn't destroyed or shutdown. preUpdate events still fire. 175 | sleep: function () 176 | { 177 | }, 178 | 179 | // Called when a Scene is woken from a sleeping state. 180 | wake: function () 181 | { 182 | }, 183 | 184 | // Called when a Scene shuts down, it may then come back again later (which will invoke the 'start' event) but should be considered dormant. 185 | shutdown: function () 186 | { 187 | }, 188 | 189 | // Called when a Scene is destroyed by the Scene Manager. There is no coming back from a destroyed Scene, so clear up all resources here. 190 | destroy: function () 191 | { 192 | this.shutdown(); 193 | 194 | this.scene = undefined; 195 | } 196 | 197 | }; 198 | 199 | BasePlugin.prototype.constructor = BasePlugin; 200 | 201 | // Make sure you export the plugin for webpack to expose 202 | 203 | module.exports = BasePlugin; 204 | 205 | 206 | /***/ }) 207 | /******/ ]); 208 | }); -------------------------------------------------------------------------------- /dist/BasePlugin.min.js: -------------------------------------------------------------------------------- 1 | !function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("BasePlugin",[],e):"object"==typeof exports?exports.BasePlugin=e():t.BasePlugin=e()}("undefined"!=typeof self?self:this,function(){return function(t){function e(n){if(s[n])return s[n].exports;var o=s[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var s={};return e.m=t,e.c=s,e.d=function(t,s,n){e.o(t,s)||Object.defineProperty(t,s,{configurable:!1,enumerable:!0,get:n})},e.n=function(t){var s=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(s,"a",s),s},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=0)}([function(t,e){/** 2 | * @author Richard Davey 3 | * @copyright 2018 Photon Storm Ltd. 4 | * @license {@link https://github.com/photonstorm/phaser3-plugin-template/blob/master/LICENSE|MIT License} 5 | */ 6 | var s=function(t){this.scene=t,this.systems=t.sys,t.sys.settings.isBooted||t.sys.events.once("boot",this.boot,this)};s.register=function(t){t.register("BasePlugin",s,"base")},s.prototype={boot:function(){var t=this.systems.events;t.on("start",this.start,this),t.on("preupdate",this.preUpdate,this),t.on("update",this.update,this),t.on("postupdate",this.postUpdate,this),t.on("pause",this.pause,this),t.on("resume",this.resume,this),t.on("sleep",this.sleep,this),t.on("wake",this.wake,this),t.on("shutdown",this.shutdown,this),t.on("destroy",this.destroy,this)},test:function(t){console.log("BasePlugin says hello "+t+"!")},start:function(){},preUpdate:function(t,e){},update:function(t,e){},postUpdate:function(t,e){},pause:function(){},resume:function(){},sleep:function(){},wake:function(){},shutdown:function(){},destroy:function(){this.shutdown(),this.scene=void 0}},s.prototype.constructor=s,t.exports=s}])}); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phaser3-plugin-template", 3 | "version": "1.0.0", 4 | "description": "Phaser 3 Plugin Template", 5 | "main": "src/main.js", 6 | "scripts": { 7 | "build": "webpack" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/photonstorm/phaser3-plugin-template.git" 12 | }, 13 | "author": "Richard Davey", 14 | "license": "MIT", 15 | "bugs": { 16 | "url": "https://github.com/photonstorm/phaser3-plugin-template/issues" 17 | }, 18 | "homepage": "https://github.com/photonstorm/phaser3-plugin-template#readme", 19 | "devDependencies": { 20 | "webpack": "^3.10.0" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @author Richard Davey 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 BasePlugin = function (scene) 8 | { 9 | // The Scene that owns this plugin 10 | this.scene = scene; 11 | 12 | this.systems = scene.sys; 13 | 14 | if (!scene.sys.settings.isBooted) 15 | { 16 | scene.sys.events.once('boot', this.boot, this); 17 | } 18 | }; 19 | 20 | // Static function called by the PluginFile Loader. 21 | BasePlugin.register = function (PluginManager) 22 | { 23 | // Register this plugin with the PluginManager, so it can be added to Scenes. 24 | 25 | // The first argument is the name this plugin will be known as in the PluginManager. It should not conflict with already registered plugins. 26 | // The second argument is a reference to the plugin object, which will be instantiated by the PluginManager when the Scene boots. 27 | // The third argument is the local mapping. This will make the plugin available under `this.sys.base` and also `this.base` from a Scene if 28 | // it has an entry in the InjectionMap. 29 | PluginManager.register('BasePlugin', BasePlugin, 'base'); 30 | }; 31 | 32 | BasePlugin.prototype = { 33 | 34 | // Called when the Plugin is booted by the PluginManager. 35 | // If you need to reference other systems in the Scene (like the Loader or DisplayList) then set-up those references now, not in the constructor. 36 | boot: function () 37 | { 38 | var eventEmitter = this.systems.events; 39 | 40 | // Listening to the following events is entirely optional, although we would recommend cleanly shutting down and destroying at least. 41 | // If you don't need any of these events then remove the listeners and the relevant methods too. 42 | 43 | eventEmitter.on('start', this.start, this); 44 | 45 | eventEmitter.on('preupdate', this.preUpdate, this); 46 | eventEmitter.on('update', this.update, this); 47 | eventEmitter.on('postupdate', this.postUpdate, this); 48 | 49 | eventEmitter.on('pause', this.pause, this); 50 | eventEmitter.on('resume', this.resume, this); 51 | 52 | eventEmitter.on('sleep', this.sleep, this); 53 | eventEmitter.on('wake', this.wake, this); 54 | 55 | eventEmitter.on('shutdown', this.shutdown, this); 56 | eventEmitter.on('destroy', this.destroy, this); 57 | }, 58 | 59 | // A test method. 60 | test: function (name) 61 | { 62 | console.log('BasePlugin says hello ' + name + '!'); 63 | }, 64 | 65 | // Called when a Scene is started by the SceneManager. The Scene is now active, visible and running. 66 | start: function () 67 | { 68 | }, 69 | 70 | // Called every Scene step - phase 1 71 | preUpdate: function (time, delta) 72 | { 73 | }, 74 | 75 | // Called every Scene step - phase 2 76 | update: function (time, delta) 77 | { 78 | }, 79 | 80 | // Called every Scene step - phase 3 81 | postUpdate: function (time, delta) 82 | { 83 | }, 84 | 85 | // Called when a Scene is paused. A paused scene doesn't have its Step run, but still renders. 86 | pause: function () 87 | { 88 | }, 89 | 90 | // Called when a Scene is resumed from a paused state. 91 | resume: function () 92 | { 93 | }, 94 | 95 | // Called when a Scene is put to sleep. A sleeping scene doesn't update or render, but isn't destroyed or shutdown. preUpdate events still fire. 96 | sleep: function () 97 | { 98 | }, 99 | 100 | // Called when a Scene is woken from a sleeping state. 101 | wake: function () 102 | { 103 | }, 104 | 105 | // Called when a Scene shuts down, it may then come back again later (which will invoke the 'start' event) but should be considered dormant. 106 | shutdown: function () 107 | { 108 | }, 109 | 110 | // Called when a Scene is destroyed by the Scene Manager. There is no coming back from a destroyed Scene, so clear up all resources here. 111 | destroy: function () 112 | { 113 | this.shutdown(); 114 | 115 | this.scene = undefined; 116 | } 117 | 118 | }; 119 | 120 | BasePlugin.prototype.constructor = BasePlugin; 121 | 122 | // Make sure you export the plugin for webpack to expose 123 | 124 | module.exports = BasePlugin; 125 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const webpack = require('webpack'); 4 | const path = require('path'); 5 | const UglifyJSPlugin = require('uglifyjs-webpack-plugin'); 6 | 7 | module.exports = { 8 | 9 | context: `${__dirname}/src/`, 10 | 11 | entry: { 12 | BasePlugin: './main.js', 13 | 'BasePlugin.min': './main.js' 14 | }, 15 | 16 | output: { 17 | path: `${__dirname}/dist/`, 18 | filename: '[name].js', 19 | library: 'BasePlugin', 20 | libraryTarget: 'umd', 21 | umdNamedDefine: true 22 | }, 23 | 24 | plugins: [ 25 | 26 | new UglifyJSPlugin({ 27 | include: /\.min\.js$/, 28 | parallel: true, 29 | sourceMap: false, 30 | uglifyOptions: { 31 | compress: true, 32 | ie8: false, 33 | ecma: 5, 34 | output: { 35 | comments: false 36 | }, 37 | warnings: false 38 | }, 39 | warningsFilter: (src) => false 40 | }) 41 | 42 | ] 43 | 44 | }; 45 | --------------------------------------------------------------------------------