game-data-gen 6.0.0 → 6.1.0

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/README.md CHANGED
@@ -77,17 +77,18 @@ A set of typed arrays of fixed capacity, ideal for cache-friendly iteration over
77
77
  # <name> struct
78
78
  ```
79
79
 
80
- A TypeScript type with create, copy, and zero functions. Fields can be primitives, primitive arrays, or other structs.
81
-
82
- | Field type | Description |
83
- | ----------- | ---------------------------------------------------- |
84
- | `string` | A string primitive |
85
- | `number` | A number primitive |
86
- | `boolean` | A boolean primitive |
87
- | `string[]` | A dynamic array of strings |
88
- | `number[]` | A dynamic array of numbers |
89
- | `boolean[]` | A dynamic array of booleans |
90
- | `<name>` | A nested struct (must be defined before this struct) |
80
+ A TypeScript type with create and zero functions. Fields can be primitives, arrays, or other structs.
81
+
82
+ | Field type | Description |
83
+ | ------------ | ---------------------------------------------------- |
84
+ | `string` | A string primitive |
85
+ | `number` | A number primitive |
86
+ | `boolean` | A boolean primitive |
87
+ | `string[]` | A dynamic array of strings |
88
+ | `number[]` | A dynamic array of numbers |
89
+ | `boolean[]` | A dynamic array of booleans |
90
+ | `<name>` | A nested struct (must be defined before this struct) |
91
+ | `<name>[]` | A dynamic array of structs |
91
92
 
92
93
  ### Array of Structures (AOS)
93
94
 
@@ -150,7 +151,7 @@ This will create or update the `src/data.ts` file (see below). The data and func
150
151
  export let score = 0;
151
152
  export let name = "";
152
153
  export let isPaused = false;
153
- export const enemies: Array<number> = [];
154
+ export const enemies: number[] = [];
154
155
 
155
156
  /** Set the value of the score field within the game group. */
156
157
  export function setScore(v: number) {
@@ -243,12 +244,6 @@ export function createVector2() {
243
244
  return obj;
244
245
  }
245
246
 
246
- /** Copy the values of Vector2 object b into Vector2 object a. */
247
- export function copyVector2(a: Vector2, b: Vector2) {
248
- a.x = b.x;
249
- a.y = b.y;
250
- }
251
-
252
247
  /** Zero the given Vector2 object. */
253
248
  export function zeroVector2(obj: Vector2) {
254
249
  obj.x = 0;
@@ -280,18 +275,6 @@ export function createEntity() {
280
275
  return obj;
281
276
  }
282
277
 
283
- /** Copy the values of Entity object b into Entity object a. */
284
- export function copyEntity(a: Entity, b: Entity) {
285
- a.name = b.name;
286
- a.health = b.health;
287
- a.isActive = b.isActive;
288
- copyVector2(a.position, b.position);
289
- a.tags.length = b.tags.length;
290
- for (let i = 0; i < b.tags.length; i++) {
291
- a.tags[i] = b.tags[i];
292
- }
293
- }
294
-
295
278
  /** Zero the given Entity object. */
296
279
  export function zeroEntity(obj: Entity) {
297
280
  obj.name = "";
package/dist/consts.js CHANGED
@@ -20,8 +20,4 @@ export var FieldType;
20
20
  FieldType["STRING"] = "string";
21
21
  FieldType["NUMBER"] = "number";
22
22
  FieldType["BOOLEAN"] = "boolean";
23
- // Primitive arrays (Group and Struct)
24
- FieldType["ARRAY_STRING"] = "string[]";
25
- FieldType["ARRAY_NUMBER"] = "number[]";
26
- FieldType["ARRAY_BOOLEAN"] = "boolean[]";
27
23
  })(FieldType || (FieldType = {}));
package/dist/lib/aos.js CHANGED
@@ -27,7 +27,7 @@ function addArrayOfStructuresZeroFunction(name, struct, length, output) {
27
27
  function addArrayOfStructuresZeroAtIndexFunction(name, struct, output) {
28
28
  output.push("");
29
29
  output.push(`/** Zero an object at a specific index within the ${name} array of structures. */`);
30
- output.push(`export function zero${capitalize(name)}At(index: number) {`);
31
- output.push(` zero${capitalize(struct)}(${name}[index])`);
30
+ output.push(`export function zero${capitalize(name)}At(i: number) {`);
31
+ output.push(` zero${capitalize(struct)}(${name}[i])`);
32
32
  output.push("}");
33
33
  }
package/dist/lib/group.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { FieldType } from "../consts.js";
2
- import { addHeader, capitalize } from "./utils.js";
2
+ import { addHeader, capitalize, isArrayType } from "./utils.js";
3
3
  export function addGroup(header, fields, output) {
4
4
  const [name] = header.split(" ");
5
5
  addHeader(`${name} (Group)`, output);
@@ -16,48 +16,32 @@ export function addGroup(header, fields, output) {
16
16
  }
17
17
  function addFieldDefinition(field, output) {
18
18
  const [fieldName, fieldType] = field.split(" ");
19
- switch (fieldType) {
20
- case FieldType.STRING:
21
- output.push(`export let ${fieldName} = ""`);
22
- break;
23
- case FieldType.NUMBER:
24
- output.push(`export let ${fieldName} = 0`);
25
- break;
26
- case FieldType.BOOLEAN:
27
- output.push(`export let ${fieldName} = false`);
28
- break;
29
- case FieldType.ARRAY_STRING:
30
- output.push(`export const ${fieldName}: Array<string> = []`);
31
- break;
32
- case FieldType.ARRAY_NUMBER:
33
- output.push(`export const ${fieldName}: Array<number> = []`);
34
- break;
35
- case FieldType.ARRAY_BOOLEAN:
36
- output.push(`export const ${fieldName}: Array<boolean> = []`);
37
- break;
19
+ if (isArrayType(fieldType)) {
20
+ output.push(`export const ${fieldName}: ${fieldType} = []`);
21
+ }
22
+ else {
23
+ switch (fieldType) {
24
+ case FieldType.STRING:
25
+ output.push(`export let ${fieldName} = ""`);
26
+ break;
27
+ case FieldType.NUMBER:
28
+ output.push(`export let ${fieldName} = 0`);
29
+ break;
30
+ case FieldType.BOOLEAN:
31
+ output.push(`export let ${fieldName} = false`);
32
+ break;
33
+ }
38
34
  }
39
35
  }
40
36
  function addFieldSetFunction(name, field, output) {
41
37
  const [fieldName, fieldType] = field.split(" ");
42
38
  switch (fieldType) {
43
39
  case FieldType.STRING:
44
- output.push("");
45
- output.push(`/** Set the value of the ${fieldName} field within the ${name} group. */`);
46
- output.push(`export function set${capitalize(fieldName)}(v: string) {`);
47
- output.push(` ${fieldName} = v`);
48
- output.push("}");
49
- break;
50
40
  case FieldType.NUMBER:
51
- output.push("");
52
- output.push(`/** Set the value of the ${fieldName} field within the ${name} group. */`);
53
- output.push(`export function set${capitalize(fieldName)}(v: number) {`);
54
- output.push(` ${fieldName} = v`);
55
- output.push("}");
56
- break;
57
41
  case FieldType.BOOLEAN:
58
42
  output.push("");
59
43
  output.push(`/** Set the value of the ${fieldName} field within the ${name} group. */`);
60
- output.push(`export function set${capitalize(fieldName)}(v: boolean) {`);
44
+ output.push(`export function set${capitalize(fieldName)}(v: ${fieldType}) {`);
61
45
  output.push(` ${fieldName} = v`);
62
46
  output.push("}");
63
47
  break;
@@ -82,20 +66,20 @@ function addZeroFunction(name, fields, output) {
82
66
  output.push("}");
83
67
  }
84
68
  function zeroField(name, type, output) {
85
- switch (type) {
86
- case FieldType.STRING:
87
- output.push(` ${name} = ""`);
88
- break;
89
- case FieldType.NUMBER:
90
- output.push(` ${name} = 0`);
91
- break;
92
- case FieldType.BOOLEAN:
93
- output.push(` ${name} = false`);
94
- break;
95
- case FieldType.ARRAY_STRING:
96
- case FieldType.ARRAY_NUMBER:
97
- case FieldType.ARRAY_BOOLEAN:
98
- output.push(` ${name}.length = 0`);
99
- break;
69
+ if (isArrayType(type)) {
70
+ output.push(` ${name}.length = 0`);
71
+ }
72
+ else {
73
+ switch (type) {
74
+ case FieldType.STRING:
75
+ output.push(` ${name} = ""`);
76
+ break;
77
+ case FieldType.NUMBER:
78
+ output.push(` ${name} = 0`);
79
+ break;
80
+ case FieldType.BOOLEAN:
81
+ output.push(` ${name} = false`);
82
+ break;
83
+ }
100
84
  }
101
85
  }
@@ -1,11 +1,10 @@
1
1
  import { FieldType } from "../consts.js";
2
- import { addHeader, capitalize } from "./utils.js";
2
+ import { addHeader, capitalize, isArrayType } from "./utils.js";
3
3
  export function addStruct(header, fields, output) {
4
4
  const [name] = header.split(" ");
5
5
  addHeader(`${name} (Struct)`, output);
6
6
  addStructTypeDefinition(name, fields, output);
7
7
  addStructCreateFunction(name, fields, output);
8
- addStructCopyFunction(name, fields, output);
9
8
  addStructZeroFunction(name, fields, output);
10
9
  }
11
10
  function addStructTypeDefinition(name, fields, output) {
@@ -23,52 +22,26 @@ function addStructCreateFunction(name, fields, output) {
23
22
  output.push(` const obj: ${name} = Object.create(null)`);
24
23
  for (const field of fields) {
25
24
  const [fieldName, fieldType] = field.split(" ");
26
- switch (fieldType) {
27
- case FieldType.STRING:
28
- output.push(` obj.${fieldName} = ""`);
29
- break;
30
- case FieldType.NUMBER:
31
- output.push(` obj.${fieldName} = 0`);
32
- break;
33
- case FieldType.BOOLEAN:
34
- output.push(` obj.${fieldName} = false`);
35
- break;
36
- case FieldType.ARRAY_STRING:
37
- case FieldType.ARRAY_NUMBER:
38
- case FieldType.ARRAY_BOOLEAN:
39
- output.push(` obj.${fieldName} = []`);
40
- break;
41
- default:
42
- output.push(` obj.${fieldName} = create${capitalize(fieldType)}()`);
25
+ if (isArrayType(fieldType)) {
26
+ output.push(` obj.${fieldName} = []`);
43
27
  }
44
- }
45
- output.push(" return obj");
46
- output.push("}");
47
- }
48
- function addStructCopyFunction(name, fields, output) {
49
- output.push("");
50
- output.push(`/** Copy the values of ${capitalize(name)} object b into ${capitalize(name)} object a. */`);
51
- output.push(`export function copy${capitalize(name)}(a: ${name}, b: ${name}) {`);
52
- for (const field of fields) {
53
- const [fieldName, fieldType] = field.split(" ");
54
- switch (fieldType) {
55
- case FieldType.STRING:
56
- case FieldType.NUMBER:
57
- case FieldType.BOOLEAN:
58
- output.push(` a.${fieldName} = b.${fieldName}`);
59
- break;
60
- case FieldType.ARRAY_STRING:
61
- case FieldType.ARRAY_NUMBER:
62
- case FieldType.ARRAY_BOOLEAN:
63
- output.push(` a.${fieldName}.length = b.${fieldName}.length`);
64
- output.push(` for (let i = 0; i < b.${fieldName}.length; i++) {`);
65
- output.push(` a.${fieldName}[i] = b.${fieldName}[i]`);
66
- output.push(` }`);
67
- break;
68
- default:
69
- output.push(` copy${capitalize(fieldType)}(a.${fieldName}, b.${fieldName})`);
28
+ else {
29
+ switch (fieldType) {
30
+ case FieldType.STRING:
31
+ output.push(` obj.${fieldName} = ""`);
32
+ break;
33
+ case FieldType.NUMBER:
34
+ output.push(` obj.${fieldName} = 0`);
35
+ break;
36
+ case FieldType.BOOLEAN:
37
+ output.push(` obj.${fieldName} = false`);
38
+ break;
39
+ default:
40
+ output.push(` obj.${fieldName} = create${capitalize(fieldType)}()`);
41
+ }
70
42
  }
71
43
  }
44
+ output.push(" return obj");
72
45
  output.push("}");
73
46
  }
74
47
  function addStructZeroFunction(name, fields, output) {
@@ -77,23 +50,23 @@ function addStructZeroFunction(name, fields, output) {
77
50
  output.push(`export function zero${capitalize(name)}(obj: ${name}) {`);
78
51
  for (const field of fields) {
79
52
  const [fieldName, fieldType] = field.split(" ");
80
- switch (fieldType) {
81
- case FieldType.STRING:
82
- output.push(` obj.${fieldName} = ""`);
83
- break;
84
- case FieldType.NUMBER:
85
- output.push(` obj.${fieldName} = 0`);
86
- break;
87
- case FieldType.BOOLEAN:
88
- output.push(` obj.${fieldName} = false`);
89
- break;
90
- case FieldType.ARRAY_STRING:
91
- case FieldType.ARRAY_NUMBER:
92
- case FieldType.ARRAY_BOOLEAN:
93
- output.push(` obj.${fieldName}.length = 0`);
94
- break;
95
- default:
96
- output.push(` zero${capitalize(fieldType)}(obj.${fieldName})`);
53
+ if (isArrayType(fieldType)) {
54
+ output.push(` obj.${fieldName}.length = 0`);
55
+ }
56
+ else {
57
+ switch (fieldType) {
58
+ case FieldType.STRING:
59
+ output.push(` obj.${fieldName} = ""`);
60
+ break;
61
+ case FieldType.NUMBER:
62
+ output.push(` obj.${fieldName} = 0`);
63
+ break;
64
+ case FieldType.BOOLEAN:
65
+ output.push(` obj.${fieldName} = false`);
66
+ break;
67
+ default:
68
+ output.push(` zero${capitalize(fieldType)}(obj.${fieldName})`);
69
+ }
97
70
  }
98
71
  }
99
72
  output.push("}");
package/dist/lib/utils.js CHANGED
@@ -10,3 +10,6 @@ export function addHeader(name, output) {
10
10
  export function capitalize(str) {
11
11
  return `${str.substring(0, 1).toUpperCase()}${str.substring(1)}`;
12
12
  }
13
+ export function isArrayType(type) {
14
+ return type.endsWith("[]");
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "game-data-gen",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"