tuxman/client/js/input.js

78 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-06-14 01:18:01 +00:00
import { Key } from "./logic.js";
const debug_style = document.body.appendChild(document.createElement("style"))
var debug_enabled = false
export function startInputListener() {
let dir = 0;
let start = false;
// document.getElementById("start").onclick = e => {
// e.preventDefault();
// start = true;
// }
let keymap = {
"KeyW": Key.UP,
"KeyA": Key.LEFT,
"KeyS": Key.DOWN,
"KeyD": Key.RIGHT,
};
document.getElementById("start").onclick = function() {
start = true
}
window.addEventListener("keydown", ev => {
if(ev.repeat) {
return;
}
if(!(ev.code in keymap)) {
if (ev.code === "KeyB") {
debug_enabled = !debug_enabled
if (debug_enabled) {
2023-06-14 02:22:42 +00:00
debug_style.innerHTML = "* {box-shadow: 0 0 3px red inset;}"
2023-06-14 01:18:01 +00:00
} else {
debug_style.innerHTML = ""
}
}
return;
}
dir = keymap[ev.code];
});
window.addEventListener("keyup", ev => {
if (ev.repeat) {
return;
}
if (!(ev.code in keymap)) {
return
}
if (dir == keymap[ev.code]) {
dir = Key.NOTHING
}
})
let last = {
dir: 0,
}
return function() {
if(dir === last.dir && !start) {
return;
}
last = {
dir,
};
let s = start;
start = false;
return {
dir,
start: s,
}
}
}