silphius/app/react-components/ecs.jsx

71 lines
1.9 KiB
React
Raw Normal View History

2024-06-12 13:19:16 -05:00
import {Container} from '@pixi/react';
2024-06-28 08:53:20 -05:00
import {useState} from 'react';
2024-06-10 22:42:30 -05:00
2024-06-13 12:24:32 -05:00
import {RESOLUTION} from '@/constants.js';
import {useEcs, useEcsTick} from '@/context/ecs.js';
import {useMainEntity} from '@/context/main-entity.js';
2024-06-10 22:42:30 -05:00
2024-06-11 18:42:48 -05:00
import Entities from './entities.jsx';
2024-06-18 04:36:44 -05:00
import TargetingGhost from './targeting-ghost.jsx';
2024-06-25 01:46:07 -05:00
import TargetingGrid from './targeting-grid.jsx';
2024-06-11 18:42:48 -05:00
import TileLayer from './tile-layer.jsx';
2024-06-11 15:06:43 -05:00
2024-06-10 22:42:30 -05:00
export default function EcsComponent() {
2024-06-22 23:32:57 -05:00
const [ecs] = useEcs();
2024-06-10 22:42:30 -05:00
const [entities, setEntities] = useState({});
const [mainEntity] = useMainEntity();
useEcsTick((payload) => {
2024-06-10 22:42:30 -05:00
const updatedEntities = {...entities};
for (const id in payload) {
const update = payload[id];
2024-06-26 04:18:46 -05:00
if (false === update) {
2024-06-10 22:42:30 -05:00
delete updatedEntities[id];
}
else {
2024-06-11 02:52:23 -05:00
updatedEntities[id] = ecs.get(id);
2024-06-26 04:18:46 -05:00
if (update.Emitter?.emit) {
updatedEntities[id].Emitter.emitting = {
...updatedEntities[id].Emitter.emitting,
...update.Emitter.emit,
};
}
2024-06-10 22:42:30 -05:00
}
}
setEntities(updatedEntities);
2024-06-24 09:01:30 -05:00
}, [ecs, entities, mainEntity]);
2024-06-11 01:41:19 -05:00
if (!mainEntity) {
return false;
}
2024-06-24 10:53:01 -05:00
const entity = ecs.get(mainEntity);
2024-06-28 08:53:20 -05:00
const {Direction, Position, Wielder} = entity;
const projected = Wielder.activeItem()?.project(Position.tile, Direction.direction)
2024-06-24 10:53:01 -05:00
const {Camera} = entity;
2024-06-27 02:14:47 -05:00
const {TileLayers: {layers: [layer]}} = ecs.get(1);
2024-06-12 19:39:40 -05:00
const [cx, cy] = [
Math.round(Camera.x - RESOLUTION.x / 2),
Math.round(Camera.y - RESOLUTION.y / 2),
];
2024-06-10 22:42:30 -05:00
return (
2024-06-24 09:19:24 -05:00
<Container
x={-cx}
y={-cy}
>
2024-06-27 02:14:47 -05:00
<TileLayer tileLayer={layer} />
2024-06-28 08:53:20 -05:00
{projected && (
2024-06-25 08:41:20 -05:00
<TargetingGrid
2024-06-27 02:14:47 -05:00
tileLayer={layer}
2024-06-28 08:53:20 -05:00
x={Position.x}
y={Position.y}
2024-06-25 08:41:20 -05:00
/>
2024-06-25 01:46:07 -05:00
)}
2024-06-24 09:19:24 -05:00
<Entities entities={entities} />
2024-06-28 08:53:20 -05:00
{projected?.length > 0 && (
2024-06-25 06:20:45 -05:00
<TargetingGhost
2024-06-28 08:53:20 -05:00
projected={projected}
2024-06-27 02:14:47 -05:00
tileLayer={layer}
2024-06-25 06:20:45 -05:00
/>
2024-06-24 09:01:30 -05:00
)}
2024-06-11 15:06:43 -05:00
</Container>
2024-06-10 22:42:30 -05:00
)
}