react-game-kit

Component library for making games with React & React Native

README

Maintenance Status

react-game-kit


Make games with React & React Native!


Install


npm install react-game-kit --save

Get Started


react-game-kitprovides a set of helper components to make it easier to create games with React and React Native.

You'll want to begin by importing the components you need:

  1. ``` js
  2. import { Loop, Stage } from 'react-game-kit';
  3. ```

Loop & Stage


Next, in your render method of your top level component, you'll want to put the Loopcomponent at the top level, optionally followed by the Stagecomponent:

  1. ``` js
  2. render() {
  3.   return (
  4.     <Loop>
  5.       <Stage>
  6.         // Game specific components go here
  7.       </Stage>
  8.     </Loop>
  9.   );
  10. }
  11. ```

The Loopcomponent uses contextto pass a subscribable game tick down your component tree. The Stagecomponent does the same with game scale.

World


If you intend on using physics in your game, a good next component would be the Worldcomponent, which creates and provides a physics engine & world:

  1. ``` js
  2. render() {
  3.   return (
  4.     <Loop>
  5.       <Stage>
  6.         <World>
  7.           // Game specific components go here
  8.         </World>
  9.       </Stage>
  10.     </Loop>
  11.   );
  12. }
  13. ```

Physics Bodies


Once you have a physics engine/world established, you can use the Bodycomponent to define physics bodies inline:

  1. ``` js
  2. render() {
  3.   return (
  4.     <Loop>
  5.       <Stage>
  6.         <World>
  7.           <Body args={[0,0,75,75]} ref={ (b) => this.body = b.body }>
  8.             // Sprites go here
  9.           </Body>
  10.         </World>
  11.       </Stage>
  12.     </Loop>
  13.   );
  14. }
  15. ```

Using a ref you can obtain a reference to the physics body and modify its properties via the Matter-js API .

Next Steps


Once this general structure is established, what follows usually depends on what kind of game you intend to make. Check out the API documentation below for further clarity regarding use of these components.

React Native


Using this library with React Native is a simple as importing from the native directory:

  1. ``` js
  2. import { Loop, Stage, ...etc } from 'react-game-kit/native';
  3. ```

Note: AudioPlayer and KeyListener are not implemented on the React Native version.


API


####

The Loopcomponent acts much like a Redux provider, in that it passes a GameLoop instance down the component tree via this.context.loop.

This allows you to subscribe and unsubscribe to the main game loop anywhere in your component tree. Here is an example of how this would generally look:

  1. ``` js
  2. class ChildComponent extends React.Component {
  3.   static contextTypes = {
  4.     loop: PropTypes.object,
  5.   };

  6.   componentDidMount() {
  7.     this.context.loop.subscribe(this.update);
  8.   }

  9.   componentWillUnmount() {
  10.     this.context.loop.unsubscribe(this.update);
  11.   }

  12.   update() {
  13.     // tick logic
  14.   };
  15. }
  16. ```

--

####

height(number) : Base game height. Defaults to 576.

width(number) : Base game width. Defaults to 1024.

The Stagecomponent also leverages contextmuch like Loop, except it passes game scale as this.context.scale. You can use this value to appropriately scale positioning and dimension values within your game. Again, you would have to specify scale: PropTypes.numberin your component's contextTypesto receive this value.

--

####

gravity(object) : World gravity object.

Defaults:

  1. ``` js
  2. gravity={{
  3.   x: 0,
  4.   y: 1,
  5.   scale: 0.001,
  6. }}
  7. ```

onCollision(func) : World collision callback.

onInit(func) : World init callback.

onUpdate(func) : World update callback.

The Worldcomponent is used as the first step in setting up game physics. It passes a matter-jsEngine instance down via context as this.context.engine. Generally speaking, when getting or settings physics properties you'll want to do this after the physics world is updated in the main tick cycle. You can hook into this using the onUpdateprop, or in child components use Matter.Events.on(this.context.engine, 'afterUpdate', this.update);to subscribe to the engine updates.

The onInitcallback is a great place to do your initial world setup, things like creating static bodies for walls and the floor.

--

####

args(array) : Initial body creation arguments. Depends on the shapeprop, which maps to Matter.Bodies body creation methods detailed here: Matter.Bodies Documentation

All other props on the body component map directly to Matter-js Body properties .

The Bodycomponent is used to define physics bodies. You will generally want to use refto obtain a reference to the body, at which point you can call Matter-js methods on it, as well as listen to and react to its physic properties in the world update callback.

--

####

offset(array) : Sprite sheet x,y offset.

onPlayStateChanged(func) : Sprite play state changed callback.

repeat(bool) : Determines whether sprite animation should loop.

scale(number) : Scale value for sprite image.

src(string) : src path for sprite sheet.

state(number) : Vertical position in sprite sheet.

steps(array) : Number of animation steps for current row (state).

ticksPerFrame(number) : Number of loop ticks per animation frame.

tileHeight(number) : Height of spritesheet tile.

tileWidth(number) : Width of spritesheet tile.

The Spritecomponent lets you define sprite animations using sprite sheets. When creating a sprite sheet, define sprite tile dimensions that will be provided via the tileHeight& tileWidthprops. Next, each animation state is represented by a row, with steps of the animation represented as columns.

--

####

columns(number) : number of columns in tile map.

layers(array) : Array of arrays that contain tile indexes.

renderTile(func) : Overrideable tile rendering function.

rows(number) : Number of rows in tile map.

scale(number) : Tile map scale.

src(string) : Tilemap image src path.

tileSize(number) : Tilemap tile size.

width(number) : Tilemap width.

height(number) : Tilemap height.

The TileMapcomponent lets you define tile maps from a tile atlas. Your tilemap is made of up rows and columns. Each layer is then drawn using those numbers as reference. So for example, if you had 4 rows and 4 columns, with 1 layer, your layersprop would look like:

  1. ``` js
  2. layers={[
  3.   [
  4.     0, 0, 0, 0,
  5.     1, 0, 1, 1,
  6.     0, 0, 1, 0,
  7.     1, 0, 0, 0,
  8.   ]
  9. ]}
  10. ```

--

License


MIT License

Maintenance Status


Archived:This project is no longer maintained by Formidable. We are no longer responding to issues or pull requests unless they relate to security concerns. We encourage interested developers to fork this project and make it their own!