Skip to content

Commit cc61ca6

Browse files
committed
v2.0.0 - Support Phaser 3
1 parent d3c9683 commit cc61ca6

File tree

3 files changed

+52
-97
lines changed

3 files changed

+52
-97
lines changed

README.md

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<p align="center">
2-
<img src="https://s3.amazonaws.com/howler.js/phaser-webpack-loader.png" alt="Phaser Webpack Loader">
2+
<img src="https://s3.amazonaws.com/howler.js/phaser-webpack-loader2.png" alt="Phaser Webpack Loader">
33
</p>
44

5-
# Phaser Webpack Loader
6-
Instead of manually calling `game.load.image`, `game.load.audio`, etc for every asset in your game (and then dealing with caching issues), phaser-webpack-loader lets you define all assets in a simple manifest file and handles the rest for you.
5+
# Phaser 3 Webpack Loader
6+
Instead of manually calling `scene.load.image`, `scene.load.audio`, etc for every asset in your game (and then dealing with caching issues), phaser-webpack-loader lets you define all assets in a simple manifest file and handles the rest for you.
77

8-
View sample usage in this [Phaser ES6 Boilerplate](https://github.com/goldfire/phaser-boilerplate).
8+
**NOTE:** This plugin now only supports Phaser 3 and later. If you need support for Phaser 2, [use v1.1.0](https://github.com/goldfire/phaser-webpack-loader/tree/d3c9683e6a5ba9541ec0b0cd11c89fb824ff45b6).
99

1010
## Features
1111

@@ -14,8 +14,7 @@ View sample usage in this [Phaser ES6 Boilerplate](https://github.com/goldfire/p
1414
* Integrated with Webpack for automatic cache-busting.
1515
* Supports all filetypes.
1616
* Supports asset postfix for retina support ('@2x', '@3x', etc).
17-
* Supports automatic loading of compressed textures (PVRTC, S3TC, ETC1).
18-
* Optional callback to track each file load (including fonts).
17+
* Custom event to track each file load (including fonts).
1918

2019
## Install
2120

@@ -71,37 +70,32 @@ In your preload state, add the plugin. It uses promises, which makes it flexible
7170
import WebpackLoader from 'phaser-webpack-loader';
7271
import AssetManifest from '../AssetManifest';
7372

74-
export default class Preload extends Phaser.State {
73+
export default class Preload extends Phaser.Scene {
74+
preload() {
75+
this.load.scenePlugin('WebpackLoader', WebpackLoader, 'loader', 'loader');
76+
}
77+
7578
create() {
76-
this.game.plugins.add(WebpackLoader, AssetManifest)
77-
.load()
78-
.then(() => {
79-
this.game.state.start('Main');
80-
});
79+
this.loader.start(AssetManifest);
80+
this.loader.load().then(() => {
81+
// Done loading!
82+
});
8183
}
8284
}
8385
```
8486

8587
If you want to load high DPI assets, you can pass an optional postfix string:
8688

8789
```javascript
88-
this.game.plugins.add(WebpackLoader, AssetManifest, '@2x')
89-
.load()
90-
.then(() => {
91-
this.game.state.start('Main');
92-
});
90+
this.loader.start(AssetManifest, '@2x');
9391
```
9492

9593
If you want to know when each file is loaded, use the optional callback:
9694

9795
```javascript
98-
this.game.plugins.add(WebpackLoader, AssetManifest, '@2x', () => {
99-
console.log('File loaded!');
100-
})
101-
.load()
102-
.then(() => {
103-
this.game.state.start('Main');
104-
});
96+
this.loader.systems.events.on('load', (file) => {
97+
console.log('File loaded!', file);
98+
});
10599
```
106100

107101
## Loading Fonts
@@ -112,16 +106,6 @@ The font loader uses [Web Font Loader](https://github.com/typekit/webfontloader)
112106

113107
All sprite/atlas files are loaded as JSON hashes (which can be output using [TexturePacker](https://www.codeandweb.com/texturepacker), [Shoebox](http://renderhjs.net/shoebox/) and others). All you have to specify in the manifest is the image filename, but you'll also need to include the JSON hash file alongside it, which will automatically get loaded and used.
114108

115-
## Loading Compressed Textures (Phaser-CE 2.7.7+)
116-
117-
[Compressed textures](https://phaser.io/tutorials/advanced-rendering-tutorial/part6) can be great to lower memory usage, especially on mobile devices; however, it isn't necessarily straight-forward how to load these (especially for atlas files). This loader makes it as simple as putting the files in your directory, so long as they match the naming conventions:
118-
119-
#### PVRTC `texture.pvrtc.pvr` & `texture.pvrtc.json`
120-
#### S3TC `texture.s3tc.pvr` & `texture.s3tc.json`
121-
#### ETC1 `texture.etc1.pkm` & `texture.etc1.json`
122-
123-
Simply include any or all of these files alongside the `PNG` equivalent and if one of them is compatible with the current browser/hardware it will be loaded and used (falling back to the `PNG` file).
124-
125109
## Directory Structure
126110

127111
Specify the base directory in your Webpack config:

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "phaser-webpack-loader",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"author": "GoldFire Studios, Inc. (http://goldfirestudios.com)",
55
"description": "Asset loader for Phaser + Webpack.",
66
"author": "James Simpson <[email protected]> (http://goldfirestudios.com)",

src/index.js

Lines changed: 33 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
1-
import Phaser from 'phaser';
21
import WebFont from 'webfontloader';
32

43
/**
54
* Phaser Webpack Loader plugin for Phaser.
65
*/
7-
export default class WebpackLoader extends Phaser.Plugin {
6+
export default class WebpackLoader extends Phaser.Plugins.ScenePlugin {
87
/**
9-
* Initialize the load plugin.
8+
* Setup the plugin.
9+
* @param {Object} scene Reference to scene owner.
10+
* @param {Object} pluginManager Scene's plugin manager.
11+
*/
12+
constructor(scene, pluginManager) {
13+
super(scene, pluginManager);
14+
}
15+
16+
/**
17+
* Start the loader plugin.
1018
* @param {Object} manifest Your asset manifest file contents.
1119
* @param {String} postfix (optional) Postfix to append to assets.
12-
* @param {Function} assetLoadCallback Callback to fire when each file has loaded.
1320
*/
14-
init(manifest, postfix = '', assetLoadCallback) {
15-
// Define the asset load callback.
16-
this._assetLoadCallback = assetLoadCallback || (() => {});
17-
21+
start(manifest, postfix = '') {
1822
// Pull the font values out of the manifest.
1923
this.fonts = manifest.fonts || {};
2024

@@ -46,7 +50,15 @@ export default class WebpackLoader extends Phaser.Plugin {
4650
return Promise.all([
4751
this._loadAssets(),
4852
this._loadFonts(),
49-
]).then(this._addSprites.bind(this));
53+
]);
54+
}
55+
56+
/**
57+
* Emit load event for each file that loads.
58+
* @param {Object} file Reference to file that has loaded.
59+
*/
60+
_emitLoad(file) {
61+
this.systems.events.emit('load', file);
5062
}
5163

5264
/**
@@ -64,17 +76,17 @@ export default class WebpackLoader extends Phaser.Plugin {
6476
});
6577
});
6678

67-
// Setup listener for each asset loading.
68-
this.game.load.onFileComplete.add(this._assetLoadCallback);
79+
// Emit load event on each file.
80+
this.scene.load.on('load', this._emitLoad, this);
6981

7082
// Once everything has loaded, resolve the promise.
71-
this.game.load.onLoadComplete.addOnce(() => {
72-
this.game.load.onFileComplete.remove(this._assetLoadCallback);
83+
this.scene.load.once('complete', () => {
84+
this.scene.load.off('load', this._emitLoad);
7385
resolve();
7486
});
7587

7688
// Start the loading of the assets.
77-
this.game.load.start();
89+
this.scene.load.start();
7890
});
7991
}
8092

@@ -90,35 +102,13 @@ export default class WebpackLoader extends Phaser.Plugin {
90102
return new Promise((resolve) => {
91103
WebFont.load(Object.assign({}, this.fonts, {
92104
active: () => {
93-
this._assetLoadCallback();
105+
this.systems.events.emit('load');
94106
resolve();
95107
},
96108
}));
97109
});
98110
}
99111

100-
/**
101-
* Create the texture atlases once the image data has loaded.
102-
* This is required in order to load compressed textures as an atlas.
103-
*/
104-
_addSprites() {
105-
this.assets.sprites.forEach((asset) => {
106-
// Get the image/data for the sprite atlas.
107-
const dir = 'sprites/';
108-
const name = asset.split('.')[0];
109-
const image = this.game.cache.getItem(name, Phaser.Cache.IMAGE);
110-
111-
if (image) {
112-
const compression = image.data.compressionAlgorithm;
113-
const format = compression ? `.${compression.toLowerCase()}` : '';
114-
const data = require(`assets/${dir}${name}${this.postfix}${format}.json`);
115-
116-
// Add the sprite atlas to the cache.
117-
this.game.cache.addTextureAtlas(name, null, image.data, data, Phaser.Loader.TEXTURE_ATLAS_JSON_HASH);
118-
}
119-
});
120-
}
121-
122112
/**
123113
* Load an image.
124114
* @param {String} name Name of the file.
@@ -127,7 +117,7 @@ export default class WebpackLoader extends Phaser.Plugin {
127117
_loadImage(name, ext) {
128118
const dir = 'images/';
129119
const file = require(`assets/${dir}${name}${this.postfix}.${ext}`);
130-
this.game.load.image(name, file);
120+
this.scene.load.image(name, file);
131121
}
132122

133123
/**
@@ -137,28 +127,9 @@ export default class WebpackLoader extends Phaser.Plugin {
137127
*/
138128
_loadSprite(name, ext) {
139129
const dir = 'sprites/';
140-
141-
// Determine which formats are available.
142-
const formats = {};
143-
144-
try {
145-
formats.truecolor = require(`assets/${dir}${name}${this.postfix}.${ext}`);
146-
} catch (e) {}
147-
148-
try {
149-
formats.pvrtc = require(`assets/${dir}${name}${this.postfix}.pvrtc.pvr`);
150-
} catch (e) {}
151-
152-
try {
153-
formats.s3tc = require(`assets/${dir}${name}${this.postfix}.s3tc.pvr`);
154-
} catch (e) {}
155-
156-
try {
157-
formats.etc1 = require(`assets/${dir}${name}${this.postfix}.etc1.pkm`);
158-
} catch (e) {}
159-
160-
// Load the format that works on this platform.
161-
this.game.load.image(name, formats);
130+
const file = require(`assets/${dir}${name}${this.postfix}.${ext}`);
131+
const data = require(`assets/${dir}${name}${this.postfix}.json`);
132+
this.scene.load.atlas(name, file, data);
162133
}
163134

164135
/**
@@ -169,7 +140,7 @@ export default class WebpackLoader extends Phaser.Plugin {
169140
_loadAudio(name, ext) {
170141
const dir = 'audio/';
171142
const file = require(`assets/${dir}${name}.${ext}`);
172-
this.game.load.audio(name, file);
143+
this.scene.load.audio(name, file);
173144
}
174145

175146
/**
@@ -181,6 +152,6 @@ export default class WebpackLoader extends Phaser.Plugin {
181152
const dir = 'fonts/';
182153
const file = require(`assets/${dir}${name}${this.postfix}.${ext}`);
183154
const data = require(`assets/${dir}${name}${this.postfix}.xml`);
184-
this.game.load.bitmapFont(name, file, data);
155+
this.scene.load.bitmapFont(name, file, data);
185156
}
186157
}

0 commit comments

Comments
 (0)