@perplexdotgg/mecs 0.3.2 → 0.4.1

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/build/index.d.ts CHANGED
@@ -2,6 +2,7 @@ import { InputType } from 'monomorph';
2
2
  import { MonomorphClass } from 'monomorph';
3
3
  import { MonomorphInstance } from 'monomorph';
4
4
  import { NumberArray } from 'monomorph';
5
+ import { PartialRecursive } from 'monomorph';
5
6
  import { WithPool } from 'monomorph';
6
7
 
7
8
  export declare type ComponentConfig<ComponentType, ComponentInputType extends any = InputType<ComponentType> extends never ? ComponentType : InputType<ComponentType>> = {
@@ -49,18 +50,19 @@ export declare type ComponentMapInput<CM extends ComponentMap> = Partial<{
49
50
 
50
51
  export declare function createEntityClass<C>(options?: EntityCodeGenerationOptions): <CM extends ComponentMap, QM extends QueryMap<C, CM>, I extends ComponentMapInput<CM> = ComponentMapInput<CM>>(componentMap: CM, queries?: QM) => EntityClassWithStatics<C, CM, QM, I>;
51
52
 
52
- export declare type CreateEntityFunction<C, CM extends ComponentMap, I extends ComponentMapInput<CM>> = (data?: I, pool?: EntityPoolClass<C>) => EntityInstanceWithPool<CM, I, C>;
53
+ export declare type CreateEntityFunction<C, CM extends ComponentMap, I extends ComponentMapInput<CM>> = (data?: I, updateQueryMemberships?: boolean, pool?: EntityPoolClass<C>) => EntityInstanceWithPool<CM, I, C>;
53
54
 
54
55
  declare type ElementOfArray<T> = T extends (infer E)[] ? E : never;
55
56
 
56
57
  declare interface EntityBaseProperties<CM extends ComponentMap, I extends any = ComponentMapInput<CM>> {
57
58
  componentMap: CM;
58
59
  __typescriptOnlyInputType: I;
59
- addComponent: <CCK extends LowercaseFirstLetter<keyof CM>, CC extends CM[OriginalComponentKey<CCK, CM>] = CM[OriginalComponentKey<CCK, CM>]>(...a: (undefined extends ComponentInput<ExtractMonomorphClass<CC>> ? [key: CCK, data?: ComponentInput<ExtractMonomorphClass<CC>>] : [key: CCK, data: ComponentInput<ExtractMonomorphClass<CC>>])) => void;
60
- removeComponent: <CCK extends LowercaseFirstLetter<keyof CM>>(key: CCK) => void;
60
+ addComponent: <CCK extends LowercaseFirstLetter<keyof CM>, CC extends CM[OriginalComponentKey<CCK, CM>] = CM[OriginalComponentKey<CCK, CM>]>(...a: (undefined extends ComponentInput<ExtractMonomorphClass<CC>> ? [key: CCK, data?: ComponentInput<ExtractMonomorphClass<CC>>, updateQueryMemberships?: boolean] : [key: CCK, data: ComponentInput<ExtractMonomorphClass<CC>>, updateQueryMemberships?: boolean])) => void;
61
+ removeComponent: <CCK extends LowercaseFirstLetter<keyof CM>>(key: CCK, updateQueryMemberships?: boolean) => void;
61
62
  hasComponent: <CCK extends LowercaseFirstLetter<keyof CM>>(key: CCK) => boolean;
62
63
  clone(): this;
63
- destroy(): void;
64
+ reset(data?: PartialRecursive<I>, updateQueryMemberships?: boolean, pool?: EntityPoolClass<this>): void;
65
+ destroy(updateQueryMemberships?: boolean): void;
64
66
  isDestroyed(): boolean;
65
67
  }
66
68
 
@@ -93,7 +95,7 @@ declare type EntityCodeGenerationOptions = {
93
95
  };
94
96
 
95
97
  export declare interface EntityConstructor<QM extends QueryMap<any, CM>, E extends EntityInstance<CM, I>, I extends any, CM extends ComponentMap> {
96
- new (data: I): E;
98
+ new (data: I, index?: number, pool?: EntityPoolClass<E>, updateQueryMemberships?: boolean): E;
97
99
  componentMap: CM;
98
100
  __typescriptOnlyInputType: I;
99
101
  pool: EntityPoolClass<E>;
package/build/mecs.js CHANGED
@@ -42,18 +42,8 @@ function getEntityClassCode(componentMap, queries, options) {
42
42
  `;
43
43
  componentFlags.push(componentFlagValue);
44
44
  componentFlagsCode += `${componentFlagValue}n, `;
45
- createFromDataCode += `
46
- if ('${componentKey}' in data) {
47
- this.addComponent('${componentKey}', data.${componentKey}, false);
48
- } else {
49
- this.${componentKey} = null;
50
- }
51
- `;
52
- createFromDataCodeAllNulls += `
53
- this.${componentKey} = null;
54
- `;
55
45
  destroyCode += `
56
- if ((thisComponentFlags & ${componentFlagValue}n) !== 0n) {
46
+ if ((this.componentFlags & ${componentFlagValue}n) !== 0n) {
57
47
  this.removeComponent('${componentKey}', updateQueryMemberships);
58
48
  }
59
49
  `;
@@ -229,9 +219,8 @@ function getEntityClassCode(componentMap, queries, options) {
229
219
  `;
230
220
  removeComponentFunctionsCode += `
231
221
  (instance, entity, componentKey, updateQueryMemberships) => {
232
- const newFlagValue = entity.componentFlags & ~${componentFlagValue}n;
233
222
  if (updateQueryMemberships) {
234
- entity.updateQueryMemberships(newFlagValue);
223
+ entity.updateQueryMemberships(entity.componentFlags & ~${componentFlagValue}n);
235
224
  }
236
225
 
237
226
  ${beforeComponentRemovedCode}
@@ -239,10 +228,28 @@ function getEntityClassCode(componentMap, queries, options) {
239
228
  ${componentKey}ComponentToEntity[instance.index] = null;
240
229
  instance.destroy();
241
230
 
242
- entity.componentFlags = newFlagValue;
231
+ ${""}
232
+ ${""}
233
+ entity.componentFlags = entity.componentFlags & ~${componentFlagValue}n;
243
234
  ${nullComponentCode};
244
235
  },
245
236
  `;
237
+ createFromDataCode += `
238
+ if ('${componentKey}' in data) {
239
+ if (this.hasComponent('${componentKey}')) {
240
+ this.${componentKey}.reset(data.${componentKey}, false);
241
+ } else {
242
+ this.addComponent('${componentKey}', data.${componentKey}, false);
243
+ }
244
+ } else if (!this.hasComponent('${componentKey}')) {
245
+ this.${componentKey} = null;
246
+ }
247
+ `;
248
+ createFromDataCodeAllNulls += `
249
+ if (!this.hasComponent('${componentKey}')) {
250
+ this.${componentKey} = null;
251
+ }
252
+ `;
246
253
  } else {
247
254
  addComponentFunctionsCode += `
248
255
  (data, entity, componentKey, updateQueryMemberships) => {
@@ -269,6 +276,22 @@ function getEntityClassCode(componentMap, queries, options) {
269
276
  ${nullComponentCode};
270
277
  },
271
278
  `;
279
+ createFromDataCode += `
280
+ if ('${componentKey}' in data) {
281
+ if (this.hasComponent('${componentKey}')) {
282
+ this.${componentKey} = data.${componentKey};
283
+ } else {
284
+ this.addComponent('${componentKey}', data.${componentKey}, false);
285
+ }
286
+ } else if (!this.hasComponent('${componentKey}')) {
287
+ this.${componentKey} = null;
288
+ }
289
+ `;
290
+ createFromDataCodeAllNulls += `
291
+ if (!this.hasComponent('${componentKey}')) {
292
+ this.${componentKey} = null;
293
+ }
294
+ `;
272
295
  }
273
296
  componentFlagValue <<= 1n;
274
297
  componentIndex++;
@@ -553,10 +576,10 @@ function getEntityClassCode(componentMap, queries, options) {
553
576
  }
554
577
 
555
578
  const theClass = class {
556
- constructor(data, index = 0, updateQueryMemberships = true) {
579
+ constructor(data, index = 0, updateQueryMemberships = true, pool = this.constructor.pool) {
557
580
  this.index = index;
558
581
  this.version = 0; ${""}
559
- this.pool = this.constructor.pool;
582
+ this.pool = pool;
560
583
  this.queryIndices = new Array(${queryIndex}).fill(-1);
561
584
  this.componentFlags = 0n; ${""}
562
585
  if (data) {
@@ -569,9 +592,10 @@ function getEntityClassCode(componentMap, queries, options) {
569
592
  }
570
593
  }
571
594
 
572
- fullSet(data, updateQueryMemberships = true) {
595
+ reset(data, updateQueryMemberships = true, pool = this.pool) {
573
596
  this.queryIndices.fill(-1);
574
597
  this.componentFlags = 0n;
598
+ this.pool = pool;
575
599
  if (data) {
576
600
  ${createFromDataCode}
577
601
  } else {
@@ -612,7 +636,6 @@ function getEntityClassCode(componentMap, queries, options) {
612
636
  }
613
637
 
614
638
  destroy(updateQueryMemberships = true) {
615
- const thisComponentFlags = this.componentFlags;
616
639
  ${destroyCode}
617
640
  if (this.version & 1 === 1) {
618
641
  ${""}
@@ -782,7 +805,7 @@ function getEntityClassCode(componentMap, queries, options) {
782
805
  }
783
806
 
784
807
  create(data, updateQueryMemberships = true) {
785
- return this.classConstructor.create(data, this, updateQueryMemberships);
808
+ return this.classConstructor.create(data, updateQueryMemberships, this);
786
809
  }
787
810
 
788
811
  toArray(array, startOffset = 0, skipPoolReferences = false) {
@@ -952,17 +975,16 @@ function getEntityClassCode(componentMap, queries, options) {
952
975
  ${componentWithEntityIteratorFunctionsCode}
953
976
  };
954
977
 
955
- theClass.create = function(data, updateQueryMemberships = true) {
956
- const pool = this.pool;
978
+ theClass.create = function(data, updateQueryMemberships = true, pool = this.pool) {
957
979
  if (pool.freeIndices.length) {
958
980
  const index = pool.freeIndices.pop();
959
981
  pool.length++;
960
982
  const instance = pool.array[index];
961
983
  instance.version++; ${""}
962
- instance.fullSet(data, updateQueryMemberships);
984
+ instance.reset(data, updateQueryMemberships, pool);
963
985
  return instance;
964
986
  } else {
965
- const instance = new this(data, pool.length, updateQueryMemberships);
987
+ const instance = new this(data, pool.length, updateQueryMemberships, pool);
966
988
  pool.array[pool.length] = instance;
967
989
  pool.length++;
968
990
  return instance;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perplexdotgg/mecs",
3
- "version": "0.3.2",
3
+ "version": "0.4.1",
4
4
  "description": "MECS - Monomorph ECS - A high-performance Entity Component System for TypeScript and JavaScript projects, designed for games and simulations.",
5
5
  "repository": {
6
6
  "type": "git",