@codehz/ecs 0.7.0 → 0.7.2
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.en.md +14 -8
- package/README.md +14 -8
- package/builder.d.mts +10 -2
- package/package.json +1 -1
- package/world.mjs +9 -30
- package/world.mjs.map +1 -1
package/README.en.md
CHANGED
|
@@ -204,8 +204,10 @@ world.sync();
|
|
|
204
204
|
### Running Examples
|
|
205
205
|
|
|
206
206
|
```bash
|
|
207
|
-
bun run examples/simple
|
|
208
|
-
bun run examples/advanced-scheduling
|
|
207
|
+
bun run examples/simple.ts
|
|
208
|
+
bun run examples/advanced-scheduling.ts
|
|
209
|
+
bun run examples/parent-child-hierarchy.ts
|
|
210
|
+
bun run examples/inventory-system-relations.ts
|
|
209
211
|
```
|
|
210
212
|
|
|
211
213
|
## API Overview
|
|
@@ -410,11 +412,14 @@ src/
|
|
|
410
412
|
└── __tests__/ # Unit tests & performance tests
|
|
411
413
|
|
|
412
414
|
examples/
|
|
413
|
-
├──
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
415
|
+
├── advanced-scheduling.ts # Pipeline scheduling example
|
|
416
|
+
├── collision-detection.ts # Collision detection example
|
|
417
|
+
├── parent-child-hierarchy.ts # Parent-child hierarchy and transform propagation example
|
|
418
|
+
├── serialization.ts # Serialization example
|
|
419
|
+
├── simple.ts # Basic example
|
|
420
|
+
├── spatial-grid.ts # Spatial grid example
|
|
421
|
+
├── state-machine.ts # State machine example
|
|
422
|
+
└── tag-filtering.ts # Tag filtering example
|
|
418
423
|
|
|
419
424
|
scripts/
|
|
420
425
|
├── build.ts # Build script
|
|
@@ -427,7 +432,8 @@ scripts/
|
|
|
427
432
|
bun install
|
|
428
433
|
bun test # Run tests
|
|
429
434
|
bunx tsc --noEmit # Type check
|
|
430
|
-
bun run examples/simple
|
|
435
|
+
bun run examples/simple.ts # Run example
|
|
436
|
+
bun run examples/parent-child-hierarchy.ts
|
|
431
437
|
bun run scripts/build.ts # Build
|
|
432
438
|
```
|
|
433
439
|
|
package/README.md
CHANGED
|
@@ -204,8 +204,10 @@ world.sync();
|
|
|
204
204
|
### 运行示例
|
|
205
205
|
|
|
206
206
|
```bash
|
|
207
|
-
bun run examples/simple
|
|
208
|
-
bun run examples/advanced-scheduling
|
|
207
|
+
bun run examples/simple.ts
|
|
208
|
+
bun run examples/advanced-scheduling.ts
|
|
209
|
+
bun run examples/parent-child-hierarchy.ts
|
|
210
|
+
bun run examples/inventory-system-relations.ts
|
|
209
211
|
```
|
|
210
212
|
|
|
211
213
|
## API 概述
|
|
@@ -410,11 +412,14 @@ src/
|
|
|
410
412
|
└── __tests__/ # 单元测试 & 性能测试
|
|
411
413
|
|
|
412
414
|
examples/
|
|
413
|
-
├──
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
415
|
+
├── advanced-scheduling.ts # Pipeline 调度示例
|
|
416
|
+
├── collision-detection.ts # 碰撞检测示例
|
|
417
|
+
├── parent-child-hierarchy.ts # 父子层级与 Transform 传播示例
|
|
418
|
+
├── serialization.ts # 序列化示例
|
|
419
|
+
├── simple.ts # 基本示例
|
|
420
|
+
├── spatial-grid.ts # 空间网格示例
|
|
421
|
+
├── state-machine.ts # 状态机示例
|
|
422
|
+
└── tag-filtering.ts # 标签过滤示例
|
|
418
423
|
|
|
419
424
|
scripts/
|
|
420
425
|
├── build.ts # 构建脚本
|
|
@@ -427,7 +432,8 @@ scripts/
|
|
|
427
432
|
bun install
|
|
428
433
|
bun test # 运行测试
|
|
429
434
|
bunx tsc --noEmit # 类型检查
|
|
430
|
-
bun run examples/simple
|
|
435
|
+
bun run examples/simple.ts # 运行示例
|
|
436
|
+
bun run examples/parent-child-hierarchy.ts
|
|
431
437
|
bun run scripts/build.ts # 构建
|
|
432
438
|
```
|
|
433
439
|
|
package/builder.d.mts
CHANGED
|
@@ -1070,6 +1070,9 @@ declare class World {
|
|
|
1070
1070
|
* @overload getOptional<T>(entityId: EntityId<T>): { value: T } | undefined
|
|
1071
1071
|
* Retrieves the entity's primary component safely.
|
|
1072
1072
|
*
|
|
1073
|
+
* @overload getOptional<T>(entityId: EntityId, componentType: WildcardRelationId<T>): { value: [EntityId<unknown>, T][] } | undefined
|
|
1074
|
+
* Retrieves all matching relation values safely.
|
|
1075
|
+
*
|
|
1073
1076
|
* @overload getOptional<T>(entityId: EntityId, componentType: EntityId<T>): { value: T } | undefined
|
|
1074
1077
|
* Retrieves a specific component safely.
|
|
1075
1078
|
*
|
|
@@ -1084,6 +1087,9 @@ declare class World {
|
|
|
1084
1087
|
getOptional<T>(entityId: EntityId<T>): {
|
|
1085
1088
|
value: T;
|
|
1086
1089
|
} | undefined;
|
|
1090
|
+
getOptional<T>(entityId: EntityId, componentType: WildcardRelationId<T>): {
|
|
1091
|
+
value: [EntityId<unknown>, T][];
|
|
1092
|
+
} | undefined;
|
|
1087
1093
|
getOptional<T>(entityId: EntityId, componentType: EntityId<T>): {
|
|
1088
1094
|
value: T;
|
|
1089
1095
|
} | undefined;
|
|
@@ -1333,7 +1339,8 @@ declare class EntityBuilder {
|
|
|
1333
1339
|
* builder.with(Position, { x: 10, y: 20 });
|
|
1334
1340
|
* builder.with(Marker); // void component
|
|
1335
1341
|
*/
|
|
1336
|
-
with<T>(componentId: EntityId<T
|
|
1342
|
+
with<T extends void>(componentId: EntityId<T>): this;
|
|
1343
|
+
with<T>(componentId: EntityId<T>, value: T): this;
|
|
1337
1344
|
/**
|
|
1338
1345
|
* Add a relation component to the entity under construction.
|
|
1339
1346
|
*
|
|
@@ -1347,7 +1354,8 @@ declare class EntityBuilder {
|
|
|
1347
1354
|
* builder.withRelation(Parent, parentEntity);
|
|
1348
1355
|
* builder.withRelation(ChildOf, childEntity, { order: 1 });
|
|
1349
1356
|
*/
|
|
1350
|
-
withRelation<T>(componentId: ComponentId<T>, targetEntity: EntityId<any
|
|
1357
|
+
withRelation<T extends void>(componentId: ComponentId<T>, targetEntity: EntityId<any>): this;
|
|
1358
|
+
withRelation<T>(componentId: ComponentId<T>, targetEntity: EntityId<any>, value: T): this;
|
|
1351
1359
|
/**
|
|
1352
1360
|
* Create the entity and enqueue all configured components.
|
|
1353
1361
|
* The entity and components are only materialised after {@link World.sync} is called.
|
package/package.json
CHANGED
package/world.mjs
CHANGED
|
@@ -600,20 +600,7 @@ var EntityBuilder = class {
|
|
|
600
600
|
constructor(world) {
|
|
601
601
|
this.world = world;
|
|
602
602
|
}
|
|
603
|
-
|
|
604
|
-
* Add a regular component to the entity under construction.
|
|
605
|
-
*
|
|
606
|
-
* @template T - The component data type
|
|
607
|
-
* @param componentId - The component type to add
|
|
608
|
-
* @param args - Component data (omit for void components)
|
|
609
|
-
* @returns This builder for chaining
|
|
610
|
-
*
|
|
611
|
-
* @example
|
|
612
|
-
* builder.with(Position, { x: 10, y: 20 });
|
|
613
|
-
* builder.with(Marker); // void component
|
|
614
|
-
*/
|
|
615
|
-
with(componentId, ...args) {
|
|
616
|
-
const value = args.length > 0 ? args[0] : void 0;
|
|
603
|
+
with(componentId, value) {
|
|
617
604
|
this.components.push({
|
|
618
605
|
type: "component",
|
|
619
606
|
id: componentId,
|
|
@@ -621,21 +608,7 @@ var EntityBuilder = class {
|
|
|
621
608
|
});
|
|
622
609
|
return this;
|
|
623
610
|
}
|
|
624
|
-
|
|
625
|
-
* Add a relation component to the entity under construction.
|
|
626
|
-
*
|
|
627
|
-
* @template T - The relation data type
|
|
628
|
-
* @param componentId - The base component type for the relation
|
|
629
|
-
* @param targetEntity - The target entity or component for the relation
|
|
630
|
-
* @param args - Relation data (omit for void relations)
|
|
631
|
-
* @returns This builder for chaining
|
|
632
|
-
*
|
|
633
|
-
* @example
|
|
634
|
-
* builder.withRelation(Parent, parentEntity);
|
|
635
|
-
* builder.withRelation(ChildOf, childEntity, { order: 1 });
|
|
636
|
-
*/
|
|
637
|
-
withRelation(componentId, targetEntity, ...args) {
|
|
638
|
-
const value = args.length > 0 ? args[0] : void 0;
|
|
611
|
+
withRelation(componentId, targetEntity, value) {
|
|
639
612
|
this.components.push({
|
|
640
613
|
type: "relation",
|
|
641
614
|
componentId,
|
|
@@ -2036,10 +2009,16 @@ function collectMultiHookComponents(ctx, entityId, componentTypes) {
|
|
|
2036
2009
|
/**
|
|
2037
2010
|
* Reconstructs wildcard relation data by merging current data with removed components.
|
|
2038
2011
|
* Returns an array of [targetId, value] tuples for the wildcard relation.
|
|
2012
|
+
*
|
|
2013
|
+
* This is used during "on_remove" hook invocation: the removed components have already
|
|
2014
|
+
* been taken out of the entity's archetype, but the hook callback expects to see the
|
|
2015
|
+
* full data as it existed *before* removal. We reconstruct that snapshot by taking the
|
|
2016
|
+
* current wildcard data (post-removal) and adding back the entries that were just removed.
|
|
2039
2017
|
*/
|
|
2040
2018
|
function reconstructWildcardWithRemoved(ctx, entityId, wildcardId, removedComponents) {
|
|
2041
2019
|
const currentData = ctx.get(entityId, wildcardId);
|
|
2042
|
-
|
|
2020
|
+
if (!Array.isArray(currentData)) throw new Error(`Expected wildcard relation data to be an array, but got ${typeof currentData} for entity ${entityId} and wildcard ${wildcardId}. This indicates a HooksContext implementation that does not conform to the expected contract.`);
|
|
2021
|
+
const result = [...currentData];
|
|
2043
2022
|
for (const [removedCompId, removedValue] of removedComponents.entries()) if (componentMatchesHookType(removedCompId, wildcardId)) {
|
|
2044
2023
|
const targetId = getTargetIdFromRelationId(removedCompId);
|
|
2045
2024
|
if (targetId !== void 0) result.push([targetId, removedValue]);
|