import type { ComponentDef } from "./component"; // ── Query ───────────────────────────────────────────── /** * Describes a component filter. * * Use `query()` to construct one, optionally chaining `.without()`. * * @example * ```ts * query(Position, Velocity) // entities with both * query(Position).without(Dead) // Position but not Dead * ``` */ export class Query { /** Components an entity must have to match. */ readonly with: ComponentDef[]; /** Components an entity must NOT have to match. */ readonly not: ComponentDef[]; constructor( withComponents: ComponentDef[], withoutComponents: ComponentDef[] = [], ) { this.with = withComponents; this.not = withoutComponents; } /** Return a new Query that also excludes these components. */ without(...components: ComponentDef[]): Query { return new Query(this.with, [...this.not, ...components]); } } /** * Build a query matching entities that have all the given components. */ export function query(...components: ComponentDef[]): Query { return new Query(components); }