From d834d7d5d13e79b5fe0fd11b0772f2b00f5d72eb Mon Sep 17 00:00:00 2001 From: Cazka Date: Sat, 26 Nov 2022 02:49:27 +0100 Subject: [PATCH 1/4] initial input.ts --- src/apis/input.ts | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 src/apis/input.ts diff --git a/src/apis/input.ts b/src/apis/input.ts new file mode 100644 index 0000000..2faa9ff --- /dev/null +++ b/src/apis/input.ts @@ -0,0 +1,78 @@ +import { game } from '../apis/game'; + +const sleep = (ms: number): Promise => new Promise((resolve, reject) => setTimeout(resolve, ms)); + +class Input { + #gameCanvas: HTMLCanvasElement; + + constructor() { + game.once('ready', () => { + this.#gameCanvas = document.getElementById('canvas') as HTMLCanvasElement; + }); + } + + keyDown(key: string | number) { + if (typeof key == 'string') { + key = this.#toKeyCode(key); + } + + const keydown = new KeyboardEvent('keydown', { + key: '', + code: '', + keyCode: key, + which: key, + cancelable: true, + composed: true, + bubbles: true, + }); + + _window.dispatchEvent(keydown); + } + + keyUp(key: string | number) { + if (typeof key == 'string') { + key = this.#toKeyCode(key); + } + + const keyup = new KeyboardEvent('keyup', { + key: '', + code: '', + keyCode: key, + which: key, + cancelable: true, + composed: true, + bubbles: true, + }); + + _window.dispatchEvent(keyup); + } + + async keyPress(key: number): Promise { + this.keyDown(key); + await sleep(200); + this.keyUp(key); + await sleep(10); + } + + mouse(x: number, y: number) { + const mousemove = new MouseEvent('mousemove', { + clientX: x, + clientY: y, + cancelable: true, + composed: true, + bubbles: true, + }); + + this.#gameCanvas.dispatchEvent(mousemove); + } + + #toKeyCode(key: string): number { + if (key.length != 1) { + throw new Error(`diepAPI: Unsupported key: ${key}`); + } + + return key.toUpperCase().charCodeAt(0); + } +} + +export const input = new Input(); From da1f0ab3c784d0006a03ed40ab3d3622a6938d39 Mon Sep 17 00:00:00 2001 From: Cazka Date: Sat, 26 Nov 2022 02:49:37 +0100 Subject: [PATCH 2/4] add input to apis --- src/apis/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/apis/index.ts b/src/apis/index.ts index 5cfe037..d3a12ba 100644 --- a/src/apis/index.ts +++ b/src/apis/index.ts @@ -1,6 +1,7 @@ export { arena } from './arena'; export { camera } from './camera'; export { game } from './game'; +export { input } from './input'; export { minimap } from './minimap'; export { player } from './player'; export { playerMovement } from './player_movement'; From c88d5e27301cd70783b65f8b1e8c79192acd6e15 Mon Sep 17 00:00:00 2001 From: Cazka Date: Sat, 26 Nov 2022 02:50:47 +0100 Subject: [PATCH 3/4] declare types explicit --- src/apis/input.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/apis/input.ts b/src/apis/input.ts index 2faa9ff..db4cd44 100644 --- a/src/apis/input.ts +++ b/src/apis/input.ts @@ -11,7 +11,7 @@ class Input { }); } - keyDown(key: string | number) { + keyDown(key: string | number): void { if (typeof key == 'string') { key = this.#toKeyCode(key); } @@ -29,7 +29,7 @@ class Input { _window.dispatchEvent(keydown); } - keyUp(key: string | number) { + keyUp(key: string | number): void { if (typeof key == 'string') { key = this.#toKeyCode(key); } @@ -54,7 +54,7 @@ class Input { await sleep(10); } - mouse(x: number, y: number) { + mouse(x: number, y: number): void { const mousemove = new MouseEvent('mousemove', { clientX: x, clientY: y, From f9b322b061dcbb53e86b2e451dda9a3ff9596bd3 Mon Sep 17 00:00:00 2001 From: Cazka Date: Sat, 26 Nov 2022 02:54:02 +0100 Subject: [PATCH 4/4] use the new input class --- src/apis/player.ts | 62 +++++++++++++++------------------------------- 1 file changed, 20 insertions(+), 42 deletions(-) diff --git a/src/apis/player.ts b/src/apis/player.ts index a24763c..2d8b92e 100644 --- a/src/apis/player.ts +++ b/src/apis/player.ts @@ -4,6 +4,7 @@ import { Vector } from '../core/vector'; import { game } from './game'; import { gamepad } from './gamepad'; +import { input } from './input'; import { playerMovement } from './player_movement'; import { scaling } from './scaling'; @@ -150,35 +151,12 @@ class Player extends EventEmitter { gamepad.connected = value; } - keyDown(key: number | string): void { - if (typeof key == 'string') { - if (key.length != 1) throw new Error(`diepAPI: Unsupported key: ${key}`); - key = key.toUpperCase().charCodeAt(0); - } - _window.input.keyDown(key); - this.#onkeydown({ keyCode: key } as KeyboardEvent); - } - keyUp(key: number | string): void { - if (typeof key == 'string') { - if (key.length != 1) throw new Error(`diepAPI: Unsupported key: ${key}`); - key = key.toUpperCase().charCodeAt(0); - } - _window.input.keyUp(key); - this.#onkeyup({ keyCode: key } as KeyboardEvent); - } - async keyPress(key: number | string): Promise { - this.keyDown(key); - await sleep(200); - this.keyUp(key); - await sleep(10); - } - async spawn(name: string, attempts: number = 0): Promise { if (!this.#isDead) return; if (name !== undefined) (document.getElementById('textInput') as HTMLInputElement).value = name; - await this.keyPress(13); + await input.keyPress(13); await sleep(250); @@ -188,11 +166,11 @@ class Player extends EventEmitter { async upgrade_stat(id: number, level: number): Promise { if (id < 1 || id > 8) throw `diepAPI: ${id} is not a supported stat`; - this.keyDown(85); + input.keyDown(85); for (let i = 0; i < level; i++) { - await this.keyPress(48 + id); + await input.keyPress(48 + id); } - this.keyUp(85); + input.keyUp(85); await sleep(250); } @@ -204,8 +182,8 @@ class Player extends EventEmitter { const y = scaling.screenToCanvasUnits(scaling.windowRatio * (y_index * 110 + 120)); this.#mouseLock = true; - _window.input.mouse(x, y); - await this.keyPress(1); + input.mouse(x, y); + await input.keyPress(1); // wait 200 ms before disabling mouselock await sleep(200); this.#mouseLock = false; @@ -233,32 +211,32 @@ class Player extends EventEmitter { const direction = Vector.subtract(arenaPos, this.position); if (direction.x > 0) { - this.keyUp('a'); - this.keyDown('d'); + input.keyUp('a'); + input.keyDown('d'); } else if (direction.x < 0) { - this.keyUp('d'); - this.keyDown('a'); + input.keyUp('d'); + input.keyDown('a'); } else { - this.keyUp('a'); - this.keyUp('d'); + input.keyUp('a'); + input.keyUp('d'); } if (direction.y > 0) { - this.keyUp('w'); - this.keyDown('s'); + input.keyUp('w'); + input.keyDown('s'); } else if (direction.y < 0) { - this.keyUp('s'); - this.keyDown('w'); + input.keyUp('s'); + input.keyDown('w'); } else { - this.keyUp('w'); - this.keyUp('s'); + input.keyUp('w'); + input.keyUp('s'); } } } lookAt(arenaPos: Vector): void { const position = scaling.toCanvasPos(arenaPos); - _window.input.mouse(position.x, position.y); + input.mouse(position.x, position.y); this.#onmousemove({ clientX: position.x, clientY: position.y } as MouseEvent); }