@codehz/ecs 0.0.7 → 0.0.10

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 CHANGED
@@ -177,7 +177,7 @@ bun run examples/simple/demo.ts
177
177
  - `registerSystem(system)`: 注册系统
178
178
  - `registerLifecycleHook(componentId, hook)`: 注册组件或通配符关系生命周期钩子
179
179
  - `unregisterLifecycleHook(componentId, hook)`: 注销组件或通配符关系生命周期钩子
180
- - `update(deltaTime)`: 更新世界
180
+ - `update(...params)`: 更新世界(参数取决于泛型配置)
181
181
  - `flushCommands()`: 应用命令缓冲区
182
182
 
183
183
  ### Entity
@@ -196,19 +196,48 @@ bun run examples/simple/demo.ts
196
196
 
197
197
  ```typescript
198
198
  class MySystem implements System {
199
- update(world: World, deltaTime: number): void {
199
+ update(): void {
200
200
  // 系统逻辑
201
201
  }
202
202
  }
203
203
  ```
204
204
 
205
- 系统支持依赖关系排序,确保正确的执行顺序:
205
+ 如果需要接收额外参数(如时间增量),可以指定泛型参数:
206
206
 
207
207
  ```typescript
208
- // 注册系统时指定依赖
208
+ class MovementSystem implements System<[deltaTime: number]> {
209
+ update(deltaTime: number): void {
210
+ // 使用 deltaTime 更新位置
211
+ }
212
+ }
213
+ ```
214
+
215
+ 系统支持依赖关系排序,确保正确的执行顺序。依赖关系通过系统的 `dependencies` 属性指定:
216
+
217
+ ```typescript
218
+ class InputSystem implements System<[deltaTime: number]> {
219
+ readonly dependencies: readonly System<[deltaTime: number]>[] = [];
220
+ update(deltaTime: number): void {
221
+ // 处理输入
222
+ }
223
+ }
224
+
225
+ class MovementSystem implements System<[deltaTime: number]> {
226
+ readonly dependencies: readonly System<[deltaTime: number]>[];
227
+
228
+ constructor(inputSystem: InputSystem) {
229
+ this.dependencies = [inputSystem]; // 指定依赖
230
+ }
231
+
232
+ update(deltaTime: number): void {
233
+ // 更新位置
234
+ }
235
+ }
236
+
237
+ // 注册系统
238
+ const inputSystem = new InputSystem();
209
239
  world.registerSystem(inputSystem);
210
- world.registerSystem(movementSystem, [inputSystem]); // movementSystem 依赖 inputSystem
211
- world.registerSystem(renderSystem, [movementSystem]); // renderSystem 依赖 movementSystem
240
+ world.registerSystem(new MovementSystem(inputSystem));
212
241
  ```
213
242
 
214
243
  系统将按照拓扑排序执行,依赖系统始终在被依赖系统之前运行。
package/entity.d.ts CHANGED
@@ -47,7 +47,7 @@ export declare function createEntityId(id: number): EntityId;
47
47
  /**
48
48
  * Type for relation ID based on component and target types
49
49
  */
50
- type RelationIdType<T, R> = R extends ComponentId<infer U> ? U extends void ? ComponentRelationId<T> : ComponentRelationId<T & U> : R extends EntityId<any> ? EntityRelationId<T> : never;
50
+ type RelationIdType<T, R> = R extends ComponentId<infer U> ? U extends void ? ComponentRelationId<T> : ComponentRelationId<T extends void ? U : T & U> : R extends EntityId<any> ? EntityRelationId<T> : never;
51
51
  /**
52
52
  * Create a relation ID by associating a component with another ID (entity or component)
53
53
  * @param componentId The component ID (0-1023)
package/index.js CHANGED
@@ -773,7 +773,7 @@ class World {
773
773
  update(...params) {
774
774
  const systems = this.systemScheduler.getExecutionOrder();
775
775
  for (const system of systems) {
776
- system.update(this, ...params);
776
+ system.update(...params);
777
777
  }
778
778
  this.commandBuffer.execute();
779
779
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codehz/ecs",
3
- "version": "0.0.7",
3
+ "version": "0.0.10",
4
4
  "type": "module",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",
@@ -2,19 +2,19 @@ import type { System } from "./system";
2
2
  /**
3
3
  * System Scheduler for managing system dependencies and execution order
4
4
  */
5
- export declare class SystemScheduler<ExtraParams extends any[] = [deltaTime: number]> {
5
+ export declare class SystemScheduler<UpdateParams extends any[] = [deltaTime: number]> {
6
6
  private systems;
7
7
  private cachedExecutionOrder;
8
8
  /**
9
9
  * Add a system with optional dependencies
10
10
  * @param system The system to add
11
11
  */
12
- addSystem(system: System<ExtraParams>): void;
12
+ addSystem(system: System<UpdateParams>): void;
13
13
  /**
14
14
  * Get the execution order of systems based on dependencies
15
15
  * Uses topological sort
16
16
  */
17
- getExecutionOrder(): System<ExtraParams>[];
17
+ getExecutionOrder(): System<UpdateParams>[];
18
18
  /**
19
19
  * Clear all systems and dependencies
20
20
  */
package/system.d.ts CHANGED
@@ -1,14 +1,13 @@
1
- import type { World } from "./world";
2
1
  /**
3
2
  * Base System interface
4
3
  */
5
- export interface System<ExtraParams extends any[] = [deltaTime: number]> {
4
+ export interface System<UpdateParams extends any[] = []> {
6
5
  /**
7
6
  * Update the system
8
7
  */
9
- update(world: World<ExtraParams>, ...params: ExtraParams): void;
8
+ update(...params: UpdateParams): void;
10
9
  /**
11
10
  * Dependencies of this system (systems that must run before this one)
12
11
  */
13
- readonly dependencies?: readonly System<ExtraParams>[];
12
+ readonly dependencies?: readonly System<UpdateParams>[];
14
13
  }
package/world.d.ts CHANGED
@@ -10,7 +10,7 @@ import type { ComponentTuple, LifecycleHook } from "./types";
10
10
  * World class for ECS architecture
11
11
  * Manages entities, components, and systems
12
12
  */
13
- export declare class World<ExtraParams extends any[] = [deltaTime: number]> {
13
+ export declare class World<UpdateParams extends any[] = []> {
14
14
  private entityIdManager;
15
15
  private archetypes;
16
16
  private archetypeMap;
@@ -85,7 +85,7 @@ export declare class World<ExtraParams extends any[] = [deltaTime: number]> {
85
85
  /**
86
86
  * Register a system with optional dependencies
87
87
  */
88
- registerSystem(system: System<ExtraParams>): void;
88
+ registerSystem(system: System<UpdateParams>): void;
89
89
  /**
90
90
  * Register a lifecycle hook for component or wildcard relation events
91
91
  */
@@ -102,7 +102,7 @@ export declare class World<ExtraParams extends any[] = [deltaTime: number]> {
102
102
  /**
103
103
  * Update the world (run all systems in dependency order)
104
104
  */
105
- update(...params: ExtraParams): void;
105
+ update(...params: UpdateParams): void;
106
106
  /**
107
107
  * Execute all deferred commands immediately without running systems
108
108
  */