Skip to content

Commit

Permalink
refactor(ecs): use intersectionR for Group.addExisting(), update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Nov 1, 2019
1 parent 90dd3b9 commit 810b7ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/ecs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@thi.ng/checks": "^2.4.0",
"@thi.ng/dcons": "^2.1.5",
"@thi.ng/soa": "^0.0.1",
"@thi.ng/transducers": "^5.4.5",
"@thi.ng/vectors": "^3.3.1"
},
"keywords": [
Expand Down
22 changes: 12 additions & 10 deletions packages/ecs/src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
FnO3,
IID
} from "@thi.ng/api";
import { intersection } from "@thi.ng/associative";
import { intersectionR } from "@thi.ng/associative";
import { map, transduce } from "@thi.ng/transducers";
import {
ComponentID,
EVENT_ADDED,
Expand Down Expand Up @@ -173,12 +174,11 @@ export class Group<SPEC, K extends ComponentID<SPEC>> implements IID<string> {
}

protected addExisting() {
const existing = this.components
.slice(1)
.reduce(
(acc, c) => intersection(acc, new Set(c.keys())),
new Set<number>(this.components[0].keys())
);
const existing: Set<number> = transduce(
map((c) => c.keys()),
intersectionR(),
this.components
);
for (let id of existing) {
this.addID(id, false);
}
Expand All @@ -197,10 +197,12 @@ export class Group<SPEC, K extends ComponentID<SPEC>> implements IID<string> {
}

protected reorderOwned(id: number, n: number) {
if (!this.owned.length) return;
const id2 = this.owned[0].dense[n];
const owned = this.owned;
if (!owned.length) return;
const id2 = owned[0].dense[n];
let swapped = false;
for (let comp of this.owned) {
for (let i = owned.length; --i >= 0; ) {
const comp = owned[i];
// console.log(`moving id: ${id} in ${comp.id}...`);
swapped = comp.swapIndices(comp.sparse[id], n) || swapped;
}
Expand Down

0 comments on commit 810b7ea

Please sign in to comment.