Skip to content

Commit

Permalink
moved map and foreach enteties input to be the second arg
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoieh committed Nov 28, 2021
1 parent e4d7b60 commit 6c8c7da
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/ecs/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export class Query {
return entities.filter(this._filter);
}

public map(entities: IEntity[], callback: (entity: IEntity) => IEntity): IEntity[] {
public map(callback: (entity: IEntity) => IEntity, entities: IEntity[]): IEntity[] {
return entities.map((entity) => {
return callback(entity);
});
}

public foreach(entities: IEntity[], callback: (entity: IEntity) => void): void {
public foreach(callback: (entity: IEntity) => void, entities: IEntity[]): void {
entities.forEach((entity) => {
if (this._filter(entity)) {
callback(entity);
Expand Down
4 changes: 2 additions & 2 deletions tests/_test_classes/SimpelSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ export class SimpelSystem extends BaseSystem {
public onUpdate(delta: number): void {
const entities = this.q.filter(this.entityManager.entities);

this.q.foreach(entities, (entity: IEntity) => {
this.q.foreach((entity: IEntity) => {
const cValue = entity.get(CValue);
cValue.value = delta + 1;
});
}, entities);
}
}
42 changes: 21 additions & 21 deletions tests/ecs/Query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('>>> Query', () => {
describe('>>> Query map', () => {
it('should map all entities with a componet C1', () => {
const query = new Query((entity: IEntity) => entity.has(C1));
const entities = query.map(entityManager.entities, (entity: IEntity) => entity);
const entities = query.map((entity: IEntity) => entity, entityManager.entities);

expect(entities).toBeDefined();
expect(entities.length).toBe(3);
Expand All @@ -132,7 +132,7 @@ describe('>>> Query', () => {
expect(beforeFilter).toBeDefined();
expect(beforeFilter.length).toBe(1);

const beforeEntities = beforeQuery.map(beforeFilter, (entity: IEntity) => entity);
const beforeEntities = beforeQuery.map((entity: IEntity) => entity, beforeFilter);

expect(beforeEntities).toBeDefined();
expect(beforeEntities.length).toBe(1);
Expand All @@ -143,7 +143,7 @@ describe('>>> Query', () => {
expect(updateFilter).toBeDefined();
expect(updateFilter.length).toBe(2);

const updateEntities = updateQuery.map(updateFilter, (entity: IEntity) => entity.add(new C3()));
const updateEntities = updateQuery.map((entity: IEntity) => entity.add(new C3()), updateFilter);

expect(updateEntities).toBeDefined();
expect(updateEntities.length).toBe(2);
Expand All @@ -154,7 +154,7 @@ describe('>>> Query', () => {
expect(afterFilter).toBeDefined();
expect(afterFilter.length).toBe(beforeEntities.length + updateEntities.length);

const afterEntities = afterQuery.map(afterFilter, (entity: IEntity) => entity);
const afterEntities = afterQuery.map((entity: IEntity) => entity, afterFilter);

expect(afterEntities).toBeDefined();
expect(afterEntities.length).toBe(beforeEntities.length + updateEntities.length);
Expand All @@ -167,7 +167,7 @@ describe('>>> Query', () => {
expect(beforeFilter).toBeDefined();
expect(beforeFilter.length).toBe(1);

const beforeEntities = beforeQuery.map(beforeFilter, (entity: IEntity) => entity);
const beforeEntities = beforeQuery.map((entity: IEntity) => entity, beforeFilter);

expect(beforeEntities).toBeDefined();
expect(beforeEntities.length).toBe(1);
Expand All @@ -178,7 +178,7 @@ describe('>>> Query', () => {
expect(updateFilter).toBeDefined();
expect(updateFilter.length).toBe(1);

const updateEntities = updateQuery.map(updateFilter, (entity: IEntity) => entity.remove(C3));
const updateEntities = updateQuery.map((entity: IEntity) => entity.remove(C3), updateFilter);

expect(updateEntities).toBeDefined();
expect(updateEntities.length).toBe(1);
Expand All @@ -189,7 +189,7 @@ describe('>>> Query', () => {
expect(afterFilter).toBeDefined();
expect(afterFilter.length).toBe(beforeEntities.length - updateEntities.length);

const afterEntities = afterQuery.map(afterFilter, (entity: IEntity) => entity);
const afterEntities = afterQuery.map((entity: IEntity) => entity, afterFilter);

expect(afterEntities).toBeDefined();
expect(afterEntities.length).toBe(beforeEntities.length - updateEntities.length);
Expand All @@ -202,9 +202,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(3);
});
Expand All @@ -214,9 +214,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(3);
});
Expand All @@ -226,9 +226,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(2);
});
Expand All @@ -238,9 +238,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(1);
});
Expand All @@ -250,9 +250,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(3);
});
Expand All @@ -262,9 +262,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(3);
});
Expand All @@ -274,9 +274,9 @@ describe('>>> Query', () => {
const entities = query.filter(entityManager.entities);

let count = 0;
query.foreach(entities, (entity: IEntity) => {
query.foreach((entity: IEntity) => {
count++;
});
}, entities);

expect(count).toBe(2);
});
Expand Down

0 comments on commit 6c8c7da

Please sign in to comment.