2024-04-07 00:33:54 +02:00
|
|
|
import './Controls.css';
|
2024-04-30 10:52:29 +02:00
|
|
|
import {ReadyState} from 'react-use-websocket';
|
2024-04-07 00:33:54 +02:00
|
|
|
import {useEffect} from 'react';
|
2024-04-30 10:52:29 +02:00
|
|
|
import {makeApiUrl, useMyWebSocket} from './serverCalls.tsx';
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
export default function Controls({player}: { player: string }) {
|
2024-04-21 14:34:45 +02:00
|
|
|
const url = makeApiUrl('/controls', 'ws');
|
2024-04-28 12:53:18 +02:00
|
|
|
url.searchParams.set('playerName', player);
|
2024-04-17 23:09:01 +02:00
|
|
|
|
2024-04-07 00:33:54 +02:00
|
|
|
const {
|
|
|
|
sendMessage,
|
2024-04-07 21:09:52 +02:00
|
|
|
getWebSocket,
|
2024-04-14 18:26:13 +02:00
|
|
|
readyState
|
2024-04-30 10:52:29 +02:00
|
|
|
} = useMyWebSocket(url.toString(), {});
|
2024-04-07 00:33:54 +02:00
|
|
|
|
|
|
|
const socket = getWebSocket();
|
|
|
|
if (socket)
|
|
|
|
(socket as WebSocket).binaryType = 'arraybuffer';
|
|
|
|
|
|
|
|
const keyEventListener = (type: string) => (event: KeyboardEvent) => {
|
|
|
|
if (event.defaultPrevented)
|
|
|
|
return;
|
|
|
|
|
2024-04-07 01:27:11 +02:00
|
|
|
const typeCode = type === 'input-on' ? 0x01 : 0x02;
|
2024-04-07 00:33:54 +02:00
|
|
|
const controls = {
|
2024-04-07 01:27:11 +02:00
|
|
|
'ArrowLeft': 0x03,
|
|
|
|
'ArrowUp': 0x01,
|
|
|
|
'ArrowRight': 0x04,
|
|
|
|
'ArrowDown': 0x02,
|
|
|
|
'Space': 0x05,
|
|
|
|
'KeyW': 0x01,
|
|
|
|
'KeyA': 0x03,
|
|
|
|
'KeyS': 0x02,
|
|
|
|
'KeyD': 0x04,
|
2024-04-07 00:33:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const value = controls[event.code];
|
|
|
|
if (!value)
|
|
|
|
return;
|
|
|
|
|
2024-04-13 23:07:08 +02:00
|
|
|
event.preventDefault();
|
2024-04-07 01:27:11 +02:00
|
|
|
const message = new Uint8Array([typeCode, value]);
|
|
|
|
sendMessage(message);
|
2024-04-07 00:33:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-04-14 18:26:13 +02:00
|
|
|
if (readyState !== ReadyState.OPEN)
|
|
|
|
return;
|
|
|
|
|
2024-04-07 00:33:54 +02:00
|
|
|
const up = keyEventListener('input-off');
|
|
|
|
const down = keyEventListener('input-on');
|
|
|
|
window.onkeyup = up;
|
|
|
|
window.onkeydown = down;
|
|
|
|
return () => {
|
|
|
|
window.onkeydown = null;
|
|
|
|
window.onkeyup = null;
|
|
|
|
};
|
2024-04-14 18:26:13 +02:00
|
|
|
}, [readyState]);
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-05-02 19:07:46 +02:00
|
|
|
if (readyState != ReadyState.OPEN)
|
|
|
|
return <></>;
|
|
|
|
|
2024-04-14 19:05:37 +02:00
|
|
|
return <div className="Controls flex-row">
|
2024-04-28 12:53:18 +02:00
|
|
|
<div className="flex-column Controls-Container">
|
2024-04-14 12:57:45 +02:00
|
|
|
<h3>Move</h3>
|
|
|
|
<kbd>▲</kbd>
|
2024-04-28 12:53:18 +02:00
|
|
|
<div className="flex-row Controls-Container">
|
2024-04-14 11:16:34 +02:00
|
|
|
<kbd>◄</kbd>
|
|
|
|
<kbd>▼</kbd>
|
|
|
|
<kbd>►</kbd>
|
2024-04-07 00:33:54 +02:00
|
|
|
</div>
|
2024-04-14 00:49:39 +02:00
|
|
|
</div>
|
|
|
|
|
2024-04-28 12:53:18 +02:00
|
|
|
<div className="flex-column Controls-Container">
|
2024-04-14 12:57:45 +02:00
|
|
|
<h3>Fire</h3>
|
|
|
|
<kbd className="space">Space</kbd>
|
|
|
|
</div>
|
2024-04-14 19:05:37 +02:00
|
|
|
</div>;
|
2024-04-07 00:33:54 +02:00
|
|
|
}
|