|
| 1 | +import Phaser from 'phaser'; |
| 2 | +import WebFont from 'webfontloader'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Phaser Webpack Loader plugin for Phaser. |
| 6 | + */ |
| 7 | +export default class WebpackLoader extends Phaser.Plugin { |
| 8 | + /** |
| 9 | + * Initialize the load plugin. |
| 10 | + * @param {Object} manifest Your asset manifest file contents. |
| 11 | + * @param {String} postfix (optional) Postfix to append to assets. |
| 12 | + */ |
| 13 | + init(manifest, postfix = '') { |
| 14 | + // Pull the base directory out of the manifest. |
| 15 | + this.baseDir = manifest.baseDir || 'assets/'; |
| 16 | + |
| 17 | + // Pull the font values out of the manifest. |
| 18 | + this.fonts = manifest.fonts || {}; |
| 19 | + |
| 20 | + // Pull the asset values out of the manifest. |
| 21 | + this.assets = { |
| 22 | + images: manifest.images || [], |
| 23 | + sprites: manifest.sprites || [], |
| 24 | + audio: manifest.audio || [], |
| 25 | + bitmapFonts: manifest.bitmapFonts || [], |
| 26 | + }; |
| 27 | + |
| 28 | + // Define the loaders for the different asset types. |
| 29 | + this.loaders = { |
| 30 | + images: this._loadImage, |
| 31 | + sprites: this._loadSprite, |
| 32 | + audio: this._loadAudio, |
| 33 | + bitmapFonts: this._loadBitmapFont, |
| 34 | + }; |
| 35 | + |
| 36 | + // Define the postfix string to apply to image assets (ex: @2x). |
| 37 | + this.postfix = postfix; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Begins loading of all assets. |
| 42 | + * @return {Promise} Returns when loading is complete. |
| 43 | + */ |
| 44 | + load() { |
| 45 | + return Promise.all([ |
| 46 | + this._loadAssets(), |
| 47 | + this._loadFonts(), |
| 48 | + ]); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Load all assets in parallel. |
| 53 | + * @return {Promise} Returns when assets are loaded. |
| 54 | + */ |
| 55 | + _loadAssets() { |
| 56 | + return new Promise((resolve) => { |
| 57 | + // Loop through all of the asset types and begin loading. |
| 58 | + Object.keys(this.assets).forEach((key) => { |
| 59 | + // Loop through each asset of this type. |
| 60 | + this.assets[key].forEach((asset) => { |
| 61 | + const assetData = asset.split('.'); |
| 62 | + this.loaders[key](assetData[0], assetData[1]); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + // Once everything has loaded, resolve the promise. |
| 67 | + this.game.load.onLoadComplete.addOnce(() => { |
| 68 | + resolve(); |
| 69 | + }); |
| 70 | + |
| 71 | + // Start the loading of the assets. |
| 72 | + this.game.load.start(); |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Loads all defined web fonts using webfontloader. |
| 78 | + * @return {Promise} Returns when fonts are ready to use. |
| 79 | + */ |
| 80 | + _loadFonts() { |
| 81 | + return new Promise((resolve) => { |
| 82 | + WebFont.load(Object.assign({}, this.fonts, { |
| 83 | + active: resolve, |
| 84 | + })); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Load an image. |
| 90 | + * @param {String} name Name of the file. |
| 91 | + * @param {String} ext File extension. |
| 92 | + */ |
| 93 | + _loadImage(name, ext) { |
| 94 | + const file = require(`${this.baseDir}images/${name}${this.postfix}.${ext}`); |
| 95 | + this.game.load.image(name, file); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Load a spritesheet. |
| 100 | + * @param {String} name Name of the file. |
| 101 | + * @param {String} ext File extension. |
| 102 | + */ |
| 103 | + _loadSprite(name, ext) { |
| 104 | + const file = require(`${this.baseDir}sprites/${name}${this.postfix}.${ext}`); |
| 105 | + const data = require(`${this.baseDir}sprites/${name}${this.postfix}.json`); |
| 106 | + this.game.load.atlasJSONHash(name, file, null, data); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Load an audio file. |
| 111 | + * @param {String} name Name of the file. |
| 112 | + * @param {String} ext File extension. |
| 113 | + */ |
| 114 | + _loadAudio(name, ext) { |
| 115 | + const file = require(`${this.baseDir}audio/${name}.${ext}`); |
| 116 | + this.game.load.audio(name, file); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Load a bitmap font. |
| 121 | + * @param {String} name Name of the file. |
| 122 | + * @param {String} ext File extension. |
| 123 | + */ |
| 124 | + _loadBitmapFont(name, ext) { |
| 125 | + const file = require(`${this.baseDir}fonts/${name}${this.postfix}.${ext}`); |
| 126 | + const data = require(`${this.baseDir}fonts/${name}${this.postfix}.xml`); |
| 127 | + this.game.load.bitmapFont(name, file, data); |
| 128 | + } |
| 129 | +} |
0 commit comments