Skip to content

Commit

Permalink
Add Phaser and setup basic game state management with components
Browse files Browse the repository at this point in the history
  • Loading branch information
nevantan committed Jan 14, 2018
1 parent 7427a5f commit dbeae88
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 23 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets":["env", "react"],
"plugins":[
"transform-class-properties"
]
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
"dependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"expose-loader": "^0.7.4",
"phaser-ce": "^2.9.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"webpack": "^3.10.0"
Expand Down
8 changes: 6 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import PIXI from 'expose-loader?PIXI!phaser-ce/build/custom/pixi.js';
import p2 from 'expose-loader?p2!phaser-ce/build/custom/p2.js';
import Phaser from 'expose-loader?Phaser!phaser-ce/build/custom/phaser-split.js';

import React from 'react';
import ReactDOM from 'react-dom';

const jsx = <p>Hello world!</p>;
import Game from './components/Game';

ReactDOM.render(jsx, document.getElementById('app'));
ReactDOM.render(<Game />, document.getElementById('app'));
42 changes: 42 additions & 0 deletions src/components/Game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import React from 'react';

import Boot from './states/Boot';
import Play, { PlayState } from './states/Play';

export default class Game extends React.Component {
state = {
gameState: null
};

componentWillMount = () => {
this.game = new Phaser.Game(800, 600, Phaser.AUTO, 'game');
this.game.state.add('play', PlayState);

this.switchState('boot');
};

switchState = (state) => {
switch (state) {
case 'boot':
this.setState(() => ({
gameState: <Boot game={this.game} switchState={this.switchState} />
}));
break;
case 'play':
this.setState(() => ({
gameState: <Play game={this.game} switchState={this.switchState} />
}));
break;
}
};

render() {
return (
<div>
<div id="game" />
{this.state.gameState}
<p>Game Component</p>
</div>
);
}
}
22 changes: 22 additions & 0 deletions src/components/states/Boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';

class BootState {
constructor(props) {
this.props = props;
}

create() {
console.log('Boot state started!');
this.props.switchState('play');
}
}

export default class Boot extends React.Component {
componentWillMount() {
this.props.game.state.add('boot', new BootState(this.props), true);
}

render() {
return <p>Boot State</p>;
}
}
21 changes: 21 additions & 0 deletions src/components/states/Play.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';

class PlayState {
constructor(props) {
this.props = props;
}

create() {
console.log('Play state started!');
}
}

export default class Play extends React.Component {
componentWillMount() {
this.props.game.state.add('play', new PlayState(this.props), true);
}

render() {
return <p>Play State</p>;
}
}
39 changes: 18 additions & 21 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
const path = require('path');

module.exports = {
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'public', 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { presets: ['env', 'react'] }
}
}
]
},
devServer: {
contentBase: path.join(__dirname, 'public'),
publicPath: '/dist/'
}
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'public', 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
}
]
},
devServer: {
contentBase: path.join(__dirname, 'public'),
publicPath: '/dist/'
}
};
21 changes: 21 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,10 @@ babel-plugin-syntax-async-functions@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-async-functions/-/babel-plugin-syntax-async-functions-6.13.0.tgz#cad9cad1191b5ad634bf30ae0872391e0647be95"

babel-plugin-syntax-class-properties@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-class-properties/-/babel-plugin-syntax-class-properties-6.13.0.tgz#d7eb23b79a317f8543962c505b827c7d6cac27de"

babel-plugin-syntax-exponentiation-operator@^6.8.0:
version "6.13.0"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-exponentiation-operator/-/babel-plugin-syntax-exponentiation-operator-6.13.0.tgz#9ee7e8337290da95288201a6a57f4170317830de"
Expand Down Expand Up @@ -504,6 +508,15 @@ babel-plugin-transform-async-to-generator@^6.22.0:
babel-plugin-syntax-async-functions "^6.8.0"
babel-runtime "^6.22.0"

babel-plugin-transform-class-properties@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-class-properties/-/babel-plugin-transform-class-properties-6.24.1.tgz#6a79763ea61d33d36f37b611aa9def81a81b46ac"
dependencies:
babel-helper-function-name "^6.24.1"
babel-plugin-syntax-class-properties "^6.8.0"
babel-runtime "^6.22.0"
babel-template "^6.24.1"

babel-plugin-transform-es2015-arrow-functions@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-es2015-arrow-functions/-/babel-plugin-transform-es2015-arrow-functions-6.22.0.tgz#452692cb711d5f79dc7f85e440ce41b9f244d221"
Expand Down Expand Up @@ -1976,6 +1989,10 @@ expect@^22.0.6:
jest-message-util "^22.0.6"
jest-regex-util "^22.0.6"

expose-loader@^0.7.4:
version "0.7.4"
resolved "https://registry.yarnpkg.com/expose-loader/-/expose-loader-0.7.4.tgz#9bcdd3878b5da9107930b55a03f65afe90b3314a"

express@^4.16.2:
version "4.16.2"
resolved "https://registry.yarnpkg.com/express/-/express-4.16.2.tgz#e35c6dfe2d64b7dca0a5cd4f21781be3299e076c"
Expand Down Expand Up @@ -4116,6 +4133,10 @@ performance-now@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"

phaser-ce@^2.9.4:
version "2.9.4"
resolved "https://registry.yarnpkg.com/phaser-ce/-/phaser-ce-2.9.4.tgz#06cf9f85b7dcd20a274237846b2b7c8461ddc1c4"

pify@^2.0.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
Expand Down

0 comments on commit dbeae88

Please sign in to comment.