silphius/app/react-components/ui.jsx

224 lines
5.9 KiB
React
Raw Normal View History

2024-06-27 04:08:52 -05:00
import {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-27 04:08:52 -05:00
import {useClient} from '@/context/client.js';
2024-06-25 08:54:19 -05:00
import {useDebug} from '@/context/debug.js';
2024-06-22 23:32:57 -05:00
import {useEcs} from '@/context/ecs.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-24 04:43:11 -05:00
function emptySlots() {
return Array(10).fill(undefined);
}
2024-06-13 02:47:28 -05:00
export default function Ui({disconnected}) {
2024-06-10 22:42:30 -05:00
// Key input.
2024-06-27 04:08:52 -05:00
const client = useClient();
const [mainEntity, setMainEntity] = useMainEntity();
2024-06-25 08:54:19 -05:00
const [debug, setDebug] = useDebug();
2024-06-22 23:32:57 -05:00
const [ecs] = useEcs();
2024-06-13 12:24:32 -05:00
const [showDisconnected, setShowDisconnected] = useState(false);
2024-06-24 08:14:32 -05:00
const [bufferSlot, setBufferSlot] = useState();
2024-06-24 04:43:11 -05:00
const [hotbarSlots, setHotbarSlots] = useState(emptySlots());
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(() => {
2024-06-25 08:54:19 -05:00
return addKeyListener(document.body, ({event, type, payload}) => {
const KEY_MAP = {
keyDown: 1,
keyUp: 0,
};
let actionPayload;
switch (payload) {
2024-06-25 08:54:19 -05:00
case 'F3': {
if (event) {
event.preventDefault();
}
if ('keyDown' === type) {
setDebug(!debug);
}
break;
}
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
});
}
});
2024-06-25 08:54:19 -05:00
}, [client, debug, setDebug]);
usePacket('Tick', (payload) => {
if (0 === Object.keys(payload.ecs).length) {
return;
}
2024-06-22 23:32:57 -05:00
ecs.apply(payload.ecs);
let localMainEntity = mainEntity;
for (const id in payload.ecs) {
2024-06-24 04:43:11 -05:00
const entity = ecs.get(id);
2024-06-22 23:32:57 -05:00
const update = payload.ecs[id];
2024-06-25 12:29:09 -05:00
if (update.Sound?.play) {
for (const sound of update.Sound.play) {
(new Audio(sound)).play();
}
}
2024-06-22 23:32:57 -05:00
if (update?.MainEntity) {
setMainEntity(localMainEntity = id);
}
if (localMainEntity === id) {
2024-06-22 23:32:57 -05:00
if (update.Inventory) {
2024-06-24 08:14:32 -05:00
setBufferSlot(entity.Inventory.slots[0]);
2024-06-24 04:43:11 -05:00
const newHotbarSlots = emptySlots();
for (let i = 1; i < 11; ++i) {
2024-06-24 05:02:13 -05:00
newHotbarSlots[i - 1] = entity.Inventory.slots[i];
2024-06-22 22:04:24 -05:00
}
2024-06-22 11:44:49 -05:00
setHotbarSlots(newHotbarSlots);
}
2024-06-22 23:32:57 -05:00
if (update.Wielder && 'activeSlot' in update.Wielder) {
setActiveSlot(update.Wielder.activeSlot);
}
}
}
2024-06-22 11:44:49 -05:00
}, [hotbarSlots, mainEntity, setMainEntity]);
2024-06-10 22:42:30 -05:00
return (
2024-06-24 08:14:32 -05:00
<div
className={styles.ui}
>
2024-06-10 22:42:30 -05:00
<style>
{`
@media (max-aspect-ratio: ${ratio}) { .${styles.ui} { width: 100%; } }
@media (min-aspect-ratio: ${ratio}) { .${styles.ui} { height: 100%; } }
2024-06-24 08:14:32 -05:00
.${styles.ui} {
cursor: ${
bufferSlot
? `url('${bufferSlot.source}/icon.png'), auto !important`
: 'auto'
};
}
2024-06-10 22:42:30 -05:00
`}
</style>
<Pixi />
{mainEntity && (
<Dom>
<HotBar
active={activeSlot}
onActivate={(i) => {
client.send({
type: 'Action',
2024-06-24 06:40:47 -05:00
payload: {type: 'swapSlots', value: [0, i + 1]},
});
}}
2024-06-22 11:44:49 -05:00
slots={hotbarSlots}
/>
{showDisconnected && (
<Disconnected />
)}
</Dom>
)}
2024-06-10 22:42:30 -05:00
</div>
);
}