@dcl/ecs 7.1.8 → 7.1.9-4767602514.commit-64fd553

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.
@@ -148,24 +148,7 @@ function preEngine() {
148
148
  const componentId = typeof componentIdOrName === 'number' ? componentIdOrName : componentNumberFromName(componentIdOrName);
149
149
  componentsDefinition.delete(componentId);
150
150
  }
151
- const Transform = components.Transform({ defineComponentFromSchema });
152
- function* getTreeEntityArray(firstEntity, proccesedEntities) {
153
- // This avoid infinite loop when there is a cyclic parenting
154
- if (proccesedEntities.find((value) => firstEntity === value))
155
- return;
156
- proccesedEntities.push(firstEntity);
157
- for (const [entity, value] of getEntitiesWith(Transform)) {
158
- if (value.parent === firstEntity) {
159
- yield* getTreeEntityArray(entity, proccesedEntities);
160
- }
161
- }
162
- yield firstEntity;
163
- }
164
- function removeEntityWithChildren(firstEntity) {
165
- for (const entity of getTreeEntityArray(firstEntity, [])) {
166
- removeEntity(entity);
167
- }
168
- }
151
+ components.Transform({ defineComponentFromSchema });
169
152
  function seal() {
170
153
  if (!sealed) {
171
154
  sealed = true;
@@ -184,7 +167,6 @@ function preEngine() {
184
167
  getComponent,
185
168
  getComponentOrNull,
186
169
  removeComponentDefinition,
187
- removeEntityWithChildren,
188
170
  registerComponentDefinition,
189
171
  entityContainer,
190
172
  componentsIter,
@@ -212,7 +194,6 @@ export function Engine(options) {
212
194
  return {
213
195
  addEntity: partialEngine.addEntity,
214
196
  removeEntity: partialEngine.removeEntity,
215
- removeEntityWithChildren: partialEngine.removeEntityWithChildren,
216
197
  addSystem: partialEngine.addSystem,
217
198
  removeSystem: partialEngine.removeSystem,
218
199
  defineComponent: partialEngine.defineComponent,
@@ -56,11 +56,6 @@ export interface IEngine {
56
56
  * @param entity - entity
57
57
  */
58
58
  removeEntity(entity: Entity): void;
59
- /**
60
- * Remove all components of each entity in the tree made with Transform parenting
61
- * @param firstEntity - the root entity of the tree
62
- */
63
- removeEntityWithChildren(firstEntity: Entity): void;
64
59
  /**
65
60
  *
66
61
  * @public
package/dist/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from './engine';
2
2
  export * from './schemas';
3
3
  export * from './runtime/initialization';
4
4
  export * from './runtime/types';
5
+ export * from './runtime/helpers';
5
6
  export { cyclicParentingChecker } from './systems/cyclicParentingChecker';
6
7
  export * from './systems/events';
7
8
  export * from './systems/raycast';
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export * from './engine';
3
3
  export * from './schemas';
4
4
  export * from './runtime/initialization';
5
5
  export * from './runtime/types';
6
+ export * from './runtime/helpers';
6
7
  export { cyclicParentingChecker } from './systems/cyclicParentingChecker';
7
8
  export * from './systems/events';
8
9
  export * from './systems/raycast';
@@ -0,0 +1 @@
1
+ export * from './tree';
@@ -0,0 +1 @@
1
+ export * from './tree';
@@ -0,0 +1,32 @@
1
+ import { Entity } from '../../engine/entity';
2
+ import { ComponentDefinition, IEngine } from '../../engine';
3
+ /**
4
+ * Get an iterator of entities that follow a tree structure for a component
5
+ * @public
6
+ * @param engine - the engine running the entities
7
+ * @param entity - the root entity of the tree
8
+ * @param component - the parenting component to filter by
9
+ * @returns An iterator of an array as [entity, entity2, ...]
10
+ *
11
+ * Example:
12
+ * ```ts
13
+ * const TreeComponent = engine.defineComponent('custom::TreeComponent', {
14
+ * label: Schemas.String,
15
+ * parent: Schemas.Entity
16
+ * })
17
+ *
18
+ * for (const entity of getComponentEntityTree(engine, entity, TreeComponent)) {
19
+ * // entity in the tree
20
+ * }
21
+ * ```
22
+ */
23
+ export declare function getComponentEntityTree<T>(engine: IEngine, entity: Entity, component: ComponentDefinition<T & {
24
+ parent?: Entity;
25
+ }>): Generator<Entity>;
26
+ /**
27
+ * Remove all components of each entity in the tree made with Transform parenting
28
+ * @param engine - the engine running the entities
29
+ * @param firstEntity - the root entity of the tree
30
+ * @public
31
+ */
32
+ export declare function removeEntityWithChildren(engine: IEngine, entity: Entity): void;
@@ -0,0 +1,49 @@
1
+ import * as components from '../../components';
2
+ function* genEntityTree(entity, entities) {
3
+ // This avoid infinite loop when there is a cyclic parenting
4
+ if (!entities.has(entity))
5
+ return;
6
+ entities.delete(entity);
7
+ for (const [_entity, value] of entities) {
8
+ if (value.parent === entity) {
9
+ yield* genEntityTree(_entity, entities);
10
+ }
11
+ }
12
+ yield entity;
13
+ }
14
+ /**
15
+ * Get an iterator of entities that follow a tree structure for a component
16
+ * @public
17
+ * @param engine - the engine running the entities
18
+ * @param entity - the root entity of the tree
19
+ * @param component - the parenting component to filter by
20
+ * @returns An iterator of an array as [entity, entity2, ...]
21
+ *
22
+ * Example:
23
+ * ```ts
24
+ * const TreeComponent = engine.defineComponent('custom::TreeComponent', {
25
+ * label: Schemas.String,
26
+ * parent: Schemas.Entity
27
+ * })
28
+ *
29
+ * for (const entity of getComponentEntityTree(engine, entity, TreeComponent)) {
30
+ * // entity in the tree
31
+ * }
32
+ * ```
33
+ */
34
+ export function getComponentEntityTree(engine, entity, component) {
35
+ const entities = new Map(engine.getEntitiesWith(component));
36
+ return genEntityTree(entity, entities);
37
+ }
38
+ /**
39
+ * Remove all components of each entity in the tree made with Transform parenting
40
+ * @param engine - the engine running the entities
41
+ * @param firstEntity - the root entity of the tree
42
+ * @public
43
+ */
44
+ export function removeEntityWithChildren(engine, entity) {
45
+ const Transform = components.Transform(engine);
46
+ for (const _entity of getComponentEntityTree(engine, entity, Transform)) {
47
+ engine.removeEntity(_entity);
48
+ }
49
+ }
package/package.json CHANGED
@@ -1,11 +1,11 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.1.8",
4
+ "version": "7.1.9-4767602514.commit-64fd553",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/ecs/issues",
7
7
  "dependencies": {
8
- "@dcl/js-runtime": "7.1.8"
8
+ "@dcl/js-runtime": "7.1.9-4767602514.commit-64fd553"
9
9
  },
10
10
  "devDependencies": {
11
11
  "ts-proto": "^1.122.0"
@@ -34,5 +34,5 @@
34
34
  },
35
35
  "types": "./dist/index.d.ts",
36
36
  "typings": "./dist/index.d.ts",
37
- "commit": "42042b569cac98bd9654f6c24e1774fad2e75551"
37
+ "commit": "64fd55305b172e36b5f88f59fddcd69835b0bd1a"
38
38
  }