@esengine/ecs-framework 2.1.29 → 2.1.30
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/bin/Core.d.ts +279 -0
- package/bin/Core.d.ts.map +1 -0
- package/bin/Core.js +400 -0
- package/bin/Core.js.map +1 -0
- package/bin/ECS/Component.d.ts +120 -0
- package/bin/ECS/Component.d.ts.map +1 -0
- package/bin/ECS/Component.js +129 -0
- package/bin/ECS/Component.js.map +1 -0
- package/bin/ECS/Components/IUpdatable.d.ts +22 -0
- package/bin/ECS/Components/IUpdatable.d.ts.map +1 -0
- package/bin/ECS/Components/IUpdatable.js +17 -0
- package/bin/ECS/Components/IUpdatable.js.map +1 -0
- package/bin/ECS/Components/SceneComponent.d.ts +40 -0
- package/bin/ECS/Components/SceneComponent.d.ts.map +1 -0
- package/bin/ECS/Components/SceneComponent.js +57 -0
- package/bin/ECS/Components/SceneComponent.js.map +1 -0
- package/bin/ECS/Core/ArchetypeSystem.d.ts +100 -0
- package/bin/ECS/Core/ArchetypeSystem.d.ts.map +1 -0
- package/bin/ECS/Core/ArchetypeSystem.js +193 -0
- package/bin/ECS/Core/ArchetypeSystem.js.map +1 -0
- package/bin/ECS/Core/ComponentIndex.d.ts +151 -0
- package/bin/ECS/Core/ComponentIndex.d.ts.map +1 -0
- package/bin/ECS/Core/ComponentIndex.js +405 -0
- package/bin/ECS/Core/ComponentIndex.js.map +1 -0
- package/bin/ECS/Core/ComponentPool.d.ts +88 -0
- package/bin/ECS/Core/ComponentPool.d.ts.map +1 -0
- package/bin/ECS/Core/ComponentPool.js +159 -0
- package/bin/ECS/Core/ComponentPool.js.map +1 -0
- package/bin/ECS/Core/ComponentStorage/ComponentRegistry.d.ts +92 -0
- package/bin/ECS/Core/ComponentStorage/ComponentRegistry.d.ts.map +1 -0
- package/bin/ECS/Core/ComponentStorage/ComponentRegistry.js +167 -0
- package/bin/ECS/Core/ComponentStorage/ComponentRegistry.js.map +1 -0
- package/bin/ECS/Core/ComponentStorage.d.ts +233 -0
- package/bin/ECS/Core/ComponentStorage.d.ts.map +1 -0
- package/bin/ECS/Core/ComponentStorage.js +463 -0
- package/bin/ECS/Core/ComponentStorage.js.map +1 -0
- package/bin/ECS/Core/DirtyTrackingSystem.d.ts +197 -0
- package/bin/ECS/Core/DirtyTrackingSystem.d.ts.map +1 -0
- package/bin/ECS/Core/DirtyTrackingSystem.js +297 -0
- package/bin/ECS/Core/DirtyTrackingSystem.js.map +1 -0
- package/bin/ECS/Core/EntityManager.d.ts +379 -0
- package/bin/ECS/Core/EntityManager.d.ts.map +1 -0
- package/bin/ECS/Core/EntityManager.js +686 -0
- package/bin/ECS/Core/EntityManager.js.map +1 -0
- package/bin/ECS/Core/EventBus.d.ts +218 -0
- package/bin/ECS/Core/EventBus.d.ts.map +1 -0
- package/bin/ECS/Core/EventBus.js +402 -0
- package/bin/ECS/Core/EventBus.js.map +1 -0
- package/bin/ECS/Core/EventSystem.d.ts +237 -0
- package/bin/ECS/Core/EventSystem.d.ts.map +1 -0
- package/bin/ECS/Core/EventSystem.js +478 -0
- package/bin/ECS/Core/EventSystem.js.map +1 -0
- package/bin/ECS/Core/Events/index.d.ts +3 -0
- package/bin/ECS/Core/Events/index.d.ts.map +1 -0
- package/bin/ECS/Core/Events/index.js +3 -0
- package/bin/ECS/Core/Events/index.js.map +1 -0
- package/bin/ECS/Core/FluentAPI/ComponentBuilder.d.ts +35 -0
- package/bin/ECS/Core/FluentAPI/ComponentBuilder.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI/ComponentBuilder.js +48 -0
- package/bin/ECS/Core/FluentAPI/ComponentBuilder.js.map +1 -0
- package/bin/ECS/Core/FluentAPI/ECSFluentAPI.d.ts +135 -0
- package/bin/ECS/Core/FluentAPI/ECSFluentAPI.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI/ECSFluentAPI.js +166 -0
- package/bin/ECS/Core/FluentAPI/ECSFluentAPI.js.map +1 -0
- package/bin/ECS/Core/FluentAPI/EntityBatchOperator.d.ts +58 -0
- package/bin/ECS/Core/FluentAPI/EntityBatchOperator.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI/EntityBatchOperator.js +85 -0
- package/bin/ECS/Core/FluentAPI/EntityBatchOperator.js.map +1 -0
- package/bin/ECS/Core/FluentAPI/EntityBuilder.d.ts +110 -0
- package/bin/ECS/Core/FluentAPI/EntityBuilder.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI/EntityBuilder.js +174 -0
- package/bin/ECS/Core/FluentAPI/EntityBuilder.js.map +1 -0
- package/bin/ECS/Core/FluentAPI/SceneBuilder.d.ts +53 -0
- package/bin/ECS/Core/FluentAPI/SceneBuilder.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI/SceneBuilder.js +79 -0
- package/bin/ECS/Core/FluentAPI/SceneBuilder.js.map +1 -0
- package/bin/ECS/Core/FluentAPI/index.d.ts +6 -0
- package/bin/ECS/Core/FluentAPI/index.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI/index.js +6 -0
- package/bin/ECS/Core/FluentAPI/index.js.map +1 -0
- package/bin/ECS/Core/FluentAPI.d.ts +2 -0
- package/bin/ECS/Core/FluentAPI.d.ts.map +1 -0
- package/bin/ECS/Core/FluentAPI.js +3 -0
- package/bin/ECS/Core/FluentAPI.js.map +1 -0
- package/bin/ECS/Core/Performance/index.d.ts +4 -0
- package/bin/ECS/Core/Performance/index.d.ts.map +1 -0
- package/bin/ECS/Core/Performance/index.js +4 -0
- package/bin/ECS/Core/Performance/index.js.map +1 -0
- package/bin/ECS/Core/Query/index.d.ts +4 -0
- package/bin/ECS/Core/Query/index.d.ts.map +1 -0
- package/bin/ECS/Core/Query/index.js +4 -0
- package/bin/ECS/Core/Query/index.js.map +1 -0
- package/bin/ECS/Core/QuerySystem.d.ts +438 -0
- package/bin/ECS/Core/QuerySystem.d.ts.map +1 -0
- package/bin/ECS/Core/QuerySystem.js +974 -0
- package/bin/ECS/Core/QuerySystem.js.map +1 -0
- package/bin/ECS/Core/SoAStorage.d.ts +97 -0
- package/bin/ECS/Core/SoAStorage.d.ts.map +1 -0
- package/bin/ECS/Core/SoAStorage.js +548 -0
- package/bin/ECS/Core/SoAStorage.js.map +1 -0
- package/bin/ECS/Core/Storage/index.d.ts +4 -0
- package/bin/ECS/Core/Storage/index.d.ts.map +1 -0
- package/bin/ECS/Core/Storage/index.js +4 -0
- package/bin/ECS/Core/Storage/index.js.map +1 -0
- package/bin/ECS/CoreEvents.d.ts +131 -0
- package/bin/ECS/CoreEvents.d.ts.map +1 -0
- package/bin/ECS/CoreEvents.js +163 -0
- package/bin/ECS/CoreEvents.js.map +1 -0
- package/bin/ECS/Entity.d.ts +455 -0
- package/bin/ECS/Entity.d.ts.map +1 -0
- package/bin/ECS/Entity.js +837 -0
- package/bin/ECS/Entity.js.map +1 -0
- package/bin/ECS/Scene.d.ts +225 -0
- package/bin/ECS/Scene.d.ts.map +1 -0
- package/bin/ECS/Scene.js +325 -0
- package/bin/ECS/Scene.js.map +1 -0
- package/bin/ECS/Systems/EntitySystem.d.ts +200 -0
- package/bin/ECS/Systems/EntitySystem.d.ts.map +1 -0
- package/bin/ECS/Systems/EntitySystem.js +431 -0
- package/bin/ECS/Systems/EntitySystem.js.map +1 -0
- package/bin/ECS/Systems/IntervalSystem.d.ts +33 -0
- package/bin/ECS/Systems/IntervalSystem.d.ts.map +1 -0
- package/bin/ECS/Systems/IntervalSystem.js +50 -0
- package/bin/ECS/Systems/IntervalSystem.js.map +1 -0
- package/bin/ECS/Systems/PassiveSystem.d.ts +17 -0
- package/bin/ECS/Systems/PassiveSystem.d.ts.map +1 -0
- package/bin/ECS/Systems/PassiveSystem.js +19 -0
- package/bin/ECS/Systems/PassiveSystem.js.map +1 -0
- package/bin/ECS/Systems/ProcessingSystem.d.ts +21 -0
- package/bin/ECS/Systems/ProcessingSystem.d.ts.map +1 -0
- package/bin/ECS/Systems/ProcessingSystem.js +20 -0
- package/bin/ECS/Systems/ProcessingSystem.js.map +1 -0
- package/bin/ECS/Systems/index.d.ts +5 -0
- package/bin/ECS/Systems/index.d.ts.map +1 -0
- package/bin/ECS/Systems/index.js +6 -0
- package/bin/ECS/Systems/index.js.map +1 -0
- package/bin/ECS/Utils/BigIntCompatibility.d.ts +164 -0
- package/bin/ECS/Utils/BigIntCompatibility.d.ts.map +1 -0
- package/bin/ECS/Utils/BigIntCompatibility.js +578 -0
- package/bin/ECS/Utils/BigIntCompatibility.js.map +1 -0
- package/bin/ECS/Utils/Bits.d.ts +163 -0
- package/bin/ECS/Utils/Bits.d.ts.map +1 -0
- package/bin/ECS/Utils/Bits.js +286 -0
- package/bin/ECS/Utils/Bits.js.map +1 -0
- package/bin/ECS/Utils/ComponentTypeManager.d.ts +50 -0
- package/bin/ECS/Utils/ComponentTypeManager.d.ts.map +1 -0
- package/bin/ECS/Utils/ComponentTypeManager.js +84 -0
- package/bin/ECS/Utils/ComponentTypeManager.js.map +1 -0
- package/bin/ECS/Utils/EntityList.d.ts +108 -0
- package/bin/ECS/Utils/EntityList.d.ts.map +1 -0
- package/bin/ECS/Utils/EntityList.js +262 -0
- package/bin/ECS/Utils/EntityList.js.map +1 -0
- package/bin/ECS/Utils/EntityProcessorList.d.ts +56 -0
- package/bin/ECS/Utils/EntityProcessorList.d.ts.map +1 -0
- package/bin/ECS/Utils/EntityProcessorList.js +102 -0
- package/bin/ECS/Utils/EntityProcessorList.js.map +1 -0
- package/bin/ECS/Utils/IdentifierPool.d.ts +203 -0
- package/bin/ECS/Utils/IdentifierPool.d.ts.map +1 -0
- package/bin/ECS/Utils/IdentifierPool.js +325 -0
- package/bin/ECS/Utils/IdentifierPool.js.map +1 -0
- package/bin/ECS/Utils/Matcher.d.ts +143 -0
- package/bin/ECS/Utils/Matcher.d.ts.map +1 -0
- package/bin/ECS/Utils/Matcher.js +249 -0
- package/bin/ECS/Utils/Matcher.js.map +1 -0
- package/bin/ECS/Utils/index.d.ts +8 -0
- package/bin/ECS/Utils/index.d.ts.map +1 -0
- package/bin/ECS/Utils/index.js +9 -0
- package/bin/ECS/Utils/index.js.map +1 -0
- package/bin/ECS/index.d.ts +12 -0
- package/bin/ECS/index.d.ts.map +1 -0
- package/bin/ECS/index.js +12 -0
- package/bin/ECS/index.js.map +1 -0
- package/bin/Types/index.d.ts +471 -0
- package/bin/Types/index.d.ts.map +1 -0
- package/bin/Types/index.js +5 -0
- package/bin/Types/index.js.map +1 -0
- package/bin/Utils/Debug/ComponentDataCollector.d.ts +28 -0
- package/bin/Utils/Debug/ComponentDataCollector.d.ts.map +1 -0
- package/bin/Utils/Debug/ComponentDataCollector.js +248 -0
- package/bin/Utils/Debug/ComponentDataCollector.js.map +1 -0
- package/bin/Utils/Debug/DebugDataFormatter.d.ts +22 -0
- package/bin/Utils/Debug/DebugDataFormatter.d.ts.map +1 -0
- package/bin/Utils/Debug/DebugDataFormatter.js +117 -0
- package/bin/Utils/Debug/DebugDataFormatter.js.map +1 -0
- package/bin/Utils/Debug/DebugManager.d.ts +104 -0
- package/bin/Utils/Debug/DebugManager.d.ts.map +1 -0
- package/bin/Utils/Debug/DebugManager.js +647 -0
- package/bin/Utils/Debug/DebugManager.js.map +1 -0
- package/bin/Utils/Debug/EntityDataCollector.d.ts +92 -0
- package/bin/Utils/Debug/EntityDataCollector.d.ts.map +1 -0
- package/bin/Utils/Debug/EntityDataCollector.js +864 -0
- package/bin/Utils/Debug/EntityDataCollector.js.map +1 -0
- package/bin/Utils/Debug/PerformanceDataCollector.d.ts +32 -0
- package/bin/Utils/Debug/PerformanceDataCollector.d.ts.map +1 -0
- package/bin/Utils/Debug/PerformanceDataCollector.js +217 -0
- package/bin/Utils/Debug/PerformanceDataCollector.js.map +1 -0
- package/bin/Utils/Debug/SceneDataCollector.d.ts +16 -0
- package/bin/Utils/Debug/SceneDataCollector.d.ts.map +1 -0
- package/bin/Utils/Debug/SceneDataCollector.js +46 -0
- package/bin/Utils/Debug/SceneDataCollector.js.map +1 -0
- package/bin/Utils/Debug/SystemDataCollector.d.ts +11 -0
- package/bin/Utils/Debug/SystemDataCollector.d.ts.map +1 -0
- package/bin/Utils/Debug/SystemDataCollector.js +59 -0
- package/bin/Utils/Debug/SystemDataCollector.js.map +1 -0
- package/bin/Utils/Debug/WebSocketManager.d.ts +59 -0
- package/bin/Utils/Debug/WebSocketManager.d.ts.map +1 -0
- package/bin/Utils/Debug/WebSocketManager.js +148 -0
- package/bin/Utils/Debug/WebSocketManager.js.map +1 -0
- package/bin/Utils/Debug/index.d.ts +8 -0
- package/bin/Utils/Debug/index.d.ts.map +1 -0
- package/bin/Utils/Debug/index.js +8 -0
- package/bin/Utils/Debug/index.js.map +1 -0
- package/bin/Utils/Emitter.d.ts +43 -0
- package/bin/Utils/Emitter.d.ts.map +1 -0
- package/bin/Utils/Emitter.js +69 -0
- package/bin/Utils/Emitter.js.map +1 -0
- package/bin/Utils/Extensions/NumberExtension.d.ts +13 -0
- package/bin/Utils/Extensions/NumberExtension.d.ts.map +1 -0
- package/bin/Utils/Extensions/NumberExtension.js +17 -0
- package/bin/Utils/Extensions/NumberExtension.js.map +1 -0
- package/bin/Utils/Extensions/TypeUtils.d.ts +13 -0
- package/bin/Utils/Extensions/TypeUtils.d.ts.map +1 -0
- package/bin/Utils/Extensions/TypeUtils.js +15 -0
- package/bin/Utils/Extensions/TypeUtils.js.map +1 -0
- package/bin/Utils/Extensions/index.d.ts +3 -0
- package/bin/Utils/Extensions/index.d.ts.map +1 -0
- package/bin/Utils/Extensions/index.js +4 -0
- package/bin/Utils/Extensions/index.js.map +1 -0
- package/bin/Utils/GlobalManager.d.ts +32 -0
- package/bin/Utils/GlobalManager.d.ts.map +1 -0
- package/bin/Utils/GlobalManager.js +53 -0
- package/bin/Utils/GlobalManager.js.map +1 -0
- package/bin/Utils/Logger.d.ts +156 -0
- package/bin/Utils/Logger.d.ts.map +1 -0
- package/bin/Utils/Logger.js +256 -0
- package/bin/Utils/Logger.js.map +1 -0
- package/bin/Utils/PerformanceMonitor.d.ts +211 -0
- package/bin/Utils/PerformanceMonitor.d.ts.map +1 -0
- package/bin/Utils/PerformanceMonitor.js +270 -0
- package/bin/Utils/PerformanceMonitor.js.map +1 -0
- package/bin/Utils/Pool/IPoolable.d.ts +29 -0
- package/bin/Utils/Pool/IPoolable.d.ts.map +1 -0
- package/bin/Utils/Pool/IPoolable.js +2 -0
- package/bin/Utils/Pool/IPoolable.js.map +1 -0
- package/bin/Utils/Pool/Pool.d.ts +109 -0
- package/bin/Utils/Pool/Pool.d.ts.map +1 -0
- package/bin/Utils/Pool/Pool.js +238 -0
- package/bin/Utils/Pool/Pool.js.map +1 -0
- package/bin/Utils/Pool/PoolManager.d.ts +91 -0
- package/bin/Utils/Pool/PoolManager.d.ts.map +1 -0
- package/bin/Utils/Pool/PoolManager.js +197 -0
- package/bin/Utils/Pool/PoolManager.js.map +1 -0
- package/bin/Utils/Pool/index.d.ts +4 -0
- package/bin/Utils/Pool/index.d.ts.map +1 -0
- package/bin/Utils/Pool/index.js +4 -0
- package/bin/Utils/Pool/index.js.map +1 -0
- package/bin/Utils/Time.d.ts +47 -0
- package/bin/Utils/Time.d.ts.map +1 -0
- package/bin/Utils/Time.js +64 -0
- package/bin/Utils/Time.js.map +1 -0
- package/bin/Utils/Timers/ITimer.d.ts +16 -0
- package/bin/Utils/Timers/ITimer.d.ts.map +1 -0
- package/bin/Utils/Timers/ITimer.js +2 -0
- package/bin/Utils/Timers/ITimer.js.map +1 -0
- package/bin/Utils/Timers/Timer.d.ts +30 -0
- package/bin/Utils/Timers/Timer.d.ts.map +1 -0
- package/bin/Utils/Timers/Timer.js +58 -0
- package/bin/Utils/Timers/Timer.js.map +1 -0
- package/bin/Utils/Timers/TimerManager.d.ts +19 -0
- package/bin/Utils/Timers/TimerManager.d.ts.map +1 -0
- package/bin/Utils/Timers/TimerManager.js +33 -0
- package/bin/Utils/Timers/TimerManager.js.map +1 -0
- package/bin/Utils/index.d.ts +9 -0
- package/bin/Utils/index.d.ts.map +1 -0
- package/bin/Utils/index.js +9 -0
- package/bin/Utils/index.js.map +1 -0
- package/bin/index.d.ts +16 -0
- package/bin/index.d.ts.map +1 -0
- package/bin/index.js +19 -0
- package/bin/index.js.map +1 -0
- package/package.json +52 -25
- package/.npmignore +0 -40
- package/LICENSE +0 -201
- package/README.md +0 -465
- package/SECURITY.md +0 -53
- package/index.cjs +0 -2
- package/index.cjs.map +0 -1
- package/index.d.ts +0 -6562
- package/index.mjs +0 -2
- package/index.mjs.map +0 -1
|
@@ -0,0 +1,837 @@
|
|
|
1
|
+
import { ComponentRegistry } from './Core/ComponentStorage';
|
|
2
|
+
import { BigIntFactory } from './Utils/BigIntCompatibility';
|
|
3
|
+
import { createLogger } from '../Utils/Logger';
|
|
4
|
+
/**
|
|
5
|
+
* 实体比较器
|
|
6
|
+
*
|
|
7
|
+
* 用于比较两个实体的优先级,首先按更新顺序比较,然后按ID比较。
|
|
8
|
+
*/
|
|
9
|
+
export class EntityComparer {
|
|
10
|
+
/**
|
|
11
|
+
* 比较两个实体
|
|
12
|
+
*
|
|
13
|
+
* @param self - 第一个实体
|
|
14
|
+
* @param other - 第二个实体
|
|
15
|
+
* @returns 比较结果,负数表示self优先级更高,正数表示other优先级更高,0表示相等
|
|
16
|
+
*/
|
|
17
|
+
compare(self, other) {
|
|
18
|
+
let compare = self.updateOrder - other.updateOrder;
|
|
19
|
+
if (compare == 0)
|
|
20
|
+
compare = self.id - other.id;
|
|
21
|
+
return compare;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* 游戏实体类
|
|
26
|
+
*
|
|
27
|
+
* ECS架构中的实体(Entity),作为组件的容器。
|
|
28
|
+
* 实体本身不包含游戏逻辑,所有功能都通过组件来实现。
|
|
29
|
+
* 支持父子关系,可以构建实体层次结构。
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```typescript
|
|
33
|
+
* // 创建实体
|
|
34
|
+
* const entity = new Entity("Player", 1);
|
|
35
|
+
*
|
|
36
|
+
* // 添加组件
|
|
37
|
+
* const healthComponent = entity.addComponent(new HealthComponent(100));
|
|
38
|
+
*
|
|
39
|
+
* // 获取组件
|
|
40
|
+
* const health = entity.getComponent(HealthComponent);
|
|
41
|
+
*
|
|
42
|
+
* // 添加位置组件
|
|
43
|
+
* entity.addComponent(new PositionComponent(100, 200));
|
|
44
|
+
*
|
|
45
|
+
* // 添加子实体
|
|
46
|
+
* const weapon = new Entity("Weapon", 2);
|
|
47
|
+
* entity.addChild(weapon);
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export class Entity {
|
|
51
|
+
/**
|
|
52
|
+
* 构造函数
|
|
53
|
+
*
|
|
54
|
+
* @param name - 实体名称
|
|
55
|
+
* @param id - 实体唯一标识符
|
|
56
|
+
*/
|
|
57
|
+
constructor(name, id) {
|
|
58
|
+
/**
|
|
59
|
+
* 组件集合
|
|
60
|
+
*
|
|
61
|
+
* 存储实体拥有的所有组件。
|
|
62
|
+
*/
|
|
63
|
+
this.components = [];
|
|
64
|
+
/**
|
|
65
|
+
* 所属场景引用
|
|
66
|
+
*
|
|
67
|
+
* 指向实体所在的场景实例。
|
|
68
|
+
*/
|
|
69
|
+
this.scene = null;
|
|
70
|
+
/**
|
|
71
|
+
* 更新间隔
|
|
72
|
+
*
|
|
73
|
+
* 控制实体更新的频率,值越大更新越不频繁。
|
|
74
|
+
*/
|
|
75
|
+
this.updateInterval = 1;
|
|
76
|
+
/**
|
|
77
|
+
* 销毁状态标志
|
|
78
|
+
*
|
|
79
|
+
* 标记实体是否已被销毁。
|
|
80
|
+
*/
|
|
81
|
+
this._isDestroyed = false;
|
|
82
|
+
/**
|
|
83
|
+
* 父实体引用
|
|
84
|
+
*
|
|
85
|
+
* 指向父级实体,用于构建实体层次结构。
|
|
86
|
+
*/
|
|
87
|
+
this._parent = null;
|
|
88
|
+
/**
|
|
89
|
+
* 子实体集合
|
|
90
|
+
*
|
|
91
|
+
* 存储所有子级实体的数组。
|
|
92
|
+
*/
|
|
93
|
+
this._children = [];
|
|
94
|
+
/**
|
|
95
|
+
* 激活状态
|
|
96
|
+
*
|
|
97
|
+
* 控制实体是否处于激活状态。
|
|
98
|
+
*/
|
|
99
|
+
this._active = true;
|
|
100
|
+
/**
|
|
101
|
+
* 实体标签
|
|
102
|
+
*
|
|
103
|
+
* 用于分类和查询的数字标签。
|
|
104
|
+
*/
|
|
105
|
+
this._tag = 0;
|
|
106
|
+
/**
|
|
107
|
+
* 启用状态
|
|
108
|
+
*
|
|
109
|
+
* 控制实体是否启用更新和处理。
|
|
110
|
+
*/
|
|
111
|
+
this._enabled = true;
|
|
112
|
+
/**
|
|
113
|
+
* 更新顺序
|
|
114
|
+
*
|
|
115
|
+
* 控制实体在系统中的更新优先级。
|
|
116
|
+
*/
|
|
117
|
+
this._updateOrder = 0;
|
|
118
|
+
/**
|
|
119
|
+
* 组件位掩码
|
|
120
|
+
*
|
|
121
|
+
* 用于快速查询实体拥有的组件类型。
|
|
122
|
+
*/
|
|
123
|
+
this._componentMask = BigIntFactory.zero();
|
|
124
|
+
/**
|
|
125
|
+
* 组件类型到索引的映射
|
|
126
|
+
*
|
|
127
|
+
* 用于快速定位组件在数组中的位置。
|
|
128
|
+
*/
|
|
129
|
+
this._componentTypeToIndex = new Map();
|
|
130
|
+
this.name = name;
|
|
131
|
+
this.id = id;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* 获取销毁状态
|
|
135
|
+
*
|
|
136
|
+
* @returns 如果实体已被销毁则返回true
|
|
137
|
+
*/
|
|
138
|
+
get isDestroyed() {
|
|
139
|
+
return this._isDestroyed;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 获取父实体
|
|
143
|
+
*
|
|
144
|
+
* @returns 父实体,如果没有父实体则返回null
|
|
145
|
+
*/
|
|
146
|
+
get parent() {
|
|
147
|
+
return this._parent;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 获取子实体数组的只读副本
|
|
151
|
+
*
|
|
152
|
+
* @returns 子实体数组的副本
|
|
153
|
+
*/
|
|
154
|
+
get children() {
|
|
155
|
+
return [...this._children];
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* 获取子实体数量
|
|
159
|
+
*
|
|
160
|
+
* @returns 子实体的数量
|
|
161
|
+
*/
|
|
162
|
+
get childCount() {
|
|
163
|
+
return this._children.length;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 获取活跃状态
|
|
167
|
+
*
|
|
168
|
+
* @returns 如果实体处于活跃状态则返回true
|
|
169
|
+
*/
|
|
170
|
+
get active() {
|
|
171
|
+
return this._active;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* 设置活跃状态
|
|
175
|
+
*
|
|
176
|
+
* 设置实体的活跃状态,会影响子实体的有效活跃状态。
|
|
177
|
+
*
|
|
178
|
+
* @param value - 新的活跃状态
|
|
179
|
+
*/
|
|
180
|
+
set active(value) {
|
|
181
|
+
if (this._active !== value) {
|
|
182
|
+
this._active = value;
|
|
183
|
+
this.onActiveChanged();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* 获取实体的有效活跃状态
|
|
188
|
+
*
|
|
189
|
+
* 考虑父实体的活跃状态,只有当实体本身和所有父实体都处于活跃状态时才返回true。
|
|
190
|
+
*
|
|
191
|
+
* @returns 有效的活跃状态
|
|
192
|
+
*/
|
|
193
|
+
get activeInHierarchy() {
|
|
194
|
+
if (!this._active)
|
|
195
|
+
return false;
|
|
196
|
+
if (this._parent)
|
|
197
|
+
return this._parent.activeInHierarchy;
|
|
198
|
+
return true;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* 获取实体标签
|
|
202
|
+
*
|
|
203
|
+
* @returns 实体的数字标签
|
|
204
|
+
*/
|
|
205
|
+
get tag() {
|
|
206
|
+
return this._tag;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* 设置实体标签
|
|
210
|
+
*
|
|
211
|
+
* @param value - 新的标签值
|
|
212
|
+
*/
|
|
213
|
+
set tag(value) {
|
|
214
|
+
this._tag = value;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 获取启用状态
|
|
218
|
+
*
|
|
219
|
+
* @returns 如果实体已启用则返回true
|
|
220
|
+
*/
|
|
221
|
+
get enabled() {
|
|
222
|
+
return this._enabled;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* 设置启用状态
|
|
226
|
+
*
|
|
227
|
+
* @param value - 新的启用状态
|
|
228
|
+
*/
|
|
229
|
+
set enabled(value) {
|
|
230
|
+
this._enabled = value;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 获取更新顺序
|
|
234
|
+
*
|
|
235
|
+
* @returns 实体的更新顺序值
|
|
236
|
+
*/
|
|
237
|
+
get updateOrder() {
|
|
238
|
+
return this._updateOrder;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* 设置更新顺序
|
|
242
|
+
*
|
|
243
|
+
* @param value - 新的更新顺序值
|
|
244
|
+
*/
|
|
245
|
+
set updateOrder(value) {
|
|
246
|
+
this._updateOrder = value;
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* 获取组件位掩码
|
|
250
|
+
*
|
|
251
|
+
* @returns 实体的组件位掩码
|
|
252
|
+
*/
|
|
253
|
+
get componentMask() {
|
|
254
|
+
return this._componentMask;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* 创建并添加组件
|
|
258
|
+
*
|
|
259
|
+
* @param componentType - 组件类型
|
|
260
|
+
* @param args - 组件构造函数参数
|
|
261
|
+
* @returns 创建的组件实例
|
|
262
|
+
*/
|
|
263
|
+
createComponent(componentType, ...args) {
|
|
264
|
+
const component = new componentType(...args);
|
|
265
|
+
return this.addComponent(component);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 内部添加组件方法(不进行重复检查,用于初始化)
|
|
269
|
+
*
|
|
270
|
+
* @param component - 要添加的组件实例
|
|
271
|
+
* @returns 添加的组件实例
|
|
272
|
+
*/
|
|
273
|
+
addComponentInternal(component) {
|
|
274
|
+
const componentType = component.constructor;
|
|
275
|
+
// 注册组件类型(如果尚未注册)
|
|
276
|
+
if (!ComponentRegistry.isRegistered(componentType)) {
|
|
277
|
+
ComponentRegistry.register(componentType);
|
|
278
|
+
}
|
|
279
|
+
// 设置组件的实体引用
|
|
280
|
+
component.entity = this;
|
|
281
|
+
// 添加到组件列表并建立索引映射
|
|
282
|
+
const index = this.components.length;
|
|
283
|
+
this.components.push(component);
|
|
284
|
+
this._componentTypeToIndex.set(componentType, index);
|
|
285
|
+
// 更新位掩码
|
|
286
|
+
const componentMask = ComponentRegistry.getBitMask(componentType);
|
|
287
|
+
this._componentMask = this._componentMask.or(componentMask);
|
|
288
|
+
return component;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* 添加组件到实体
|
|
292
|
+
*
|
|
293
|
+
* @param component - 要添加的组件实例
|
|
294
|
+
* @returns 添加的组件实例
|
|
295
|
+
* @throws {Error} 如果组件类型已存在
|
|
296
|
+
*/
|
|
297
|
+
addComponent(component) {
|
|
298
|
+
const componentType = component.constructor;
|
|
299
|
+
// 检查是否已有此类型的组件
|
|
300
|
+
if (this.hasComponent(componentType)) {
|
|
301
|
+
throw new Error(`Entity ${this.name} already has component ${componentType.name}`);
|
|
302
|
+
}
|
|
303
|
+
// 使用内部方法添加组件
|
|
304
|
+
this.addComponentInternal(component);
|
|
305
|
+
// 如果场景存在且有组件存储管理器,添加到存储器
|
|
306
|
+
if (this.scene && this.scene.componentStorageManager) {
|
|
307
|
+
this.scene.componentStorageManager.addComponent(this.id, component);
|
|
308
|
+
}
|
|
309
|
+
// 调用组件的生命周期方法
|
|
310
|
+
component.onAddedToEntity();
|
|
311
|
+
// 发射组件添加事件
|
|
312
|
+
if (Entity.eventBus) {
|
|
313
|
+
Entity.eventBus.emitComponentAdded({
|
|
314
|
+
timestamp: Date.now(),
|
|
315
|
+
source: 'Entity',
|
|
316
|
+
entityId: this.id,
|
|
317
|
+
entityName: this.name,
|
|
318
|
+
entityTag: this.tag?.toString(),
|
|
319
|
+
componentType: componentType.name,
|
|
320
|
+
component: component
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
// 通知QuerySystem实体组件已改变,需要重新索引
|
|
324
|
+
if (this.scene && this.scene.querySystem) {
|
|
325
|
+
// 移除旧的索引,重新添加以更新索引
|
|
326
|
+
this.scene.querySystem.removeEntity(this);
|
|
327
|
+
this.scene.querySystem.addEntity(this);
|
|
328
|
+
}
|
|
329
|
+
return component;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* 获取指定类型的组件
|
|
333
|
+
*
|
|
334
|
+
* @param type - 组件类型
|
|
335
|
+
* @returns 组件实例或null
|
|
336
|
+
*/
|
|
337
|
+
getComponent(type) {
|
|
338
|
+
// 首先检查位掩码,快速排除(O(1))
|
|
339
|
+
if (!ComponentRegistry.isRegistered(type)) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
const mask = ComponentRegistry.getBitMask(type);
|
|
343
|
+
if (this._componentMask.and(mask).isZero()) {
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
// 尝试从索引映射获取(O(1))
|
|
347
|
+
const index = this._componentTypeToIndex.get(type);
|
|
348
|
+
if (index !== undefined && index < this.components.length) {
|
|
349
|
+
const component = this.components[index];
|
|
350
|
+
if (component && component.constructor === type) {
|
|
351
|
+
return component;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// 如果场景有组件存储管理器,从存储器获取
|
|
355
|
+
if (this.scene && this.scene.componentStorageManager) {
|
|
356
|
+
const component = this.scene.componentStorageManager.getComponent(this.id, type);
|
|
357
|
+
if (component) {
|
|
358
|
+
// 重建索引映射
|
|
359
|
+
this.rebuildComponentIndex();
|
|
360
|
+
return component;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// 最后回退到线性搜索并重建索引(O(n),但n很小且很少发生)
|
|
364
|
+
for (let i = 0; i < this.components.length; i++) {
|
|
365
|
+
const component = this.components[i];
|
|
366
|
+
if (component instanceof type) {
|
|
367
|
+
// 重建索引映射
|
|
368
|
+
this._componentTypeToIndex.set(type, i);
|
|
369
|
+
return component;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* 重建组件索引映射
|
|
376
|
+
*/
|
|
377
|
+
rebuildComponentIndex() {
|
|
378
|
+
this._componentTypeToIndex.clear();
|
|
379
|
+
for (let i = 0; i < this.components.length; i++) {
|
|
380
|
+
const component = this.components[i];
|
|
381
|
+
const componentType = component.constructor;
|
|
382
|
+
this._componentTypeToIndex.set(componentType, i);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
/**
|
|
386
|
+
* 检查实体是否有指定类型的组件
|
|
387
|
+
*
|
|
388
|
+
* @param type - 组件类型
|
|
389
|
+
* @returns 如果有该组件则返回true
|
|
390
|
+
*/
|
|
391
|
+
hasComponent(type) {
|
|
392
|
+
if (!ComponentRegistry.isRegistered(type)) {
|
|
393
|
+
return false;
|
|
394
|
+
}
|
|
395
|
+
const mask = ComponentRegistry.getBitMask(type);
|
|
396
|
+
return !this._componentMask.and(mask).isZero();
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* 获取或创建指定类型的组件
|
|
400
|
+
*
|
|
401
|
+
* @param type - 组件类型
|
|
402
|
+
* @param args - 组件构造函数参数(仅在创建时使用)
|
|
403
|
+
* @returns 组件实例
|
|
404
|
+
*/
|
|
405
|
+
getOrCreateComponent(type, ...args) {
|
|
406
|
+
let component = this.getComponent(type);
|
|
407
|
+
if (!component) {
|
|
408
|
+
component = this.createComponent(type, ...args);
|
|
409
|
+
}
|
|
410
|
+
return component;
|
|
411
|
+
}
|
|
412
|
+
/**
|
|
413
|
+
* 移除指定的组件
|
|
414
|
+
*
|
|
415
|
+
* @param component - 要移除的组件实例
|
|
416
|
+
*/
|
|
417
|
+
removeComponent(component) {
|
|
418
|
+
const componentType = component.constructor;
|
|
419
|
+
// 从组件列表中移除
|
|
420
|
+
const index = this.components.indexOf(component);
|
|
421
|
+
if (index !== -1) {
|
|
422
|
+
this.components.splice(index, 1);
|
|
423
|
+
// 重建索引映射(因为数组索引发生了变化)
|
|
424
|
+
this.rebuildComponentIndex();
|
|
425
|
+
}
|
|
426
|
+
// 更新位掩码
|
|
427
|
+
if (ComponentRegistry.isRegistered(componentType)) {
|
|
428
|
+
const componentMask = ComponentRegistry.getBitMask(componentType);
|
|
429
|
+
this._componentMask = this._componentMask.and(componentMask.not());
|
|
430
|
+
}
|
|
431
|
+
// 从组件存储管理器中移除
|
|
432
|
+
if (this.scene && this.scene.componentStorageManager) {
|
|
433
|
+
this.scene.componentStorageManager.removeComponent(this.id, componentType);
|
|
434
|
+
}
|
|
435
|
+
// 调用组件的生命周期方法
|
|
436
|
+
if (component.onRemovedFromEntity) {
|
|
437
|
+
component.onRemovedFromEntity();
|
|
438
|
+
}
|
|
439
|
+
// 发射组件移除事件
|
|
440
|
+
if (Entity.eventBus) {
|
|
441
|
+
Entity.eventBus.emitComponentRemoved({
|
|
442
|
+
timestamp: Date.now(),
|
|
443
|
+
source: 'Entity',
|
|
444
|
+
entityId: this.id,
|
|
445
|
+
entityName: this.name,
|
|
446
|
+
entityTag: this.tag?.toString(),
|
|
447
|
+
componentType: componentType.name,
|
|
448
|
+
component: component
|
|
449
|
+
});
|
|
450
|
+
}
|
|
451
|
+
// 清除组件的实体引用
|
|
452
|
+
component.entity = null;
|
|
453
|
+
// 通知QuerySystem实体组件已改变,需要重新索引
|
|
454
|
+
if (this.scene && this.scene.querySystem) {
|
|
455
|
+
// 移除旧的索引,重新添加以更新索引
|
|
456
|
+
this.scene.querySystem.removeEntity(this);
|
|
457
|
+
this.scene.querySystem.addEntity(this);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* 移除指定类型的组件
|
|
462
|
+
*
|
|
463
|
+
* @param type - 组件类型
|
|
464
|
+
* @returns 被移除的组件实例或null
|
|
465
|
+
*/
|
|
466
|
+
removeComponentByType(type) {
|
|
467
|
+
const component = this.getComponent(type);
|
|
468
|
+
if (component) {
|
|
469
|
+
this.removeComponent(component);
|
|
470
|
+
return component;
|
|
471
|
+
}
|
|
472
|
+
return null;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* 移除所有组件
|
|
476
|
+
*/
|
|
477
|
+
removeAllComponents() {
|
|
478
|
+
// 复制组件列表,避免在迭代时修改
|
|
479
|
+
const componentsToRemove = [...this.components];
|
|
480
|
+
// 清空索引和位掩码
|
|
481
|
+
this._componentTypeToIndex.clear();
|
|
482
|
+
this._componentMask = BigIntFactory.zero();
|
|
483
|
+
// 移除组件
|
|
484
|
+
for (const component of componentsToRemove) {
|
|
485
|
+
const componentType = component.constructor;
|
|
486
|
+
// 从组件存储管理器中移除
|
|
487
|
+
if (this.scene && this.scene.componentStorageManager) {
|
|
488
|
+
this.scene.componentStorageManager.removeComponent(this.id, componentType);
|
|
489
|
+
}
|
|
490
|
+
// 调用组件的生命周期方法
|
|
491
|
+
component.onRemovedFromEntity();
|
|
492
|
+
// 清除组件的实体引用
|
|
493
|
+
component.entity = null;
|
|
494
|
+
}
|
|
495
|
+
// 清空组件列表
|
|
496
|
+
this.components.length = 0;
|
|
497
|
+
}
|
|
498
|
+
/**
|
|
499
|
+
* 批量添加组件
|
|
500
|
+
*
|
|
501
|
+
* @param components - 要添加的组件数组
|
|
502
|
+
* @returns 添加的组件数组
|
|
503
|
+
*/
|
|
504
|
+
addComponents(components) {
|
|
505
|
+
const addedComponents = [];
|
|
506
|
+
for (const component of components) {
|
|
507
|
+
try {
|
|
508
|
+
addedComponents.push(this.addComponent(component));
|
|
509
|
+
}
|
|
510
|
+
catch (error) {
|
|
511
|
+
// 如果某个组件添加失败,继续添加其他组件
|
|
512
|
+
Entity._logger.warn(`添加组件失败 ${component.constructor.name}:`, error);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
return addedComponents;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* 批量移除组件类型
|
|
519
|
+
*
|
|
520
|
+
* @param componentTypes - 要移除的组件类型数组
|
|
521
|
+
* @returns 被移除的组件数组
|
|
522
|
+
*/
|
|
523
|
+
removeComponentsByTypes(componentTypes) {
|
|
524
|
+
const removedComponents = [];
|
|
525
|
+
for (const componentType of componentTypes) {
|
|
526
|
+
removedComponents.push(this.removeComponentByType(componentType));
|
|
527
|
+
}
|
|
528
|
+
return removedComponents;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* 获取所有指定类型的组件
|
|
532
|
+
*
|
|
533
|
+
* @param type - 组件类型
|
|
534
|
+
* @returns 组件实例数组
|
|
535
|
+
*/
|
|
536
|
+
getComponents(type) {
|
|
537
|
+
const result = [];
|
|
538
|
+
for (const component of this.components) {
|
|
539
|
+
if (component instanceof type) {
|
|
540
|
+
result.push(component);
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
return result;
|
|
544
|
+
}
|
|
545
|
+
/**
|
|
546
|
+
* 添加子实体
|
|
547
|
+
*
|
|
548
|
+
* @param child - 要添加的子实体
|
|
549
|
+
* @returns 添加的子实体
|
|
550
|
+
*/
|
|
551
|
+
addChild(child) {
|
|
552
|
+
if (child === this) {
|
|
553
|
+
throw new Error("Entity cannot be its own child");
|
|
554
|
+
}
|
|
555
|
+
if (child._parent === this) {
|
|
556
|
+
return child; // 已经是子实体
|
|
557
|
+
}
|
|
558
|
+
// 如果子实体已有父实体,先从原父实体移除
|
|
559
|
+
if (child._parent) {
|
|
560
|
+
child._parent.removeChild(child);
|
|
561
|
+
}
|
|
562
|
+
// 设置父子关系
|
|
563
|
+
child._parent = this;
|
|
564
|
+
this._children.push(child);
|
|
565
|
+
// 如果子实体还没有场景,设置为父实体的场景
|
|
566
|
+
if (!child.scene && this.scene) {
|
|
567
|
+
child.scene = this.scene;
|
|
568
|
+
this.scene.addEntity(child);
|
|
569
|
+
}
|
|
570
|
+
return child;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* 移除子实体
|
|
574
|
+
*
|
|
575
|
+
* @param child - 要移除的子实体
|
|
576
|
+
* @returns 是否成功移除
|
|
577
|
+
*/
|
|
578
|
+
removeChild(child) {
|
|
579
|
+
const index = this._children.indexOf(child);
|
|
580
|
+
if (index === -1) {
|
|
581
|
+
return false;
|
|
582
|
+
}
|
|
583
|
+
// 移除父子关系
|
|
584
|
+
this._children.splice(index, 1);
|
|
585
|
+
child._parent = null;
|
|
586
|
+
return true;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* 移除所有子实体
|
|
590
|
+
*/
|
|
591
|
+
removeAllChildren() {
|
|
592
|
+
// 复制子实体列表,避免在迭代时修改
|
|
593
|
+
const childrenToRemove = [...this._children];
|
|
594
|
+
for (const child of childrenToRemove) {
|
|
595
|
+
this.removeChild(child);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
/**
|
|
599
|
+
* 根据名称查找子实体
|
|
600
|
+
*
|
|
601
|
+
* @param name - 子实体名称
|
|
602
|
+
* @param recursive - 是否递归查找
|
|
603
|
+
* @returns 找到的子实体或null
|
|
604
|
+
*/
|
|
605
|
+
findChild(name, recursive = false) {
|
|
606
|
+
// 在直接子实体中查找
|
|
607
|
+
for (const child of this._children) {
|
|
608
|
+
if (child.name === name) {
|
|
609
|
+
return child;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
// 递归查找
|
|
613
|
+
if (recursive) {
|
|
614
|
+
for (const child of this._children) {
|
|
615
|
+
const found = child.findChild(name, true);
|
|
616
|
+
if (found) {
|
|
617
|
+
return found;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
/**
|
|
624
|
+
* 根据标签查找子实体
|
|
625
|
+
*
|
|
626
|
+
* @param tag - 标签
|
|
627
|
+
* @param recursive - 是否递归查找
|
|
628
|
+
* @returns 找到的子实体数组
|
|
629
|
+
*/
|
|
630
|
+
findChildrenByTag(tag, recursive = false) {
|
|
631
|
+
const result = [];
|
|
632
|
+
// 在直接子实体中查找
|
|
633
|
+
for (const child of this._children) {
|
|
634
|
+
if (child.tag === tag) {
|
|
635
|
+
result.push(child);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
// 递归查找
|
|
639
|
+
if (recursive) {
|
|
640
|
+
for (const child of this._children) {
|
|
641
|
+
result.push(...child.findChildrenByTag(tag, true));
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
return result;
|
|
645
|
+
}
|
|
646
|
+
/**
|
|
647
|
+
* 获取根实体
|
|
648
|
+
*
|
|
649
|
+
* @returns 层次结构的根实体
|
|
650
|
+
*/
|
|
651
|
+
getRoot() {
|
|
652
|
+
let root = this;
|
|
653
|
+
while (root._parent) {
|
|
654
|
+
root = root._parent;
|
|
655
|
+
}
|
|
656
|
+
return root;
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* 检查是否是指定实体的祖先
|
|
660
|
+
*
|
|
661
|
+
* @param entity - 要检查的实体
|
|
662
|
+
* @returns 如果是祖先则返回true
|
|
663
|
+
*/
|
|
664
|
+
isAncestorOf(entity) {
|
|
665
|
+
let current = entity._parent;
|
|
666
|
+
while (current) {
|
|
667
|
+
if (current === this) {
|
|
668
|
+
return true;
|
|
669
|
+
}
|
|
670
|
+
current = current._parent;
|
|
671
|
+
}
|
|
672
|
+
return false;
|
|
673
|
+
}
|
|
674
|
+
/**
|
|
675
|
+
* 检查是否是指定实体的后代
|
|
676
|
+
*
|
|
677
|
+
* @param entity - 要检查的实体
|
|
678
|
+
* @returns 如果是后代则返回true
|
|
679
|
+
*/
|
|
680
|
+
isDescendantOf(entity) {
|
|
681
|
+
return entity.isAncestorOf(this);
|
|
682
|
+
}
|
|
683
|
+
/**
|
|
684
|
+
* 获取层次深度
|
|
685
|
+
*
|
|
686
|
+
* @returns 在层次结构中的深度(根实体为0)
|
|
687
|
+
*/
|
|
688
|
+
getDepth() {
|
|
689
|
+
let depth = 0;
|
|
690
|
+
let current = this._parent;
|
|
691
|
+
while (current) {
|
|
692
|
+
depth++;
|
|
693
|
+
current = current._parent;
|
|
694
|
+
}
|
|
695
|
+
return depth;
|
|
696
|
+
}
|
|
697
|
+
/**
|
|
698
|
+
* 遍历所有子实体(深度优先)
|
|
699
|
+
*
|
|
700
|
+
* @param callback - 对每个子实体执行的回调函数
|
|
701
|
+
* @param recursive - 是否递归遍历
|
|
702
|
+
*/
|
|
703
|
+
forEachChild(callback, recursive = false) {
|
|
704
|
+
this._children.forEach((child, index) => {
|
|
705
|
+
callback(child, index);
|
|
706
|
+
if (recursive) {
|
|
707
|
+
child.forEachChild(callback, true);
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* 活跃状态改变时的回调
|
|
713
|
+
*/
|
|
714
|
+
onActiveChanged() {
|
|
715
|
+
// 通知所有组件活跃状态改变
|
|
716
|
+
for (const component of this.components) {
|
|
717
|
+
if ('onActiveChanged' in component && typeof component.onActiveChanged === 'function') {
|
|
718
|
+
component.onActiveChanged();
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
// 通知场景实体状态改变
|
|
722
|
+
if (this.scene && this.scene.eventSystem) {
|
|
723
|
+
this.scene.eventSystem.emitSync('entity:activeChanged', {
|
|
724
|
+
entity: this,
|
|
725
|
+
active: this._active,
|
|
726
|
+
activeInHierarchy: this.activeInHierarchy
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
/**
|
|
731
|
+
* 更新实体
|
|
732
|
+
*
|
|
733
|
+
* 调用所有组件的更新方法,并更新子实体。
|
|
734
|
+
*/
|
|
735
|
+
update() {
|
|
736
|
+
if (!this.activeInHierarchy || this._isDestroyed) {
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
// 更新所有组件
|
|
740
|
+
for (const component of this.components) {
|
|
741
|
+
if (component.enabled) {
|
|
742
|
+
component.update();
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
// 更新所有子实体
|
|
746
|
+
for (const child of this._children) {
|
|
747
|
+
child.update();
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
/**
|
|
751
|
+
* 销毁实体
|
|
752
|
+
*
|
|
753
|
+
* 移除所有组件、子实体并标记为已销毁。
|
|
754
|
+
*/
|
|
755
|
+
destroy() {
|
|
756
|
+
if (this._isDestroyed) {
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
this._isDestroyed = true;
|
|
760
|
+
// 销毁所有子实体
|
|
761
|
+
const childrenToDestroy = [...this._children];
|
|
762
|
+
for (const child of childrenToDestroy) {
|
|
763
|
+
child.destroy();
|
|
764
|
+
}
|
|
765
|
+
// 从父实体中移除
|
|
766
|
+
if (this._parent) {
|
|
767
|
+
this._parent.removeChild(this);
|
|
768
|
+
}
|
|
769
|
+
// 移除所有组件
|
|
770
|
+
this.removeAllComponents();
|
|
771
|
+
// 从场景中移除
|
|
772
|
+
if (this.scene) {
|
|
773
|
+
// 从查询系统中移除
|
|
774
|
+
if (this.scene.querySystem) {
|
|
775
|
+
this.scene.querySystem.removeEntity(this);
|
|
776
|
+
}
|
|
777
|
+
// 从实体列表中移除
|
|
778
|
+
if (this.scene.entities) {
|
|
779
|
+
this.scene.entities.remove(this);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
}
|
|
783
|
+
/**
|
|
784
|
+
* 比较实体
|
|
785
|
+
*
|
|
786
|
+
* @param other - 另一个实体
|
|
787
|
+
* @returns 比较结果
|
|
788
|
+
*/
|
|
789
|
+
compareTo(other) {
|
|
790
|
+
return EntityComparer.prototype.compare(this, other);
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* 获取实体的字符串表示
|
|
794
|
+
*
|
|
795
|
+
* @returns 实体的字符串描述
|
|
796
|
+
*/
|
|
797
|
+
toString() {
|
|
798
|
+
return `Entity[${this.name}:${this.id}]`;
|
|
799
|
+
}
|
|
800
|
+
/**
|
|
801
|
+
* 获取实体的调试信息(包含组件缓存信息)
|
|
802
|
+
*
|
|
803
|
+
* @returns 包含实体详细信息的对象
|
|
804
|
+
*/
|
|
805
|
+
getDebugInfo() {
|
|
806
|
+
return {
|
|
807
|
+
name: this.name,
|
|
808
|
+
id: this.id,
|
|
809
|
+
enabled: this._enabled,
|
|
810
|
+
active: this._active,
|
|
811
|
+
activeInHierarchy: this.activeInHierarchy,
|
|
812
|
+
destroyed: this._isDestroyed,
|
|
813
|
+
componentCount: this.components.length,
|
|
814
|
+
componentTypes: this.components.map(c => c.constructor.name),
|
|
815
|
+
componentMask: this._componentMask.toString(2), // 二进制表示
|
|
816
|
+
parentId: this._parent?.id || null,
|
|
817
|
+
childCount: this._children.length,
|
|
818
|
+
childIds: this._children.map(c => c.id),
|
|
819
|
+
depth: this.getDepth(),
|
|
820
|
+
indexMappingSize: this._componentTypeToIndex.size
|
|
821
|
+
};
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
/**
|
|
825
|
+
* Entity专用日志器
|
|
826
|
+
*/
|
|
827
|
+
Entity._logger = createLogger('Entity');
|
|
828
|
+
/**
|
|
829
|
+
* 实体比较器实例
|
|
830
|
+
*/
|
|
831
|
+
Entity.entityComparer = new EntityComparer();
|
|
832
|
+
/**
|
|
833
|
+
* 全局事件总线实例
|
|
834
|
+
* 用于发射组件相关事件
|
|
835
|
+
*/
|
|
836
|
+
Entity.eventBus = null;
|
|
837
|
+
//# sourceMappingURL=Entity.js.map
|