-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
input refactor for window system and justice
- Loading branch information
1 parent
5dd2c06
commit d39d541
Showing
3 changed files
with
72 additions
and
68 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
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,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); | ||
} | ||
} |
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