diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/app.ts | 12 | ||||
-rw-r--r-- | src/util.ts | 9 | ||||
-rw-r--r-- | src/view/InputHandler.ts | 12 |
3 files changed, 31 insertions, 2 deletions
@@ -11,6 +11,16 @@ import MapContext from './control/MapContext'; import MapData from './model/MapData'; import InputHandler from './view/InputHandler'; +import * as util from './util'; + + +var relevantKeys = [ + InputHandler.Up, + InputHandler.Right, + InputHandler.Down, + InputHandler.Left, +]; + var mapContext: MapContext; @@ -19,7 +29,7 @@ window.onload = () => { xhr.onload = function() { var mapDef = new MapData(JSON.parse(this.responseText)); - var inputHandler = new InputHandler(); + var inputHandler = new InputHandler(util.numberArrayToMap(relevantKeys)); mapContext = new MapContext(mapDef, inputHandler); } diff --git a/src/util.ts b/src/util.ts index 6ab90ce..ad2b482 100644 --- a/src/util.ts +++ b/src/util.ts @@ -11,3 +11,12 @@ export function mapPromises<T>(promises: {[key: string]: Promise<T>}): Promise<{ return Promise.all(p).then(() => ret); } + +export function numberArrayToMap(arr: number[]): {[key: number]: boolean} { + var ret: {[key: number]: boolean} = {}; + + for (let v of arr) + ret[v] = true; + + return ret; +} diff --git a/src/view/InputHandler.ts b/src/view/InputHandler.ts index 86d4ec6..d06bcf8 100644 --- a/src/view/InputHandler.ts +++ b/src/view/InputHandler.ts @@ -13,8 +13,13 @@ class InputHandler { this.listeners.forEach(l => l()); } - constructor() { + constructor(private relevantKeys: {[key: number]: boolean}) { window.addEventListener('keydown', (ev) => { + if (!relevantKeys[ev.keyCode]) + return; + + ev.preventDefault(); + if (!this.keys[ev.keyCode]) { this.keys[ev.keyCode] = true; this.callListeners(); @@ -22,6 +27,11 @@ class InputHandler { }); window.addEventListener('keyup', (ev) => { + if (!relevantKeys[ev.keyCode]) + return; + + ev.preventDefault(); + if (this.keys[ev.keyCode]) { delete this.keys[ev.keyCode]; this.callListeners(); |