Skip to content

Commit 2caa788

Browse files
committed
Initial commit
1 parent 1fe9bbe commit 2caa788

File tree

4 files changed

+266
-2
lines changed

4 files changed

+266
-2
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules/

README.md

+107-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,107 @@
1-
# phaser-asset-loader
2-
Asset loader for Phaser + Webpack.
1+
<p align="center">
2+
<img src="https://s3.amazonaws.com/howler.js/phaser-webpack-loader.png" alt="Phaser Webpack Loader">
3+
</p>
4+
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.
7+
8+
View sample usage in this [Phaser ES6 Boilerplate](https://github.com/goldfire/phaser-boilerplate).
9+
10+
## Features
11+
12+
* Load all game assets in parallel.
13+
* Load images, spritesheets, atlases, audio, bitmap fonts and web fonts.
14+
* Integrated with Webpack for automatic cache-busting.
15+
* Supports all filetypes.
16+
17+
## Install
18+
19+
Install the plugin through NPM (or Yarn):
20+
21+
```
22+
npm install phaser-webpack-loader --save
23+
```
24+
25+
Then create your manifest file and add the plugin as outlined below.
26+
27+
## Manifest File (AssetManifest.js)
28+
29+
```javascript
30+
const AssetManifest = {
31+
baseDir: 'src/assets/',
32+
images: [
33+
'image001.png',
34+
'image002.jpg',
35+
'...',
36+
],
37+
spritesheets: [
38+
'sprite001.png',
39+
'sprite002.png',
40+
'...',
41+
],
42+
audio: [
43+
'audio001.webm',
44+
'audio002.mp3',
45+
'...',
46+
],
47+
bitmapFonts: [
48+
'font001.png',
49+
'font002.png',
50+
'...',
51+
],
52+
fonts: {
53+
google: {
54+
families: [
55+
'Open Sans:300,700',
56+
],
57+
},
58+
},
59+
};
60+
61+
export default AssetManifest;
62+
```
63+
64+
## Running Plugin (Preload.js)
65+
66+
In your preload state, add the plugin. It uses promises, which makes it flexible to move to the next step when ready.
67+
68+
```javascript
69+
import WebpackLoader from 'phaser-webpack-loader';
70+
import AssetManifest from '../AssetManifest';
71+
72+
export default class Preload extends Phaser.State {
73+
create() {
74+
this.game.plugins.add(WebpackLoader, AssetManifest)
75+
.load()
76+
.then(() => {
77+
this.game.state.start('Main');
78+
});
79+
}
80+
}
81+
```
82+
83+
## Loading Fonts
84+
85+
The font loader uses [Web Font Loader](https://github.com/typekit/webfontloader), which supports loading web fonts from all major providers. Simply provide their standard configuration object in your manifest.
86+
87+
## Loading Sprites/Atlases
88+
89+
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.
90+
91+
## Directory Structure
92+
93+
You can specify whatever base directory you would like in the manifest file, but then each asset type should go as follows:
94+
95+
```
96+
assets/
97+
├── images/
98+
├── sprites/
99+
├── audio/
100+
└── fonts/
101+
```
102+
103+
### License
104+
105+
Copyright (c) 2017 [James Simpson](https://twitter.com/GoldFireStudios) and [GoldFire Studios, Inc.](http://goldfirestudios.com)
106+
107+
Released under the [MIT License](https://github.com/goldfire/phaser-webpack-loader/blob/master/LICENSE.md).

package.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "phaser-webpack-loader",
3+
"version": "0.1.0",
4+
"author": "GoldFire Studios, Inc. (http://goldfirestudios.com)",
5+
"description": "Asset loader for Phaser + Webpack.",
6+
"author": "James Simpson <james@goldfirestudios.com> (http://goldfirestudios.com)",
7+
"repository": {
8+
"type": "git",
9+
"url": "git://github.com/goldfire/phaser-webpack-loader.git"
10+
},
11+
"main": "src/index.js",
12+
"scripts": {
13+
"release": "VERSION=`printf 'v' && node -e 'console.log(require(\"./package.json\").version)'` && git tag $VERSION && git push && git push origin $VERSION && npm publish"
14+
},
15+
"dependencies": {
16+
"webfontloader": "^1.6.27"
17+
},
18+
"engines": {
19+
"node": ">=4.0.0"
20+
},
21+
"keywords": [
22+
"phaser",
23+
"assets",
24+
"loader",
25+
"webpack",
26+
"asset loader",
27+
"manifest"
28+
]
29+
}

src/index.js

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)