game-data-gen 1.0.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.
Files changed (3) hide show
  1. package/README.md +170 -0
  2. package/dist/main.js +2377 -0
  3. package/package.json +24 -0
package/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # Game Data Generation
2
+
3
+ A Javascript (Typescript) library to generate data structures with zeroing functions.
4
+
5
+ ## The problems
6
+
7
+ If you're making a game in Javascript then you might (this was actually me):
8
+
9
+ - hit the garbage collector (GC) a bunch causing frame drops because you're creating/destroying objects every frame (particles, for example)
10
+ - read a book about [Data Oriented Design](https://www.amazon.com/dp/1916478700)
11
+ - notice the performance implications of OOP (especially classes and calling their methods) versus using something like Structure of Arrays
12
+ - wanting to implement Structure of Arrays instead of Array of Structures (which is a list of class instances, see previous point)
13
+ - notice that Javascript can not simply zero out data structures (resetting all data back to initial values) like languages such as C and Rust
14
+
15
+ ## The solution
16
+
17
+ This library:
18
+
19
+ - creates data structures based on a very easy syntax (I tried JSON, didn't feel it)
20
+ - each data structure gets associated functions to zero out its memory so it can be reused
21
+
22
+ ## Installation
23
+
24
+ ```shell
25
+ npm i -D game-data-gen
26
+ ```
27
+
28
+ ## Usage
29
+
30
+ ```shell
31
+ npx game-data-gen <input-file-path> <optional-output-file-path>
32
+ ```
33
+
34
+ ## Format
35
+
36
+ ```
37
+ name type? length?
38
+ fieldName fieldType fieldArrayType? fieldArrayLength?
39
+ ```
40
+
41
+ `name`
42
+
43
+ The name of the data structure.
44
+
45
+ `type` (optional)
46
+
47
+ Supported data structure types:
48
+
49
+ - soa (Structure of Arrays)
50
+
51
+ If no type is given, it will act as a group which gets a zero function for the whole group.
52
+
53
+ `length` (optional)
54
+
55
+ The length of the arrays within the Structure of Arrays data structure.
56
+
57
+ If no length is given to the type and no length is given to a field it is considered a dynamic array and zeroing will set the array's length back to zero (emptying it).
58
+
59
+ `fieldName`
60
+
61
+ The name of one of the fields within the data structure.
62
+
63
+ `fieldType`
64
+
65
+ Supported field types:
66
+
67
+ - array
68
+
69
+ `fieldArrayType` (optional, required if fieldType=array)
70
+
71
+ Supported array field types:
72
+
73
+ - number
74
+ - int8
75
+ - int16
76
+ - int32
77
+ - uint8
78
+ - uint16
79
+ - uint32
80
+ - float32
81
+ - float64
82
+
83
+ `fieldArrayLength` (optional)
84
+
85
+ The length of the array field.
86
+
87
+ In case of a Structure of Arrays data structure (type=soa), setting the length on the type is recommended so that all arrays have the same length.
88
+
89
+ ## Example
90
+
91
+ Create a plain text file somewhere in your source code (it does not need a file extension). For example `src/data/game`:
92
+
93
+ ```
94
+ Game
95
+ activeEntities array number
96
+
97
+ Entity soa 2048
98
+ posX array float32
99
+ posy array float32
100
+ isActive array uint8
101
+ ```
102
+
103
+ Run the package with (consider making this a script in your package.json):
104
+
105
+ ```shell
106
+ npx game-data-gen src/data/game
107
+ ```
108
+
109
+ This will create or update the `src/data/game.ts` file:
110
+
111
+ ```typescript
112
+ /*
113
+ * --------------------------------------------------
114
+ * Game
115
+ * --------------------------------------------------
116
+ */
117
+
118
+ export const activeEntities = new Array<number>();
119
+
120
+ /** Zero the activeEntities field within the Game group. */
121
+ export function zeroActiveEntities() {
122
+ activeEntities.length = 0;
123
+ }
124
+
125
+ /** Zero all fields within the Game group. */
126
+ export function zeroGameData() {
127
+ activeEntities.length = 0;
128
+ }
129
+
130
+ /*
131
+ * --------------------------------------------------
132
+ * Entity
133
+ * --------------------------------------------------
134
+ */
135
+
136
+ export const posX = new Float32Array(2048);
137
+ export const posY = new Float32Array(2048);
138
+ export const isActive = new Uint8Array(2048);
139
+
140
+ /** Zero an index within the Entity Structure of Arrays. */
141
+ export function zeroEntity(idx: number) {
142
+ posX[idx] = 0;
143
+ posY[idx] = 0;
144
+ isActive[idx] = 0;
145
+ }
146
+
147
+ /** Zero the posX field within the Entity Structure of Arrays. */
148
+ export function zeroPosX() {
149
+ posX.fill(0);
150
+ }
151
+
152
+ /** Zero the posY field within the Entity Structure of Arrays. */
153
+ export function zeroPosY() {
154
+ posY.fill(0);
155
+ }
156
+
157
+ /** Zero the isActive field within the Entity Structure of Arrays. */
158
+ export function zeroIsActive() {
159
+ isActive.fill(0);
160
+ }
161
+
162
+ /** Zero all fields within the Entity Structure of Arrays. */
163
+ export function zeroEntityData() {
164
+ posX.fill(0);
165
+ posY.fill(0);
166
+ isActive.fill(0);
167
+ }
168
+ ```
169
+
170
+ Then import the data and its functions from `src/data/game.ts` into your code.