Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions src/lib/ecs/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,40 +220,30 @@ export class Query {
/**
* Runs a callback for each entity that matches the query.
*
* If the callback returns `false`, the iteration will stop, and no other
* entities in this query will be iterated over.
*
* #### Usage Example:
* ```ts
* query.forEach((entity) => {
* // ...
* });
* ```
*
* @param callback The callback to run for each entity.
* TODO: This should be turned into a *[Symbol.iterator] method whenever
* that is supported.
*/
public forEach(callback: (entityId: EntityId) => boolean | void): void {
public *iter(): Generator<EntityId> {
for (const archetype of this.archetypes) {
for (const entityId of archetype.entities) {
if (callback(entityId) === false) {
return;
}
yield entityId;
}
}
}

/**
* Runs a callback for each entity that matches the query.
*
* TODO: This should be turned into a *[Symbol.iterator] method whenever
* that is supported.
* @returns an array of all matching entities mapped to their IDs.
*/
public *iterate(): Generator<EntityId> {
public items(): Array<EntityId> {
const idList: Array<EntityId> = new Array();

for (const archetype of this.archetypes) {
for (const entityId of archetype.entities) {
yield entityId;
idList.push(entityId);
}
}

return idList;
}

/**
Expand Down