import { Direction, type Movement, type Position } from '@dflike/shared'; import type { World } from '../ecs/World.js'; function directionFromDelta(dx: number, dy: number): number { if (Math.abs(dx) > Math.abs(dy)) return dx > 0 ? Direction.RIGHT : Direction.LEFT; return dy > 0 ? Direction.DOWN : Direction.UP; } export function movementSystem(world: World): void { for (const entity of world.query('position', 'movement')) { const movement = world.getComponent(entity, 'movement')!; const pos = world.getComponent(entity, 'position')!; if (movement.state !== 'walking' || movement.path.length === 0) { if (movement.state === 'walking' && movement.path.length === 0) { movement.state = 'idle'; movement.target = null; } continue; } const next = movement.path.shift()!; movement.direction = directionFromDelta(next.x - pos.x, next.y - pos.y); pos.x = next.x; pos.y = next.y; if (movement.path.length === 0) { movement.state = 'idle'; movement.target = null; } } }