Skip to content

Commit 0898d91

Browse files
committed
refactor(entity): replace createComponentId with component function
1 parent cfd3ea2 commit 0898d91

4 files changed

Lines changed: 22 additions & 16 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ bun install
2323

2424
```typescript
2525
import { World } from "@codehz/ecs";
26-
import { createComponentId } from "@codehz/ecs";
26+
import { component } from "@codehz/ecs";
2727

2828
// 定义组件类型
2929
type Position = { x: number; y: number };
3030
type Velocity = { x: number; y: number };
3131

3232
// 定义组件ID
33-
const PositionId = createComponentId<Position>(1);
34-
const VelocityId = createComponentId<Velocity>(2);
33+
const PositionId = component<Position>(1);
34+
const VelocityId = component<Velocity>(2);
3535

3636
// 创建世界
3737
const world = new World();
@@ -85,13 +85,13 @@ world.flushCommands(); // 钩子在这里被调用
8585
ECS 还支持通配符关系生命周期钩子,可以监听特定组件的所有关系变化:
8686

8787
```typescript
88-
import { World, createComponentId, relation } from "@codehz/ecs";
88+
import { World, component, relation } from "@codehz/ecs";
8989

9090
// 定义组件类型
9191
type Position = { x: number; y: number };
9292

9393
// 定义组件ID
94-
const PositionId = createComponentId<Position>(1);
94+
const PositionId = component<Position>(1);
9595

9696
// 创建世界
9797
const world = new World();
@@ -147,7 +147,7 @@ bun run examples/simple/demo.ts
147147

148148
### Entity
149149

150-
- `createComponentId<T>(id)`: 创建类型安全的组件ID
150+
- `component<T>(id)`: 分配类型安全的组件ID(上限:1022个)
151151

152152
### Query
153153

examples/simple/demo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createComponentId } from "../../src/entity";
1+
import { component } from "../../src/entity";
22
import type { Query } from "../../src/query";
33
import type { System } from "../../src/system";
44
import { World } from "../../src/world";
@@ -8,8 +8,8 @@ type Position = { x: number; y: number };
88
type Velocity = { x: number; y: number };
99

1010
// 定义组件ID
11-
const PositionId = createComponentId<Position>(1);
12-
const VelocityId = createComponentId<Velocity>(2);
11+
const PositionId = component<Position>();
12+
const VelocityId = component<Velocity>();
1313

1414
// 移动系统
1515
class MovementSystem implements System {

src/entity.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from "bun:test";
22
import type { ComponentId, EntityId } from "./entity";
33
import {
44
COMPONENT_ID_MAX,
5-
ComponentIdManager,
5+
ComponentIdAllocator,
66
createComponentId,
77
createEntityId,
88
relation,
@@ -373,22 +373,22 @@ describe("EntityIdManager", () => {
373373
describe("ComponentIdManager", () => {
374374
describe("Allocation", () => {
375375
it("should allocate sequential component IDs starting from 1", () => {
376-
const manager = new ComponentIdManager();
376+
const manager = new ComponentIdAllocator();
377377
expect(manager.allocate()).toBe(createComponentId(1));
378378
expect(manager.allocate()).toBe(createComponentId(2));
379379
expect(manager.allocate()).toBe(createComponentId(3));
380380
});
381381

382382
it("should allocate up to COMPONENT_ID_MAX", () => {
383-
const manager = new ComponentIdManager();
383+
const manager = new ComponentIdAllocator();
384384
for (let i = 1; i <= COMPONENT_ID_MAX; i++) {
385385
expect(manager.allocate()).toBe(createComponentId(i));
386386
}
387387
expect(manager.hasAvailableIds()).toBe(false);
388388
});
389389

390390
it("should throw error when exceeding maximum component IDs", () => {
391-
const manager = new ComponentIdManager();
391+
const manager = new ComponentIdAllocator();
392392
// Allocate all available IDs
393393
for (let i = 1; i <= COMPONENT_ID_MAX; i++) {
394394
manager.allocate();
@@ -399,7 +399,7 @@ describe("ComponentIdManager", () => {
399399

400400
describe("State Queries", () => {
401401
it("should report correct next ID", () => {
402-
const manager = new ComponentIdManager();
402+
const manager = new ComponentIdAllocator();
403403
expect(manager.getNextId()).toBe(1);
404404
manager.allocate();
405405
expect(manager.getNextId()).toBe(2);
@@ -408,7 +408,7 @@ describe("ComponentIdManager", () => {
408408
});
409409

410410
it("should correctly report available IDs", () => {
411-
const manager = new ComponentIdManager();
411+
const manager = new ComponentIdAllocator();
412412
expect(manager.hasAvailableIds()).toBe(true);
413413

414414
// Allocate all but one

src/entity.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export const WILDCARD_TARGET_ID = 0;
4242
/**
4343
* Create a component ID
4444
* @param id Component identifier (1-1023)
45+
* @see component
4546
*/
4647
export function createComponentId<T = void>(id: number): ComponentId<T> {
4748
if (id < 1 || id > COMPONENT_ID_MAX) {
@@ -365,7 +366,7 @@ export class EntityIdManager {
365366
* Component ID Manager for automatic allocation
366367
* Components are typically registered once and not recycled
367368
*/
368-
export class ComponentIdManager {
369+
export class ComponentIdAllocator {
369370
private nextId: number = 1;
370371

371372
/**
@@ -395,3 +396,8 @@ export class ComponentIdManager {
395396
return this.nextId <= COMPONENT_ID_MAX;
396397
}
397398
}
399+
400+
const globalComponentIdAllocator = new ComponentIdAllocator();
401+
export function component<T>(): ComponentId<T> {
402+
return globalComponentIdAllocator.allocate<T>();
403+
}

0 commit comments

Comments
 (0)