2024-04-07 21:09:52 +02:00
|
|
|
import useWebSocket from 'react-use-websocket';
|
2024-04-07 00:33:54 +02:00
|
|
|
import {useEffect, useRef} from 'react';
|
|
|
|
import './ClientScreen.css';
|
2024-04-14 14:55:01 +02:00
|
|
|
import {hslToString, Theme} from "./theme.ts";
|
2024-04-16 00:07:44 +02:00
|
|
|
import {Guid} from "./Guid.ts";
|
2024-04-07 00:33:54 +02:00
|
|
|
|
|
|
|
const pixelsPerRow = 352;
|
|
|
|
const pixelsPerCol = 160;
|
2024-04-16 00:07:44 +02:00
|
|
|
const observerMessageSize = pixelsPerCol * pixelsPerRow / 8;
|
|
|
|
|
2024-04-16 18:28:09 +02:00
|
|
|
enum GamePixelEntityType {
|
|
|
|
Wall = 0x0,
|
|
|
|
Tank = 0x1,
|
|
|
|
Bullet = 0x2
|
|
|
|
}
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-04-16 18:28:09 +02:00
|
|
|
function getPixelDataIndexes(bitIndex: number) {
|
2024-04-07 00:33:54 +02:00
|
|
|
return {
|
2024-04-12 16:05:24 +02:00
|
|
|
byteIndex: Math.floor(bitIndex / 8),
|
2024-04-07 00:33:54 +02:00
|
|
|
bitInByteIndex: 7 - bitIndex % 8
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-04-14 13:23:04 +02:00
|
|
|
function normalizeColor(context: CanvasRenderingContext2D, color: string) {
|
|
|
|
context.fillStyle = color;
|
|
|
|
context.fillRect(0, 0, 1, 1);
|
|
|
|
return context.getImageData(0, 0, 1, 1).data;
|
|
|
|
}
|
|
|
|
|
2024-04-16 18:28:09 +02:00
|
|
|
function parseAdditionalDataNibble(nibble: number) {
|
|
|
|
const isPlayerMask = 1;
|
|
|
|
const entityTypeMask = 12;
|
|
|
|
|
|
|
|
return {
|
|
|
|
isCurrentPlayer: (nibble & isPlayerMask) != 0,
|
|
|
|
entityType: ((nibble & entityTypeMask) >> 2) as GamePixelEntityType,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawPixelsToCanvas(
|
|
|
|
{
|
|
|
|
context, width, height, pixels, additional, foreground, background, playerColor, otherTanksColor
|
|
|
|
}: {
|
|
|
|
context: CanvasRenderingContext2D,
|
|
|
|
width: number,
|
|
|
|
height: number,
|
|
|
|
pixels: Uint8ClampedArray,
|
|
|
|
additional: Uint8ClampedArray | null,
|
|
|
|
background: Uint8ClampedArray,
|
|
|
|
foreground: Uint8ClampedArray,
|
|
|
|
playerColor: Uint8ClampedArray,
|
|
|
|
otherTanksColor: Uint8ClampedArray
|
|
|
|
}
|
|
|
|
) {
|
2024-04-16 00:07:44 +02:00
|
|
|
let additionalDataIndex = 0;
|
|
|
|
let additionalDataByte: number | null = null;
|
|
|
|
const nextPixelColor = (isOn: boolean) => {
|
|
|
|
if (!isOn)
|
|
|
|
return background;
|
|
|
|
if (!additional)
|
|
|
|
return foreground;
|
|
|
|
|
|
|
|
let info;
|
|
|
|
if (additionalDataByte === null) {
|
|
|
|
additionalDataByte = additional[additionalDataIndex];
|
|
|
|
additionalDataIndex++;
|
2024-04-16 18:28:09 +02:00
|
|
|
info = parseAdditionalDataNibble(additionalDataByte);
|
2024-04-16 00:07:44 +02:00
|
|
|
} else {
|
2024-04-16 18:28:09 +02:00
|
|
|
info = parseAdditionalDataNibble(additionalDataByte >> 4);
|
2024-04-16 00:07:44 +02:00
|
|
|
additionalDataByte = null;
|
|
|
|
}
|
|
|
|
|
2024-04-16 18:28:09 +02:00
|
|
|
if (info.isCurrentPlayer)
|
2024-04-16 00:07:44 +02:00
|
|
|
return playerColor;
|
2024-04-16 18:28:09 +02:00
|
|
|
|
|
|
|
if (info.entityType == GamePixelEntityType.Tank)
|
|
|
|
return otherTanksColor;
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
return foreground;
|
|
|
|
}
|
2024-04-14 13:23:04 +02:00
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
const imageData = context.getImageData(0, 0, width, height, {colorSpace: 'srgb'});
|
2024-04-07 00:33:54 +02:00
|
|
|
const data = imageData.data;
|
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
for (let y = 0; y < height; y++) {
|
|
|
|
for (let x = 0; x < width; x++) {
|
|
|
|
const pixelIndex = y * pixelsPerRow + x;
|
2024-04-16 18:28:09 +02:00
|
|
|
const {byteIndex, bitInByteIndex} = getPixelDataIndexes(pixelIndex);
|
2024-04-16 00:07:44 +02:00
|
|
|
const isOn = (pixels[byteIndex] & (1 << bitInByteIndex)) !== 0;
|
|
|
|
const color = nextPixelColor(isOn);
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-04-11 20:48:21 +02:00
|
|
|
for (let colorChannel of [0, 1, 2, 3])
|
|
|
|
data[pixelIndex * 4 + colorChannel] = color[colorChannel];
|
2024-04-07 00:33:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
context.putImageData(imageData, 0, 0);
|
2024-04-07 00:33:54 +02:00
|
|
|
}
|
2024-04-11 20:48:21 +02:00
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
export default function ClientScreen({logout, theme, playerId}: {
|
|
|
|
logout: () => void,
|
|
|
|
theme: Theme,
|
|
|
|
playerId?: Guid
|
|
|
|
}) {
|
2024-04-07 00:33:54 +02:00
|
|
|
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
const url = new URL('/screen', import.meta.env.VITE_TANK_WS);
|
|
|
|
if (playerId)
|
|
|
|
url.searchParams.set('player', playerId);
|
|
|
|
|
2024-04-07 00:33:54 +02:00
|
|
|
const {
|
|
|
|
lastMessage,
|
|
|
|
sendMessage,
|
|
|
|
getWebSocket
|
2024-04-16 00:07:44 +02:00
|
|
|
} = useWebSocket(url.toString(), {
|
2024-04-14 21:10:21 +02:00
|
|
|
onError: logout,
|
|
|
|
shouldReconnect: () => true,
|
2024-04-07 00:33:54 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const socket = getWebSocket();
|
|
|
|
if (socket)
|
|
|
|
(socket as WebSocket).binaryType = 'arraybuffer';
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (lastMessage === null)
|
|
|
|
return;
|
|
|
|
if (canvasRef.current === null)
|
|
|
|
throw new Error('canvas null');
|
|
|
|
|
2024-04-16 00:07:44 +02:00
|
|
|
const canvas = canvasRef.current;
|
|
|
|
const drawContext = canvas.getContext('2d');
|
|
|
|
if (!drawContext)
|
|
|
|
throw new Error('could not get draw context');
|
|
|
|
|
|
|
|
let pixels = new Uint8ClampedArray(lastMessage.data);
|
|
|
|
let additionalData: Uint8ClampedArray | null = null;
|
|
|
|
if (pixels.length > observerMessageSize) {
|
|
|
|
additionalData = pixels.slice(observerMessageSize);
|
|
|
|
pixels = pixels.slice(0, observerMessageSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
drawPixelsToCanvas({
|
|
|
|
context: drawContext,
|
|
|
|
width: canvas.width,
|
|
|
|
height: canvas.height,
|
|
|
|
pixels,
|
|
|
|
additional: additionalData,
|
2024-04-16 18:28:09 +02:00
|
|
|
background: normalizeColor(drawContext, hslToString(theme.background)),
|
|
|
|
foreground: normalizeColor(drawContext, hslToString(theme.primary)),
|
|
|
|
playerColor: normalizeColor(drawContext, hslToString(theme.secondary)),
|
|
|
|
otherTanksColor: normalizeColor(drawContext, hslToString(theme.tertiary))
|
2024-04-16 00:07:44 +02:00
|
|
|
});
|
2024-04-07 00:33:54 +02:00
|
|
|
sendMessage('');
|
2024-04-14 14:55:01 +02:00
|
|
|
}, [lastMessage, canvasRef.current, theme]);
|
2024-04-07 00:33:54 +02:00
|
|
|
|
2024-04-13 23:07:08 +02:00
|
|
|
return <canvas ref={canvasRef} id="screen" width={pixelsPerRow} height={pixelsPerCol}/>;
|
2024-04-07 00:33:54 +02:00
|
|
|
}
|