QueryFilter

View source

Base class for filters that refine the archetypes selected by a Query.

Extend this abstract class to add custom query-selection rules. Override QueryFilter#isRequired to change descriptor requirements or QueryFilter#archetype to accept only matching archetypes. The default contract leaves every query descriptor required and accepts every archetype.

Examples

import { Query, QueryFilter, World } from '@wimaengine/ecs'
import { typeid } from '@wimaengine/type'

class Visible {}

class VisibleOnly extends QueryFilter {
  constructor() {
    super()
    this.typeid = typeid(Visible)
  }

  archetype(types) {
    return types.includes(this.typeid)
  }
}

const world = new World()
world.spawn([new Visible()])
const query = new Query(world, [], [new VisibleOnly()])

Constructor


constructor<Transform extends FilterTransform = FilterTransform>()

View source

Creates a filter base for a subclass.

The base class is abstract and cannot be instantiated directly. A subclass must call this constructor through super().

Type Parameters

Transform extends FilterTransform = FilterTransform

Throws

If called as new QueryFilter() rather than through a subclass.

Properties


typeTransform: Transform

View source

Compile-time transformation applied to a query’s component tuple.

Use an identity transform when the filter only selects archetypes, or a component transform when it changes the type of a returned component. The member is a typing contract and is not read while Query applies filters at runtime.

Methods


isRequired(_type: TypeId): boolean

View source

Returns whether a query descriptor must be present before archetype matching.

Query calls this method once for each descriptor while the query is constructed. Returning false removes that descriptor from the required set; it does not by itself accept an archetype or change the result tuple. The default keeps every descriptor required.

Parameters

NameDescriptionDefault
_type

Descriptor TypeId being tested.

—

Returns

Whether the descriptor must be present in a matching archetype.

archetype(_types: readonly TypeId[]): boolean

View source

Returns whether an archetype’s component layout matches this filter.

Query#update passes the complete, read-only TypeId list from the Archetype. The method does not receive an entity, row, or component value. The default accepts every archetype; a query includes an archetype only when this method returns true for every configured filter.

Parameters

NameDescriptionDefault
_types

Complete component TypeIds in the archetype.

—

Returns

Whether the archetype is accepted by this filter.