@codehz/ecs 0.0.0
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/LICENSE +21 -0
- package/README.md +131 -0
- package/archetype.d.ts +107 -0
- package/command-buffer.d.ts +45 -0
- package/entity.d.ts +141 -0
- package/index.d.ts +5 -0
- package/index.js +756 -0
- package/package.json +16 -0
- package/query-filter.d.ts +16 -0
- package/query.d.ts +62 -0
- package/system.d.ts +10 -0
- package/types.d.ts +7 -0
- package/utils.d.ts +19 -0
- package/world.d.ts +106 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 codehz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# @codehz/ecs
|
|
2
|
+
|
|
3
|
+
一个高性能的Entity Component System (ECS) 库,使用 TypeScript 和 Bun 运行时构建。
|
|
4
|
+
|
|
5
|
+
## 特性
|
|
6
|
+
|
|
7
|
+
- 🚀 高性能:基于原型的组件存储和高效的查询系统
|
|
8
|
+
- 🔧 类型安全:完整的 TypeScript 支持
|
|
9
|
+
- 🏗️ 模块化:清晰的架构,支持自定义系统和组件
|
|
10
|
+
- 📦 轻量级:零依赖,易于集成
|
|
11
|
+
|
|
12
|
+
## 安装
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
bun install
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 用法
|
|
19
|
+
|
|
20
|
+
### 基本示例
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { World } from "@codehz/ecs";
|
|
24
|
+
import { createComponentId } from "@codehz/ecs";
|
|
25
|
+
|
|
26
|
+
// 定义组件类型
|
|
27
|
+
type Position = { x: number; y: number };
|
|
28
|
+
type Velocity = { x: number; y: number };
|
|
29
|
+
|
|
30
|
+
// 定义组件ID
|
|
31
|
+
const PositionId = createComponentId<Position>(1);
|
|
32
|
+
const VelocityId = createComponentId<Velocity>(2);
|
|
33
|
+
|
|
34
|
+
// 创建世界
|
|
35
|
+
const world = new World();
|
|
36
|
+
|
|
37
|
+
// 创建实体
|
|
38
|
+
const entity = world.createEntity();
|
|
39
|
+
world.addComponent(entity, PositionId, { x: 0, y: 0 });
|
|
40
|
+
world.addComponent(entity, VelocityId, { x: 1, y: 0.5 });
|
|
41
|
+
|
|
42
|
+
// 应用更改
|
|
43
|
+
world.flushCommands();
|
|
44
|
+
|
|
45
|
+
// 创建查询并更新
|
|
46
|
+
const query = world.createQuery([PositionId, VelocityId]);
|
|
47
|
+
query.forEach([PositionId, VelocityId], (entity, position, velocity) => {
|
|
48
|
+
position.x += velocity.x * deltaTime;
|
|
49
|
+
position.y += velocity.y * deltaTime;
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### 运行示例
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
bun run examples/simple/demo.ts
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## API 概述
|
|
60
|
+
|
|
61
|
+
### World
|
|
62
|
+
|
|
63
|
+
- `createEntity()`: 创建新实体
|
|
64
|
+
- `addComponent(entity, componentId, data)`: 向实体添加组件
|
|
65
|
+
- `removeComponent(entity, componentId)`: 从实体移除组件
|
|
66
|
+
- `createQuery(componentIds)`: 创建查询
|
|
67
|
+
- `registerSystem(system)`: 注册系统
|
|
68
|
+
- `update(deltaTime)`: 更新世界
|
|
69
|
+
- `flushCommands()`: 应用命令缓冲区
|
|
70
|
+
|
|
71
|
+
### Entity
|
|
72
|
+
|
|
73
|
+
- `createComponentId<T>(id)`: 创建类型安全的组件ID
|
|
74
|
+
|
|
75
|
+
### Query
|
|
76
|
+
|
|
77
|
+
- `forEach(componentIds, callback)`: 遍历匹配的实体
|
|
78
|
+
|
|
79
|
+
### System
|
|
80
|
+
|
|
81
|
+
实现 `System` 接口来创建自定义系统:
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
class MySystem implements System {
|
|
85
|
+
update(world: World, deltaTime: number): void {
|
|
86
|
+
// 系统逻辑
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## 开发
|
|
92
|
+
|
|
93
|
+
### 运行测试
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
bun test
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### 类型检查
|
|
100
|
+
|
|
101
|
+
```bash
|
|
102
|
+
bunx tsc --noEmit
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
## 项目结构
|
|
106
|
+
|
|
107
|
+
```
|
|
108
|
+
src/
|
|
109
|
+
├── index.ts # 入口文件
|
|
110
|
+
├── entity.ts # 实体和组件管理
|
|
111
|
+
├── world.ts # 世界管理
|
|
112
|
+
├── archetype.ts # 原型系统
|
|
113
|
+
├── query.ts # 查询系统
|
|
114
|
+
├── system.ts # 系统接口
|
|
115
|
+
├── command-buffer.ts # 命令缓冲区
|
|
116
|
+
├── types.ts # 类型定义
|
|
117
|
+
└── utils.ts # 工具函数
|
|
118
|
+
|
|
119
|
+
examples/
|
|
120
|
+
└── simple/
|
|
121
|
+
├── demo.ts # 基本示例
|
|
122
|
+
└── README.md # 示例说明
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## 许可证
|
|
126
|
+
|
|
127
|
+
MIT
|
|
128
|
+
|
|
129
|
+
## 贡献
|
|
130
|
+
|
|
131
|
+
欢迎提交 Issue 和 Pull Request!
|
package/archetype.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { EntityId } from "./entity";
|
|
2
|
+
import type { ComponentTuple } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Archetype class for ECS architecture
|
|
5
|
+
* Represents a group of entities that share the same set of components
|
|
6
|
+
* Optimized for fast iteration and component access
|
|
7
|
+
*/
|
|
8
|
+
export declare class Archetype {
|
|
9
|
+
/**
|
|
10
|
+
* The component types that define this archetype
|
|
11
|
+
*/
|
|
12
|
+
readonly componentTypes: EntityId<any>[];
|
|
13
|
+
/**
|
|
14
|
+
* List of entities in this archetype
|
|
15
|
+
*/
|
|
16
|
+
private entities;
|
|
17
|
+
/**
|
|
18
|
+
* Component data storage - maps component type to array of component data
|
|
19
|
+
* Each array index corresponds to the entity index in the entities array
|
|
20
|
+
*/
|
|
21
|
+
private componentData;
|
|
22
|
+
/**
|
|
23
|
+
* Reverse mapping from entity to its index in this archetype
|
|
24
|
+
*/
|
|
25
|
+
private entityToIndex;
|
|
26
|
+
/**
|
|
27
|
+
* Cache for pre-computed component data sources to avoid repeated calculations
|
|
28
|
+
* For regular components: data array
|
|
29
|
+
* For wildcards: matching relation types array
|
|
30
|
+
*/
|
|
31
|
+
private componentDataSourcesCache;
|
|
32
|
+
/**
|
|
33
|
+
* Create a new archetype with the specified component types
|
|
34
|
+
* @param componentTypes The component types that define this archetype
|
|
35
|
+
*/
|
|
36
|
+
constructor(componentTypes: EntityId<any>[]);
|
|
37
|
+
/**
|
|
38
|
+
* Get the number of entities in this archetype
|
|
39
|
+
*/
|
|
40
|
+
get size(): number;
|
|
41
|
+
/**
|
|
42
|
+
* Check if this archetype matches the given component types
|
|
43
|
+
* @param componentTypes The component types to check
|
|
44
|
+
*/
|
|
45
|
+
matches(componentTypes: EntityId<any>[]): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Add an entity to this archetype with initial component data
|
|
48
|
+
* @param entityId The entity to add
|
|
49
|
+
* @param componentData Map of component type to component data
|
|
50
|
+
*/
|
|
51
|
+
addEntity(entityId: EntityId, componentData: Map<EntityId<any>, any>): void;
|
|
52
|
+
/**
|
|
53
|
+
* Remove an entity from this archetype
|
|
54
|
+
* @param entityId The entity to remove
|
|
55
|
+
* @returns The component data of the removed entity
|
|
56
|
+
*/
|
|
57
|
+
removeEntity(entityId: EntityId): Map<EntityId<any>, any> | undefined;
|
|
58
|
+
/**
|
|
59
|
+
* Check if an entity is in this archetype
|
|
60
|
+
* @param entityId The entity to check
|
|
61
|
+
*/
|
|
62
|
+
hasEntity(entityId: EntityId): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Get component data for a specific entity and component type
|
|
65
|
+
* @param entityId The entity
|
|
66
|
+
* @param componentType The component type
|
|
67
|
+
*/
|
|
68
|
+
getComponent<T>(entityId: EntityId, componentType: EntityId<T>): T | undefined;
|
|
69
|
+
/**
|
|
70
|
+
* Set component data for a specific entity and component type
|
|
71
|
+
* @param entityId The entity
|
|
72
|
+
* @param componentType The component type
|
|
73
|
+
* @param data The component data
|
|
74
|
+
*/
|
|
75
|
+
setComponent<T>(entityId: EntityId, componentType: EntityId<T>, data: T): void;
|
|
76
|
+
/**
|
|
77
|
+
* Get all entities in this archetype
|
|
78
|
+
*/
|
|
79
|
+
getEntities(): EntityId[];
|
|
80
|
+
/**
|
|
81
|
+
* Get component data for all entities of a specific component type
|
|
82
|
+
* @param componentType The component type
|
|
83
|
+
*/
|
|
84
|
+
getComponentData<T>(componentType: EntityId<T>): T[];
|
|
85
|
+
/**
|
|
86
|
+
* Get entities with their component data for specified component types
|
|
87
|
+
* Optimized for bulk component access with pre-computed indices
|
|
88
|
+
* @param componentTypes Array of component types to retrieve
|
|
89
|
+
* @returns Array of objects with entity and component data
|
|
90
|
+
*/
|
|
91
|
+
getEntitiesWithComponents<const T extends readonly EntityId<any>[]>(componentTypes: T): Array<{
|
|
92
|
+
entity: EntityId;
|
|
93
|
+
components: ComponentTuple<T>;
|
|
94
|
+
}>;
|
|
95
|
+
/**
|
|
96
|
+
* Iterate over entities with their component data for specified component types
|
|
97
|
+
* Optimized for bulk component access
|
|
98
|
+
* @param componentTypes Array of component types to retrieve
|
|
99
|
+
* @param callback Function called for each entity with its components
|
|
100
|
+
*/
|
|
101
|
+
forEachWithComponents<const T extends readonly EntityId<any>[]>(componentTypes: T, callback: (entity: EntityId, ...components: ComponentTuple<T>) => void): void;
|
|
102
|
+
/**
|
|
103
|
+
* Iterate over all entities with their component data
|
|
104
|
+
* @param callback Function called for each entity with its component data
|
|
105
|
+
*/
|
|
106
|
+
forEach(callback: (entityId: EntityId, components: Map<EntityId<any>, any>) => void): void;
|
|
107
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { EntityId } from "./entity";
|
|
2
|
+
/**
|
|
3
|
+
* Command for deferred execution
|
|
4
|
+
*/
|
|
5
|
+
export interface Command {
|
|
6
|
+
type: "addComponent" | "removeComponent" | "destroyEntity";
|
|
7
|
+
entityId: EntityId;
|
|
8
|
+
componentType?: EntityId<any>;
|
|
9
|
+
component?: any;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Command buffer for deferred structural changes
|
|
13
|
+
*/
|
|
14
|
+
export declare class CommandBuffer {
|
|
15
|
+
private commands;
|
|
16
|
+
private executeEntityCommands;
|
|
17
|
+
/**
|
|
18
|
+
* Create a command buffer with an executor function
|
|
19
|
+
*/
|
|
20
|
+
constructor(executeEntityCommands: (entityId: EntityId, commands: Command[]) => void);
|
|
21
|
+
/**
|
|
22
|
+
* Add a component to an entity (deferred)
|
|
23
|
+
*/
|
|
24
|
+
addComponent<T>(entityId: EntityId, componentType: EntityId<T>, component: T): void;
|
|
25
|
+
/**
|
|
26
|
+
* Remove a component from an entity (deferred)
|
|
27
|
+
*/
|
|
28
|
+
removeComponent<T>(entityId: EntityId, componentType: EntityId<T>): void;
|
|
29
|
+
/**
|
|
30
|
+
* Destroy an entity (deferred)
|
|
31
|
+
*/
|
|
32
|
+
destroyEntity(entityId: EntityId): void;
|
|
33
|
+
/**
|
|
34
|
+
* Execute all commands and clear the buffer
|
|
35
|
+
*/
|
|
36
|
+
execute(): void;
|
|
37
|
+
/**
|
|
38
|
+
* Get current commands (for testing)
|
|
39
|
+
*/
|
|
40
|
+
getCommands(): Command[];
|
|
41
|
+
/**
|
|
42
|
+
* Clear all commands
|
|
43
|
+
*/
|
|
44
|
+
clear(): void;
|
|
45
|
+
}
|
package/entity.d.ts
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brand for EntityId to create nominal typing with component type information
|
|
3
|
+
*/
|
|
4
|
+
declare const __entityIdBrand: unique symbol;
|
|
5
|
+
/**
|
|
6
|
+
* Brand for wildcard relation IDs
|
|
7
|
+
*/
|
|
8
|
+
declare const __wildcardRelationBrand: unique symbol;
|
|
9
|
+
/**
|
|
10
|
+
* Entity ID type for ECS architecture
|
|
11
|
+
* Based on 52-bit integers within safe integer range
|
|
12
|
+
* - Component IDs: 1-1023
|
|
13
|
+
* - Entity IDs: 1024+
|
|
14
|
+
* - Relation IDs: negative numbers encoding component and entity associations
|
|
15
|
+
*/
|
|
16
|
+
export type EntityId<T = void> = number & {
|
|
17
|
+
readonly [__entityIdBrand]: T;
|
|
18
|
+
};
|
|
19
|
+
export type WildcardRelationId<T = void> = EntityId<T> & {
|
|
20
|
+
readonly [__wildcardRelationBrand]: true;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Constants for ID ranges
|
|
24
|
+
*/
|
|
25
|
+
export declare const INVALID_COMPONENT_ID = 0;
|
|
26
|
+
export declare const COMPONENT_ID_MAX = 1023;
|
|
27
|
+
export declare const ENTITY_ID_START = 1024;
|
|
28
|
+
/**
|
|
29
|
+
* Constants for relation ID encoding
|
|
30
|
+
*/
|
|
31
|
+
export declare const RELATION_SHIFT: number;
|
|
32
|
+
export declare const WILDCARD_TARGET_ID = 0;
|
|
33
|
+
/**
|
|
34
|
+
* Create a component ID
|
|
35
|
+
* @param id Component identifier (1-1023)
|
|
36
|
+
*/
|
|
37
|
+
export declare function createComponentId<T = void>(id: number): EntityId<T>;
|
|
38
|
+
/**
|
|
39
|
+
* Create an entity ID
|
|
40
|
+
* @param id Entity identifier (starting from 1024)
|
|
41
|
+
*/
|
|
42
|
+
export declare function createEntityId(id: number): EntityId;
|
|
43
|
+
/**
|
|
44
|
+
* Type for relation ID based on component and target types
|
|
45
|
+
*/
|
|
46
|
+
type RelationIdType<T, U> = U extends void ? EntityId<T> : T extends void ? EntityId<U> : EntityId<never>;
|
|
47
|
+
/**
|
|
48
|
+
* Create a relation ID by associating a component with another ID (entity or component)
|
|
49
|
+
* @param componentId The component ID (0-1023)
|
|
50
|
+
* @param targetId The target ID (entity, component, or '*' for wildcard)
|
|
51
|
+
*/
|
|
52
|
+
export declare function createRelationId<T>(componentId: EntityId<T>, targetId: "*"): WildcardRelationId<T>;
|
|
53
|
+
export declare function createRelationId<T, U>(componentId: EntityId<T>, targetId: EntityId<U>): RelationIdType<T, U>;
|
|
54
|
+
/**
|
|
55
|
+
* Check if an ID is a component ID
|
|
56
|
+
*/
|
|
57
|
+
export declare function isComponentId(id: EntityId<any>): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Check if an ID is an entity ID
|
|
60
|
+
*/
|
|
61
|
+
export declare function isEntityId(id: EntityId<any>): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* Check if an ID is a relation ID
|
|
64
|
+
*/
|
|
65
|
+
export declare function isRelationId(id: EntityId): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Decode a relation ID into component and target IDs
|
|
68
|
+
* @param relationId The relation ID (must be negative)
|
|
69
|
+
* @returns Object with componentId, targetId, and relation type
|
|
70
|
+
*/
|
|
71
|
+
export declare function decodeRelationId(relationId: EntityId<any>): {
|
|
72
|
+
componentId: EntityId<any>;
|
|
73
|
+
targetId: EntityId<any>;
|
|
74
|
+
type: "entity" | "component" | "wildcard";
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Get the string representation of an ID type
|
|
78
|
+
*/
|
|
79
|
+
export declare function getIdType(id: EntityId<any>): "component" | "entity" | "entity-relation" | "component-relation" | "wildcard-relation" | "invalid";
|
|
80
|
+
/**
|
|
81
|
+
* Get detailed type information for an EntityId
|
|
82
|
+
* @param id The EntityId to analyze
|
|
83
|
+
* @returns Detailed type information including relation subtypes
|
|
84
|
+
*/
|
|
85
|
+
export declare function getDetailedIdType(id: EntityId<any>): {
|
|
86
|
+
type: "component" | "entity" | "entity-relation" | "component-relation" | "wildcard-relation" | "invalid";
|
|
87
|
+
componentId?: EntityId<any>;
|
|
88
|
+
targetId?: EntityId<any>;
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Inspect an EntityId and return a human-readable string representation
|
|
92
|
+
* @param id The EntityId to inspect
|
|
93
|
+
* @returns A friendly string representation of the ID
|
|
94
|
+
*/
|
|
95
|
+
export declare function inspectEntityId(id: EntityId<any>): string;
|
|
96
|
+
/**
|
|
97
|
+
* Entity ID Manager for automatic allocation and freelist recycling
|
|
98
|
+
*/
|
|
99
|
+
export declare class EntityIdManager {
|
|
100
|
+
private nextId;
|
|
101
|
+
private freelist;
|
|
102
|
+
/**
|
|
103
|
+
* Allocate a new entity ID
|
|
104
|
+
* Uses freelist if available, otherwise increments counter
|
|
105
|
+
*/
|
|
106
|
+
allocate(): EntityId;
|
|
107
|
+
/**
|
|
108
|
+
* Deallocate an entity ID, adding it to the freelist for reuse
|
|
109
|
+
* @param id The entity ID to deallocate
|
|
110
|
+
*/
|
|
111
|
+
deallocate(id: EntityId<any>): void;
|
|
112
|
+
/**
|
|
113
|
+
* Get the current freelist size (for debugging/monitoring)
|
|
114
|
+
*/
|
|
115
|
+
getFreelistSize(): number;
|
|
116
|
+
/**
|
|
117
|
+
* Get the next ID that would be allocated (for debugging)
|
|
118
|
+
*/
|
|
119
|
+
getNextId(): number;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Component ID Manager for automatic allocation
|
|
123
|
+
* Components are typically registered once and not recycled
|
|
124
|
+
*/
|
|
125
|
+
export declare class ComponentIdManager {
|
|
126
|
+
private nextId;
|
|
127
|
+
/**
|
|
128
|
+
* Allocate a new component ID
|
|
129
|
+
* Increments counter sequentially from 1
|
|
130
|
+
*/
|
|
131
|
+
allocate<T = void>(): EntityId<T>;
|
|
132
|
+
/**
|
|
133
|
+
* Get the next ID that would be allocated (for debugging)
|
|
134
|
+
*/
|
|
135
|
+
getNextId(): number;
|
|
136
|
+
/**
|
|
137
|
+
* Check if more component IDs are available
|
|
138
|
+
*/
|
|
139
|
+
hasAvailableIds(): boolean;
|
|
140
|
+
}
|
|
141
|
+
export {};
|