Slide 1

Slide 1 text

REACT AND GAMES An experiment about making games

Slide 2

Slide 2 text

@BOBYLITO

Slide 3

Slide 3 text

+ = ?

Slide 4

Slide 4 text

LITTLE SHOOTER V2

Slide 5

Slide 5 text

1. (re)Introduction to React 2. Game dev patterns with React 3. Demo 4. Next

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

RENDERING 1 React.render(
Hello World
, document.body );

Slide 8

Slide 8 text

CUSTOM COMPONENTS 1 var MyComponent = React.createClass({ 2 render: function(){ 3 return
4 } 5 }); 6 React.render( , document.body );

Slide 9

Slide 9 text

COMPONENT STATE 1 var MyComponent = React.createClass({ 2 //state: {} 3 getInitialState: function(){ return {foo:"bar"} }, 4 render: function(){ 5 var valueFromState = this.state.foo; 6 return
7 } 8 //setState: function( newState ){ /*set the new state*/ } 9 });

Slide 10

Slide 10 text

LIFECYCLE AND HOOKS 1 var MyComponent = React.createClass({ 2 render: function(){ 3 return
4 }, 5 componentWillMount: function(){}, 6 componentWillReceiveProps: function( props ){ /* new values handling, modify state ;) */} 7 });

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

T-0 T-1 Diff T-0 T-1

Slide 13

Slide 13 text

LET’S MAKE A GAME!

Slide 14

Slide 14 text

GAME LOOP

Slide 15

Slide 15 text

GAME LOOP

Slide 16

Slide 16 text

GAME LOOP Repeat

Slide 17

Slide 17 text

GAME LOOP Repeat Render

Slide 18

Slide 18 text

GAME LOOP Repeat Render Update game state

Slide 19

Slide 19 text

GAME LOOP Repeat Render Update game state Get input

Slide 20

Slide 20 text

1 var GameApp = React.createClass({ 2 getInitialState: function(){ 3 return { 4 input : { 5 time : (Date.now()) } }; 6 }, 7 render : function(){ 8 return 9 }, 10 tick : function(){ 11 var t = Date.now(); 12 requestAnimationFrame(this.tick); 13 this.setState({ 14 input:{ 15 time : t 16 } 17 }); 18 }, 19 componentWillMount : function(){ 20 requestAnimationFrame( this.tick ); 21 }, 22 }); 23 24 var output = d.getElementById("main"); 25 React.renderComponent( , output);

Slide 21

Slide 21 text

INPUTS • Time • Keys

Slide 22

Slide 22 text

TIME • Date.now() at the tick • Store the time in the state • Propagate to sub components • Will trigger the rendering even if nothing else happen from the real world

Slide 23

Slide 23 text

KEYS • Bind event listeners to the main component • Properly handle auto fire

Slide 24

Slide 24 text

1 ... 2 render : function(){ 3 return
; 6 }, 7 keyHandler : function(valueToSet, e){ 8 var newKeys = { 9 left : this.state.input.keys.left, 10 right : this.state.input.keys.right, 11 up : this.state.input.keys.up, 12 down : this.state.input.keys.down, 13 space : this.state.input.keys.space 14 }; 15 16 if(e.keyCode === 37) newKeys.left = valueToSet; 17 if(e.keyCode === 38) newKeys.up = valueToSet; 18 if(e.keyCode === 39) newKeys.right = valueToSet; 19 if(e.keyCode === 40) newKeys.down = valueToSet; 20 if(e.keyCode === 32) newKeys.space = valueToSet; 21 22 this.setState({ 23 input:{ 24 time : this.state.input.time, 25 keys : newKeys } }); 26 } 27 ...

Slide 25

Slide 25 text

GAME SCREENS

Slide 26

Slide 26 text

A B C div div div React.render( )
A B C div div div

Slide 27

Slide 27 text

A B C div div div React.render( )
A B C div div div

Slide 28

Slide 28 text

A B C div div div React.render( )
A B C div div div

Slide 29

Slide 29 text

A B C div div div React.render( )
A B C div div div

Slide 30

Slide 30 text

A B C div div div React.render( )
A B C div div div

Slide 31

Slide 31 text

A B C div div div React.render( )
A B C div div div

Slide 32

Slide 32 text

A B C div div div React.render( )
A B C div div div

Slide 33

Slide 33 text

A B C div div div React.render( )
A B C div div div

Slide 34

Slide 34 text

A B C div div div React.render( )
A B C div div div

Slide 35

Slide 35 text

A B C div div div React.render( )
A B C div div div

Slide 36

Slide 36 text

A B C div div div

Slide 37

Slide 37 text

A B C div div div

Slide 38

Slide 38 text

GameApp Game Intro Score Horde Ouno HUD Ship Rocket Launcher Rocket Particle System

Slide 39

Slide 39 text

GameApp Game Intro Score Horde Ouno HUD Ship Rocket Launcher Rocket Particle System Router

Slide 40

Slide 40 text

GameApp Game Intro Score Horde Ouno HUD Ship Rocket Launcher Rocket Particle System Screens Router

Slide 41

Slide 41 text

GameApp Game Intro Score Horde Ouno HUD Ship Rocket Launcher Rocket Particle System Screens Other Router

Slide 42

Slide 42 text

THE WORLD

Slide 43

Slide 43 text

MODEL • One centralized model for the game • Old school prototypal objects • One function to update the world

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

ONE WAY

Slide 46

Slide 46 text

ONE WAY MailBox

Slide 47

Slide 47 text

ONE WAY MailBox

Slide 48

Slide 48 text

ONE WAY MailBox

Slide 49

Slide 49 text

ONE WAY MailBox

Slide 50

Slide 50 text

COMMUNICATION • Every frame there is an update • Simple message box system • Every component can send events • Each tick, the model is updated with the events • Message box is emptied once read

Slide 51

Slide 51 text

World App state Special fx

Slide 52

Slide 52 text

DEMO

Slide 53

Slide 53 text

FUTURE • Mixins • Game loop • Game screen • Sprites • Ludum dare

Slide 54

Slide 54 text

QUESTIONS? https://github.com/bobylito/littleshooter2/

Slide 55

Slide 55 text

REFERENCES • React : http://facebook.github.io/react/ • Other related frameworks based on React for doing similar stuff : • Pixi / react : https://github.com/Izzimach/react- pixi • React art : https://github.com/reactjs/react-art