-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecs): add ECS main class, update types, Component, Group
- move defComponent to ECS - add event consts - make component default vals optional - add Component.notifyChange() - fix component value caching (on delete) - add Group.run() - rename Group.deleteID() => removeID()
- Loading branch information
1 parent
1d33037
commit 40dc1b6
Showing
6 changed files
with
145 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { | ||
assert, | ||
IObjectOf, | ||
Type, | ||
TypedArray, | ||
typedArray, | ||
TypedArrayTypeMap | ||
} from "@thi.ng/api"; | ||
import { isArray, isString } from "@thi.ng/checks"; | ||
import { ReadonlyVec } from "@thi.ng/vectors"; | ||
import { ComponentOpts, GroupOpts } from "./api"; | ||
import { Component } from "./component"; | ||
import { Group } from "./group"; | ||
import { IDGen } from "./id"; | ||
|
||
export class ECS { | ||
idgen: IDGen; | ||
components: Map<string, Component<TypedArray>>; | ||
groups: Map<string, Group>; | ||
|
||
constructor(capacity = 1000) { | ||
this.idgen = new IDGen(capacity); | ||
this.components = new Map(); | ||
this.groups = new Map(); | ||
} | ||
|
||
defEntity( | ||
comps?: | ||
| string[] | ||
| Component<TypedArray>[] | ||
| IObjectOf<ReadonlyVec | undefined> | ||
) { | ||
const id = this.idgen.next(); | ||
if (comps) { | ||
if (isArray(comps)) { | ||
if (!comps.length) return id; | ||
for (let cid of comps) { | ||
const comp = isString(cid) ? this.components.get(cid) : cid; | ||
assert(!!comp, `unknown component ID: ${cid}`); | ||
comp!.add(id); | ||
} | ||
} else { | ||
for (let cid in comps) { | ||
const comp = this.components.get(cid); | ||
assert(!!comp, `unknown component ID: ${cid}`); | ||
comp!.add(id, comps[cid]); | ||
} | ||
} | ||
} | ||
return id; | ||
} | ||
|
||
defComponent<T extends Type = Type.F32>(opts: Partial<ComponentOpts>) { | ||
const cap = this.idgen.capacity; | ||
const utype = uintType(cap); | ||
const comp = new Component<TypedArrayTypeMap[T]>( | ||
typedArray(utype, cap), | ||
typedArray(utype, cap), | ||
opts | ||
); | ||
// TODO add exist check | ||
this.components.set(comp.id, comp); | ||
return comp; | ||
} | ||
|
||
defGroup( | ||
comps: Component<TypedArray>[], | ||
owned: Component<TypedArray>[] = comps, | ||
opts: Partial<GroupOpts> = {} | ||
) { | ||
const g = new Group(comps, owned, opts); | ||
// TODO add exist check | ||
this.groups.set(g.id, g); | ||
return g; | ||
} | ||
} | ||
|
||
const uintType = (num: number) => | ||
num <= 0x100 ? Type.U8 : num <= 0x10000 ? Type.U16 : Type.U32; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./api"; | ||
export * from "./component"; | ||
export * from "./ecs"; | ||
export * from "./group"; | ||
export * from "./lru"; | ||
export * from "./unbounded"; |