silphius/app/react/components/pixi/entities.jsx

102 lines
3.0 KiB
React
Raw Normal View History

2024-07-01 18:12:53 -05:00
import {AdjustmentFilter} from '@pixi/filter-adjustment';
import {GlowFilter} from '@pixi/filter-glow';
2024-06-27 13:57:18 -05:00
import {Container} from '@pixi/react';
2024-07-31 13:01:33 -05:00
import {useCallback, useEffect, useRef, useState} from 'react';
2024-07-01 18:12:53 -05:00
2024-07-20 04:32:33 -05:00
import {usePacket} from '@/react/context/client.js';
import {useEcs, useEcsTick} from '@/react/context/ecs.js';
import {useMainEntity} from '@/react/context/main-entity.js';
import {useRadians} from '@/react/context/radians.js';
2024-06-27 13:57:18 -05:00
2024-06-27 02:57:28 -05:00
import Entity from './entity.jsx';
2024-06-18 07:24:20 -05:00
2024-07-30 12:31:40 -05:00
export default function Entities({monopolizers, particleWorker}) {
2024-07-01 18:12:53 -05:00
const [ecs] = useEcs();
2024-07-31 13:01:33 -05:00
const entities = useRef({});
2024-07-01 18:12:53 -05:00
const [mainEntity] = useMainEntity();
2024-07-14 21:07:46 -05:00
const radians = useRadians();
2024-07-01 18:12:53 -05:00
const [willInteractWith, setWillInteractWith] = useState(0);
2024-07-14 21:07:46 -05:00
const [interactionFilters] = useState([
new AdjustmentFilter(),
new GlowFilter({color: 0x0}),
]);
2024-07-31 13:01:33 -05:00
const [, forceRender] = useState();
2024-07-30 09:56:53 -05:00
useEffect(() => {
if (!ecs || !particleWorker) {
return;
}
async function onMessage(diff) {
await ecs.apply(diff.data);
for (const id in diff.data) {
if (!diff.data[id]) {
2024-07-31 13:01:33 -05:00
delete entities.current[id]
2024-07-30 09:56:53 -05:00
}
else {
2024-07-31 13:01:33 -05:00
entities.current[id] = ecs.get(id);
2024-07-30 09:56:53 -05:00
}
}
2024-07-31 13:01:33 -05:00
forceRender(Math.random());
2024-07-30 09:56:53 -05:00
}
particleWorker.addEventListener('message', onMessage);
return () => {
particleWorker.removeEventListener('message', onMessage);
};
}, [ecs, particleWorker]);
2024-07-14 21:07:46 -05:00
const pulse = (Math.cos(radians / 4) + 1) * 0.5;
2024-07-05 18:25:57 -05:00
interactionFilters[0].brightness = (pulse * 0.75) + 1;
interactionFilters[1].outerStrength = pulse * 0.5;
2024-07-11 03:09:28 -05:00
usePacket('EcsChange', async () => {
2024-07-31 13:01:33 -05:00
entities.current = {};
2024-07-31 09:29:33 -05:00
});
const onEcsTickEntities = useCallback((payload, ecs) => {
2024-07-11 03:09:28 -05:00
for (const id in payload) {
2024-07-11 16:44:41 -05:00
if ('1' === id) {
continue;
}
2024-07-11 03:09:28 -05:00
const update = payload[id];
if (false === update) {
2024-07-31 13:01:33 -05:00
delete entities.current[id];
2024-07-13 16:33:23 -05:00
continue;
}
2024-07-31 13:01:33 -05:00
entities.current[id] = ecs.get(id);
2024-07-13 16:33:23 -05:00
}
2024-07-31 13:01:33 -05:00
forceRender(Math.random());
2024-07-31 09:29:33 -05:00
}, []);
useEcsTick(onEcsTickEntities);
const onEcsTickParticles = useCallback((payload) => {
for (const id in payload) {
const update = payload[id];
if (update.Emitter?.emit) {
for (const id in update.Emitter.emit) {
particleWorker?.postMessage(update.Emitter.emit[id]);
}
}
2024-07-11 03:09:28 -05:00
}
2024-07-31 09:29:33 -05:00
}, [particleWorker]);
useEcsTick(onEcsTickParticles);
const onEcsTickInteractions = useCallback((payload, ecs) => {
2024-07-11 03:09:28 -05:00
const main = ecs.get(mainEntity);
if (main) {
setWillInteractWith(main.Interacts.willInteractWith);
}
2024-07-31 09:29:33 -05:00
}, [mainEntity]);
useEcsTick(onEcsTickInteractions);
2024-06-18 07:24:20 -05:00
const renderables = [];
2024-07-31 13:01:33 -05:00
for (const id in entities.current) {
2024-07-13 17:08:23 -05:00
const isHighlightedInteraction = 0 === monopolizers.length && id == willInteractWith;
2024-06-18 07:24:20 -05:00
renderables.push(
2024-06-27 02:57:28 -05:00
<Entity
2024-07-10 14:16:47 -05:00
filters={isHighlightedInteraction ? interactionFilters : null}
2024-07-31 13:01:33 -05:00
entity={entities.current[id]}
key={id}
2024-06-27 02:57:28 -05:00
/>
2024-06-11 18:42:48 -05:00
);
}
2024-06-27 13:57:18 -05:00
return (
<Container
sortableChildren
>
{renderables}
</Container>
);
2024-06-11 18:42:48 -05:00
}