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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// =============================================
|
| 2 |
+
// VIKING — PLAYER CONTROLS
|
| 3 |
+
// =============================================
|
| 4 |
+
|
| 5 |
+
export function createControls(game) {
|
| 6 |
+
const keys = new Set();
|
| 7 |
+
const pressedButtons = new Set();
|
| 8 |
+
|
| 9 |
+
let disposed = false;
|
| 10 |
+
|
| 11 |
+
const canvas = document.getElementById("game-canvas");
|
| 12 |
+
|
| 13 |
+
const attackButton = document.getElementById("attack-button");
|
| 14 |
+
const blockButton = document.getElementById("block-button");
|
| 15 |
+
|
| 16 |
+
const moveForwardButton = document.getElementById("move-forward");
|
| 17 |
+
const moveBackwardButton = document.getElementById("move-backward");
|
| 18 |
+
const moveLeftButton = document.getElementById("move-left");
|
| 19 |
+
const moveRightButton = document.getElementById("move-right");
|
| 20 |
+
|
| 21 |
+
// =========================================
|
| 22 |
+
// KEYBOARD INPUT
|
| 23 |
+
// =========================================
|
| 24 |
+
|
| 25 |
+
function onKeyDown(event) {
|
| 26 |
+
if (disposed) return;
|
| 27 |
+
|
| 28 |
+
const key = event.key.toLowerCase();
|
| 29 |
+
|
| 30 |
+
const gameKeys = [
|
| 31 |
+
"w", "a", "s", "d",
|
| 32 |
+
"arrowup", "arrowdown",
|
| 33 |
+
"arrowleft", "arrowright",
|
| 34 |
+
"shift", " "
|
| 35 |
+
];
|
| 36 |
+
|
| 37 |
+
if (gameKeys.includes(key)) {
|
| 38 |
+
event.preventDefault();
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
// Avoid triggering an action repeatedly when
|
| 42 |
+
// a key is held down.
|
| 43 |
+
const alreadyPressed = keys.has(key);
|
| 44 |
+
|
| 45 |
+
keys.add(key);
|
| 46 |
+
|
| 47 |
+
if (alreadyPressed) return;
|
| 48 |
+
|
| 49 |
+
if (key === " " || key === "j") {
|
| 50 |
+
game.character?.attack();
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
if (key === "k") {
|
| 54 |
+
game.character?.block(true);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
if (key === "escape") {
|
| 58 |
+
game.camera?.reset();
|
| 59 |
+
}
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
function onKeyUp(event) {
|
| 63 |
+
const key = event.key.toLowerCase();
|
| 64 |
+
|
| 65 |
+
keys.delete(key);
|
| 66 |
+
|
| 67 |
+
if (key === "k") {
|
| 68 |
+
game.character?.block(false);
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
function onBlur() {
|
| 73 |
+
keys.clear();
|
| 74 |
+
pressedButtons.clear();
|
| 75 |
+
|
| 76 |
+
game.character?.block(false);
|
| 77 |
+
|
| 78 |
+
clearButtonStates();
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
window.addEventListener("keydown", onKeyDown);
|
| 82 |
+
window.addEventListener("keyup", onKeyUp);
|
| 83 |
+
window.addEventListener("blur", onBlur);
|
| 84 |
+
|
| 85 |
+
// =========================================
|
| 86 |
+
// MOVEMENT INPUT
|
| 87 |
+
// =========================================
|
| 88 |
+
|
| 89 |
+
function getMovementInput() {
|
| 90 |
+
let x = 0;
|
| 91 |
+
let z = 0;
|
| 92 |
+
|
| 93 |
+
if (keys.has("a") || keys.has("arrowleft")) {
|
| 94 |
+
x -= 1;
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
if (keys.has("d") || keys.has("arrowright")) {
|
| 98 |
+
x += 1;
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
if (keys.has("w") || keys.has("arrowup")) {
|
| 102 |
+
z -= 1;
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
if (keys.has("s") || keys.has("arrowdown")) {
|
| 106 |
+
z += 1;
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
if (pressedButtons.has("left")) {
|
| 110 |
+
x -= 1;
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
if (pressedButtons.has("right")) {
|
| 114 |
+
x += 1;
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
if (pressedButtons.has("forward")) {
|
| 118 |
+
z -= 1;
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
if (pressedButtons.has("backward")) {
|
| 122 |
+
z += 1;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Normalize diagonal movement so it isn't faster.
|
| 126 |
+
const length = Math.hypot(x, z);
|
| 127 |
+
|
| 128 |
+
if (length > 1) {
|
| 129 |
+
x /= length;
|
| 130 |
+
z /= length;
|
| 131 |
+
}
|
| 132 |
+
|
| 133 |
+
return { x, z };
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
// =========================================
|
| 137 |
+
// MOBILE BUTTON HELPERS
|
| 138 |
+
// =========================================
|
| 139 |
+
|
| 140 |
+
function bindHoldButton(button, name) {
|
| 141 |
+
if (!button) return;
|
| 142 |
+
|
| 143 |
+
const start = event => {
|
| 144 |
+
event.preventDefault();
|
| 145 |
+
|
| 146 |
+
pressedButtons.add(name);
|
| 147 |
+
|
| 148 |
+
button.classList.add("pressed");
|
| 149 |
+
|
| 150 |
+
if (button.setPointerCapture && event.pointerId !== undefined) {
|
| 151 |
+
try {
|
| 152 |
+
button.setPointerCapture(event.pointerId);
|
| 153 |
+
} catch {
|
| 154 |
+
// Some browsers may reject capture.
|
| 155 |
+
}
|
| 156 |
+
}
|
| 157 |
+
};
|
| 158 |
+
|
| 159 |
+
const stop = event => {
|
| 160 |
+
if (event) {
|
| 161 |
+
event.preventDefault();
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
pressedButtons.delete(name);
|
| 165 |
+
|
| 166 |
+
button.classList.remove("pressed");
|
| 167 |
+
};
|
| 168 |
+
|
| 169 |
+
button.addEventListener("pointerdown", start);
|
| 170 |
+
button.addEventListener("pointerup", stop);
|
| 171 |
+
button.addEventListener("pointercancel", stop);
|
| 172 |
+
button.addEventListener("lostpointercapture", stop);
|
| 173 |
+
button.addEventListener("contextmenu", preventContextMenu);
|
| 174 |
+
|
| 175 |
+
return () => {
|
| 176 |
+
button.removeEventListener("pointerdown", start);
|
| 177 |
+
button.removeEventListener("pointerup", stop);
|
| 178 |
+
button.removeEventListener("pointercancel", stop);
|
| 179 |
+
button.removeEventListener("lostpointercapture", stop);
|
| 180 |
+
button.removeEventListener("contextmenu", preventContextMenu);
|
| 181 |
+
};
|
| 182 |
+
}
|
| 183 |
+
|
| 184 |
+
// =========================================
|
| 185 |
+
// ATTACK BUTTON
|
| 186 |
+
// =========================================
|
| 187 |
+
|
| 188 |
+
function bindActionButton(button, action) {
|
| 189 |
+
if (!button) return;
|
| 190 |
+
|
| 191 |
+
const onPointerDown = event => {
|
| 192 |
+
event.preventDefault();
|
| 193 |
+
|
| 194 |
+
button.classList.add("pressed");
|
| 195 |
+
|
| 196 |
+
action();
|
| 197 |
+
};
|
| 198 |
+
|
| 199 |
+
const onPointerUp = () => {
|
| 200 |
+
button.classList.remove("pressed");
|
| 201 |
+
};
|
| 202 |
+
|
| 203 |
+
button.addEventListener("pointerdown", onPointerDown);
|
| 204 |
+
button.addEventListener("pointerup", onPointerUp);
|
| 205 |
+
button.addEventListener("pointercancel", onPointerUp);
|
| 206 |
+
button.addEventListener("contextmenu", preventContextMenu);
|
| 207 |
+
|
| 208 |
+
return () => {
|
| 209 |
+
button.removeEventListener("pointerdown", onPointerDown);
|
| 210 |
+
button.removeEventListener("pointerup", onPointerUp);
|
| 211 |
+
button.removeEventListener("pointercancel", onPointerUp);
|
| 212 |
+
button.removeEventListener("contextmenu", preventContextMenu);
|
| 213 |
+
};
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
// =========================================
|
| 217 |
+
// BLOCK BUTTON
|
| 218 |
+
// =========================================
|
| 219 |
+
|
| 220 |
+
function bindBlockButton(button) {
|
| 221 |
+
if (!button) return;
|
| 222 |
+
|
| 223 |
+
const start = event => {
|
| 224 |
+
event.preventDefault();
|
| 225 |
+
|
| 226 |
+
button.classList.add("pressed");
|
| 227 |
+
|
| 228 |
+
game.character?.block(true);
|
| 229 |
+
};
|
| 230 |
+
|
| 231 |
+
const stop = event => {
|
| 232 |
+
if (event) {
|
| 233 |
+
event.preventDefault();
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
button.classList.remove("pressed");
|
| 237 |
+
|
| 238 |
+
game.character?.block(false);
|
| 239 |
+
};
|
| 240 |
+
|
| 241 |
+
button.addEventListener("pointerdown", start);
|
| 242 |
+
button.addEventListener("pointerup", stop);
|
| 243 |
+
button.addEventListener("pointercancel", stop);
|
| 244 |
+
button.addEventListener("lostpointercapture", stop);
|
| 245 |
+
button.addEventListener("contextmenu", preventContextMenu);
|
| 246 |
+
|
| 247 |
+
return () => {
|
| 248 |
+
button.removeEventListener("pointerdown", start);
|
| 249 |
+
button.removeEventListener("pointerup", stop);
|
| 250 |
+
button.removeEventListener("pointercancel", stop);
|
| 251 |
+
button.removeEventListener("lostpointercapture", stop);
|
| 252 |
+
button.removeEventListener("contextmenu", preventContextMenu);
|
| 253 |
+
};
|
| 254 |
+
}
|
| 255 |
+
|
| 256 |
+
function preventContextMenu(event) {
|
| 257 |
+
event.preventDefault();
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
// =========================================
|
| 261 |
+
// REGISTER MOBILE CONTROLS
|
| 262 |
+
// =========================================
|
| 263 |
+
|
| 264 |
+
const cleanupFunctions = [];
|
| 265 |
+
|
| 266 |
+
cleanupFunctions.push(
|
| 267 |
+
bindHoldButton(moveForwardButton, "forward"),
|
| 268 |
+
bindHoldButton(moveBackwardButton, "backward"),
|
| 269 |
+
bindHoldButton(moveLeftButton, "left"),
|
| 270 |
+
bindHoldButton(moveRightButton, "right"),
|
| 271 |
+
|
| 272 |
+
bindActionButton(attackButton, () => {
|
| 273 |
+
game.character?.attack();
|
| 274 |
+
}),
|
| 275 |
+
|
| 276 |
+
bindBlockButton(blockButton)
|
| 277 |
+
);
|
| 278 |
+
|
| 279 |
+
// =========================================
|
| 280 |
+
// BUTTON STATE RESET
|
| 281 |
+
// =========================================
|
| 282 |
+
|
| 283 |
+
function clearButtonStates() {
|
| 284 |
+
document.querySelectorAll("#mobile-controls button")
|
| 285 |
+
.forEach(button => {
|
| 286 |
+
button.classList.remove("pressed");
|
| 287 |
+
});
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
// =========================================
|
| 291 |
+
// UPDATE
|
| 292 |
+
// =========================================
|
| 293 |
+
|
| 294 |
+
function update(delta) {
|
| 295 |
+
if (disposed || !game.character) {
|
| 296 |
+
return;
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
const { x, z } = getMovementInput();
|
| 300 |
+
|
| 301 |
+
const running = keys.has("shift");
|
| 302 |
+
|
| 303 |
+
game.character.move(
|
| 304 |
+
x,
|
| 305 |
+
z,
|
| 306 |
+
running,
|
| 307 |
+
delta
|
| 308 |
+
);
|
| 309 |
+
|
| 310 |
+
// Camera-relative movement will be added
|
| 311 |
+
// after the basic controller is verified.
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
// =========================================
|
| 315 |
+
// CLEANUP
|
| 316 |
+
// =========================================
|
| 317 |
+
|
| 318 |
+
function dispose() {
|
| 319 |
+
if (disposed) return;
|
| 320 |
+
|
| 321 |
+
disposed = true;
|
| 322 |
+
|
| 323 |
+
window.removeEventListener("keydown", onKeyDown);
|
| 324 |
+
window.removeEventListener("keyup", onKeyUp);
|
| 325 |
+
window.removeEventListener("blur", onBlur);
|
| 326 |
+
|
| 327 |
+
cleanupFunctions.forEach(cleanup => {
|
| 328 |
+
if (typeof cleanup === "function") {
|
| 329 |
+
cleanup();
|
| 330 |
+
}
|
| 331 |
+
});
|
| 332 |
+
|
| 333 |
+
keys.clear();
|
| 334 |
+
pressedButtons.clear();
|
| 335 |
+
|
| 336 |
+
clearButtonStates();
|
| 337 |
+
}
|
| 338 |
+
|
| 339 |
+
// =========================================
|
| 340 |
+
// PUBLIC API
|
| 341 |
+
// =========================================
|
| 342 |
+
|
| 343 |
+
return {
|
| 344 |
+
update,
|
| 345 |
+
dispose
|
| 346 |
+
};
|
| 347 |
+
}
|