@dcl/ecs 7.0.6-3808797047.commit-ee7ea04 → 7.0.6-3808892339.commit-e47ed81

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.
@@ -1,17 +1,39 @@
1
- import { EntityUtils } from './entity-utils';
1
+ /**
2
+ * @public It only defines the type explicitly, no effects.
3
+ */
4
+ export declare type uint32 = number;
5
+ export declare const MAX_U16 = 65535;
6
+ export declare const MASK_UPPER_16_ON_32 = 4294901760;
7
+ export declare const AMOUNT_VERSION_AVAILABLE: number;
8
+ /**
9
+ * @public The Entity is a number type, the cast is only for typescript, the final javascript code treat as a number
10
+ * version number
11
+ * [31...16][15...0]
12
+ *
13
+ * Convertion from entity to its compound numbers:
14
+ * To get the version => ((entity & MASK_UPPER_16_ON_32) >> 16) & MAX_U16
15
+ * To get the number => entity & MAX_U16
16
+ *
17
+ * Convertion from its compound numbers to entity:
18
+ * entity = (entityNumber & MAX_U16) | ((entityVersion & MAX_U16) << 16)
19
+ */
20
+ export declare type Entity = uint32 & {
21
+ __entity_type: '';
22
+ };
23
+ /**
24
+ * @public This first 512 entities are reserved by the renderer
25
+ */
26
+ export declare const RESERVED_STATIC_ENTITIES = 512;
2
27
  /**
3
28
  * @public
4
29
  */
5
- export declare type Entity = unknown;
30
+ export declare const MAX_ENTITY_NUMBER = 65535;
6
31
  export declare function EntityContainer(): {
7
- generateEntity(dynamic?: boolean): Entity;
32
+ generateEntity(): Entity;
8
33
  removeEntity(entity: Entity): boolean;
9
34
  entityExists(entity: Entity): boolean;
10
35
  getExistingEntities(): Set<Entity>;
36
+ entityVersion: (entity: Entity) => number;
37
+ entityNumber: (entity: Entity) => number;
38
+ entityId: (entityNumber: number, entityVersion: number) => Entity;
11
39
  };
12
- declare function Entity(range: EntityUtils.EntityRange): {
13
- getExistingEntities(): Set<unknown>;
14
- generateEntity: () => Entity;
15
- removeEntity: (entity: Entity) => boolean;
16
- };
17
- export {};
@@ -1,59 +1,72 @@
1
- import { EntityUtils } from './entity-utils';
1
+ export const MAX_U16 = 0xffff;
2
+ export const MASK_UPPER_16_ON_32 = 0xffff0000;
3
+ export const AMOUNT_VERSION_AVAILABLE = MAX_U16 + 1;
4
+ /**
5
+ * @public This first 512 entities are reserved by the renderer
6
+ */
7
+ export const RESERVED_STATIC_ENTITIES = 512;
8
+ /**
9
+ * @public
10
+ */
11
+ export const MAX_ENTITY_NUMBER = MAX_U16;
2
12
  export function EntityContainer() {
3
- const staticEntity = Entity(EntityUtils.STATIC_ENTITIES_RANGE);
4
- const dynamicEntity = Entity(EntityUtils.DYNAMIC_ENTITIES_RANGE);
5
- return {
6
- generateEntity(dynamic = false) {
7
- /* istanbul ignore next */
8
- if (dynamic) {
9
- // Dynamic entities are not being used, but since we dont know the future of the dynamic entities
10
- // I prefer to comment the code instead of removing all the logic
11
- /* istanbul ignore next */
12
- return dynamicEntity.generateEntity();
13
- }
14
- else {
15
- return staticEntity.generateEntity();
16
- }
17
- },
18
- removeEntity(entity) {
19
- return (staticEntity.removeEntity(entity) || dynamicEntity.removeEntity(entity));
20
- },
21
- entityExists(entity) {
22
- return (EntityUtils.isReservedEntity(entity) ||
23
- staticEntity.getExistingEntities().has(entity) ||
24
- dynamicEntity.getExistingEntities().has(entity));
25
- },
26
- getExistingEntities() {
27
- return new Set([
28
- ...staticEntity.getExistingEntities(),
29
- ...dynamicEntity.getExistingEntities()
30
- ]);
13
+ let entityCounter = RESERVED_STATIC_ENTITIES;
14
+ const usedEntities = new Set();
15
+ const removedEntities = new Map();
16
+ function entityVersion(entity) {
17
+ return (((entity & MASK_UPPER_16_ON_32) >> 16) & MAX_U16) >>> 0;
18
+ }
19
+ function entityNumber(entity) {
20
+ return (entity & MAX_U16) >>> 0;
21
+ }
22
+ function entityId(entityNumber, entityVersion) {
23
+ return (((entityNumber & MAX_U16) | ((entityVersion & MAX_U16) << 16)) >>>
24
+ 0);
25
+ }
26
+ function generateNewEntity() {
27
+ if (entityCounter > MAX_ENTITY_NUMBER - 1) {
28
+ throw new Error(`It fails trying to generate an entity out of range ${MAX_ENTITY_NUMBER}.`);
31
29
  }
32
- };
33
- }
34
- function Entity(range) {
35
- function createEntity(entity) {
30
+ const entity = entityCounter++;
31
+ usedEntities.add(entity);
36
32
  return entity;
37
33
  }
38
- let entityCounter = range[0];
39
- const usedEntities = new Set();
40
34
  function generateEntity() {
41
- if (entityCounter >= range[1]) {
42
- throw new Error(`It fails trying to generate an entity out of range [${range[0]}, ${range[1]}].`);
35
+ if (usedEntities.size + RESERVED_STATIC_ENTITIES >= entityCounter) {
36
+ return generateNewEntity();
43
37
  }
44
- const entity = createEntity(entityCounter);
45
- entityCounter++;
46
- usedEntities.add(entity);
47
- return entity;
38
+ for (const [number, version] of removedEntities) {
39
+ if (version < MAX_U16) {
40
+ const entity = entityId(number, version + 1);
41
+ usedEntities.add(entity);
42
+ removedEntities.delete(number);
43
+ return entity;
44
+ }
45
+ }
46
+ return generateNewEntity();
48
47
  }
49
48
  function removeEntity(entity) {
50
- return usedEntities.delete(entity);
49
+ const deleted = usedEntities.delete(entity);
50
+ if (deleted) {
51
+ removedEntities.set(entityNumber(entity), entityVersion(entity));
52
+ }
53
+ return deleted;
51
54
  }
52
55
  return {
56
+ generateEntity() {
57
+ return generateEntity();
58
+ },
59
+ removeEntity(entity) {
60
+ return removeEntity(entity);
61
+ },
62
+ entityExists(entity) {
63
+ return entity < RESERVED_STATIC_ENTITIES || usedEntities.has(entity);
64
+ },
53
65
  getExistingEntities() {
54
66
  return new Set(usedEntities);
55
67
  },
56
- generateEntity,
57
- removeEntity
68
+ entityVersion,
69
+ entityNumber,
70
+ entityId
58
71
  };
59
72
  }
@@ -7,12 +7,12 @@ import { Task } from '../../systems/async-task';
7
7
  export declare const engine: IEngine;
8
8
  export declare const inputSystem: import("../../engine").IInputSystem;
9
9
  export declare const pointerEventsSystem: {
10
- removeOnClick(entity: unknown): void;
11
- removeOnPointerDown(entity: unknown): void;
12
- removeOnPointerUp(entity: unknown): void;
13
- onClick(entity: unknown, cb: import("../../systems/events").EventSystemCallback, opts?: Partial<import("../../systems/events").EventSystemOptions> | undefined): void;
14
- onPointerDown(entity: unknown, cb: import("../../systems/events").EventSystemCallback, opts?: Partial<import("../../systems/events").EventSystemOptions> | undefined): void;
15
- onPointerUp(entity: unknown, cb: import("../../systems/events").EventSystemCallback, opts?: Partial<import("../../systems/events").EventSystemOptions> | undefined): void;
10
+ removeOnClick(entity: import("../../engine").Entity): void;
11
+ removeOnPointerDown(entity: import("../../engine").Entity): void;
12
+ removeOnPointerUp(entity: import("../../engine").Entity): void;
13
+ onClick(entity: import("../../engine").Entity, cb: import("../../systems/events").EventSystemCallback, opts?: Partial<import("../../systems/events").EventSystemOptions> | undefined): void;
14
+ onPointerDown(entity: import("../../engine").Entity, cb: import("../../systems/events").EventSystemCallback, opts?: Partial<import("../../systems/events").EventSystemOptions> | undefined): void;
15
+ onPointerUp(entity: import("../../engine").Entity, cb: import("../../systems/events").EventSystemCallback, opts?: Partial<import("../../systems/events").EventSystemOptions> | undefined): void;
16
16
  };
17
17
  /**
18
18
  * @public
@@ -6,5 +6,5 @@ export declare function crdtSceneSystem(engine: Pick<IEngine, 'getComponentOrNul
6
6
  sendMessages: (dirtyEntities: Map<Entity, Set<number>>) => Promise<void>;
7
7
  receiveMessages: () => Promise<void>;
8
8
  addTransport: (transport: Transport) => void;
9
- updateState: () => Map<unknown, Set<number>>;
9
+ updateState: () => Map<Entity, Set<number>>;
10
10
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
- "version": "7.0.6-3808797047.commit-ee7ea04",
3
+ "version": "7.0.6-3808892339.commit-e47ed81",
4
4
  "description": "Decentraland ECS",
5
5
  "main": "./dist/index.js",
6
6
  "typings": "./dist/index.d.ts",
@@ -27,7 +27,7 @@
27
27
  "ts-proto": "^1.112.0"
28
28
  },
29
29
  "dependencies": {
30
- "@dcl/crdt": "7.0.6-3808797047.commit-ee7ea04",
30
+ "@dcl/crdt": "7.0.6-3808892339.commit-e47ed81",
31
31
  "@dcl/js-runtime": "file:../js-runtime",
32
32
  "@dcl/protocol": "^1.0.0-3808528053.commit-d66d462"
33
33
  },
@@ -41,5 +41,5 @@
41
41
  "displayName": "ECS",
42
42
  "tsconfig": "./tsconfig.json"
43
43
  },
44
- "commit": "ee7ea0434ecbd9e92db737237a67cd02af989b73"
44
+ "commit": "e47ed813e0b5bd0e5fd6a0fd9e2eeb80783dab27"
45
45
  }
@@ -1,17 +0,0 @@
1
- import { Entity } from './entity';
2
- export declare namespace EntityUtils {
3
- /**
4
- * Range is the first element and the last possible element, they both are included in the interval.
5
- * [start, end]
6
- */
7
- type EntityRange = readonly [number, number];
8
- const MAX_ENTITIES = 4294967295;
9
- const DYNAMIC_ENTITIES_START_AT = 100000;
10
- const RESERVED_STATIC_ENTITIES = 512;
11
- const RESERVED_ENTITIES_RANGE: EntityRange;
12
- const STATIC_ENTITIES_RANGE: EntityRange;
13
- const DYNAMIC_ENTITIES_RANGE: EntityRange;
14
- function isStaticEntity(entity: Entity): boolean;
15
- function isReservedEntity(entity: Entity): boolean;
16
- }
17
- export default EntityUtils;
@@ -1,36 +0,0 @@
1
- export var EntityUtils;
2
- (function (EntityUtils) {
3
- EntityUtils.MAX_ENTITIES = 0xffffffff;
4
- EntityUtils.DYNAMIC_ENTITIES_START_AT = 100e3;
5
- EntityUtils.RESERVED_STATIC_ENTITIES = 512;
6
- EntityUtils.RESERVED_ENTITIES_RANGE = [
7
- 0,
8
- EntityUtils.RESERVED_STATIC_ENTITIES - 1
9
- ];
10
- EntityUtils.STATIC_ENTITIES_RANGE = [
11
- EntityUtils.RESERVED_STATIC_ENTITIES,
12
- EntityUtils.MAX_ENTITIES
13
- // DYNAMIC_ENTITIES_START_AT - 1
14
- ];
15
- EntityUtils.DYNAMIC_ENTITIES_RANGE = [
16
- EntityUtils.DYNAMIC_ENTITIES_START_AT,
17
- EntityUtils.MAX_ENTITIES
18
- ];
19
- function isInRange(entity, range) {
20
- return entity >= range[0] && entity <= range[1];
21
- }
22
- // @internal
23
- function isDynamicEntity(entity) {
24
- return isInRange(entity, EntityUtils.DYNAMIC_ENTITIES_RANGE);
25
- }
26
- EntityUtils.isDynamicEntity = isDynamicEntity;
27
- function isStaticEntity(entity) {
28
- return isInRange(entity, EntityUtils.STATIC_ENTITIES_RANGE);
29
- }
30
- EntityUtils.isStaticEntity = isStaticEntity;
31
- function isReservedEntity(entity) {
32
- return isInRange(entity, EntityUtils.RESERVED_ENTITIES_RANGE);
33
- }
34
- EntityUtils.isReservedEntity = isReservedEntity;
35
- })(EntityUtils || (EntityUtils = {}));
36
- export default EntityUtils;