From 24ca4e2a36614933d404974794deee2c95faab4a Mon Sep 17 00:00:00 2001 From: Thilo Behnke Date: Sun, 19 Jun 2022 16:04:06 +0200 Subject: [PATCH] working controls --- client/svelte-client/src/App.svelte | 20 +++++++++++-- client/svelte-client/src/Canvas.svelte | 40 +++++++------------------ client/svelte-client/src/Input.svelte | 38 +++++++++++++++++++++++ client/svelte-client/src/game/engine.ts | 20 +++++++++++++ 4 files changed, 85 insertions(+), 33 deletions(-) create mode 100644 client/svelte-client/src/Input.svelte diff --git a/client/svelte-client/src/App.svelte b/client/svelte-client/src/App.svelte index d0b2586..88931f7 100644 --- a/client/svelte-client/src/App.svelte +++ b/client/svelte-client/src/App.svelte @@ -2,16 +2,30 @@ import { FieldWrapper } from "wasm-app"; import Canvas from "./Canvas.svelte"; import Fps from "./Fps.svelte"; - const field = FieldWrapper.new(); - console.log({field}) - export let name: string; + import {keysPressed} from "./game/engine"; + import Input from "./Input.svelte"; + + function handleKeydown({key}) { + if ($keysPressed.includes(key)) { + return; + } + $keysPressed = [...$keysPressed, key] + } + function handleKeyup({key}) { + if (!$keysPressed.includes(key)) { + return; + } + $keysPressed = $keysPressed.filter(key => key !== key) + }
+
+ diff --git a/client/svelte-client/src/Input.svelte b/client/svelte-client/src/Input.svelte new file mode 100644 index 0000000..2b38049 --- /dev/null +++ b/client/svelte-client/src/Input.svelte @@ -0,0 +1,38 @@ + + +
+ {#each controls as control} +
+ {control} +
+ {/each} +
+ + + diff --git a/client/svelte-client/src/game/engine.ts b/client/svelte-client/src/game/engine.ts index 72189a8..dc73f66 100644 --- a/client/svelte-client/src/game/engine.ts +++ b/client/svelte-client/src/game/engine.ts @@ -9,6 +9,26 @@ export const pixelRatio = writable(window.devicePixelRatio); export const keysPressed: Writable = writable([]) +export const playerInputs = derived( + keysPressed, + $keysPressed => { + return $keysPressed.map(key => { + switch(key.toLowerCase()) { + case 'w': + return {input: 'UP', obj_id: 0, player: 1} + case 's': + return {input: 'DOWN', obj_id: 0, player: 1} + case 'arrowup': + return {input: 'UP', obj_id: 1, player: 2} + case 'arrowdown': + return {input: 'DOWN', obj_id: 1, player: 2} + default: + return null + } + }).filter(it => !!it); + } +) + // A more convenient store for grabbing all game props export const props = deriveObject({ width,