@georgeluo/ecs 0.1.1 → 0.1.2
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.
- package/README.md +87 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -51,7 +51,81 @@ import {
|
|
|
51
51
|
} from '@georgeluo/ecs';
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
##
|
|
54
|
+
## Quick Start (SimulationPlayer)
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
import {
|
|
58
|
+
EntityManager,
|
|
59
|
+
ComponentManager,
|
|
60
|
+
SystemManager,
|
|
61
|
+
SimulationPlayer,
|
|
62
|
+
Bus,
|
|
63
|
+
FrameFilter,
|
|
64
|
+
TimeSystem,
|
|
65
|
+
type Frame,
|
|
66
|
+
type Acknowledgement,
|
|
67
|
+
} from '@georgeluo/ecs';
|
|
68
|
+
|
|
69
|
+
const entities = new EntityManager();
|
|
70
|
+
const components = new ComponentManager();
|
|
71
|
+
const systems = new SystemManager(entities, components);
|
|
72
|
+
const inbound = new Bus<unknown>();
|
|
73
|
+
const outbound = new Bus<Frame | Acknowledgement>();
|
|
74
|
+
const player = new SimulationPlayer(systems, inbound, outbound, new FrameFilter());
|
|
75
|
+
|
|
76
|
+
const systemId = player.injectSystem({ system: new TimeSystem() });
|
|
77
|
+
outbound.subscribe((message) => {
|
|
78
|
+
console.log('outbound:', message);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
player.start();
|
|
82
|
+
setTimeout(() => player.pause(), 200);
|
|
83
|
+
setTimeout(() => player.stop(), 500);
|
|
84
|
+
setTimeout(() => player.ejectSystem({ systemId }), 800);
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Control Simulation via Inbound Bus
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
import { SimulationMessageType } from '@georgeluo/ecs';
|
|
91
|
+
|
|
92
|
+
inbound.publish({ type: SimulationMessageType.START, payload: { messageId: 'm-1' } });
|
|
93
|
+
inbound.publish({ type: SimulationMessageType.PAUSE, payload: { messageId: 'm-2' } });
|
|
94
|
+
inbound.publish({ type: SimulationMessageType.STOP, payload: { messageId: 'm-3' } });
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Custom Component + System
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
import { System, type SystemContext, type ComponentType } from '@georgeluo/ecs';
|
|
101
|
+
|
|
102
|
+
const TemperatureComponent: ComponentType<{ value: number }> = {
|
|
103
|
+
id: 'temperature',
|
|
104
|
+
validate: (payload) => Number.isFinite(payload?.value),
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
class TemperatureSystem extends System {
|
|
108
|
+
private entity: number | null = null;
|
|
109
|
+
|
|
110
|
+
initialize(context: SystemContext): void {
|
|
111
|
+
this.entity = context.entityManager.create();
|
|
112
|
+
context.componentManager.addComponent(this.entity, TemperatureComponent, { value: 72 });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
update(context: SystemContext): void {
|
|
116
|
+
if (this.entity === null) {
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
const current = context.componentManager.getComponent(this.entity, TemperatureComponent);
|
|
120
|
+
const nextValue = (current?.payload.value ?? 72) + 0.25;
|
|
121
|
+
context.componentManager.addComponent(this.entity, TemperatureComponent, { value: nextValue });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
player.injectSystem({ system: new TemperatureSystem() });
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## EvaluationPlayer: Inject Systems
|
|
55
129
|
|
|
56
130
|
```ts
|
|
57
131
|
const entities = new EntityManager();
|
|
@@ -65,7 +139,7 @@ const systemId = player.injectSystem({ system: myEvaluationSystem });
|
|
|
65
139
|
player.ejectSystem({ systemId });
|
|
66
140
|
```
|
|
67
141
|
|
|
68
|
-
## Inject
|
|
142
|
+
## EvaluationPlayer: Inject Frames
|
|
69
143
|
|
|
70
144
|
```ts
|
|
71
145
|
inbound.publish({
|
|
@@ -85,3 +159,14 @@ player.injectFrame({
|
|
|
85
159
|
frame: { tick: 1, entities: {} },
|
|
86
160
|
});
|
|
87
161
|
```
|
|
162
|
+
|
|
163
|
+
## API Surface
|
|
164
|
+
|
|
165
|
+
- Managers:
|
|
166
|
+
- `EntityManager`, `ComponentManager`, `SystemManager`
|
|
167
|
+
- Players:
|
|
168
|
+
- `Player`, `IOPlayer`, `SimulationPlayer`, `EvaluationPlayer`
|
|
169
|
+
- Base model:
|
|
170
|
+
- `System`, `SystemContext`, `ComponentType`, `Frame`, `Acknowledgement`
|
|
171
|
+
- Messaging:
|
|
172
|
+
- `Bus`, `FrameFilter`, `SimulationMessageType`, `EvaluationMessageType`
|