refactor: camera and targeting

This commit is contained in:
cha0s 2024-06-25 08:41:20 -05:00
parent df5d55a0ac
commit 7a2485b588
3 changed files with 22 additions and 10 deletions

View File

@ -27,10 +27,9 @@ export default class FollowCamera extends System {
updateCamera(portion, entity) { updateCamera(portion, entity) {
const {Camera, Position} = entity; const {Camera, Position} = entity;
if (Camera && Position) { if (Camera && Position) {
const {AreaSize: {x, y}} = this.ecs.get(1);
const [px, py] = [ const [px, py] = [
Math.max(hx, Math.min(Math.round(Position.x), x - hx)), Math.round(Position.x),
Math.max(hy, Math.min(Math.round(Position.y), y - hy)), Math.round(Position.y),
]; ];
if (Camera.x === px && Camera.y === py) { if (Camera.x === px && Camera.y === py) {
return; return;

View File

@ -68,7 +68,10 @@ export default function EcsComponent() {
> >
<TileLayer tileLayer={TileLayers.layers[0]} /> <TileLayer tileLayer={TileLayers.layers[0]} />
{activeTool && ( {activeTool && (
<TargetingGrid entity={entity} /> <TargetingGrid
ecs={ecs}
entity={entity}
/>
)} )}
<Entities entities={entities} /> <Entities entities={entities} />
{activeTool && ( {activeTool && (

View File

@ -7,7 +7,7 @@ import {Sprite} from '@pixi/sprite';
import {useEffect, useState} from 'react'; import {useEffect, useState} from 'react';
const tileSize = {x: 16, y: 16}; const tileSize = {x: 16, y: 16};
const radius = 17; const radius = 9;
function makeFade(renderer) { function makeFade(renderer) {
const fade = new Graphics(); const fade = new Graphics();
@ -18,7 +18,7 @@ function makeFade(renderer) {
tileSize.y * (radius / 2), tileSize.y * (radius / 2),
((tileSize.x + tileSize.y) / 2) * (radius * 0.35), ((tileSize.x + tileSize.y) / 2) * (radius * 0.35),
) )
fade.filters = [new BlurFilter(((tileSize.x + tileSize.y) / 2) * 2.5)]; fade.filters = [new BlurFilter(24)];
const renderTexture = RenderTexture.create({ const renderTexture = RenderTexture.create({
width: tileSize.x * radius, width: tileSize.x * radius,
height: tileSize.y * radius, height: tileSize.y * radius,
@ -28,7 +28,7 @@ function makeFade(renderer) {
} }
const TargetingGridInternal = PixiComponent('TargetingGrid', { const TargetingGridInternal = PixiComponent('TargetingGrid', {
create: ({app}) => { create: ({app, ecs}) => {
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,9 +59,18 @@ 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);
return container; // Clamp within layer area.
const area = new Graphics();
area.beginFill(0xffffff);
const {TileLayers: {layers: [layer]}} = ecs.get(1)
const {x, y} = layer.area;
area.drawRect(0, 0, x * tileSize.x, y * tileSize.y);
container.mask = area;
const top = new Container();
top.addChild(container, area);
return top;
}, },
applyProps: (container, oldProps, {entity, radians}) => { applyProps: ({children: [container]}, oldProps, {entity, radians}) => {
const pulse = (Math.cos(radians) + 1) * 0.5; const pulse = (Math.cos(radians) + 1) * 0.5;
const {Position: {x, y}} = entity; const {Position: {x, y}} = entity;
const tileX = x - (x % tileSize.x); const tileX = x - (x % tileSize.x);
@ -77,7 +86,7 @@ const TargetingGridInternal = PixiComponent('TargetingGrid', {
}, },
}) })
export default function TargetingGrid({entity}) { export default function TargetingGrid({ecs, entity}) {
const app = useApp(); const app = useApp();
const [radians, setRadians] = useState(0); const [radians, setRadians] = useState(0);
useEffect(() => { useEffect(() => {
@ -91,6 +100,7 @@ export default function TargetingGrid({entity}) {
return ( return (
<TargetingGridInternal <TargetingGridInternal
app={app} app={app}
ecs={ecs}
entity={entity} entity={entity}
radians={radians} radians={radians}
/> />