{"size": 1661, "ext": "js", "max_stars_repo_path": "src/components/Game.js", "max_stars_repo_name": "nervestaple/react-phaser-kit", "max_stars_repo_stars_event_min_datetime": "2018-04-11T06:15:11.000Z", "max_stars_repo_stars_event_max_datetime": "2019-03-14T07:32:32.000Z", "max_issues_repo_path": "src/components/Game.js", "max_issues_repo_name": "nervestaple/react-phaser-kit", "max_issues_repo_issues_event_min_datetime": "2019-05-09T22:31:09.000Z", "max_issues_repo_issues_event_max_datetime": "2019-05-09T22:31:09.000Z", "max_forks_repo_path": "src/components/Game.js", "max_forks_repo_name": "nervestaple/react-phaser-kit", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 4, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 23.7285714286, "max_line_length": 82, "alphanum_fraction": 0.6472004816}
import React from 'react';
import PropTypes from 'prop-types';

import createPhaserGame from '../createPhaserGame';
import PhaserRenderer from '../PhaserRenderer';

class Game extends React.Component {
  static defaultProps = {
    width: window.innerWidth,
    height: window.innerHeight,
    assets: [],
    children: null,
  };

  static propTypes = {
    width: PropTypes.number,
    height: PropTypes.number,
    assets: PropTypes.arrayOf(PropTypes.string),
    children: PropTypes.node,
  };

  static childContextTypes = {
    scene: PropTypes.object,
  };

  getChildContext() {
    return {
      scene: this.scene,
    };
  }

  componentDidMount() {
    const {
      width,
      height,
      assets,
    } = this.props;

    const files = assets.map(url => ({ type: 'image', key: url, url }));

    createPhaserGame({ width, height, files, parent: this.phaserContainerRef })
      .then((scene) => {
        this.scene = scene;
        this.mountNode = PhaserRenderer.createContainer(this.scene);
        PhaserRenderer.updateContainer(this.props.children, this.mountNode, this);

        PhaserRenderer.injectIntoDevTools({
          findFiberByHostInstance: PhaserRenderer.findFiberByHostInstance,
          bundleType: 1,
          version: '0.0.2',
          rendererPackageName: 'react-phaser-kit',
        });
      });
  }

  componentDidUpdate() {
    PhaserRenderer.updateContainer(this.props.children, this.mountNode, this);
  }

  componentWillUnmount() {
    PhaserRenderer.updateContainer(null, this.mountNode, this);
  }

  render() {
    return <div ref={(ref) => { this.phaserContainerRef = ref; }} />;
  }
}

export default Game;
