-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Phaser and setup basic game state management with components
- Loading branch information
Showing
8 changed files
with
139 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"presets":["env", "react"], | ||
"plugins":[ | ||
"transform-class-properties" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/' | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters