@dcl/sdk 7.0.0-2875155444.commit-bb5e498 → 7.0.0-2884920754.commit-e7fba73

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.
@@ -70,21 +70,125 @@ declare interface Color3 {
70
70
  */
71
71
  export declare type ComponentDefinition<T extends ISchema = ISchema<any>> = {
72
72
  _id: number;
73
+ /**
74
+ * Return the default value of the current component
75
+ */
76
+ default(): DeepReadonly<ComponentType<T>>;
77
+ /**
78
+ * Get if the entity has this component
79
+ * @param entity
80
+ *
81
+ * Example:
82
+ * ```ts
83
+ * const myEntity = engine.addEntity()
84
+ * Transform.has(myEntity) // return false
85
+ * Transform.create(myEntity)
86
+ * Transform.has(myEntity) // return true
87
+ * ```
88
+ */
73
89
  has(entity: Entity): boolean;
74
- getFrom(entity: Entity): DeepReadonly<ComponentType<T>>;
90
+ /**
91
+ * Get the readonly component of the entity (to mutate it, use getMutable instead), throw an error if the entity doesn't have the component.
92
+ * @param entity
93
+ * @return
94
+ * Example:
95
+ * ```ts
96
+ * const myEntity = engine.addEntity()
97
+ * Transform.create(myEntity)
98
+ * const transform = Transform.get(myEntity) // return true
99
+ * log(transform.position.x === 0) // log 'true'
100
+ *
101
+ * transform.position.y = 10 // illegal statement, to mutate the component use getMutable
102
+ * ```
103
+ *
104
+ * ```ts
105
+ * const otherEntity = engine.addEntity()
106
+ * Transform.get(otherEntity) // throw an error!!
107
+ * ```
108
+ */
109
+ get(entity: Entity): DeepReadonly<ComponentType<T>>;
110
+ /**
111
+ * Get the readonly component of the entity (to mutate it, use getMutable instead), or null if the entity doesn't have the component.
112
+ * @param entity
113
+ * @return
114
+ *
115
+ * Example:
116
+ * ```ts
117
+ * const otherEntity = engine.addEntity()
118
+ * log(Transform.get(otherEntity) === null) // log 'true'
119
+ * ```
120
+ */
75
121
  getOrNull(entity: Entity): DeepReadonly<ComponentType<T>> | null;
122
+ /**
123
+ * Add the current component to an entity, throw an error if the component already exists (use `createOrReplace` instead).
124
+ * - Internal comment: This method adds the <entity,component> to the list to be reviewed next frame
125
+ * @param entity
126
+ * @param val The initial value
127
+ *
128
+ * Example:
129
+ * ```ts
130
+ * const myEntity = engine.addEntity()
131
+ * Transform.create(myEntity, { ...Transform.default(), position: {x: 4, y: 0, z: 4} }) // ok!
132
+ * Transform.create(myEntity) // throw an error, the `Transform` component already exists in `myEntity`
133
+ * ````
134
+ */
76
135
  create(entity: Entity, val?: ComponentType<T>): ComponentType<T>;
77
- mutable(entity: Entity): ComponentType<T>;
136
+ /**
137
+ * Add the current component to an entity or replace the content if the entity already has the component
138
+ * - Internal comment: This method adds the <entity,component> to the list to be reviewed next frame
139
+ * @param entity
140
+ * @param val The initial or new value
141
+ *
142
+ * Example:
143
+ * ```ts
144
+ * const myEntity = engine.addEntity()
145
+ * Transform.create(myEntity) // ok!
146
+ * Transform.createOrReplace(myEntity, { ...Transform.default(), position: {x: 4, y: 0, z: 4} }) // ok!
147
+ * ````
148
+ */
78
149
  createOrReplace(entity: Entity, val?: ComponentType<T>): ComponentType<T>;
150
+ /**
151
+ * Delete the current component to an entity, return null if the entity doesn't have the current component.
152
+ * - Internal comment: This method adds the <entity,component> to the list to be reviewed next frame
153
+ * @param entity
154
+ *
155
+ * Example:
156
+ * ```ts
157
+ * const myEntity = engine.addEntity()
158
+ * Transform.create(myEntity) // ok!
159
+ * Transform.deleteFrom(myEntity) // return the component
160
+ * Transform.deleteFrom(myEntity) // return null
161
+ * ````
162
+ */
79
163
  deleteFrom(entity: Entity): ComponentType<T> | null;
80
- upsertFromBinary(entity: Entity, data: ByteBuffer): ComponentType<T> | null;
81
- updateFromBinary(entity: Entity, data: ByteBuffer): ComponentType<T> | null;
82
- toBinary(entity: Entity): ByteBuffer;
164
+ /**
165
+ * Get the mutable component of the entity, throw an error if the entity doesn't have the component.
166
+ * - Internal comment: This method adds the <entity,component> to the list to be reviewed next frame
167
+ * @param entity
168
+ *
169
+ * Example:
170
+ * ```ts
171
+ * const myEntity = engine.addEntity()
172
+ * Transform.create(myEntity)
173
+ * Transform.getMutable(myEntity).position = {x: 4, y: 0, z: 4}
174
+ * ````
175
+ */
176
+ getMutable(entity: Entity): ComponentType<T>;
177
+ /**
178
+ * Get the mutable component of the entity, return null if the entity doesn't have the component.
179
+ * - Internal comment: This method adds the <entity,component> to the list to be reviewed next frame
180
+ * @param entity
181
+ *
182
+ * Example:
183
+ * ```ts
184
+ * const transform = Transform.getMutableOrNull(myEntity)
185
+ * if (transform) {
186
+ * transform.position = {x: 4, y: 0, z: 4}
187
+ * }
188
+ * ````
189
+ */
190
+ getMutableOrNull(entity: Entity): ComponentType<T> | null;
83
191
  writeToByteBuffer(entity: Entity, buffer: ByteBuffer): void;
84
- iterator(): Iterable<[Entity, ComponentType<T>]>;
85
- dirtyIterator(): Iterable<Entity>;
86
- clearDirty(): void;
87
- isDirty(entity: Entity): boolean;
88
192
  };
89
193
 
90
194
  /** @public */
@@ -154,7 +258,7 @@ export declare namespace Components {
154
258
  * @public
155
259
  */
156
260
  export declare type ComponentSchema<T extends [ComponentDefinition, ...ComponentDefinition[]]> = {
157
- [K in keyof T]: T[K] extends ComponentDefinition ? ReturnType<T[K]['mutable']> : never;
261
+ [K in keyof T]: T[K] extends ComponentDefinition ? ReturnType<T[K]['getMutable']> : never;
158
262
  };
159
263
 
160
264
  /**
@@ -311,7 +415,7 @@ export declare type DeepReadonly<T> = {
311
415
  readonly [P in keyof T]: DeepReadonly<T[P]>;
312
416
  };
313
417
 
314
- declare function defineLibraryComponents({ defineComponent }: Pick<IEngine, 'defineComponent'>): {
418
+ declare function defineLibraryComponents({ defineComponentFromSchema }: Pick<IEngine, 'defineComponentFromSchema'>): {
315
419
  Transform: ComponentDefinition<ISchema< {
316
420
  position: {
317
421
  x: number;
@@ -392,6 +496,8 @@ declare const entitySymbol: unique symbol;
392
496
  */
393
497
  export declare const Epsilon = 0.000001;
394
498
 
499
+ export declare const error: (message: string | Error, data?: any) => void;
500
+
395
501
  /** Excludes property keys from T where the property is assignable to U */
396
502
  declare type ExcludeUndefined<T> = {
397
503
  [P in keyof T]: undefined extends T[P] ? never : P;
@@ -415,16 +521,96 @@ declare function IArray<T>(type: ISchema<T>): ISchema<Array<T>>;
415
521
  * @public
416
522
  */
417
523
  export declare type IEngine = {
524
+ /**
525
+ * Increment the used entity counter and return the next one.
526
+ * @param dynamic
527
+ * @return the next entity unused
528
+ */
418
529
  addEntity(dynamic?: boolean): Entity;
530
+ /**
531
+ * An alias of engine.addEntity(true)
532
+ */
419
533
  addDynamicEntity(): Entity;
534
+ /**
535
+ * Remove all components of an entity
536
+ * @param entity
537
+ */
420
538
  removeEntity(entity: Entity): void;
539
+ /**
540
+ * Add the system to the engine. It will be called every tick updated.
541
+ * @param system function that receives the delta time between last tick and current one.
542
+ * @param priority a number with the priority, big number are called before smaller ones
543
+ * @param name optional: a unique name to identify it
544
+ *
545
+ * Example:
546
+ * ```ts
547
+ * function mySystem(dt: number) {
548
+ * const entitiesWithBoxShapes = engine.getEntitiesWith(BoxShape, Transform)
549
+ * for (const [entity, _boxShape, _transform] of engine.getEntitiesWith(BoxShape, Transform)) {
550
+ * // do stuffs
551
+ * }
552
+ * }
553
+ * engine.addSystem(mySystem, 10)
554
+ * ```
555
+ */
421
556
  addSystem(system: Update, priority?: number, name?: string): void;
557
+ /**
558
+ * Remove a system from the engine.
559
+ * @param selector the function or the unique name to identify
560
+ * @returns if it was found and removed
561
+ */
422
562
  removeSystem(selector: string | Update): boolean;
423
- defineComponent<T extends ISchema>(componentId: number, spec: T): ComponentDefinition<T>;
424
- mutableGroupOf<T extends [ComponentDefinition, ...ComponentDefinition[]]>(...components: T): Iterable<[Entity, ...ComponentSchema<T>]>;
425
- groupOf<T extends [ComponentDefinition, ...ComponentDefinition[]]>(...components: T): Iterable<[Entity, ...DeepReadonly<ComponentSchema<T>>]>;
563
+ /**
564
+ * Define a component and add it to the engine.
565
+ * @param spec An object with schema fields
566
+ * @param componentId unique id to identify the component, if the component id already exist, it will fail.
567
+ * @return The component definition
568
+ *
569
+ * ```ts
570
+ * const DoorComponentId = 10017
571
+ * const Door = engine.defineComponent({
572
+ * id: Schemas.Int,
573
+ * name: Schemas.String
574
+ * }, DoorComponentId)
575
+ *
576
+ * ```
577
+ */
578
+ defineComponent<T extends Spec>(spec: Spec, componentId: number): ComponentDefinition<ISchema<Result<T>>>;
579
+ /**
580
+ * Define a component and add it to the engine.
581
+ * @param spec An object with schema fields
582
+ * @param componentId unique id to identify the component, if the component id already exist, it will fail.
583
+ * @return The component definition
584
+ *
585
+ * ```ts
586
+ * const StateComponentId = 10023
587
+ * const StateComponent = engine.defineComponent(Schemas.Bool, VisibleComponentId)
588
+ * ```
589
+ */
590
+ defineComponentFromSchema<T extends ISchema>(spec: T, componentId: number): ComponentDefinition<T>;
591
+ /**
592
+ * Get the component definition from the component id.
593
+ * @param componentId
594
+ * @return the component definition, throw an error if it doesn't exist
595
+ * ```ts
596
+ * const StateComponentId = 10023
597
+ * const StateComponent = engine.getComponent(StateComponentId)
598
+ * ```
599
+ */
426
600
  getComponent<T extends ISchema>(componentId: number): ComponentDefinition<T>;
427
- update(dt: number): void;
601
+ /**
602
+ * Get a iterator of entities that has all the component requested.
603
+ * @param components a list of component definitions
604
+ * @return An iterator of an array with the [entity, component1, component2, ...]
605
+ *
606
+ * Example:
607
+ * ```ts
608
+ * for (const [entity, boxShape, transform] of engine.getEntitiesWith(BoxShape, Transform)) {
609
+ * // the properties of boxShape and transform are read only
610
+ * }
611
+ * ```
612
+ */
613
+ getEntitiesWith<T extends [ComponentDefinition, ...ComponentDefinition[]]>(...components: T): Iterable<[Entity, ...DeepReadonly<ComponentSchema<T>>]>;
428
614
  baseComponents: SdkComponents;
429
615
  };
430
616
 
@@ -479,6 +665,8 @@ export declare interface ISize {
479
665
  height: number;
480
666
  }
481
667
 
668
+ export declare const log: (...a: any[]) => void;
669
+
482
670
  /**
483
671
  * Class used to store matrix data (4x4)
484
672
  * @public
@@ -1750,7 +1938,7 @@ declare type ReceiveMessage = {
1750
1938
  /**
1751
1939
  * @public
1752
1940
  */
1753
- declare type Result<T extends Spec> = ToOptional<{
1941
+ export declare type Result<T extends Spec> = ToOptional<{
1754
1942
  [K in keyof T]: T[K] extends ISchema ? ReturnType<T[K]['deserialize']> : T[K] extends Spec ? Result<T[K]> : never;
1755
1943
  }>;
1756
1944
 
@@ -1777,7 +1965,7 @@ export declare namespace Schemas {
1777
1965
  /**
1778
1966
  * @public
1779
1967
  */
1780
- declare type SdkComponents = ReturnType<typeof defineLibraryComponents>;
1968
+ export declare type SdkComponents = ReturnType<typeof defineLibraryComponents>;
1781
1969
 
1782
1970
  /**
1783
1971
  * Defines supported spaces
@@ -1795,7 +1983,7 @@ export declare enum Space {
1795
1983
  /**
1796
1984
  * @public
1797
1985
  */
1798
- declare interface Spec {
1986
+ export declare interface Spec {
1799
1987
  [key: string]: ISchema;
1800
1988
  }
1801
1989
 
@@ -1840,14 +2028,14 @@ z: number;
1840
2028
  parent?: Entity | undefined;
1841
2029
  }>>;
1842
2030
 
1843
- declare type Transport = {
2031
+ export declare type Transport = {
1844
2032
  type: string;
1845
2033
  send(message: Uint8Array): void;
1846
2034
  onmessage?(message: Uint8Array): void;
1847
2035
  filter(message: Omit<TransportMessage, 'messageBuffer'>): boolean;
1848
2036
  };
1849
2037
 
1850
- declare type TransportMessage = Omit<ReceiveMessage, 'data'>;
2038
+ export declare type TransportMessage = Omit<ReceiveMessage, 'data'>;
1851
2039
 
1852
2040
  declare type Uint32 = number;
1853
2041
 
@@ -1859,7 +2047,7 @@ export declare type Unpacked<T> = T extends (infer U)[] ? U : T;
1859
2047
  /**
1860
2048
  * @public
1861
2049
  */
1862
- declare type Update = (dt: number) => void;
2050
+ export declare type Update = (dt: number) => void;
1863
2051
 
1864
2052
  /**
1865
2053
  * @public