silphius/app/react-components/ui.jsx

180 lines
4.7 KiB
React
Raw Normal View History

2024-06-13 12:24:32 -05:00
import {useContext, useEffect, useState} from 'react';
2024-06-10 22:42:30 -05:00
import addKeyListener from '@/add-key-listener.js';
import {RESOLUTION} from '@/constants.js';
2024-06-10 22:42:30 -05:00
import ClientContext from '@/context/client.js';
import {useMainEntity} from '@/context/main-entity.js';
import usePacket from '@/hooks/use-packet.js';
2024-06-10 22:42:30 -05:00
2024-06-13 02:47:28 -05:00
import Disconnected from './disconnected.jsx';
2024-06-10 22:42:30 -05:00
import Dom from './dom.jsx';
2024-06-11 03:14:32 -05:00
import HotBar from './hotbar.jsx';
2024-06-10 22:42:30 -05:00
import Pixi from './pixi.jsx';
import styles from './ui.module.css';
2024-06-11 02:24:45 -05:00
const ratio = RESOLUTION.x / RESOLUTION.y;
2024-06-10 22:42:30 -05:00
2024-06-13 02:47:28 -05:00
export default function Ui({disconnected}) {
2024-06-10 22:42:30 -05:00
// Key input.
const client = useContext(ClientContext);
const [mainEntity, setMainEntity] = useMainEntity();
2024-06-13 12:24:32 -05:00
const [showDisconnected, setShowDisconnected] = useState(false);
const [activeSlot, setActiveSlot] = useState(0);
2024-06-13 12:24:32 -05:00
useEffect(() => {
let handle;
if (disconnected) {
handle = setTimeout(() => {
setShowDisconnected(true);
}, 1000);
2024-06-13 12:24:32 -05:00
}
else {
setShowDisconnected(false)
}
return () => {
clearTimeout(handle);
};
}, [disconnected]);
2024-06-10 22:42:30 -05:00
useEffect(() => {
return addKeyListener(document.body, ({type, payload}) => {
const KEY_MAP = {
keyDown: 1,
keyUp: 0,
};
let actionPayload;
switch (payload) {
case 'w': {
actionPayload = {type: 'moveUp', value: KEY_MAP[type]};
break;
}
case 'a': {
actionPayload = {type: 'moveLeft', value: KEY_MAP[type]};
break;
}
case 's': {
actionPayload = {type: 'moveDown', value: KEY_MAP[type]};
break;
}
case 'd': {
actionPayload = {type: 'moveRight', value: KEY_MAP[type]};
break;
}
case ' ': {
actionPayload = {type: 'use', value: KEY_MAP[type]};
break;
}
case '1': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 1};
}
break;
}
case '2': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 2};
}
break;
}
case '3': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 3};
}
break;
}
case '4': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 4};
}
break;
}
case '5': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 5};
}
break;
}
case '6': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 6};
}
break;
}
case '7': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 7};
}
break;
}
case '8': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 8};
}
break;
}
case '9': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 9};
}
break;
}
case '0': {
if ('keyDown' === type) {
actionPayload = {type: 'changeSlot', value: 10};
}
break;
}
}
if (actionPayload) {
2024-06-10 22:42:30 -05:00
client.send({
type: 'Action',
payload: actionPayload,
2024-06-10 22:42:30 -05:00
});
}
});
});
usePacket('Tick', (payload) => {
if (0 === Object.keys(payload.ecs).length) {
return;
}
let localMainEntity = mainEntity;
for (const id in payload.ecs) {
if (payload.ecs[id]?.MainEntity) {
setMainEntity(localMainEntity = id);
}
if (localMainEntity === id) {
if (payload.ecs[id].Wielder && 'activeSlot' in payload.ecs[id].Wielder) {
setActiveSlot(payload.ecs[id].Wielder.activeSlot);
}
}
}
}, [mainEntity, setMainEntity]);
2024-06-10 22:42:30 -05:00
return (
<div className={styles.ui}>
<style>
{`
@media (max-aspect-ratio: ${ratio}) { .${styles.ui} { width: 100%; } }
@media (min-aspect-ratio: ${ratio}) { .${styles.ui} { height: 100%; } }
`}
</style>
<Pixi />
{mainEntity && (
<Dom>
<HotBar
active={activeSlot}
onActivate={(i) => {
client.send({
type: 'Action',
payload: {type: 'changeSlot', value: i + 1},
});
}}
slots={Array(10).fill(0).map(() => {})}
/>
{showDisconnected && (
<Disconnected />
)}
</Dom>
)}
2024-06-10 22:42:30 -05:00
</div>
);
}