You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/core/README.md
+65-2Lines changed: 65 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -121,7 +121,7 @@ The library does nothing more than organizing the logic of your game into entity
121
121
122
122
The created `ECS` instance provides an `update` function that you can call each frame to update the game, using whatever other library or framework you prefer:
123
123
124
-
> Calling `update` will execute all the systems in the world one time. You are expected to call `update` once per frame (or whatever other frequency you prefer).
124
+
> Calling `update` will execute all the systems in the world **one time**. You are expected to call `update` once per frame (or whatever other frequency you prefer).
125
125
126
126
```ts
127
127
// Create a world for a snake game (add systems, create entities, etc.)
@@ -163,9 +163,72 @@ const world = ECS.create<SystemTags, GameEventMap>(
163
163
);
164
164
165
165
// Apply any rendering logic by executing the `update` function from `ECS`
166
-
renderer(world.update);
166
+
renderer((deltaTime) =>world.update(deltaTime));
167
167
```
168
168
169
+
### Communication between systems
170
+
Events are used to send messages between systems. Any system has access to the `emit` function to emit events:
171
+
172
+
> Events are type-safe and must be defined in the `GameEventMap` type.
173
+
174
+
```ts
175
+
exportconst FoodEatenEvent =Symbol("FoodEaten");
176
+
177
+
exportinterfaceGameEventMapextendsEventMap {
178
+
[FoodEatenEvent]: { entityId:EntityId };
179
+
}
180
+
```
181
+
182
+
You can then emit an event using the `emit` function:
type: FoodEatenEvent, // 👈 Emit the event from its unique symbol
190
+
data: { entityId: entity.entityId }, // 👈 Pass the entity that was eaten
191
+
});
192
+
}
193
+
},
194
+
}) {}
195
+
```
196
+
197
+
Other systems can use the `poll` function to extract events and react to them:
198
+
199
+
> Important: events are **cleaned up after each update cycle**. If you want to ensure an event was emitted before executing a system you can use `dependencies` (see below).
dependencies: ["Collision"], // 👈 Ensure the `Collision` system has been executed and events collected
204
+
execute: ({ poll }) => {
205
+
poll(FoodEatenEvent).forEach(({ entityId }) => {
206
+
// Do something with the event (`entityId`)
207
+
});
208
+
},
209
+
}) {}
210
+
```
211
+
212
+
### Systems dependencies
213
+
Sometimes you need to execute a system after another system.
214
+
215
+
For example, you might want to spawn food only after the snake has eaten it. This creates a dependency between the `FoodSpawnSystem` and the `CollisionSystem`: you first want to detect collisions, and then spawn food if a collision occurs.
216
+
217
+
You can define a dependency between two systems using the *optional*`dependencies` property:
0 commit comments