|
|
|
|
@ -68,6 +68,8 @@
|
|
|
|
|
const pauseAction = document.querySelector("#pauseAction");
|
|
|
|
|
const stopAction = document.querySelector("#stopAction");
|
|
|
|
|
const endAction = document.querySelector("#endAction");
|
|
|
|
|
const fireAction = document.querySelector("#fireAction");
|
|
|
|
|
const touchButtons = [...document.querySelectorAll("[data-control]")];
|
|
|
|
|
|
|
|
|
|
const WIDTH = canvas.width;
|
|
|
|
|
const HEIGHT = canvas.height;
|
|
|
|
|
@ -703,11 +705,52 @@
|
|
|
|
|
keys.delete(event.key.toLowerCase());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function bindHoldButton(button, key) {
|
|
|
|
|
const press = (event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
button.setPointerCapture?.(event.pointerId);
|
|
|
|
|
keys.add(key);
|
|
|
|
|
canvas.focus({ preventScroll: true });
|
|
|
|
|
};
|
|
|
|
|
const release = (event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
keys.delete(key);
|
|
|
|
|
try {
|
|
|
|
|
button.releasePointerCapture?.(event.pointerId);
|
|
|
|
|
} catch {
|
|
|
|
|
// Some browsers release capture automatically on cancel/leave.
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
button.addEventListener("pointerdown", press);
|
|
|
|
|
button.addEventListener("pointerup", release);
|
|
|
|
|
button.addEventListener("pointercancel", release);
|
|
|
|
|
button.addEventListener("pointerleave", release);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const touchKeyMap = {
|
|
|
|
|
up: "arrowup",
|
|
|
|
|
down: "arrowdown",
|
|
|
|
|
left: "arrowleft",
|
|
|
|
|
right: "arrowright",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const button of touchButtons) {
|
|
|
|
|
const key = touchKeyMap[button.dataset.control];
|
|
|
|
|
if (key) bindHoldButton(button, key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
primaryAction.addEventListener("click", startOrResume);
|
|
|
|
|
if (startAction) startAction.addEventListener("click", startOrResume);
|
|
|
|
|
if (pauseAction) pauseAction.addEventListener("click", togglePause);
|
|
|
|
|
if (stopAction) stopAction.addEventListener("click", endGame);
|
|
|
|
|
if (endAction) endAction.addEventListener("click", endGame);
|
|
|
|
|
if (fireAction) {
|
|
|
|
|
fireAction.addEventListener("pointerdown", (event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
canvas.focus({ preventScroll: true });
|
|
|
|
|
if (state === "playing") fire(player);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
regenerateWalls();
|
|
|
|
|
player = tank(384, 536, "#77b255", "up", "player");
|
|
|
|
|
|