Skip to content

Commit

Permalink
input refactor for window system and justice
Browse files Browse the repository at this point in the history
  • Loading branch information
conartist6 committed Jun 23, 2018
1 parent 5dd2c06 commit d39d541
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 68 deletions.
66 changes: 57 additions & 9 deletions packages/potato-engine-components/src/game/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import React, { PureComponent } from 'react';
import Entity from '../entity';
import { entities } from 'potato-engine';
import c from 'keycode-js';

import './style.css';

const keysToDirections = {
[c.KEY_UP]: 'UP',
[c.KEY_W]: 'UP',
[c.KEY_DOWN]: 'DOWN',
[c.KEY_S]: 'DOWN',
[c.KEY_LEFT]: 'LEFT',
[c.KEY_A]: 'LEFT',
[c.KEY_RIGHT]: 'RIGHT',
[c.KEY_D]: 'RIGHT',
};

export default class Game extends PureComponent {
constructor(props) {
super(props);

this.keydown = this.keydown.bind(this);
this.forceUpdate = this.forceUpdate.bind(this);

this._iterables = {
statics: {
[Symbol.iterator]: this.iterate.bind(this, board => board.statics()),
Expand All @@ -21,27 +37,59 @@ export default class Game extends PureComponent {
}

componentDidMount() {
this.setupGame(this.props.game);
this.setupInput();
this.setupGame();
}

componentWillUnmount() {
this.teardownGame(this.props.game);
this.teardownGame();
this.teardownInput();
}

componentDidUpdate(oldProps) {
if (oldProps.input !== this.props.input) {
this.teardownInput();
this.setupInput();
}
if (oldProps.game !== this.props.game) {
this.teardownGame(oldProps.game);
this.setupGame(this.props.game);
this.teardownGame();
this.setupGame();
}
}

keydown(event) {
const direction = keysToDirections[event.keyCode];
if (direction) {
this._game.tick(direction);
}
}

setupInput() {
if (!this.props.displayOnly) {
this._input = this.props.input;
this._input.on('keydown', this.keydown);
this._input.start();
}
}

teardownInput() {
if (this._input) {
this._input.off('keydown', this.keydown);
this._input.end();
this._input = null;
}
}

setupGame(game) {
game.start();
game.on('tick', () => this.forceUpdate());
setupGame() {
this._game = this.props.game;
this._game.start();
this._game.on('tick', this.forceUpdate);
}

teardownGame(game) {
game.end();
teardownGame() {
this._game.off('tick', this.forceUpdate);
this._game.end();
this._game = null;
}

*iterate(iteratorSelector) {
Expand Down
70 changes: 11 additions & 59 deletions packages/potato-engine-components/src/input.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,29 @@
import c from 'keycode-js';
import Emitter from 'eventemitter2';
import c from 'keycode-js';

const keysToDirections = {
[c.KEY_UP]: 'UP',
[c.KEY_W]: 'UP',
[c.KEY_DOWN]: 'DOWN',
[c.KEY_S]: 'DOWN',
[c.KEY_LEFT]: 'LEFT',
[c.KEY_A]: 'LEFT',
[c.KEY_RIGHT]: 'RIGHT',
[c.KEY_D]: 'RIGHT',
};

export default class Input {
constructor(getMode) {
this._getMode = getMode;
this._emitter = new Emitter();
}

keydown(event) {
const mode = this._getMode();
if (mode === 'game') {
const direction = keysToDirections[event.keyCode];
if (direction) {
this._emitter.emit('move', direction);
}
switch (event.keyCode) {
case c.KEY_P:
this._emitter.emit('pause-unpause');
break;
case c.KEY_R:
this._emitter.emit('reset');
break;
case c.KEY_G:
this._emitter.emit('goto');
event.preventDefault();
break;
}
} else if (mode === 'dialog') {
if (/[a-zA-Z]/.test(event.keyCode)) {
this._emitter.emit('input', event.keyCode);
}
if (event.keyCode === c.KEY_ESCAPE) {
this._emitter.emit('cancel-goto');
}
}
export class BrowserInput {
constructor() {
this.__emitter = new Emitter();
this.keydown = this.keydown.bind(this);
}

on(...args) {
this._emitter.on(...args);
this.__emitter.on(...args);
}

off(...args) {
this._emitter.off(...args);
this.__emitter.off(...args);
}

setMode(mode) {
this._mode = mode;
keydown(event) {
this._emitter.emit('keydown', event);
}

start(event, listener) {
this._listener = e => this.keydown(e);
document.addEventListener('keydown', this._listener);
if (listener) {
this.on(event, listener);
}
document.addEventListener('keydown', this.keydown);
}

end() {
if (this._listener) {
document.removeEventListener('keydown', this._listener);
}
this._listener = null;
this._emitter.removeAllListeners();
document.removeEventListener('keydown', this.keydown);
}
}
4 changes: 4 additions & 0 deletions packages/potato-engine/src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export default class Game extends BoardDecorator {
this._setTickTimeout();
}

get paused() {
return this._paused;
}

/**
* Set the game's play/pause state
**/
Expand Down

0 comments on commit d39d541

Please sign in to comment.