41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
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<any>[];
|
|
/** Components an entity must NOT have to match. */
|
|
readonly not: ComponentDef<any>[];
|
|
|
|
constructor(
|
|
withComponents: ComponentDef<any>[],
|
|
withoutComponents: ComponentDef<any>[] = [],
|
|
) {
|
|
this.with = withComponents;
|
|
this.not = withoutComponents;
|
|
}
|
|
|
|
/** Return a new Query that also excludes these components. */
|
|
without(...components: ComponentDef<any>[]): 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<any>[]): Query {
|
|
return new Query(components);
|
|
}
|