@perplexdotgg/mecs 0.4.0 → 0.4.2

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
@@ -3,6 +3,7 @@ import { MonomorphClass } from 'monomorph';
3
3
  import { MonomorphInstance } from 'monomorph';
4
4
  import { NumberArray } from 'monomorph';
5
5
  import { PartialRecursive } from 'monomorph';
6
+ import { PoolClass } from 'monomorph';
6
7
  import { WithPool } from 'monomorph';
7
8
 
8
9
  export declare type ComponentConfig<ComponentType, ComponentInputType extends any = InputType<ComponentType> extends never ? ComponentType : InputType<ComponentType>> = {
@@ -11,10 +12,11 @@ export declare type ComponentConfig<ComponentType, ComponentInputType extends an
11
12
  afterComponentAddedCode?: string | ComponentConfigFunction<ComponentType, ComponentInputType>;
12
13
  beforeComponentRemoved?: (componentInstance: ComponentType extends abstract new (...args: any) => any ? InstanceType<ComponentType> : ComponentType, entity: any, componentKey: string) => void;
13
14
  beforeComponentRemovedCode?: string | ComponentConfigFunction<ComponentType, ComponentInputType>;
14
- processDataOnAdd?: (data: ComponentInputType) => ComponentInputType;
15
+ processDataOnAdd?: (data: ComponentInputType, entity: any, componentKey: string) => ComponentInputType;
15
16
  processDataOnAddCode?: string | ((options: {
16
17
  dataVariableName: string;
17
18
  componentConfigReference: string;
19
+ entityVariableName: string;
18
20
  }) => string);
19
21
  nullComponent?: (entity: any, componentKey: string) => void;
20
22
  nullComponentCode?: string | ComponentConfigFunction<ComponentType, ComponentInputType>;
@@ -105,9 +107,7 @@ export declare interface EntityConstructor<QM extends QueryMap<any, CM>, E exten
105
107
  };
106
108
  };
107
109
  components: {
108
- [K in keyof CM as ExtractMonomorphClass<CM[K]> extends MonomorphClass<infer P, infer I, infer M> ? LowercaseFirstLetter<K> : never]: {
109
- [Symbol.iterator](): IterableIterator<WithPool<InstanceType<ExtractMonomorphClass<CM[K]>>>>;
110
- };
110
+ [K in keyof CM as ExtractMonomorphClass<CM[K]> extends MonomorphClass<infer P, infer I, infer M> ? LowercaseFirstLetter<K> : never]: PoolClass<ExtractMonomorphClass<CM[K]>>;
111
111
  };
112
112
  queries: {
113
113
  [K in keyof QM]: {
@@ -124,7 +124,7 @@ export declare interface EntityConstructor<QM extends QueryMap<any, CM>, E exten
124
124
  };
125
125
  }
126
126
 
127
- export declare type EntityInput<X extends ComponentMap | EntityClass<any, any, any, any> | EntityInstanceWithPool<any, any, any> | EntityInstance<any, any>> = X extends ComponentMap ? ComponentMapInput<X> : X extends EntityConstructor<infer QM, infer E, infer I, infer CM> ? I : X extends EntityInstanceWithPool<infer CM, infer I, infer E> ? I : X extends EntityInstance<infer CM, infer I> ? I : never;
127
+ export declare type EntityInput<X extends ComponentMap | EntityClass<any, any, any, any> | EntityInstanceWithPool<any, any, any> | EntityInstance<any, any>> = Writeable<X extends ComponentMap ? ComponentMapInput<X> : X extends EntityConstructor<infer QM, infer E, infer I, infer CM> ? I : X extends EntityInstanceWithPool<infer CM, infer I, infer E> ? I : X extends EntityInstance<infer CM, infer I> ? I : never>;
128
128
 
129
129
  export declare type EntityInstance<CM extends ComponentMap, I extends any = ComponentMapInput<CM>> = ComponentMapClassProperties<CM> & EntityBaseProperties<CM, I>;
130
130
 
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
  `;
@@ -117,12 +107,12 @@ function getEntityClassCode(componentMap, queries, options) {
117
107
  referenceComponentFunctionsCode += `
118
108
  const ${componentKey}ProcessDataOnAdd = componentMap.${key}.processDataOnAdd;
119
109
  `;
120
- processedDataCode = `${componentKey}ProcessDataOnAdd(data)`;
110
+ processedDataCode = `${componentKey}ProcessDataOnAdd(data, entity, '${componentKey}')`;
121
111
  } else if ("processDataOnAddCode" in component) {
122
112
  if (typeof component.processDataOnAddCode === "string") {
123
113
  processedDataCode = component.processDataOnAddCode;
124
114
  } else {
125
- processedDataCode = component.processDataOnAddCode({ dataVariableName: "data", componentConfigReference: "componentMap." + key });
115
+ processedDataCode = component.processDataOnAddCode({ dataVariableName: "data", entityVariableName: "entity", componentConfigReference: "componentMap." + key });
126
116
  }
127
117
  }
128
118
  if ("nullComponent" in component) {
@@ -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++;
@@ -613,7 +636,6 @@ function getEntityClassCode(componentMap, queries, options) {
613
636
  }
614
637
 
615
638
  destroy(updateQueryMemberships = true) {
616
- const thisComponentFlags = this.componentFlags;
617
639
  ${destroyCode}
618
640
  if (this.version & 1 === 1) {
619
641
  ${""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@perplexdotgg/mecs",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
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",