Skip to content

Commit

Permalink
can now filter with with out any inputed entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoieh committed Nov 28, 2021
1 parent 6c8c7da commit 2bdaa4b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/ecs/Query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IEntity } from '..';
import { EntityManager } from '..';

export type QueryFilter = (entity: IEntity) => boolean;

Expand All @@ -9,22 +10,21 @@ export class Query {
return this._filter;
}

private readonly _options: { [key: string]: any };
private readonly _entities: IEntity[];

public get options(): { [key: string]: any } {
return this._options;
}
private readonly _entityManager: EntityManager;

constructor(filter: QueryFilter, options: { [key: string]: any } = {}) {
constructor(filter: QueryFilter, entityManager: EntityManager = EntityManager.instance) {
this._filter = filter;
this._options = options;
this._entityManager = entityManager;
this._entities = entityManager.entities;
}

public find(entities: IEntity[]): IEntity | undefined {
return entities.find(this._filter);
}

public filter(entities: IEntity[]): IEntity[] {
public filter(entities: IEntity[] = this._entities): IEntity[] {
return entities.filter(this._filter);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/ecs/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ describe('>>> Query', () => {
expect(query.matchesAll(entities)).toBe(true);
expect(query.matchesAllExcept(entities, e4)).toBe(true);
});

it('should fiter with out inputing entities', () => {
const query = new Query((entity: IEntity) => entity.has(C1), entityManager);
const entities = query.filter();

expect(entities).toBeDefined();
expect(entities.length).toBe(3);

expect(query.matchesNone(entities)).toBe(false);
expect(query.matchesAll(entities)).toBe(true);
expect(query.matchesAllExcept(entities, e4)).toBe(true);
});
});

describe('>>> Query map', () => {
Expand Down

0 comments on commit 2bdaa4b

Please sign in to comment.