Skip to content

Commit b2191c3

Browse files
committed
refactor(world): update registerSystem to accept additional dependencies
- Modified the registerSystem method to allow optional dependencies when registering a system. - Updated the SystemScheduler to handle additional dependencies for better system management.
1 parent c391b77 commit b2191c3

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ bun run examples/simple/demo.ts
176176
- `delete(entity, componentId)`: 从实体移除组件
177177
- `setExclusive(componentId)`: 将组件标记为独占关系
178178
- `createQuery(componentIds)`: 创建查询
179-
- `registerSystem(system)`: 注册系统
179+
- `registerSystem(system, dependencies?)`: 注册系统
180180
- `registerLifecycleHook(componentId, hook)`: 注册组件或通配符关系生命周期钩子
181181
- `unregisterLifecycleHook(componentId, hook)`: 注销组件或通配符关系生命周期钩子
182182
- `update(...params)`: 更新世界(参数取决于泛型配置)
@@ -289,7 +289,7 @@ class MovementSystem implements System<[deltaTime: number]> {
289289
}
290290
```
291291

292-
系统支持依赖关系排序,确保正确的执行顺序。依赖关系通过系统的 `dependencies` 属性指定:
292+
系统支持依赖关系排序,确保正确的执行顺序。依赖关系可以通过系统的 `dependencies` 属性指定:
293293

294294
```typescript
295295
class InputSystem implements System<[deltaTime: number]> {
@@ -314,7 +314,7 @@ class MovementSystem implements System<[deltaTime: number]> {
314314
// 注册系统
315315
const inputSystem = new InputSystem();
316316
world.registerSystem(inputSystem);
317-
world.registerSystem(new MovementSystem(inputSystem));
317+
world.registerSystem(new MovementSystem(inputSystem), [inputSystem]); // 也可以在注册时指定额外依赖
318318
```
319319

320320
系统将按照拓扑排序执行,依赖系统始终在被依赖系统之前运行。

src/system-scheduler.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ import type { System } from "./system";
55
*/
66
export class SystemScheduler<UpdateParams extends any[] = [deltaTime: number]> {
77
private systems = new Set<System<UpdateParams>>();
8+
private systemDependencies = new Map<System<UpdateParams>, Set<System<UpdateParams>>>();
89
private cachedExecutionOrder: System<UpdateParams>[] | null = null;
910

1011
/**
1112
* Add a system with optional dependencies
1213
* @param system The system to add
14+
* @param additionalDeps Additional dependencies for the system
1315
*/
14-
addSystem(system: System<UpdateParams>): void {
16+
addSystem(system: System<UpdateParams>, additionalDeps: System<UpdateParams>[] = []): void {
1517
this.systems.add(system);
1618
// Also add dependencies to the set
1719
for (const dep of system.dependencies || []) {
1820
this.systems.add(dep);
1921
}
22+
this.systemDependencies.set(system, new Set([...additionalDeps, ...(system.dependencies || [])]));
2023
this.cachedExecutionOrder = null;
2124
}
2225

@@ -41,7 +44,7 @@ export class SystemScheduler<UpdateParams extends any[] = [deltaTime: number]> {
4144

4245
visiting.add(system);
4346

44-
for (const dep of system.dependencies || []) {
47+
for (const dep of this.systemDependencies.get(system) || []) {
4548
visit(dep);
4649
}
4750

src/world.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ export class World<UpdateParams extends any[] = []> {
283283
/**
284284
* Register a system with optional dependencies
285285
*/
286-
registerSystem(system: System<UpdateParams>): void {
287-
this.systemScheduler.addSystem(system);
286+
registerSystem(system: System<UpdateParams>, additionalDeps: System<UpdateParams>[] = []): void {
287+
this.systemScheduler.addSystem(system, additionalDeps);
288288
}
289289

290290
/**

0 commit comments

Comments
 (0)