@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.
Files changed (290) hide show
  1. package/bin/Core.d.ts +279 -0
  2. package/bin/Core.d.ts.map +1 -0
  3. package/bin/Core.js +400 -0
  4. package/bin/Core.js.map +1 -0
  5. package/bin/ECS/Component.d.ts +120 -0
  6. package/bin/ECS/Component.d.ts.map +1 -0
  7. package/bin/ECS/Component.js +129 -0
  8. package/bin/ECS/Component.js.map +1 -0
  9. package/bin/ECS/Components/IUpdatable.d.ts +22 -0
  10. package/bin/ECS/Components/IUpdatable.d.ts.map +1 -0
  11. package/bin/ECS/Components/IUpdatable.js +17 -0
  12. package/bin/ECS/Components/IUpdatable.js.map +1 -0
  13. package/bin/ECS/Components/SceneComponent.d.ts +40 -0
  14. package/bin/ECS/Components/SceneComponent.d.ts.map +1 -0
  15. package/bin/ECS/Components/SceneComponent.js +57 -0
  16. package/bin/ECS/Components/SceneComponent.js.map +1 -0
  17. package/bin/ECS/Core/ArchetypeSystem.d.ts +100 -0
  18. package/bin/ECS/Core/ArchetypeSystem.d.ts.map +1 -0
  19. package/bin/ECS/Core/ArchetypeSystem.js +193 -0
  20. package/bin/ECS/Core/ArchetypeSystem.js.map +1 -0
  21. package/bin/ECS/Core/ComponentIndex.d.ts +151 -0
  22. package/bin/ECS/Core/ComponentIndex.d.ts.map +1 -0
  23. package/bin/ECS/Core/ComponentIndex.js +405 -0
  24. package/bin/ECS/Core/ComponentIndex.js.map +1 -0
  25. package/bin/ECS/Core/ComponentPool.d.ts +88 -0
  26. package/bin/ECS/Core/ComponentPool.d.ts.map +1 -0
  27. package/bin/ECS/Core/ComponentPool.js +159 -0
  28. package/bin/ECS/Core/ComponentPool.js.map +1 -0
  29. package/bin/ECS/Core/ComponentStorage/ComponentRegistry.d.ts +92 -0
  30. package/bin/ECS/Core/ComponentStorage/ComponentRegistry.d.ts.map +1 -0
  31. package/bin/ECS/Core/ComponentStorage/ComponentRegistry.js +167 -0
  32. package/bin/ECS/Core/ComponentStorage/ComponentRegistry.js.map +1 -0
  33. package/bin/ECS/Core/ComponentStorage.d.ts +233 -0
  34. package/bin/ECS/Core/ComponentStorage.d.ts.map +1 -0
  35. package/bin/ECS/Core/ComponentStorage.js +463 -0
  36. package/bin/ECS/Core/ComponentStorage.js.map +1 -0
  37. package/bin/ECS/Core/DirtyTrackingSystem.d.ts +197 -0
  38. package/bin/ECS/Core/DirtyTrackingSystem.d.ts.map +1 -0
  39. package/bin/ECS/Core/DirtyTrackingSystem.js +297 -0
  40. package/bin/ECS/Core/DirtyTrackingSystem.js.map +1 -0
  41. package/bin/ECS/Core/EntityManager.d.ts +379 -0
  42. package/bin/ECS/Core/EntityManager.d.ts.map +1 -0
  43. package/bin/ECS/Core/EntityManager.js +686 -0
  44. package/bin/ECS/Core/EntityManager.js.map +1 -0
  45. package/bin/ECS/Core/EventBus.d.ts +218 -0
  46. package/bin/ECS/Core/EventBus.d.ts.map +1 -0
  47. package/bin/ECS/Core/EventBus.js +402 -0
  48. package/bin/ECS/Core/EventBus.js.map +1 -0
  49. package/bin/ECS/Core/EventSystem.d.ts +237 -0
  50. package/bin/ECS/Core/EventSystem.d.ts.map +1 -0
  51. package/bin/ECS/Core/EventSystem.js +478 -0
  52. package/bin/ECS/Core/EventSystem.js.map +1 -0
  53. package/bin/ECS/Core/Events/index.d.ts +3 -0
  54. package/bin/ECS/Core/Events/index.d.ts.map +1 -0
  55. package/bin/ECS/Core/Events/index.js +3 -0
  56. package/bin/ECS/Core/Events/index.js.map +1 -0
  57. package/bin/ECS/Core/FluentAPI/ComponentBuilder.d.ts +35 -0
  58. package/bin/ECS/Core/FluentAPI/ComponentBuilder.d.ts.map +1 -0
  59. package/bin/ECS/Core/FluentAPI/ComponentBuilder.js +48 -0
  60. package/bin/ECS/Core/FluentAPI/ComponentBuilder.js.map +1 -0
  61. package/bin/ECS/Core/FluentAPI/ECSFluentAPI.d.ts +135 -0
  62. package/bin/ECS/Core/FluentAPI/ECSFluentAPI.d.ts.map +1 -0
  63. package/bin/ECS/Core/FluentAPI/ECSFluentAPI.js +166 -0
  64. package/bin/ECS/Core/FluentAPI/ECSFluentAPI.js.map +1 -0
  65. package/bin/ECS/Core/FluentAPI/EntityBatchOperator.d.ts +58 -0
  66. package/bin/ECS/Core/FluentAPI/EntityBatchOperator.d.ts.map +1 -0
  67. package/bin/ECS/Core/FluentAPI/EntityBatchOperator.js +85 -0
  68. package/bin/ECS/Core/FluentAPI/EntityBatchOperator.js.map +1 -0
  69. package/bin/ECS/Core/FluentAPI/EntityBuilder.d.ts +110 -0
  70. package/bin/ECS/Core/FluentAPI/EntityBuilder.d.ts.map +1 -0
  71. package/bin/ECS/Core/FluentAPI/EntityBuilder.js +174 -0
  72. package/bin/ECS/Core/FluentAPI/EntityBuilder.js.map +1 -0
  73. package/bin/ECS/Core/FluentAPI/SceneBuilder.d.ts +53 -0
  74. package/bin/ECS/Core/FluentAPI/SceneBuilder.d.ts.map +1 -0
  75. package/bin/ECS/Core/FluentAPI/SceneBuilder.js +79 -0
  76. package/bin/ECS/Core/FluentAPI/SceneBuilder.js.map +1 -0
  77. package/bin/ECS/Core/FluentAPI/index.d.ts +6 -0
  78. package/bin/ECS/Core/FluentAPI/index.d.ts.map +1 -0
  79. package/bin/ECS/Core/FluentAPI/index.js +6 -0
  80. package/bin/ECS/Core/FluentAPI/index.js.map +1 -0
  81. package/bin/ECS/Core/FluentAPI.d.ts +2 -0
  82. package/bin/ECS/Core/FluentAPI.d.ts.map +1 -0
  83. package/bin/ECS/Core/FluentAPI.js +3 -0
  84. package/bin/ECS/Core/FluentAPI.js.map +1 -0
  85. package/bin/ECS/Core/Performance/index.d.ts +4 -0
  86. package/bin/ECS/Core/Performance/index.d.ts.map +1 -0
  87. package/bin/ECS/Core/Performance/index.js +4 -0
  88. package/bin/ECS/Core/Performance/index.js.map +1 -0
  89. package/bin/ECS/Core/Query/index.d.ts +4 -0
  90. package/bin/ECS/Core/Query/index.d.ts.map +1 -0
  91. package/bin/ECS/Core/Query/index.js +4 -0
  92. package/bin/ECS/Core/Query/index.js.map +1 -0
  93. package/bin/ECS/Core/QuerySystem.d.ts +438 -0
  94. package/bin/ECS/Core/QuerySystem.d.ts.map +1 -0
  95. package/bin/ECS/Core/QuerySystem.js +974 -0
  96. package/bin/ECS/Core/QuerySystem.js.map +1 -0
  97. package/bin/ECS/Core/SoAStorage.d.ts +97 -0
  98. package/bin/ECS/Core/SoAStorage.d.ts.map +1 -0
  99. package/bin/ECS/Core/SoAStorage.js +548 -0
  100. package/bin/ECS/Core/SoAStorage.js.map +1 -0
  101. package/bin/ECS/Core/Storage/index.d.ts +4 -0
  102. package/bin/ECS/Core/Storage/index.d.ts.map +1 -0
  103. package/bin/ECS/Core/Storage/index.js +4 -0
  104. package/bin/ECS/Core/Storage/index.js.map +1 -0
  105. package/bin/ECS/CoreEvents.d.ts +131 -0
  106. package/bin/ECS/CoreEvents.d.ts.map +1 -0
  107. package/bin/ECS/CoreEvents.js +163 -0
  108. package/bin/ECS/CoreEvents.js.map +1 -0
  109. package/bin/ECS/Entity.d.ts +455 -0
  110. package/bin/ECS/Entity.d.ts.map +1 -0
  111. package/bin/ECS/Entity.js +837 -0
  112. package/bin/ECS/Entity.js.map +1 -0
  113. package/bin/ECS/Scene.d.ts +225 -0
  114. package/bin/ECS/Scene.d.ts.map +1 -0
  115. package/bin/ECS/Scene.js +325 -0
  116. package/bin/ECS/Scene.js.map +1 -0
  117. package/bin/ECS/Systems/EntitySystem.d.ts +200 -0
  118. package/bin/ECS/Systems/EntitySystem.d.ts.map +1 -0
  119. package/bin/ECS/Systems/EntitySystem.js +431 -0
  120. package/bin/ECS/Systems/EntitySystem.js.map +1 -0
  121. package/bin/ECS/Systems/IntervalSystem.d.ts +33 -0
  122. package/bin/ECS/Systems/IntervalSystem.d.ts.map +1 -0
  123. package/bin/ECS/Systems/IntervalSystem.js +50 -0
  124. package/bin/ECS/Systems/IntervalSystem.js.map +1 -0
  125. package/bin/ECS/Systems/PassiveSystem.d.ts +17 -0
  126. package/bin/ECS/Systems/PassiveSystem.d.ts.map +1 -0
  127. package/bin/ECS/Systems/PassiveSystem.js +19 -0
  128. package/bin/ECS/Systems/PassiveSystem.js.map +1 -0
  129. package/bin/ECS/Systems/ProcessingSystem.d.ts +21 -0
  130. package/bin/ECS/Systems/ProcessingSystem.d.ts.map +1 -0
  131. package/bin/ECS/Systems/ProcessingSystem.js +20 -0
  132. package/bin/ECS/Systems/ProcessingSystem.js.map +1 -0
  133. package/bin/ECS/Systems/index.d.ts +5 -0
  134. package/bin/ECS/Systems/index.d.ts.map +1 -0
  135. package/bin/ECS/Systems/index.js +6 -0
  136. package/bin/ECS/Systems/index.js.map +1 -0
  137. package/bin/ECS/Utils/BigIntCompatibility.d.ts +164 -0
  138. package/bin/ECS/Utils/BigIntCompatibility.d.ts.map +1 -0
  139. package/bin/ECS/Utils/BigIntCompatibility.js +578 -0
  140. package/bin/ECS/Utils/BigIntCompatibility.js.map +1 -0
  141. package/bin/ECS/Utils/Bits.d.ts +163 -0
  142. package/bin/ECS/Utils/Bits.d.ts.map +1 -0
  143. package/bin/ECS/Utils/Bits.js +286 -0
  144. package/bin/ECS/Utils/Bits.js.map +1 -0
  145. package/bin/ECS/Utils/ComponentTypeManager.d.ts +50 -0
  146. package/bin/ECS/Utils/ComponentTypeManager.d.ts.map +1 -0
  147. package/bin/ECS/Utils/ComponentTypeManager.js +84 -0
  148. package/bin/ECS/Utils/ComponentTypeManager.js.map +1 -0
  149. package/bin/ECS/Utils/EntityList.d.ts +108 -0
  150. package/bin/ECS/Utils/EntityList.d.ts.map +1 -0
  151. package/bin/ECS/Utils/EntityList.js +262 -0
  152. package/bin/ECS/Utils/EntityList.js.map +1 -0
  153. package/bin/ECS/Utils/EntityProcessorList.d.ts +56 -0
  154. package/bin/ECS/Utils/EntityProcessorList.d.ts.map +1 -0
  155. package/bin/ECS/Utils/EntityProcessorList.js +102 -0
  156. package/bin/ECS/Utils/EntityProcessorList.js.map +1 -0
  157. package/bin/ECS/Utils/IdentifierPool.d.ts +203 -0
  158. package/bin/ECS/Utils/IdentifierPool.d.ts.map +1 -0
  159. package/bin/ECS/Utils/IdentifierPool.js +325 -0
  160. package/bin/ECS/Utils/IdentifierPool.js.map +1 -0
  161. package/bin/ECS/Utils/Matcher.d.ts +143 -0
  162. package/bin/ECS/Utils/Matcher.d.ts.map +1 -0
  163. package/bin/ECS/Utils/Matcher.js +249 -0
  164. package/bin/ECS/Utils/Matcher.js.map +1 -0
  165. package/bin/ECS/Utils/index.d.ts +8 -0
  166. package/bin/ECS/Utils/index.d.ts.map +1 -0
  167. package/bin/ECS/Utils/index.js +9 -0
  168. package/bin/ECS/Utils/index.js.map +1 -0
  169. package/bin/ECS/index.d.ts +12 -0
  170. package/bin/ECS/index.d.ts.map +1 -0
  171. package/bin/ECS/index.js +12 -0
  172. package/bin/ECS/index.js.map +1 -0
  173. package/bin/Types/index.d.ts +471 -0
  174. package/bin/Types/index.d.ts.map +1 -0
  175. package/bin/Types/index.js +5 -0
  176. package/bin/Types/index.js.map +1 -0
  177. package/bin/Utils/Debug/ComponentDataCollector.d.ts +28 -0
  178. package/bin/Utils/Debug/ComponentDataCollector.d.ts.map +1 -0
  179. package/bin/Utils/Debug/ComponentDataCollector.js +248 -0
  180. package/bin/Utils/Debug/ComponentDataCollector.js.map +1 -0
  181. package/bin/Utils/Debug/DebugDataFormatter.d.ts +22 -0
  182. package/bin/Utils/Debug/DebugDataFormatter.d.ts.map +1 -0
  183. package/bin/Utils/Debug/DebugDataFormatter.js +117 -0
  184. package/bin/Utils/Debug/DebugDataFormatter.js.map +1 -0
  185. package/bin/Utils/Debug/DebugManager.d.ts +104 -0
  186. package/bin/Utils/Debug/DebugManager.d.ts.map +1 -0
  187. package/bin/Utils/Debug/DebugManager.js +647 -0
  188. package/bin/Utils/Debug/DebugManager.js.map +1 -0
  189. package/bin/Utils/Debug/EntityDataCollector.d.ts +92 -0
  190. package/bin/Utils/Debug/EntityDataCollector.d.ts.map +1 -0
  191. package/bin/Utils/Debug/EntityDataCollector.js +864 -0
  192. package/bin/Utils/Debug/EntityDataCollector.js.map +1 -0
  193. package/bin/Utils/Debug/PerformanceDataCollector.d.ts +32 -0
  194. package/bin/Utils/Debug/PerformanceDataCollector.d.ts.map +1 -0
  195. package/bin/Utils/Debug/PerformanceDataCollector.js +217 -0
  196. package/bin/Utils/Debug/PerformanceDataCollector.js.map +1 -0
  197. package/bin/Utils/Debug/SceneDataCollector.d.ts +16 -0
  198. package/bin/Utils/Debug/SceneDataCollector.d.ts.map +1 -0
  199. package/bin/Utils/Debug/SceneDataCollector.js +46 -0
  200. package/bin/Utils/Debug/SceneDataCollector.js.map +1 -0
  201. package/bin/Utils/Debug/SystemDataCollector.d.ts +11 -0
  202. package/bin/Utils/Debug/SystemDataCollector.d.ts.map +1 -0
  203. package/bin/Utils/Debug/SystemDataCollector.js +59 -0
  204. package/bin/Utils/Debug/SystemDataCollector.js.map +1 -0
  205. package/bin/Utils/Debug/WebSocketManager.d.ts +59 -0
  206. package/bin/Utils/Debug/WebSocketManager.d.ts.map +1 -0
  207. package/bin/Utils/Debug/WebSocketManager.js +148 -0
  208. package/bin/Utils/Debug/WebSocketManager.js.map +1 -0
  209. package/bin/Utils/Debug/index.d.ts +8 -0
  210. package/bin/Utils/Debug/index.d.ts.map +1 -0
  211. package/bin/Utils/Debug/index.js +8 -0
  212. package/bin/Utils/Debug/index.js.map +1 -0
  213. package/bin/Utils/Emitter.d.ts +43 -0
  214. package/bin/Utils/Emitter.d.ts.map +1 -0
  215. package/bin/Utils/Emitter.js +69 -0
  216. package/bin/Utils/Emitter.js.map +1 -0
  217. package/bin/Utils/Extensions/NumberExtension.d.ts +13 -0
  218. package/bin/Utils/Extensions/NumberExtension.d.ts.map +1 -0
  219. package/bin/Utils/Extensions/NumberExtension.js +17 -0
  220. package/bin/Utils/Extensions/NumberExtension.js.map +1 -0
  221. package/bin/Utils/Extensions/TypeUtils.d.ts +13 -0
  222. package/bin/Utils/Extensions/TypeUtils.d.ts.map +1 -0
  223. package/bin/Utils/Extensions/TypeUtils.js +15 -0
  224. package/bin/Utils/Extensions/TypeUtils.js.map +1 -0
  225. package/bin/Utils/Extensions/index.d.ts +3 -0
  226. package/bin/Utils/Extensions/index.d.ts.map +1 -0
  227. package/bin/Utils/Extensions/index.js +4 -0
  228. package/bin/Utils/Extensions/index.js.map +1 -0
  229. package/bin/Utils/GlobalManager.d.ts +32 -0
  230. package/bin/Utils/GlobalManager.d.ts.map +1 -0
  231. package/bin/Utils/GlobalManager.js +53 -0
  232. package/bin/Utils/GlobalManager.js.map +1 -0
  233. package/bin/Utils/Logger.d.ts +156 -0
  234. package/bin/Utils/Logger.d.ts.map +1 -0
  235. package/bin/Utils/Logger.js +256 -0
  236. package/bin/Utils/Logger.js.map +1 -0
  237. package/bin/Utils/PerformanceMonitor.d.ts +211 -0
  238. package/bin/Utils/PerformanceMonitor.d.ts.map +1 -0
  239. package/bin/Utils/PerformanceMonitor.js +270 -0
  240. package/bin/Utils/PerformanceMonitor.js.map +1 -0
  241. package/bin/Utils/Pool/IPoolable.d.ts +29 -0
  242. package/bin/Utils/Pool/IPoolable.d.ts.map +1 -0
  243. package/bin/Utils/Pool/IPoolable.js +2 -0
  244. package/bin/Utils/Pool/IPoolable.js.map +1 -0
  245. package/bin/Utils/Pool/Pool.d.ts +109 -0
  246. package/bin/Utils/Pool/Pool.d.ts.map +1 -0
  247. package/bin/Utils/Pool/Pool.js +238 -0
  248. package/bin/Utils/Pool/Pool.js.map +1 -0
  249. package/bin/Utils/Pool/PoolManager.d.ts +91 -0
  250. package/bin/Utils/Pool/PoolManager.d.ts.map +1 -0
  251. package/bin/Utils/Pool/PoolManager.js +197 -0
  252. package/bin/Utils/Pool/PoolManager.js.map +1 -0
  253. package/bin/Utils/Pool/index.d.ts +4 -0
  254. package/bin/Utils/Pool/index.d.ts.map +1 -0
  255. package/bin/Utils/Pool/index.js +4 -0
  256. package/bin/Utils/Pool/index.js.map +1 -0
  257. package/bin/Utils/Time.d.ts +47 -0
  258. package/bin/Utils/Time.d.ts.map +1 -0
  259. package/bin/Utils/Time.js +64 -0
  260. package/bin/Utils/Time.js.map +1 -0
  261. package/bin/Utils/Timers/ITimer.d.ts +16 -0
  262. package/bin/Utils/Timers/ITimer.d.ts.map +1 -0
  263. package/bin/Utils/Timers/ITimer.js +2 -0
  264. package/bin/Utils/Timers/ITimer.js.map +1 -0
  265. package/bin/Utils/Timers/Timer.d.ts +30 -0
  266. package/bin/Utils/Timers/Timer.d.ts.map +1 -0
  267. package/bin/Utils/Timers/Timer.js +58 -0
  268. package/bin/Utils/Timers/Timer.js.map +1 -0
  269. package/bin/Utils/Timers/TimerManager.d.ts +19 -0
  270. package/bin/Utils/Timers/TimerManager.d.ts.map +1 -0
  271. package/bin/Utils/Timers/TimerManager.js +33 -0
  272. package/bin/Utils/Timers/TimerManager.js.map +1 -0
  273. package/bin/Utils/index.d.ts +9 -0
  274. package/bin/Utils/index.d.ts.map +1 -0
  275. package/bin/Utils/index.js +9 -0
  276. package/bin/Utils/index.js.map +1 -0
  277. package/bin/index.d.ts +16 -0
  278. package/bin/index.d.ts.map +1 -0
  279. package/bin/index.js +19 -0
  280. package/bin/index.js.map +1 -0
  281. package/package.json +52 -25
  282. package/.npmignore +0 -40
  283. package/LICENSE +0 -201
  284. package/README.md +0 -465
  285. package/SECURITY.md +0 -53
  286. package/index.cjs +0 -2
  287. package/index.cjs.map +0 -1
  288. package/index.d.ts +0 -6562
  289. package/index.mjs +0 -2
  290. package/index.mjs.map +0 -1
@@ -0,0 +1,686 @@
1
+ import { Entity } from '../Entity';
2
+ import { IdentifierPool } from '../Utils/IdentifierPool';
3
+ import { ComponentIndexManager, IndexType } from './ComponentIndex';
4
+ import { ArchetypeSystem } from './ArchetypeSystem';
5
+ import { DirtyTrackingSystem, DirtyFlag } from './DirtyTrackingSystem';
6
+ import { EventBus } from './EventBus';
7
+ /**
8
+ * 实体查询构建器
9
+ *
10
+ * 提供流式API来构建复杂的实体查询条件。支持组件过滤、标签过滤、状态过滤和自定义条件。
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * const results = entityManager.query()
15
+ * .withAll(PositionComponent, HealthComponent)
16
+ * .without(VelocityComponent)
17
+ * .withTag(1)
18
+ * .active()
19
+ * .where(entity => entity.name.startsWith("Player"))
20
+ * .execute();
21
+ * ```
22
+ */
23
+ export class EntityQueryBuilder {
24
+ /**
25
+ * 创建查询构建器实例
26
+ * @param entityManager 实体管理器实例
27
+ */
28
+ constructor(entityManager) {
29
+ this.entityManager = entityManager;
30
+ /** 必须包含的组件类型 */
31
+ this._allComponents = [];
32
+ /** 至少包含一个的组件类型 */
33
+ this._anyComponents = [];
34
+ /** 不能包含的组件类型 */
35
+ this._withoutComponents = [];
36
+ /** 必须包含的标签 */
37
+ this._withTags = [];
38
+ /** 不能包含的标签 */
39
+ this._withoutTags = [];
40
+ /** 是否只查询激活状态的实体 */
41
+ this._activeOnly = false;
42
+ /** 是否只查询启用状态的实体 */
43
+ this._enabledOnly = false;
44
+ /** 自定义过滤条件 */
45
+ this._customPredicates = [];
46
+ }
47
+ /**
48
+ * 添加必须包含的组件条件
49
+ *
50
+ * 返回的实体必须包含所有指定的组件类型。
51
+ *
52
+ * @param componentTypes 组件类型列表
53
+ * @returns 查询构建器实例,支持链式调用
54
+ */
55
+ withAll(...componentTypes) {
56
+ this._allComponents.push(...componentTypes);
57
+ return this;
58
+ }
59
+ /**
60
+ * 添加至少包含一个的组件条件
61
+ *
62
+ * 返回的实体必须至少包含其中一个指定的组件类型。
63
+ *
64
+ * @param componentTypes 组件类型列表
65
+ * @returns 查询构建器实例,支持链式调用
66
+ */
67
+ withAny(...componentTypes) {
68
+ this._anyComponents.push(...componentTypes);
69
+ return this;
70
+ }
71
+ /**
72
+ * 添加不能包含的组件条件
73
+ *
74
+ * 返回的实体不能包含任何指定的组件类型。
75
+ *
76
+ * @param componentTypes 组件类型列表
77
+ * @returns 查询构建器实例,支持链式调用
78
+ */
79
+ without(...componentTypes) {
80
+ this._withoutComponents.push(...componentTypes);
81
+ return this;
82
+ }
83
+ /**
84
+ * 添加必须包含的标签条件
85
+ *
86
+ * 返回的实体必须具有指定的标签。
87
+ *
88
+ * @param tag 标签值
89
+ * @returns 查询构建器实例,支持链式调用
90
+ */
91
+ withTag(tag) {
92
+ this._withTags.push(tag);
93
+ return this;
94
+ }
95
+ /**
96
+ * 添加不能包含的标签条件
97
+ *
98
+ * 返回的实体不能具有指定的标签。
99
+ *
100
+ * @param tag 标签值
101
+ * @returns 查询构建器实例,支持链式调用
102
+ */
103
+ withoutTag(tag) {
104
+ this._withoutTags.push(tag);
105
+ return this;
106
+ }
107
+ /**
108
+ * 添加激活状态过滤条件
109
+ *
110
+ * 返回的实体必须处于激活状态(active = true)。
111
+ *
112
+ * @returns 查询构建器实例,支持链式调用
113
+ */
114
+ active() {
115
+ this._activeOnly = true;
116
+ return this;
117
+ }
118
+ /**
119
+ * 添加启用状态过滤条件
120
+ *
121
+ * 返回的实体必须处于启用状态(enabled = true)。
122
+ *
123
+ * @returns 查询构建器实例,支持链式调用
124
+ */
125
+ enabled() {
126
+ this._enabledOnly = true;
127
+ return this;
128
+ }
129
+ /**
130
+ * 添加自定义过滤条件
131
+ *
132
+ * 允许用户定义复杂的过滤逻辑。
133
+ *
134
+ * @param predicate 自定义过滤函数,接收实体作为参数,返回布尔值
135
+ * @returns 查询构建器实例,支持链式调用
136
+ *
137
+ * @example
138
+ * ```typescript
139
+ * .where(entity => entity.name.startsWith("Player"))
140
+ * .where(entity => entity.components.length > 5)
141
+ * ```
142
+ */
143
+ where(predicate) {
144
+ this._customPredicates.push(predicate);
145
+ return this;
146
+ }
147
+ /**
148
+ * 执行查询并返回所有匹配的实体
149
+ *
150
+ * @returns 符合所有查询条件的实体数组
151
+ */
152
+ execute() {
153
+ let candidates = [];
154
+ if (this._allComponents.length > 0) {
155
+ const indexResult = this.entityManager.queryWithComponentIndex(this._allComponents, 'AND');
156
+ candidates = Array.from(indexResult);
157
+ }
158
+ else if (this._anyComponents.length > 0) {
159
+ const indexResult = this.entityManager.queryWithComponentIndex(this._anyComponents, 'OR');
160
+ candidates = Array.from(indexResult);
161
+ }
162
+ else {
163
+ candidates = this.entityManager.getAllEntities();
164
+ }
165
+ return candidates.filter(entity => this.matchesEntity(entity));
166
+ }
167
+ /**
168
+ * 执行查询并返回第一个匹配的实体
169
+ *
170
+ * @returns 第一个符合查询条件的实体,如果没有找到则返回null
171
+ */
172
+ first() {
173
+ const entities = this.entityManager.getAllEntities();
174
+ return entities.find(entity => this.matchesEntity(entity)) || null;
175
+ }
176
+ /**
177
+ * 执行查询并返回匹配实体的数量
178
+ *
179
+ * @returns 符合查询条件的实体数量
180
+ */
181
+ count() {
182
+ const entities = this.entityManager.getAllEntities();
183
+ return entities.filter(entity => this.matchesEntity(entity)).length;
184
+ }
185
+ /**
186
+ * 对所有匹配的实体执行指定操作
187
+ *
188
+ * @param action 要执行的操作函数,接收匹配的实体作为参数
189
+ */
190
+ forEach(action) {
191
+ const entities = this.entityManager.getAllEntities();
192
+ entities.forEach(entity => {
193
+ if (this.matchesEntity(entity)) {
194
+ action(entity);
195
+ }
196
+ });
197
+ }
198
+ /**
199
+ * 检查实体是否匹配所有查询条件
200
+ *
201
+ * 按优先级顺序检查各种过滤条件,一旦发现不匹配立即返回false。
202
+ *
203
+ * @param entity 要检查的实体
204
+ * @returns 实体是否匹配所有查询条件
205
+ */
206
+ matchesEntity(entity) {
207
+ // 检查激活状态
208
+ if (this._activeOnly && !entity.active) {
209
+ return false;
210
+ }
211
+ // 检查启用状态
212
+ if (this._enabledOnly && !entity.enabled) {
213
+ return false;
214
+ }
215
+ // 检查必须包含的组件
216
+ if (this._allComponents.length > 0) {
217
+ for (const componentType of this._allComponents) {
218
+ if (!entity.hasComponent(componentType)) {
219
+ return false;
220
+ }
221
+ }
222
+ }
223
+ // 检查至少包含一个的组件
224
+ if (this._anyComponents.length > 0) {
225
+ let hasAny = false;
226
+ for (const componentType of this._anyComponents) {
227
+ if (entity.hasComponent(componentType)) {
228
+ hasAny = true;
229
+ break;
230
+ }
231
+ }
232
+ if (!hasAny) {
233
+ return false;
234
+ }
235
+ }
236
+ // 检查不能包含的组件
237
+ if (this._withoutComponents.length > 0) {
238
+ for (const componentType of this._withoutComponents) {
239
+ if (entity.hasComponent(componentType)) {
240
+ return false;
241
+ }
242
+ }
243
+ }
244
+ // 检查必须包含的标签
245
+ if (this._withTags.length > 0) {
246
+ if (!this._withTags.includes(entity.tag)) {
247
+ return false;
248
+ }
249
+ }
250
+ // 检查不能包含的标签
251
+ if (this._withoutTags.length > 0) {
252
+ if (this._withoutTags.includes(entity.tag)) {
253
+ return false;
254
+ }
255
+ }
256
+ // 检查自定义条件
257
+ if (this._customPredicates.length > 0) {
258
+ for (const predicate of this._customPredicates) {
259
+ if (!predicate(entity)) {
260
+ return false;
261
+ }
262
+ }
263
+ }
264
+ return true;
265
+ }
266
+ }
267
+ /**
268
+ * 实体管理器
269
+ *
270
+ * 提供统一的实体管理和查询机制,支持高效的实体操作。
271
+ * 包括实体的创建、销毁、查询和索引管理功能。
272
+ *
273
+ * @example
274
+ * ```typescript
275
+ * const entityManager = new EntityManager();
276
+ *
277
+ * // 创建实体
278
+ * const player = entityManager.createEntity("Player");
279
+ *
280
+ * // 查询实体
281
+ * const playerEntity = entityManager.getEntityByName("Player");
282
+ *
283
+ * // 复杂查询
284
+ * const results = entityManager.query()
285
+ * .withAll(HealthComponent, PositionComponent)
286
+ * .active()
287
+ * .execute();
288
+ * ```
289
+ */
290
+ export class EntityManager {
291
+ /**
292
+ * 创建实体管理器实例
293
+ *
294
+ * 初始化内部数据结构和ID分配器。
295
+ */
296
+ constructor() {
297
+ /** 主要实体存储,使用ID作为键 */
298
+ this._entities = new Map();
299
+ /** 按名称索引的实体映射 */
300
+ this._entitiesByName = new Map();
301
+ /** 按标签索引的实体映射 */
302
+ this._entitiesByTag = new Map();
303
+ /** 已销毁实体的ID集合 */
304
+ this._destroyedEntities = new Set();
305
+ this._identifierPool = new IdentifierPool();
306
+ // 初始化性能优化系统
307
+ this._componentIndexManager = new ComponentIndexManager(IndexType.HASH);
308
+ this._archetypeSystem = new ArchetypeSystem();
309
+ this._dirtyTrackingSystem = new DirtyTrackingSystem();
310
+ this._eventBus = new EventBus(false);
311
+ // 设置Entity的静态事件总线引用
312
+ Entity.eventBus = this._eventBus;
313
+ // 监听组件事件来同步更新索引
314
+ this._eventBus.on('component:added', (data) => {
315
+ const entity = this._entities.get(data.entityId);
316
+ if (entity) {
317
+ this._componentIndexManager.addEntity(entity);
318
+ this._archetypeSystem.addEntity(entity);
319
+ }
320
+ });
321
+ this._eventBus.on('component:removed', (data) => {
322
+ const entity = this._entities.get(data.entityId);
323
+ if (entity) {
324
+ this._componentIndexManager.removeEntity(entity);
325
+ this._archetypeSystem.removeEntity(entity);
326
+ }
327
+ });
328
+ }
329
+ /**
330
+ * 获取实体总数
331
+ *
332
+ * @returns 当前管理的实体总数量
333
+ */
334
+ get entityCount() {
335
+ return this._entities.size;
336
+ }
337
+ /**
338
+ * 获取激活状态的实体数量
339
+ *
340
+ * 只计算同时满足激活状态且未被销毁的实体。
341
+ *
342
+ * @returns 激活状态的实体数量
343
+ */
344
+ get activeEntityCount() {
345
+ let count = 0;
346
+ for (const entity of this._entities.values()) {
347
+ if (entity.active && !entity.isDestroyed) {
348
+ count++;
349
+ }
350
+ }
351
+ return count;
352
+ }
353
+ /**
354
+ * 创建新实体
355
+ *
356
+ * 分配唯一ID并将实体添加到管理系统中,同时更新相关索引。
357
+ *
358
+ * @param name 实体名称,如果未指定则使用时间戳生成默认名称
359
+ * @returns 创建的实体实例
360
+ *
361
+ * @example
362
+ * ```typescript
363
+ * const player = entityManager.createEntity("Player");
364
+ * const enemy = entityManager.createEntity(); // 使用默认名称
365
+ * ```
366
+ */
367
+ createEntity(name) {
368
+ const id = this._identifierPool.checkOut();
369
+ if (!name) {
370
+ name = `Entity_${id}`;
371
+ }
372
+ const entity = new Entity(name, id);
373
+ this._entities.set(id, entity);
374
+ this.updateNameIndex(entity, true);
375
+ this.updateTagIndex(entity, true);
376
+ if (entity.components.length > 0) {
377
+ this._componentIndexManager.addEntity(entity);
378
+ this._archetypeSystem.addEntity(entity);
379
+ this._dirtyTrackingSystem.markDirty(entity, DirtyFlag.COMPONENT_ADDED);
380
+ }
381
+ // 发射实体创建事件
382
+ this._eventBus.emitEntityCreated({
383
+ timestamp: Date.now(),
384
+ source: 'EntityManager',
385
+ entityId: entity.id,
386
+ entityName: entity.name,
387
+ entityTag: entity.tag?.toString()
388
+ });
389
+ return entity;
390
+ }
391
+ /**
392
+ * 批量创建实体
393
+ *
394
+ * 为了优化大量实体创建的性能,批量处理索引更新和事件发射。
395
+ * 适用于需要创建大量实体的场景,如子弹、粒子等。
396
+ *
397
+ * @param count 要创建的实体数量
398
+ * @param namePrefix 实体名称前缀,默认为 Entity
399
+ * @param skipEvents 是否跳过事件发射以提升性能,默认为 false
400
+ * @returns 创建的实体数组
401
+ *
402
+ * @example
403
+ * const bullets = entityManager.createEntitiesBatch(100, "Bullet", true);
404
+ * const particles = entityManager.createEntitiesBatch(500, "Particle");
405
+ */
406
+ createEntitiesBatch(count, namePrefix = "Entity", skipEvents = false) {
407
+ if (count <= 0)
408
+ return [];
409
+ const entities = [];
410
+ // 批量分配ID和创建Entity对象
411
+ for (let i = 0; i < count; i++) {
412
+ const id = this._identifierPool.checkOut();
413
+ const name = `${namePrefix}_${id}`;
414
+ const entity = new Entity(name, id);
415
+ entities.push(entity);
416
+ this._entities.set(id, entity);
417
+ }
418
+ // 批量更新索引
419
+ for (const entity of entities) {
420
+ this.updateNameIndex(entity, true);
421
+ this.updateTagIndex(entity, true);
422
+ if (entity.components.length > 0) {
423
+ this._componentIndexManager.addEntity(entity);
424
+ this._archetypeSystem.addEntity(entity);
425
+ this._dirtyTrackingSystem.markDirty(entity, DirtyFlag.COMPONENT_ADDED);
426
+ }
427
+ }
428
+ // 批量发射事件
429
+ if (!skipEvents) {
430
+ const timestamp = Date.now();
431
+ for (const entity of entities) {
432
+ this._eventBus.emitEntityCreated({
433
+ timestamp,
434
+ source: 'EntityManager',
435
+ entityId: entity.id,
436
+ entityName: entity.name,
437
+ entityTag: entity.tag?.toString()
438
+ });
439
+ }
440
+ }
441
+ return entities;
442
+ }
443
+ /**
444
+ * 销毁实体
445
+ *
446
+ * 支持通过实体对象、名称或ID来销毁实体。
447
+ * 会清理所有相关索引并回收ID。
448
+ *
449
+ * @param entityOrId 要销毁的实体,可以是实体对象、名称字符串或ID数字
450
+ * @returns 是否成功销毁实体
451
+ *
452
+ * @example
453
+ * ```typescript
454
+ * // 通过实体对象销毁
455
+ * entityManager.destroyEntity(player);
456
+ *
457
+ * // 通过名称销毁
458
+ * entityManager.destroyEntity("Enemy_1");
459
+ *
460
+ * // 通过ID销毁
461
+ * entityManager.destroyEntity(123);
462
+ * ```
463
+ */
464
+ destroyEntity(entityOrId) {
465
+ let entity = null;
466
+ if (typeof entityOrId === 'string') {
467
+ entity = this.getEntityByName(entityOrId);
468
+ }
469
+ else if (typeof entityOrId === 'number') {
470
+ entity = this._entities.get(entityOrId) || null;
471
+ }
472
+ else {
473
+ entity = this._entities.get(entityOrId.id) || null;
474
+ }
475
+ if (!entity) {
476
+ return false;
477
+ }
478
+ this._destroyedEntities.add(entity.id);
479
+ this.updateNameIndex(entity, false);
480
+ this.updateTagIndex(entity, false);
481
+ this._componentIndexManager.removeEntity(entity);
482
+ this._archetypeSystem.removeEntity(entity);
483
+ this._dirtyTrackingSystem.markDirty(entity, DirtyFlag.COMPONENT_REMOVED);
484
+ // 发射实体销毁事件
485
+ this._eventBus.emitEntityDestroyed({
486
+ timestamp: Date.now(),
487
+ source: 'EntityManager',
488
+ entityId: entity.id,
489
+ entityName: entity.name,
490
+ entityTag: entity.tag?.toString()
491
+ });
492
+ entity.destroy();
493
+ this._entities.delete(entity.id);
494
+ this._identifierPool.checkIn(entity.id);
495
+ return true;
496
+ }
497
+ /**
498
+ * 获取所有实体
499
+ *
500
+ * 返回当前管理的所有实体的副本数组。
501
+ *
502
+ * @returns 所有实体的数组
503
+ */
504
+ getAllEntities() {
505
+ return Array.from(this._entities.values());
506
+ }
507
+ /**
508
+ * 根据ID获取实体
509
+ *
510
+ * 支持字符串和数字类型的ID。
511
+ *
512
+ * @param id 实体ID,可以是字符串或数字
513
+ * @returns 对应的实体,如果不存在则返回null
514
+ */
515
+ getEntity(id) {
516
+ const numId = typeof id === 'string' ? parseInt(id) : id;
517
+ return this._entities.get(numId) || null;
518
+ }
519
+ /**
520
+ * 根据名称获取实体
521
+ *
522
+ * 如果存在多个同名实体,返回第一个找到的实体。
523
+ *
524
+ * @param name 实体名称
525
+ * @returns 匹配的实体,如果不存在则返回null
526
+ */
527
+ getEntityByName(name) {
528
+ const entities = this._entitiesByName.get(name);
529
+ return entities && entities.length > 0 ? entities[0] : null;
530
+ }
531
+ /**
532
+ * 根据标签获取实体列表
533
+ *
534
+ * 返回所有具有指定标签的实体。
535
+ *
536
+ * @param tag 标签值
537
+ * @returns 具有指定标签的实体数组
538
+ */
539
+ getEntitiesByTag(tag) {
540
+ return Array.from(this._entities.values())
541
+ .filter(entity => entity.tag === tag);
542
+ }
543
+ /**
544
+ * 获取包含指定组件的所有实体
545
+ *
546
+ * 遍历所有实体,查找包含指定组件类型的实体。
547
+ *
548
+ * @param componentType 组件类型
549
+ * @returns 包含指定组件的实体数组
550
+ *
551
+ * @example
552
+ * ```typescript
553
+ * const entitiesWithHealth = entityManager.getEntitiesWithComponent(HealthComponent);
554
+ * ```
555
+ */
556
+ getEntitiesWithComponent(componentType) {
557
+ const indexResult = this._componentIndexManager.query(componentType);
558
+ return Array.from(indexResult);
559
+ }
560
+ /**
561
+ * 创建查询构建器
562
+ *
563
+ * 返回一个新的查询构建器实例,用于构建复杂的实体查询。
564
+ *
565
+ * @returns 新的查询构建器实例
566
+ *
567
+ * @example
568
+ * ```typescript
569
+ * const results = entityManager.query()
570
+ * .withAll(PositionComponent, HealthComponent)
571
+ * .without(VelocityComponent)
572
+ * .active()
573
+ * .execute();
574
+ * ```
575
+ */
576
+ query() {
577
+ return new EntityQueryBuilder(this);
578
+ }
579
+ /**
580
+ * 使用组件索引进行多组件查询
581
+ *
582
+ * @param componentTypes 组件类型数组
583
+ * @param operation 查询操作:'AND' 或 'OR'
584
+ * @returns 匹配的实体集合
585
+ */
586
+ queryWithComponentIndex(componentTypes, operation) {
587
+ return this._componentIndexManager.queryMultiple(componentTypes, operation);
588
+ }
589
+ /**
590
+ * 标记实体组件已修改
591
+ *
592
+ * @param entity 修改的实体
593
+ * @param componentTypes 修改的组件类型
594
+ */
595
+ markEntityDirty(entity, componentTypes) {
596
+ this._dirtyTrackingSystem.markDirty(entity, DirtyFlag.COMPONENT_MODIFIED, componentTypes);
597
+ }
598
+ /**
599
+ * 获取性能优化统计信息
600
+ */
601
+ getOptimizationStats() {
602
+ return {
603
+ componentIndex: this._componentIndexManager.getStats(),
604
+ archetypeSystem: this._archetypeSystem.getAllArchetypes().map(a => ({
605
+ id: a.id,
606
+ componentTypes: a.componentTypes.map(t => t.name),
607
+ entityCount: a.entities.length
608
+ })),
609
+ dirtyTracking: this._dirtyTrackingSystem.getStats()
610
+ };
611
+ }
612
+ /**
613
+ * 获取事件总线实例
614
+ *
615
+ * 允许外部代码监听和发射ECS相关事件。
616
+ *
617
+ * @returns 事件总线实例
618
+ */
619
+ get eventBus() {
620
+ return this._eventBus;
621
+ }
622
+ /**
623
+ * 更新名称索引
624
+ *
625
+ * 维护按名称查找实体的索引结构。支持添加和移除操作。
626
+ *
627
+ * @param entity 要更新索引的实体
628
+ * @param isAdd true表示添加到索引,false表示从索引中移除
629
+ */
630
+ updateNameIndex(entity, isAdd) {
631
+ if (!entity.name) {
632
+ return;
633
+ }
634
+ if (isAdd) {
635
+ let entities = this._entitiesByName.get(entity.name);
636
+ if (!entities) {
637
+ entities = [];
638
+ this._entitiesByName.set(entity.name, entities);
639
+ }
640
+ entities.push(entity);
641
+ }
642
+ else {
643
+ const entities = this._entitiesByName.get(entity.name);
644
+ if (entities) {
645
+ const index = entities.indexOf(entity);
646
+ if (index !== -1) {
647
+ entities.splice(index, 1);
648
+ if (entities.length === 0) {
649
+ this._entitiesByName.delete(entity.name);
650
+ }
651
+ }
652
+ }
653
+ }
654
+ }
655
+ /**
656
+ * 更新标签索引
657
+ *
658
+ * 维护按标签查找实体的索引结构。支持添加和移除操作。
659
+ *
660
+ * @param entity 要更新索引的实体
661
+ * @param isAdd true表示添加到索引,false表示从索引中移除
662
+ */
663
+ updateTagIndex(entity, isAdd) {
664
+ if (isAdd) {
665
+ let entities = this._entitiesByTag.get(entity.tag);
666
+ if (!entities) {
667
+ entities = [];
668
+ this._entitiesByTag.set(entity.tag, entities);
669
+ }
670
+ entities.push(entity);
671
+ }
672
+ else {
673
+ const entities = this._entitiesByTag.get(entity.tag);
674
+ if (entities) {
675
+ const index = entities.indexOf(entity);
676
+ if (index !== -1) {
677
+ entities.splice(index, 1);
678
+ if (entities.length === 0) {
679
+ this._entitiesByTag.delete(entity.tag);
680
+ }
681
+ }
682
+ }
683
+ }
684
+ }
685
+ }
686
+ //# sourceMappingURL=EntityManager.js.map