37 lines
1.5 KiB
JavaScript
37 lines
1.5 KiB
JavaScript
// ==================== УПРАВЛЕНИЕ ====================
|
|
import { state } from '../core/state.js';
|
|
|
|
export const inp = state.inp;
|
|
|
|
export function bindHold(el, key) {
|
|
const down = (e) => { e.preventDefault(); state.inp[key] = true; };
|
|
const up = (e) => { e.preventDefault(); state.inp[key] = false; };
|
|
el.addEventListener('pointerdown', down);
|
|
el.addEventListener('pointerup', up);
|
|
el.addEventListener('pointerleave', up);
|
|
}
|
|
|
|
export function initControls() {
|
|
const leftBtn = document.getElementById('left');
|
|
const rightBtn = document.getElementById('right');
|
|
const jumpBtn = document.getElementById('jump');
|
|
const downBtn = document.getElementById('down');
|
|
|
|
if (leftBtn) bindHold(leftBtn, 'l');
|
|
if (rightBtn) bindHold(rightBtn, 'r');
|
|
if (jumpBtn) bindHold(jumpBtn, 'j');
|
|
if (downBtn) bindHold(downBtn, 's');
|
|
|
|
window.addEventListener('keydown', (e) => {
|
|
if (e.code === 'KeyA' || e.code === 'ArrowLeft') state.inp.l = true;
|
|
if (e.code === 'KeyD' || e.code === 'ArrowRight') state.inp.r = true;
|
|
if (e.code === 'Space' || e.code === 'KeyW' || e.code === 'ArrowUp') state.inp.j = true;
|
|
if (e.code === 'KeyS' || e.code === 'ArrowDown') state.inp.s = true;
|
|
});
|
|
window.addEventListener('keyup', (e) => {
|
|
if (e.code === 'KeyA' || e.code === 'ArrowLeft') state.inp.l = false;
|
|
if (e.code === 'KeyD' || e.code === 'ArrowRight') state.inp.r = false;
|
|
if (e.code === 'Space' || e.code === 'KeyW' || e.code === 'ArrowUp') state.inp.j = false;
|
|
if (e.code === 'KeyS' || e.code === 'ArrowDown') state.inp.s = false;
|
|
});
|
|
} |