Spaces:
Running
Running
Download src/controls.js from Afredbilli/Cilo: direct link, hf CLI and curl.
- Browser
- Download file 9.23 kB
-
https://huggingface.co/spaces/Afredbilli/Cilo/resolve/main/src/controls.js
- Command line
-
hf download hf://spaces/Afredbilli/Cilo/src/controls.js
-
curl -L -o controls.js https://huggingface.co/spaces/Afredbilli/Cilo/resolve/main/src/controls.js
9.23 kB
| // ============================================= | |
| // VIKING — PLAYER CONTROLS | |
| // ============================================= | |
| export function createControls(game) { | |
| const keys = new Set(); | |
| const pressedButtons = new Set(); | |
| let disposed = false; | |
| const canvas = document.getElementById("game-canvas"); | |
| const attackButton = document.getElementById("attack-button"); | |
| const blockButton = document.getElementById("block-button"); | |
| const moveForwardButton = document.getElementById("move-forward"); | |
| const moveBackwardButton = document.getElementById("move-backward"); | |
| const moveLeftButton = document.getElementById("move-left"); | |
| const moveRightButton = document.getElementById("move-right"); | |
| // ========================================= | |
| // KEYBOARD INPUT | |
| // ========================================= | |
| function onKeyDown(event) { | |
| if (disposed) return; | |
| const key = event.key.toLowerCase(); | |
| const gameKeys = [ | |
| "w", "a", "s", "d", | |
| "arrowup", "arrowdown", | |
| "arrowleft", "arrowright", | |
| "shift", " " | |
| ]; | |
| if (gameKeys.includes(key)) { | |
| event.preventDefault(); | |
| } | |
| // Avoid triggering an action repeatedly when | |
| // a key is held down. | |
| const alreadyPressed = keys.has(key); | |
| keys.add(key); | |
| if (alreadyPressed) return; | |
| if (key === " " || key === "j") { | |
| game.character?.attack(); | |
| } | |
| if (key === "k") { | |
| game.character?.block(true); | |
| } | |
| if (key === "escape") { | |
| game.camera?.reset(); | |
| } | |
| } | |
| function onKeyUp(event) { | |
| const key = event.key.toLowerCase(); | |
| keys.delete(key); | |
| if (key === "k") { | |
| game.character?.block(false); | |
| } | |
| } | |
| function onBlur() { | |
| keys.clear(); | |
| pressedButtons.clear(); | |
| game.character?.block(false); | |
| clearButtonStates(); | |
| } | |
| window.addEventListener("keydown", onKeyDown); | |
| window.addEventListener("keyup", onKeyUp); | |
| window.addEventListener("blur", onBlur); | |
| // ========================================= | |
| // MOVEMENT INPUT | |
| // ========================================= | |
| function getMovementInput() { | |
| let x = 0; | |
| let z = 0; | |
| if (keys.has("a") || keys.has("arrowleft")) { | |
| x -= 1; | |
| } | |
| if (keys.has("d") || keys.has("arrowright")) { | |
| x += 1; | |
| } | |
| if (keys.has("w") || keys.has("arrowup")) { | |
| z -= 1; | |
| } | |
| if (keys.has("s") || keys.has("arrowdown")) { | |
| z += 1; | |
| } | |
| if (pressedButtons.has("left")) { | |
| x -= 1; | |
| } | |
| if (pressedButtons.has("right")) { | |
| x += 1; | |
| } | |
| if (pressedButtons.has("forward")) { | |
| z -= 1; | |
| } | |
| if (pressedButtons.has("backward")) { | |
| z += 1; | |
| } | |
| // Normalize diagonal movement so it isn't faster. | |
| const length = Math.hypot(x, z); | |
| if (length > 1) { | |
| x /= length; | |
| z /= length; | |
| } | |
| return { x, z }; | |
| } | |
| // ========================================= | |
| // MOBILE BUTTON HELPERS | |
| // ========================================= | |
| function bindHoldButton(button, name) { | |
| if (!button) return; | |
| const start = event => { | |
| event.preventDefault(); | |
| pressedButtons.add(name); | |
| button.classList.add("pressed"); | |
| if (button.setPointerCapture && event.pointerId !== undefined) { | |
| try { | |
| button.setPointerCapture(event.pointerId); | |
| } catch { | |
| // Some browsers may reject capture. | |
| } | |
| } | |
| }; | |
| const stop = event => { | |
| if (event) { | |
| event.preventDefault(); | |
| } | |
| pressedButtons.delete(name); | |
| button.classList.remove("pressed"); | |
| }; | |
| button.addEventListener("pointerdown", start); | |
| button.addEventListener("pointerup", stop); | |
| button.addEventListener("pointercancel", stop); | |
| button.addEventListener("lostpointercapture", stop); | |
| button.addEventListener("contextmenu", preventContextMenu); | |
| return () => { | |
| button.removeEventListener("pointerdown", start); | |
| button.removeEventListener("pointerup", stop); | |
| button.removeEventListener("pointercancel", stop); | |
| button.removeEventListener("lostpointercapture", stop); | |
| button.removeEventListener("contextmenu", preventContextMenu); | |
| }; | |
| } | |
| // ========================================= | |
| // ATTACK BUTTON | |
| // ========================================= | |
| function bindActionButton(button, action) { | |
| if (!button) return; | |
| const onPointerDown = event => { | |
| event.preventDefault(); | |
| button.classList.add("pressed"); | |
| action(); | |
| }; | |
| const onPointerUp = () => { | |
| button.classList.remove("pressed"); | |
| }; | |
| button.addEventListener("pointerdown", onPointerDown); | |
| button.addEventListener("pointerup", onPointerUp); | |
| button.addEventListener("pointercancel", onPointerUp); | |
| button.addEventListener("contextmenu", preventContextMenu); | |
| return () => { | |
| button.removeEventListener("pointerdown", onPointerDown); | |
| button.removeEventListener("pointerup", onPointerUp); | |
| button.removeEventListener("pointercancel", onPointerUp); | |
| button.removeEventListener("contextmenu", preventContextMenu); | |
| }; | |
| } | |
| // ========================================= | |
| // BLOCK BUTTON | |
| // ========================================= | |
| function bindBlockButton(button) { | |
| if (!button) return; | |
| const start = event => { | |
| event.preventDefault(); | |
| button.classList.add("pressed"); | |
| game.character?.block(true); | |
| }; | |
| const stop = event => { | |
| if (event) { | |
| event.preventDefault(); | |
| } | |
| button.classList.remove("pressed"); | |
| game.character?.block(false); | |
| }; | |
| button.addEventListener("pointerdown", start); | |
| button.addEventListener("pointerup", stop); | |
| button.addEventListener("pointercancel", stop); | |
| button.addEventListener("lostpointercapture", stop); | |
| button.addEventListener("contextmenu", preventContextMenu); | |
| return () => { | |
| button.removeEventListener("pointerdown", start); | |
| button.removeEventListener("pointerup", stop); | |
| button.removeEventListener("pointercancel", stop); | |
| button.removeEventListener("lostpointercapture", stop); | |
| button.removeEventListener("contextmenu", preventContextMenu); | |
| }; | |
| } | |
| function preventContextMenu(event) { | |
| event.preventDefault(); | |
| } | |
| // ========================================= | |
| // REGISTER MOBILE CONTROLS | |
| // ========================================= | |
| const cleanupFunctions = []; | |
| cleanupFunctions.push( | |
| bindHoldButton(moveForwardButton, "forward"), | |
| bindHoldButton(moveBackwardButton, "backward"), | |
| bindHoldButton(moveLeftButton, "left"), | |
| bindHoldButton(moveRightButton, "right"), | |
| bindActionButton(attackButton, () => { | |
| game.character?.attack(); | |
| }), | |
| bindBlockButton(blockButton) | |
| ); | |
| // ========================================= | |
| // BUTTON STATE RESET | |
| // ========================================= | |
| function clearButtonStates() { | |
| document.querySelectorAll("#mobile-controls button") | |
| .forEach(button => { | |
| button.classList.remove("pressed"); | |
| }); | |
| } | |
| // ========================================= | |
| // UPDATE | |
| // ========================================= | |
| function update(delta) { | |
| if (disposed || !game.character) { | |
| return; | |
| } | |
| const { x, z } = getMovementInput(); | |
| const running = keys.has("shift"); | |
| game.character.move( | |
| x, | |
| z, | |
| running, | |
| delta | |
| ); | |
| // Camera-relative movement will be added | |
| // after the basic controller is verified. | |
| } | |
| // ========================================= | |
| // CLEANUP | |
| // ========================================= | |
| function dispose() { | |
| if (disposed) return; | |
| disposed = true; | |
| window.removeEventListener("keydown", onKeyDown); | |
| window.removeEventListener("keyup", onKeyUp); | |
| window.removeEventListener("blur", onBlur); | |
| cleanupFunctions.forEach(cleanup => { | |
| if (typeof cleanup === "function") { | |
| cleanup(); | |
| } | |
| }); | |
| keys.clear(); | |
| pressedButtons.clear(); | |
| clearButtonStates(); | |
| } | |
| // ========================================= | |
| // PUBLIC API | |
| // ========================================= | |
| return { | |
| update, | |
| dispose | |
| }; | |
| } |