@dcl/playground-assets 7.0.6-3808988484.commit-63d3e2c → 7.0.6-3823801200.commit-32470bd

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/dist/alpha.d.ts CHANGED
@@ -1162,7 +1162,7 @@ export declare type EcsElements = {
1162
1162
  /**
1163
1163
  * @public
1164
1164
  */
1165
- export declare function Engine(): IEngine;
1165
+ export declare function Engine(options?: IEngineOptions): IEngine;
1166
1166
 
1167
1167
  export declare const engine: IEngine;
1168
1168
 
@@ -1418,6 +1418,17 @@ export declare type IEngine = {
1418
1418
  * @param transport - transport which changes its onmessage to process CRDT messages
1419
1419
  */
1420
1420
  addTransport(transport: Transport): void;
1421
+ /**
1422
+ * Iterator of registered components
1423
+ */
1424
+ componentsIter(): Iterable<ComponentDefinition<unknown>>;
1425
+ };
1426
+
1427
+ /**
1428
+ * @public
1429
+ */
1430
+ export declare type IEngineOptions = {
1431
+ onChangeFunction: OnChangeFunction;
1421
1432
  };
1422
1433
 
1423
1434
  /**
@@ -2637,6 +2648,11 @@ export declare class ObserverEventState {
2637
2648
  initalize(mask: number, skipNextObservers?: boolean, target?: any, currentTarget?: any): ObserverEventState;
2638
2649
  }
2639
2650
 
2651
+ /**
2652
+ * @public
2653
+ */
2654
+ export declare type OnChangeFunction = (entity: Entity, component: ComponentDefinition<any>, operation: WireMessage.Enum) => void;
2655
+
2640
2656
  export declare const onCommsMessage: Observable<{
2641
2657
  sender: string;
2642
2658
  message: string;
package/dist/beta.d.ts CHANGED
@@ -1162,7 +1162,7 @@ export declare type EcsElements = {
1162
1162
  /**
1163
1163
  * @public
1164
1164
  */
1165
- export declare function Engine(): IEngine;
1165
+ export declare function Engine(options?: IEngineOptions): IEngine;
1166
1166
 
1167
1167
  export declare const engine: IEngine;
1168
1168
 
@@ -1418,6 +1418,17 @@ export declare type IEngine = {
1418
1418
  * @param transport - transport which changes its onmessage to process CRDT messages
1419
1419
  */
1420
1420
  addTransport(transport: Transport): void;
1421
+ /**
1422
+ * Iterator of registered components
1423
+ */
1424
+ componentsIter(): Iterable<ComponentDefinition<unknown>>;
1425
+ };
1426
+
1427
+ /**
1428
+ * @public
1429
+ */
1430
+ export declare type IEngineOptions = {
1431
+ onChangeFunction: OnChangeFunction;
1421
1432
  };
1422
1433
 
1423
1434
  /**
@@ -2637,6 +2648,11 @@ export declare class ObserverEventState {
2637
2648
  initalize(mask: number, skipNextObservers?: boolean, target?: any, currentTarget?: any): ObserverEventState;
2638
2649
  }
2639
2650
 
2651
+ /**
2652
+ * @public
2653
+ */
2654
+ export declare type OnChangeFunction = (entity: Entity, component: ComponentDefinition<any>, operation: WireMessage.Enum) => void;
2655
+
2640
2656
  export declare const onCommsMessage: Observable<{
2641
2657
  sender: string;
2642
2658
  message: string;
@@ -1162,7 +1162,7 @@ export declare type EcsElements = {
1162
1162
  /**
1163
1163
  * @public
1164
1164
  */
1165
- export declare function Engine(): IEngine;
1165
+ export declare function Engine(options?: IEngineOptions): IEngine;
1166
1166
 
1167
1167
  export declare const engine: IEngine;
1168
1168
 
@@ -1418,6 +1418,17 @@ export declare type IEngine = {
1418
1418
  * @param transport - transport which changes its onmessage to process CRDT messages
1419
1419
  */
1420
1420
  addTransport(transport: Transport): void;
1421
+ /**
1422
+ * Iterator of registered components
1423
+ */
1424
+ componentsIter(): Iterable<ComponentDefinition<unknown>>;
1425
+ };
1426
+
1427
+ /**
1428
+ * @public
1429
+ */
1430
+ export declare type IEngineOptions = {
1431
+ onChangeFunction: OnChangeFunction;
1421
1432
  };
1422
1433
 
1423
1434
  /**
@@ -2637,6 +2648,11 @@ export declare class ObserverEventState {
2637
2648
  initalize(mask: number, skipNextObservers?: boolean, target?: any, currentTarget?: any): ObserverEventState;
2638
2649
  }
2639
2650
 
2651
+ /**
2652
+ * @public
2653
+ */
2654
+ export declare type OnChangeFunction = (entity: Entity, component: ComponentDefinition<any>, operation: WireMessage.Enum) => void;
2655
+
2640
2656
  export declare const onCommsMessage: Observable<{
2641
2657
  sender: string;
2642
2658
  message: string;
package/dist/index.js CHANGED
@@ -13687,7 +13687,7 @@
13687
13687
  ComponentOperation.read = read;
13688
13688
  })(ComponentOperation || (ComponentOperation = {}));
13689
13689
 
13690
- function crdtSceneSystem(engine) {
13690
+ function crdtSceneSystem(engine, onProcessEntityComponentChange) {
13691
13691
  const transports = [];
13692
13692
  // CRDT Client
13693
13693
  const crdtClient = distExports.crdtProtocol();
@@ -13791,34 +13791,38 @@
13791
13791
  const data = createByteBuffer(opts);
13792
13792
  component.upsertFromBinary(message.entity, data, false);
13793
13793
  }
13794
+ onProcessEntityComponentChange &&
13795
+ onProcessEntityComponentChange(entity, component, type);
13794
13796
  }
13795
13797
  }
13796
13798
  }
13797
- function getDirtyMap() {
13798
- const dirtySet = new Map();
13799
- for (const [componentId, definition] of engine.componentsDefinition) {
13800
- for (const entity of definition.dirtyIterator()) {
13801
- if (!dirtySet.has(entity)) {
13802
- dirtySet.set(entity, new Set());
13803
- }
13804
- dirtySet.get(entity).add(componentId);
13805
- }
13806
- }
13807
- return dirtySet;
13808
- }
13809
13799
  /**
13810
13800
  * Updates CRDT state of the current engine dirty components
13801
+ *
13802
+ * TODO: optimize this function allocations using a bitmap
13803
+ * TODO: unify this function with sendMessages
13811
13804
  */
13812
13805
  function updateState() {
13813
- const dirtyEntities = getDirtyMap();
13814
- for (const [entity, componentsId] of getDirtyMap()) {
13815
- for (const componentId of componentsId) {
13816
- const component = engine.getComponent(componentId);
13806
+ const dirtyMap = new Map();
13807
+ for (const component of engine.componentsIter()) {
13808
+ let entitySet = null;
13809
+ for (const entity of component.dirtyIterator()) {
13810
+ if (!entitySet) {
13811
+ entitySet = [];
13812
+ dirtyMap.set(component, entitySet);
13813
+ }
13814
+ entitySet.push(entity);
13815
+ // TODO: reuse shared writer to prevent extra allocations of toBinary
13817
13816
  const componentValue = component.toBinaryOrNull(entity)?.toBinary() ?? null;
13818
- crdtClient.createEvent(entity, componentId, componentValue);
13817
+ // TODO: do not emit event if componentValue equals the value didn't change
13818
+ crdtClient.createEvent(entity, component._id, componentValue);
13819
+ onProcessEntityComponentChange &&
13820
+ onProcessEntityComponentChange(entity, component, componentValue === null
13821
+ ? WireMessage$1.Enum.DELETE_COMPONENT
13822
+ : WireMessage$1.Enum.PUT_COMPONENT);
13819
13823
  }
13820
13824
  }
13821
- return dirtyEntities;
13825
+ return dirtyMap;
13822
13826
  }
13823
13827
  /**
13824
13828
  * Iterates the dirty map and generates crdt messages to be send
@@ -13828,19 +13832,18 @@
13828
13832
  const crdtMessages = getMessages(broadcastMessages);
13829
13833
  const outdatedMessagesBkp = getMessages(outdatedMessages);
13830
13834
  const buffer = createByteBuffer();
13831
- for (const [entity, componentsId] of dirtyEntities) {
13832
- for (const componentId of componentsId) {
13835
+ for (const [component, entities] of dirtyEntities) {
13836
+ for (const entity of entities) {
13833
13837
  // Component will be always defined here since dirtyMap its an iterator of engine.componentsDefinition
13834
- const component = engine.getComponent(componentId);
13835
13838
  const { timestamp } = crdtClient
13836
13839
  .getState()
13837
13840
  .get(entity)
13838
- .get(componentId);
13841
+ .get(component._id);
13839
13842
  const offset = buffer.currentWriteOffset();
13840
13843
  const type = WireMessage$1.getType(component, entity);
13841
13844
  const transportMessage = {
13842
13845
  type,
13843
- componentId,
13846
+ componentId: component._id,
13844
13847
  entity,
13845
13848
  timestamp
13846
13849
  };
@@ -14426,6 +14429,9 @@
14426
14429
  function getSystems() {
14427
14430
  return systems.getSystems();
14428
14431
  }
14432
+ function componentsIter() {
14433
+ return componentsDefinition.values();
14434
+ }
14429
14435
  function removeComponentDefinition(componentId) {
14430
14436
  componentsDefinition.delete(componentId);
14431
14437
  }
@@ -14462,15 +14468,16 @@
14462
14468
  getComponentOrNull,
14463
14469
  removeComponentDefinition,
14464
14470
  removeEntityWithChildren,
14465
- registerCustomComponent
14471
+ registerCustomComponent,
14472
+ componentsIter
14466
14473
  };
14467
14474
  }
14468
14475
  /**
14469
14476
  * @public
14470
14477
  */
14471
- function Engine() {
14478
+ function Engine(options) {
14472
14479
  const engine = preEngine();
14473
- const crdtSystem = crdtSceneSystem(engine);
14480
+ const crdtSystem = crdtSceneSystem(engine, options?.onChangeFunction || null);
14474
14481
  async function update(dt) {
14475
14482
  await crdtSystem.receiveMessages();
14476
14483
  for (const system of engine.getSystems()) {
@@ -14496,6 +14503,7 @@
14496
14503
  getComponent: engine.getComponent,
14497
14504
  getComponentOrNull: engine.getComponentOrNull,
14498
14505
  removeComponentDefinition: engine.removeComponentDefinition,
14506
+ componentsIter: engine.componentsIter,
14499
14507
  update,
14500
14508
  RootEntity: 0,
14501
14509
  PlayerEntity: 1,