Query

Query

new Query(opts)

Source:

Used to perform matching of entities.

Example
//matches entities with 'Position' and 'Velocity' components
const q1 = Query({
  criterions: ["Position", "Velocity"],
});

//matches entities with 'Position' and 'Velocity' components and without 'Sprite' component
const q2 = Query({
  criterions: {
    include: ["Position", "Velocity"],
    exclude: ["Sprite"],
  },
});

//matches entities of type 'Ball'
const q3 = Query({
  criterions: {
    entityType: "Ball",
  },
});
Parameters:
Name Type Description
opts Object
Properties
Name Type Description
criterions Object | Array

query criterions

Methods

addToIndex(entityId)

Source:

Adds entity ID to query index.

Parameters:
Name Type Description
entityId Number

getEntities() → {Object}

Source:

Returns entities matched by query.

Returns object with following properties:

  • length - number of matched entities
  • entities - array with matched entities. This array length is usually not the same as matched entities number!
Returns:
Type
Object

initialize(allEntities)

Source:

Initializes query. Builds initial entities index.

Parameters:
Name Type Description
allEntities FastArray

fast array of all entities present in the engine

removeFromIndex(entityId)

Source:

Removes entity ID from query index.

Parameters:
Name Type Description
entityId Number

satisfiedBy(entity)

Source:

Checks if entity satisfies query criterions.

Parameters:
Name Type Description
entity Entity

entity to check

shouldUpdate()

Source:

Checks if query should update.

Query should update when entities matching its criterions are added or removed from engine. It should also update when entity that was matching criterions has changed and doesn't match it anymore.

update(allEntities)

Source:

Updates internal matched entities with index.

Parameters:
Name Type Description
allEntities FastArray

fast array of all entities present in the engine