refactor: share less

This commit is contained in:
cha0s 2024-06-27 02:14:47 -05:00
parent 929b6a5071
commit d33f849e26
3 changed files with 12 additions and 13 deletions

View File

@ -63,7 +63,7 @@ export default function EcsComponent() {
} }
const entity = ecs.get(mainEntity); const entity = ecs.get(mainEntity);
const {Camera} = entity; const {Camera} = entity;
const {TileLayers} = ecs.get(1); const {TileLayers: {layers: [layer]}} = ecs.get(1);
const [cx, cy] = [ const [cx, cy] = [
Math.round(Camera.x - RESOLUTION.x / 2), Math.round(Camera.x - RESOLUTION.x / 2),
Math.round(Camera.y - RESOLUTION.y / 2), Math.round(Camera.y - RESOLUTION.y / 2),
@ -73,19 +73,19 @@ export default function EcsComponent() {
x={-cx} x={-cx}
y={-cy} y={-cy}
> >
<TileLayer tileLayer={TileLayers.layers[0]} /> <TileLayer tileLayer={layer} />
{activeTool && ( {activeTool && (
<TargetingGrid <TargetingGrid
ecs={ecs}
entity={entity} entity={entity}
tileLayer={layer}
/> />
)} )}
<Entities entities={entities} /> <Entities entities={entities} />
{activeTool && ( {activeTool && (
<TargetingGhost <TargetingGhost
ecs={ecs}
entity={entity} entity={entity}
projection={activeTool.projection} projection={activeTool.projection}
tileLayer={layer}
/> />
)} )}
</Container> </Container>

View File

@ -33,7 +33,7 @@ const TargetingGhostInternal = PixiComponent('TargetingGhost', {
}, },
}) })
export default function TargetingGhost({ecs, entity, projection}) { export default function TargetingGhost({entity, projection, tileLayer}) {
const [radians, setRadians] = useState(0); const [radians, setRadians] = useState(0);
useEffect(() => { useEffect(() => {
const handle = setInterval(() => { const handle = setInterval(() => {
@ -43,12 +43,12 @@ export default function TargetingGhost({ecs, entity, projection}) {
clearInterval(handle); clearInterval(handle);
}; };
}, []); }, []);
const {TileLayers: {layers: [layer]}} = ecs.get(1);
const {Position, Wielder} = entity; const {Position, Wielder} = entity;
const projected = Wielder.project(Position.tile, projection) const projected = Wielder.project(Position.tile, projection)
const ghosts = []; const ghosts = [];
const {area} = tileLayer;
for (const {x, y} of projected) { for (const {x, y} of projected) {
if (x < 0 || y < 0 || x >= layer.area.x || y >= layer.area.y) { if (x < 0 || y < 0 || x >= area.x || y >= area.y) {
continue; continue;
} }
ghosts.push( ghosts.push(

View File

@ -28,7 +28,7 @@ function makeFade(renderer) {
} }
const TargetingGridInternal = PixiComponent('TargetingGrid', { const TargetingGridInternal = PixiComponent('TargetingGrid', {
create: ({app, ecs}) => { create: ({app, tileLayer}) => {
const fade = makeFade(app.renderer); const fade = makeFade(app.renderer);
const grid = new Graphics(); const grid = new Graphics();
const lineWidth = 1; const lineWidth = 1;
@ -59,11 +59,10 @@ const TargetingGridInternal = PixiComponent('TargetingGrid', {
innerGrid.mask = fade; innerGrid.mask = fade;
const container = new Container(); const container = new Container();
container.addChild(fade, grid, innerGrid); container.addChild(fade, grid, innerGrid);
// Clamp within layer area. // Clamp within tile layer area.
const area = new Graphics(); const area = new Graphics();
area.beginFill(0xffffff); area.beginFill(0xffffff);
const {TileLayers: {layers: [layer]}} = ecs.get(1) const {x, y} = tileLayer.area;
const {x, y} = layer.area;
area.drawRect(0, 0, x * tileSize.x, y * tileSize.y); area.drawRect(0, 0, x * tileSize.x, y * tileSize.y);
container.mask = area; container.mask = area;
const top = new Container(); const top = new Container();
@ -86,7 +85,7 @@ const TargetingGridInternal = PixiComponent('TargetingGrid', {
}, },
}) })
export default function TargetingGrid({ecs, entity}) { export default function TargetingGrid({entity, tileLayer}) {
const app = useApp(); const app = useApp();
const [radians, setRadians] = useState(0); const [radians, setRadians] = useState(0);
useEffect(() => { useEffect(() => {
@ -100,9 +99,9 @@ export default function TargetingGrid({ecs, entity}) {
return ( return (
<TargetingGridInternal <TargetingGridInternal
app={app} app={app}
ecs={ecs}
entity={entity} entity={entity}
radians={radians} radians={radians}
tileLayer={tileLayer}
/> />
); );
} }