@benev/archimedes 0.1.0-4 → 0.1.0-6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +24 -23
  2. package/package.json +2 -2
  3. package/s/ecs/index.ts +2 -2
  4. package/s/ecs/parts/apply-delta.ts +37 -0
  5. package/s/ecs/parts/change.ts +24 -12
  6. package/s/ecs/parts/entities.ts +1 -1
  7. package/s/ecs/parts/execute.ts +29 -0
  8. package/s/ecs/parts/lifecycle.ts +10 -9
  9. package/s/ecs/parts/types.ts +9 -8
  10. package/s/ecs/test/setup-example.ts +29 -24
  11. package/s/ecs/test/setup-lifecycle-counts.ts +9 -4
  12. package/s/ecs/test.ts +114 -61
  13. package/x/ecs/index.d.ts +2 -2
  14. package/x/ecs/index.js +2 -2
  15. package/x/ecs/index.js.map +1 -1
  16. package/x/ecs/parts/apply-delta.d.ts +3 -0
  17. package/x/ecs/parts/apply-delta.js +34 -0
  18. package/x/ecs/parts/apply-delta.js.map +1 -0
  19. package/x/ecs/parts/change.d.ts +10 -8
  20. package/x/ecs/parts/change.js +28 -8
  21. package/x/ecs/parts/change.js.map +1 -1
  22. package/x/ecs/parts/entities.d.ts +1 -1
  23. package/x/ecs/parts/entities.js +1 -1
  24. package/x/ecs/parts/entities.js.map +1 -1
  25. package/x/ecs/parts/execute.d.ts +3 -0
  26. package/x/ecs/parts/execute.js +18 -0
  27. package/x/ecs/parts/execute.js.map +1 -0
  28. package/x/ecs/parts/lifecycle.d.ts +1 -1
  29. package/x/ecs/parts/lifecycle.js +9 -8
  30. package/x/ecs/parts/lifecycle.js.map +1 -1
  31. package/x/ecs/parts/types.d.ts +8 -8
  32. package/x/ecs/parts/types.js +7 -8
  33. package/x/ecs/parts/types.js.map +1 -1
  34. package/x/ecs/test/setup-example.d.ts +11 -12
  35. package/x/ecs/test/setup-example.js +33 -25
  36. package/x/ecs/test/setup-example.js.map +1 -1
  37. package/x/ecs/test/setup-lifecycle-counts.d.ts +5 -1
  38. package/x/ecs/test/setup-lifecycle-counts.js +7 -4
  39. package/x/ecs/test/setup-lifecycle-counts.js.map +1 -1
  40. package/x/ecs/test.d.ts +4 -0
  41. package/x/ecs/test.js +107 -58
  42. package/x/ecs/test.js.map +1 -1
  43. package/s/ecs/parts/apply-change.ts +0 -33
  44. package/s/ecs/parts/execute-systems.ts +0 -18
  45. package/x/ecs/parts/apply-change.d.ts +0 -3
  46. package/x/ecs/parts/apply-change.js +0 -30
  47. package/x/ecs/parts/apply-change.js.map +0 -1
  48. package/x/ecs/parts/execute-systems.d.ts +0 -3
  49. package/x/ecs/parts/execute-systems.js +0 -12
  50. package/x/ecs/parts/execute-systems.js.map +0 -1
package/README.md CHANGED
@@ -21,7 +21,7 @@ npm install @benev/archimedes
21
21
  ## 🧩 ecs — entities, components, systems
22
22
 
23
23
  ```ts
24
- import {Entities, asSystems, change, executeSystems} from "@benev/archimedes"
24
+ import {Entities, Change, makeId, makeExecute} from "@benev/archimedes"
25
25
  ```
26
26
 
27
27
  1. ***define components.*** json-friendly data that entities could have.
@@ -31,45 +31,48 @@ import {Entities, asSystems, change, executeSystems} from "@benev/archimedes"
31
31
  bleed: number
32
32
  }
33
33
  ```
34
- 1. ***create entities map.*** fancy caching for speedy-fast lookups.
34
+ 1. ***create entities map.*** indexed for speedy-fast lookups.
35
35
  ```ts
36
- export const entitiesWritable = new Entities<MyComponents>()
37
-
38
- // only let systems touch the readonly variant!
39
- export const entities = entitiesWritable.readonly()
36
+ export const entities = new Entities<MyComponents>()
37
+ ```
38
+ 1. ***readonly entities.*** recommended to use this in your systems.
39
+ ```ts
40
+ export const entitiesReadonly = entities.readonly
40
41
  ```
41
- 1. ***define systems.*** select entities by components. yields changes.
42
+ 1. ***define systems.*** select entities by components. changes are formalized.
42
43
  ```ts
43
- const systems = asSystems<MyComponents>(
44
- function* bleeding() {
45
- for (const [id, components] of entities.select("health", "bleed")) {
46
- if (components.bleed >= 0) {
44
+ const systems = (change: Change<MyComponents>) => [
45
+ function bleeding() {
46
+ for (const [id, components] of entitiesReadonly.select("health", "bleed")) {
47
+ if (components.bleed > 0) {
47
48
  const health = components.health - components.bleed
48
- const bleed = components.bleed - 1
49
- yield change.merge(id, {health, bleed})
49
+ change.merge(id, {health})
50
50
  }
51
51
  }
52
52
  },
53
53
 
54
- function* death() {
55
- for (const [id, components] of entities.select("health")) {
54
+ function death() {
55
+ for (const [id, components] of entitiesReadonly.select("health")) {
56
56
  if (components.health <= 0)
57
- yield change.delete(id)
57
+ change.delete(id)
58
58
  }
59
59
  },
60
- )
60
+ ]
61
61
  ```
62
- 1. ***create your first entity.***
62
+ 1. ***manually insert your first entity.***
63
63
  ```ts
64
64
  const wizardId = makeId()
65
- entitiesWritable.set(wizardId, {health: 100, bleed: 2})
65
+ entities.set(wizardId, {health: 100, bleed: 2})
66
66
 
67
67
  console.log(entities.get(wizardId)?.health)
68
68
  // 100
69
69
  ```
70
- 1. ***execute systems to simulate each tick.***
70
+ 1. ***create an execute fn.*** run one tick at a time.
71
71
  ```ts
72
- executeSystems(entitiesWritable, systems)
72
+ const execute = makeExecute(entities, systems)
73
+
74
+ // simulate one tick
75
+ execute()
73
76
 
74
77
  console.log(entities.get(wizardId)?.health)
75
78
  // 98
@@ -91,5 +94,3 @@ import {Entities, asSystems, change, executeSystems} from "@benev/archimedes"
91
94
 
92
95
  *coming soon*
93
96
 
94
-
95
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@benev/archimedes",
3
- "version": "0.1.0-4",
3
+ "version": "0.1.0-6",
4
4
  "description": "game ecs with auto networking",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -17,7 +17,7 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@e280/renraku": "^0.5.7",
20
- "@e280/stz": "^0.2.29",
20
+ "@e280/stz": "^0.2.30",
21
21
  "@msgpack/msgpack": "^3.1.3",
22
22
  "sparrow-rtc": "^0.2.15"
23
23
  },
package/s/ecs/index.ts CHANGED
@@ -1,8 +1,8 @@
1
1
 
2
- export * from "./parts/apply-change.js"
2
+ export * from "./parts/apply-delta.js"
3
3
  export * from "./parts/change.js"
4
4
  export * from "./parts/entities.js"
5
- export * from "./parts/execute-systems.js"
5
+ export * from "./parts/execute.js"
6
6
  export * from "./parts/lifecycle.js"
7
7
  export * from "./parts/make-id.js"
8
8
  export * from "./parts/types.js"
@@ -0,0 +1,37 @@
1
+
2
+ import {Entities} from "./entities.js"
3
+ import {DeltaSet, Delta, Components, DeltaKind, DeltaMerge, DeltaDrop} from "./types.js"
4
+
5
+ export function applyDelta<C extends Components>(entities: Entities<C>, delta: Delta<C>) {
6
+ switch (delta[0]) {
7
+ case DeltaKind.Set: return applySet<C>(entities, <DeltaSet<C>>delta)
8
+ case DeltaKind.Merge: return applyMerge<C>(entities, <DeltaMerge<C>>delta)
9
+ case DeltaKind.Drop: return applyDrop<C>(entities, <DeltaDrop<C>>delta)
10
+ default: throw new Error(`unknown delta kind "${delta[0]}"`)
11
+ }
12
+ }
13
+
14
+ function applySet<C extends Components>(entities: Entities<C>, [, id, components]: DeltaSet<C>) {
15
+ if (components) entities.set(id, components as Partial<C>)
16
+ else entities.delete(id)
17
+ return id
18
+ }
19
+
20
+ function applyMerge<C extends Components>(entities: Entities<C>, [, id, patch]: DeltaMerge<C>) {
21
+ const components = entities.get(id)
22
+ if (!components)
23
+ return id
24
+ Object.assign(components, patch)
25
+ entities.set(id, components)
26
+ return id
27
+ }
28
+
29
+ function applyDrop<C extends Components>(entities: Entities<C>, [, id, keys]: DeltaDrop<C>) {
30
+ const components = entities.get(id)
31
+ if (!components)
32
+ return id
33
+ for (const key of keys) delete components[key]
34
+ entities.set(id, components)
35
+ return id
36
+ }
37
+
@@ -1,27 +1,39 @@
1
1
 
2
2
  import {makeId} from "./make-id.js"
3
- import {Components, ChangeSet, ChangeMerge, Id, ChangeKind, ChangeDrop} from "./types.js"
3
+ import {Components, Id, DeltaKind, Delta} from "./types.js"
4
4
 
5
- export const change = {
5
+ export class Change<C extends Components> {
6
+ constructor(private commit: (delta: Delta<C>) => void) {}
6
7
 
7
8
  /** create a new entity with the given components */
8
- create: (components: Partial<Components>): ChangeSet<Components> =>
9
- [ChangeKind.Set, makeId(), components],
9
+ create(components: Partial<C>) {
10
+ const id = makeId()
11
+ this.commit([DeltaKind.Set, id, components])
12
+ return id
13
+ }
10
14
 
11
15
  /** overwrite a whole entity to the given components */
12
- set: (id: Id, components: Partial<Components>): ChangeSet<Components> =>
13
- [ChangeKind.Set, id, components],
16
+ set(id: Id, components: Partial<C>) {
17
+ this.commit([DeltaKind.Set, id, components])
18
+ return id
19
+ }
14
20
 
15
21
  /** remove the entity */
16
- delete: (id: Id): ChangeSet<Components> =>
17
- [ChangeKind.Set, id],
22
+ delete(id: Id) {
23
+ this.commit([DeltaKind.Set, id])
24
+ return id
25
+ }
18
26
 
19
27
  /** update or add the given components onto the entity */
20
- merge: (id: Id, components: Partial<Components>): ChangeMerge<Components> =>
21
- [ChangeKind.Merge, id, components],
28
+ merge(id: Id, components: Partial<C>) {
29
+ this.commit([DeltaKind.Merge, id, components])
30
+ return id
31
+ }
22
32
 
23
33
  /** delete specific components off the entity */
24
- drop: <C extends Components>(id: Id, ...keys: (keyof C)[]): ChangeDrop<C> =>
25
- [ChangeKind.Drop, id, keys],
34
+ drop(id: Id, ...keys: (keyof C)[]) {
35
+ this.commit([DeltaKind.Drop, id, keys])
36
+ return id
37
+ }
26
38
  }
27
39
 
@@ -41,7 +41,7 @@ export class Entities<C extends Components> extends GMap<Id, Partial<C>> {
41
41
  yield* this.#makeCache(componentKeys)
42
42
  }
43
43
 
44
- readonly() {
44
+ get readonly() {
45
45
  return this as EntitiesReadonly<C>
46
46
  }
47
47
 
@@ -0,0 +1,29 @@
1
+
2
+ import {Change} from "./change.js"
3
+ import {Entities} from "./entities.js"
4
+ import {applyDelta} from "./apply-delta.js"
5
+ import {Delta, Components, Systems} from "./types.js"
6
+
7
+ export function makeExecute<C extends Components>(
8
+ entities: Entities<C>,
9
+ systems: Systems<C>,
10
+ ) {
11
+
12
+ let deltas: Delta<C>[] = []
13
+
14
+ const change = new Change<C>(delta => {
15
+ applyDelta(entities, delta)
16
+ deltas.push(delta)
17
+ })
18
+
19
+ const fns = systems(change)
20
+
21
+ return () => {
22
+ for (const fn of fns)
23
+ fn()
24
+ const ret = deltas
25
+ deltas = []
26
+ return ret
27
+ }
28
+ }
29
+
@@ -10,21 +10,22 @@ export function lifecycle<C extends Components, K extends keyof C>(
10
10
  ) {
11
11
 
12
12
  const alive = new GMap<Id, LifecycleCallbacks<C, K>>()
13
- const sel = () => entities.select(...componentKeys)
14
13
 
15
- return function*() {
16
- const current = new Map(sel())
14
+ return () => {
17
15
 
16
+ // add fresh entities
17
+ for (const [id, components] of entities.select(...componentKeys)) {
18
+ const callbacks = alive.guarantee(id, () => enter(id, components))
19
+ callbacks.tick(id, components)
20
+ }
21
+
22
+ // delete stale entities
23
+ const currentIds = new Set([...entities.select(...componentKeys)].map(([id]) => id))
18
24
  for (const [id, callbacks] of alive) {
19
- if (current.has(id)) continue
25
+ if (currentIds.has(id)) continue
20
26
  alive.delete(id)
21
27
  callbacks.exit(id)
22
28
  }
23
-
24
- for (const [id, components] of current) {
25
- const callbacks = alive.guarantee(id, () => enter(id, components))
26
- callbacks.tick(id, components)
27
- }
28
29
  }
29
30
  }
30
31
 
@@ -1,18 +1,19 @@
1
1
 
2
+ import {Change} from "./change.js"
3
+
2
4
  export type Id = string
3
5
  export type Components = Record<string, unknown>
4
6
  export type AsComponents<C extends Components> = C
5
7
  export type Select<C extends Components, K extends keyof C> = Pick<C, K> & Partial<C>
6
- export type System<C extends Components> = () => Generator<Change<C>>
7
8
 
8
- export const asSystem = <C extends Components>(system: System<C>) => system
9
- export const asSystems = <C extends Components>(...systems: System<C>[]) => systems
9
+ export enum DeltaKind {Set, Merge, Drop}
10
+ export type DeltaSet<C extends Components> = [kind: DeltaKind.Set, id: Id, components?: Partial<C>]
11
+ export type DeltaMerge<C extends Components> = [kind: DeltaKind.Merge, id: Id, patch: Partial<C>]
12
+ export type DeltaDrop<C extends Components> = [kind: DeltaKind.Drop, id: Id, keys: (keyof C)[]]
13
+ export type Delta<C extends Components> = DeltaSet<C> | DeltaMerge<C> | DeltaDrop<C>
10
14
 
11
- export enum ChangeKind {Set, Merge, Drop}
12
- export type ChangeSet<C extends Components> = [kind: ChangeKind.Set, id: Id, components?: Partial<C>]
13
- export type ChangeMerge<C extends Components> = [kind: ChangeKind.Merge, id: Id, patch: Partial<C>]
14
- export type ChangeDrop<C extends Components> = [kind: ChangeKind.Drop, id: Id, keys: (keyof C)[]]
15
- export type Change<C extends Components> = ChangeSet<C> | ChangeMerge<C> | ChangeDrop<C>
15
+ export type Systems<C extends Components> = (change: Change<C>) => (() => void)[]
16
+ export const asSystems = <C extends Components>(systems: Systems<C>) => systems
16
17
 
17
18
  export type LifecycleCallbacks<C extends Components, K extends keyof C> = {
18
19
  tick: (id: Id, components: Select<C, K>) => void
@@ -1,49 +1,54 @@
1
1
 
2
- import {change} from "../parts/change.js"
2
+ import {Change} from "../parts/change.js"
3
3
  import {asSystems} from "../parts/types.js"
4
4
  import {Entities} from "../parts/entities.js"
5
+ import {makeExecute} from "../parts/execute.js"
6
+ import {applyDelta} from "../parts/apply-delta.js"
7
+
8
+ export type ExampleComponents = {
9
+ health: number
10
+ bleed: number
11
+ mana: number
12
+ manaRegen: number
13
+ }
5
14
 
6
15
  export function setupExample() {
7
- type MyComponents = {
8
- health: number
9
- bleed: number
10
- mana: number
11
- manaRegen: number
12
- }
13
-
14
- const writableEntities = new Entities<MyComponents>()
15
- const entities = writableEntities.readonly()
16
-
17
- const systems = asSystems<MyComponents>(
18
- function* manaRegen() {
19
- for (const [id, components] of entities.select("mana", "manaRegen")) {
16
+ const entities = new Entities<ExampleComponents>()
17
+ const rentities = entities.readonly
18
+
19
+ const systems = asSystems<ExampleComponents>(change => [
20
+ function manaRegen() {
21
+ for (const [id, components] of rentities.select("mana", "manaRegen")) {
20
22
  if (components.manaRegen !== 0) {
21
23
  const mana = components.mana + components.manaRegen
22
- yield change.merge(id, {mana})
24
+ change.merge(id, {mana})
23
25
  }
24
26
  }
25
27
  },
26
28
 
27
- function* bleeding() {
28
- for (const [id, components] of entities.select("health", "bleed")) {
29
+ function bleeding() {
30
+ for (const [id, components] of rentities.select("health", "bleed")) {
29
31
  if (components.bleed >= 0) {
30
32
  const health = components.health - components.bleed
31
33
  const bleed = components.bleed - 1
32
- yield change.merge(id, {health, bleed})
34
+ change.merge(id, {health, bleed})
33
35
  }
34
36
  if (components.bleed <= 0)
35
- yield change.drop(id, "bleed")
37
+ change.drop(id, "bleed")
36
38
  }
37
39
  },
38
40
 
39
- function* death() {
40
- for (const [id, components] of entities.select("health")) {
41
+ function death() {
42
+ for (const [id, components] of rentities.select("health")) {
41
43
  if (components.health <= 0)
42
- yield change.delete(id)
44
+ change.delete(id)
43
45
  }
44
46
  },
45
- )
47
+ ])
48
+
49
+ const change = new Change<ExampleComponents>(delta => applyDelta(entities, delta))
50
+ const execute = makeExecute(entities, systems)
46
51
 
47
- return {entities: writableEntities, systems}
52
+ return {systems, entities, change, execute}
48
53
  }
49
54
 
@@ -6,10 +6,15 @@ export function setupLifecycleCounts() {
6
6
  enters: 0,
7
7
  ticks: 0,
8
8
  exits: 0,
9
- expect: (enters: number, ticks: number, exits: number) => {
10
- expect(counts.enters).is(enters)
11
- expect(counts.ticks).is(ticks)
12
- expect(counts.exits).is(exits)
9
+ expect: (e: {enters: number, ticks: number, exits: number}) => {
10
+ expect(counts.enters, `enters, expected ${e.enters}, got ${counts.enters}`)
11
+ .is(e.enters)
12
+
13
+ expect(counts.ticks, `ticks, expected ${e.ticks}, got ${counts.ticks}`)
14
+ .is(e.ticks)
15
+
16
+ expect(counts.exits, `exits, expected ${e.exits}, got ${counts.exits}`)
17
+ .is(e.exits)
13
18
  },
14
19
  }
15
20
  return counts
package/s/ecs/test.ts CHANGED
@@ -1,126 +1,179 @@
1
1
 
2
2
  import {suite, test, expect} from "@e280/science"
3
- import {change} from "./parts/change.js"
3
+ import {asSystems} from "./parts/types.js"
4
4
  import {lifecycle} from "./parts/lifecycle.js"
5
- import {applyChange} from "./parts/apply-change.js"
6
- import {setupExample} from "./test/setup-example.js"
7
- import {executeSystems} from "./parts/execute-systems.js"
5
+ import {makeExecute} from "./parts/execute.js"
8
6
  import {setupLifecycleCounts} from "./test/setup-lifecycle-counts.js"
7
+ import {ExampleComponents, setupExample} from "./test/setup-example.js"
9
8
 
10
9
  export default suite({
11
10
  "create an entity": test(async() => {
12
- const {entities} = setupExample()
11
+ const {entities, change} = setupExample()
13
12
  expect(entities.size).is(0)
14
- applyChange(entities, change.create({health: 100}))
13
+ change.create({health: 100})
15
14
  expect(entities.size).is(1)
16
15
  }),
17
16
 
18
17
  "delete an entity": test(async() => {
19
- const {entities} = setupExample()
20
- const id = applyChange(entities, change.create({health: 100}))
18
+ const {entities, change} = setupExample()
19
+ const id = change.create({health: 100})
21
20
  expect(entities.size).is(1)
22
- applyChange(entities, change.delete(id))
21
+ change.delete(id)
23
22
  expect(entities.size).is(0)
24
23
  }),
25
24
 
26
25
  "merge components into entity": test(async() => {
27
- const {entities} = setupExample()
28
- const id = applyChange(entities, change.create({health: 100, mana: 100}))
26
+ const {entities, change} = setupExample()
27
+ const id = change.create({health: 100, mana: 100})
29
28
  expect(entities.require(id).health).is(100)
30
- applyChange(entities, change.merge(id, {health: 99}))
29
+ change.merge(id, {health: 99})
31
30
  expect(entities.require(id).health).is(99)
32
31
  expect(entities.require(id).mana).is(100)
33
32
  }),
34
33
 
34
+ "ignore merge after delete": test(async() => {
35
+ const {entities, change} = setupExample()
36
+ const id = change.create({health: 100, mana: 100})
37
+ expect(entities.get(id)).ok()
38
+ change.delete(id)
39
+ expect(entities.get(id)).not.ok()
40
+ change.merge(id, {health: 99})
41
+ expect(entities.get(id)).not.ok()
42
+ }),
43
+
44
+ "ignore drop after delete": test(async() => {
45
+ const {entities, change} = setupExample()
46
+ const id = change.create({health: 100, mana: 100})
47
+ expect(entities.get(id)).ok()
48
+ change.delete(id)
49
+ expect(entities.get(id)).not.ok()
50
+ change.drop(id, "health")
51
+ expect(entities.get(id)).not.ok()
52
+ }),
53
+
35
54
  "select an entity": test(async() => {
36
- const {entities} = setupExample()
37
- applyChange(entities, change.create({health: 100}))
55
+ const {entities, change} = setupExample()
56
+ change.create({health: 100})
38
57
  expect([...entities.select("health")].length).is(1)
39
58
  }),
40
59
 
41
60
  "drop components from entity": test(async() => {
42
- const {entities} = setupExample()
43
- const id = applyChange(entities, change.create({health: 100, mana: 100}))
61
+ const {entities, change} = setupExample()
62
+ const id = change.create({health: 100, mana: 100})
44
63
  expect([...entities.select("health", "mana")].length).is(1)
45
- applyChange(entities, change.drop(id, "mana"))
64
+ change.drop(id, "mana")
46
65
  expect("mana" in entities.require(id)).is(false)
47
66
  expect([...entities.select("health", "mana")].length).is(0)
48
67
  }),
49
68
 
50
69
  "select two entities": test(async() => {
51
- const {entities} = setupExample()
52
- applyChange(entities, change.create({health: 100}))
53
- applyChange(entities, change.create({health: 100}))
70
+ const {entities, change} = setupExample()
71
+ change.create({health: 100})
72
+ change.create({health: 100})
54
73
  expect([...entities.select("health")].length).is(2)
55
74
  }),
56
75
 
57
76
  "select with no component keys selects all": test(async() => {
58
- const {entities} = setupExample()
59
- applyChange(entities, change.create({health: 100}))
60
- applyChange(entities, change.create({health: 100}))
77
+ const {entities, change} = setupExample()
78
+ change.create({health: 100})
79
+ change.create({health: 100})
61
80
  expect([...entities.select()].length).is(2)
62
81
  }),
63
82
 
64
83
  "select doesn't include non-match": test(async() => {
65
- const {entities} = setupExample()
66
- applyChange(entities, change.create({health: 100}))
84
+ const {entities, change} = setupExample()
85
+ change.create({health: 100})
67
86
  expect([...entities.select("mana")].length).is(0)
68
87
  }),
69
88
 
70
89
  "select includes entities with extra components": test(async() => {
71
- const {entities} = setupExample()
72
- applyChange(entities, change.create({health: 100, mana: 100}))
90
+ const {entities, change} = setupExample()
91
+ change.create({health: 100, mana: 100})
73
92
  expect([...entities.select("health")].length).is(1)
74
93
  }),
75
94
 
76
95
  "wizard regens mana": test(async() => {
77
- const {entities, systems} = setupExample()
78
- const creating = change.create({health: 100, mana: 50, manaRegen: 1})
79
- const wizardId = creating[1]
80
- applyChange(entities, creating)
81
- const changes = executeSystems(entities, systems)
96
+ const {entities, change, execute} = setupExample()
97
+ const wizardId = change.create({health: 100, mana: 50, manaRegen: 1})
98
+ const changes = execute()
82
99
  expect(changes.length).is(1)
83
100
  expect(entities.require(wizardId).mana).is(51)
84
101
  }),
85
102
 
86
103
  "death by bleeding": test(async() => {
87
- const {entities, systems} = setupExample()
88
- const creating = change.create({health: 3, bleed: 2})
89
- const wizardId = creating[1]
90
- applyChange(entities, creating)
104
+ const {entities, change, execute} = setupExample()
105
+ const wizardId = change.create({health: 3, bleed: 2})
91
106
  expect(entities.require(wizardId).health).is(3)
92
- executeSystems(entities, systems)
107
+ execute()
93
108
  expect(entities.require(wizardId).health).is(1)
94
- executeSystems(entities, systems)
109
+ execute()
95
110
  expect(entities.has(wizardId)).is(false)
96
111
  }),
97
112
 
98
113
  "lifecycles": test(async() => {
99
- const {entities} = setupExample()
114
+ const {entities, change} = setupExample()
100
115
  const counts = setupLifecycleCounts()
101
- const system = lifecycle(entities.readonly(), ["health"], () => {
102
- counts.enters++
103
- return {
104
- tick: () => void counts.ticks++,
105
- exit: () => void counts.exits++,
106
- }
107
- })
108
- counts.expect(0, 0, 0)
109
-
110
- const creating = change.create({health: 100, mana: 50})
111
- const wizardId = creating[1]
112
- applyChange(entities, creating)
113
- executeSystems(entities, [system])
114
- counts.expect(1, 1, 0)
115
-
116
- applyChange(entities, change.merge(wizardId, {health: 100, mana: 100}))
117
- executeSystems(entities, [system])
118
- counts.expect(1, 2, 0)
119
-
120
- applyChange(entities, change.delete(wizardId))
121
- executeSystems(entities, [system])
122
- counts.expect(1, 2, 1)
116
+ const systems = asSystems<ExampleComponents>(() => [
117
+ lifecycle(entities.readonly, ["health"], () => {
118
+ counts.enters++
119
+ return {
120
+ tick: () => void counts.ticks++,
121
+ exit: () => void counts.exits++,
122
+ }
123
+ }),
124
+ ])
125
+ const execute = makeExecute(entities, systems)
126
+ counts.expect({enters: 0, ticks: 0, exits: 0})
127
+
128
+ const wizardId = change.create({health: 100, mana: 50})
129
+ execute()
130
+ counts.expect({enters: 1, ticks: 1, exits: 0})
131
+
132
+ change.merge(wizardId, {health: 100, mana: 100})
133
+ execute()
134
+ counts.expect({enters: 1, ticks: 2, exits: 0})
135
+
136
+ change.delete(wizardId)
137
+ execute()
138
+ counts.expect({enters: 1, ticks: 2, exits: 1})
123
139
  expect(entities.size).is(0)
124
140
  }),
141
+
142
+ "lifecycle can commit changes": test(async() => {
143
+ const {entities, change} = setupExample()
144
+ const systems = asSystems<ExampleComponents>(change => [
145
+ lifecycle(entities.readonly, ["health"], () => {
146
+ change.create({mana: 50})
147
+ return {
148
+ tick: () => {},
149
+ exit: () => {},
150
+ }
151
+ }),
152
+ ])
153
+ const execute = makeExecute(entities, systems)
154
+ change.create({health: 100})
155
+ execute()
156
+ expect([...entities.select("mana")].length).is(1)
157
+ }),
158
+
159
+ "lifecycle self-deletion immediate cleanup": test(async() => {
160
+ const {entities, change} = setupExample()
161
+ let ranExit = 0
162
+ const systems = asSystems<ExampleComponents>(change => [
163
+ lifecycle(entities.readonly, ["health"], (id) => {
164
+ return {
165
+ tick: () => change.delete(id),
166
+ exit: () => { ranExit++ },
167
+ }
168
+ }),
169
+ ])
170
+ const execute = makeExecute(entities, systems)
171
+ change.create({health: 100})
172
+ expect([...entities.select("health")].length).is(1)
173
+ expect(ranExit).is(0)
174
+ execute()
175
+ expect(ranExit).is(1)
176
+ expect([...entities.select("health")].length).is(0)
177
+ }),
125
178
  })
126
179
 
package/x/ecs/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- export * from "./parts/apply-change.js";
1
+ export * from "./parts/apply-delta.js";
2
2
  export * from "./parts/change.js";
3
3
  export * from "./parts/entities.js";
4
- export * from "./parts/execute-systems.js";
4
+ export * from "./parts/execute.js";
5
5
  export * from "./parts/lifecycle.js";
6
6
  export * from "./parts/make-id.js";
7
7
  export * from "./parts/types.js";
package/x/ecs/index.js CHANGED
@@ -1,7 +1,7 @@
1
- export * from "./parts/apply-change.js";
1
+ export * from "./parts/apply-delta.js";
2
2
  export * from "./parts/change.js";
3
3
  export * from "./parts/entities.js";
4
- export * from "./parts/execute-systems.js";
4
+ export * from "./parts/execute.js";
5
5
  export * from "./parts/lifecycle.js";
6
6
  export * from "./parts/make-id.js";
7
7
  export * from "./parts/types.js";