toosoon-prng 1.4.0 → 2.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.
@@ -1,409 +0,0 @@
1
- import { Gui, GuiController } from 'toosoon-gui';
2
-
3
- import prng from './prng';
4
-
5
- export interface PRNGController<T = unknown> {
6
- seed: string;
7
- value: T;
8
- addGUI(gui: Gui, min?: number, max?: number, step?: number): GuiController;
9
- getValue(): T;
10
- dispose(): void;
11
- }
12
-
13
- export interface PRNGGroupController<T = unknown> {
14
- seed: string;
15
- addGUI(gui: Gui, min?: number, max?: number, step?: number): Gui;
16
- createController(index: number): PRNGController<T>;
17
- getValueAt(index: number): T;
18
- dispose(): void;
19
- }
20
-
21
- // *********************
22
- // Abstract controllers
23
- // *********************
24
- abstract class BasePRNGController<T> implements PRNGController<T> {
25
- seed: string;
26
- abstract value: T;
27
- gui!: GuiController;
28
-
29
- constructor(seed: string) {
30
- this.seed = seed;
31
- prng.addController(this);
32
- }
33
-
34
- abstract addGUI(gui: Gui, min?: number, max?: number, step?: number): GuiController;
35
-
36
- abstract getValue(): T;
37
-
38
- dispose() {
39
- prng.removeController(this);
40
- this.gui?.destroy();
41
- }
42
- }
43
-
44
- abstract class BasePRNGGroupController<T> implements PRNGGroupController<T> {
45
- seed: string;
46
- controllers: PRNGController<T>[] = [];
47
- gui!: Gui;
48
- guiArgs!: {
49
- min?: number;
50
- max?: number;
51
- step?: number;
52
- };
53
-
54
- constructor(seed: string) {
55
- this.seed = seed;
56
- }
57
-
58
- addGUI(gui: Gui, min?: number, max?: number, step?: number) {
59
- this.gui = gui.addFolder(this.seed).close();
60
- this.guiArgs = { min, max, step };
61
- return this.gui;
62
- }
63
-
64
- abstract createController(index: number): PRNGController<T>;
65
-
66
- getValueAt(index: number): T {
67
- let controller = this.controllers[index];
68
- if (!controller) {
69
- controller = this.createController(index);
70
- if (this.gui) {
71
- controller
72
- .addGUI(this.gui, this.guiArgs.min, this.guiArgs.max, this.guiArgs.step)
73
- .name(`${this.seed}-${index}`);
74
- }
75
- this.controllers[index] = controller;
76
- }
77
- return controller.value;
78
- }
79
-
80
- dispose() {
81
- this.controllers.forEach((controller) => controller.dispose());
82
- this.controllers = [];
83
- this.gui?.destroy();
84
- }
85
- }
86
-
87
- // *********************
88
- // Controllers
89
- // *********************
90
- export class BooleanController extends BasePRNGController<boolean> {
91
- value: boolean;
92
- probability: number;
93
-
94
- constructor(seed: string, probability: number = 0.5) {
95
- super(seed);
96
-
97
- this.probability = probability;
98
- this.value = this.getValue();
99
- }
100
-
101
- addGUI(gui: Gui) {
102
- this.gui = gui.add(this, 'value').name(this.seed);
103
- return this.gui;
104
- }
105
-
106
- getValue() {
107
- this.value = prng.randomBoolean(this.seed, this.probability);
108
- this.gui?.updateDisplay();
109
- return this.value;
110
- }
111
- }
112
-
113
- export class SignController extends BasePRNGController<number> {
114
- value: number;
115
- probability: number;
116
-
117
- constructor(seed: string, probability: number = 0.5) {
118
- super(seed);
119
-
120
- this.probability = probability;
121
- this.value = this.getValue();
122
- }
123
-
124
- addGUI(gui: Gui) {
125
- this.gui = gui.add(this, 'value', [-1, 1]).name(this.seed);
126
- return this.gui;
127
- }
128
-
129
- getValue() {
130
- this.value = prng.randomSign(this.seed, this.probability);
131
- this.gui?.updateDisplay();
132
- return this.value;
133
- }
134
- }
135
-
136
- export class FloatController extends BasePRNGController<number> {
137
- value: number;
138
- min: number;
139
- max: number;
140
-
141
- constructor(seed: string, min: number = 0, max: number = 1) {
142
- super(seed);
143
-
144
- this.min = min;
145
- this.max = max;
146
- this.value = this.getValue();
147
- }
148
-
149
- addGUI(gui: Gui, min: number, max: number, step: number = 0.01) {
150
- this.gui = gui.add(this, 'value', min, max, step).name(this.seed);
151
- return this.gui;
152
- }
153
-
154
- getValue() {
155
- this.value = prng.randomFloat(this.seed, this.min, this.max);
156
- this.gui?.updateDisplay();
157
- return this.value;
158
- }
159
- }
160
-
161
- export class IntController extends BasePRNGController<number> {
162
- value: number;
163
- min: number;
164
- max: number;
165
-
166
- constructor(seed: string, min: number, max: number) {
167
- super(seed);
168
-
169
- this.min = min;
170
- this.max = max;
171
- this.value = prng.randomInt(this.seed, min, max);
172
- }
173
-
174
- addGUI(gui: Gui, min: number, max: number, step = 1) {
175
- this.gui = gui.add(this, 'value', min, max, step).name(this.seed);
176
- return this.gui;
177
- }
178
-
179
- getValue() {
180
- this.value = prng.randomInt(this.seed, this.min, this.max);
181
- this.gui?.updateDisplay();
182
- return this.value;
183
- }
184
- }
185
-
186
- export class HexColorController extends BasePRNGController<string> {
187
- value: string;
188
-
189
- constructor(seed: string) {
190
- super(seed);
191
-
192
- this.value = this.getValue();
193
- }
194
-
195
- addGUI(gui: Gui) {
196
- this.gui = gui.addColor(this, 'value').name(this.seed);
197
- return this.gui;
198
- }
199
-
200
- getValue() {
201
- this.value = prng.randomHexColor(this.seed);
202
- this.gui?.updateDisplay();
203
- return this.value;
204
- }
205
- }
206
-
207
- export class ItemController<T = unknown> extends BasePRNGController<T> {
208
- value: T;
209
- items: T[];
210
-
211
- constructor(seed: string, items: T[]) {
212
- super(seed);
213
-
214
- this.items = items;
215
- this.value = this.getValue();
216
- }
217
-
218
- addGUI(gui: Gui) {
219
- this.gui = gui.add(this, 'value', this.items).name(this.seed);
220
- return this.gui;
221
- }
222
-
223
- getValue() {
224
- this.value = prng.randomItem(this.seed, this.items) as T;
225
- this.gui?.updateDisplay();
226
- return this.value;
227
- }
228
- }
229
-
230
- export class ObjectPropertyController<T = unknown> extends BasePRNGController<T> {
231
- value: T;
232
- object: { [key: string]: T };
233
-
234
- constructor(seed: string, object: { [key: string]: T }) {
235
- super(seed);
236
-
237
- this.object = object;
238
- this.value = this.getValue();
239
- }
240
-
241
- addGUI(gui: Gui) {
242
- this.gui = gui.add(this, 'value', this.object).name(this.seed);
243
- return this.gui;
244
- }
245
-
246
- getValue() {
247
- this.value = prng.randomObjectProperty<T>(this.seed, this.object) as T;
248
- this.gui?.updateDisplay();
249
- return this.value;
250
- }
251
- }
252
-
253
- type WeightsItems<T> = Array<{ weight: number; value: T }>;
254
-
255
- export class WeightsController<T = unknown> extends BasePRNGController<T> {
256
- value: T;
257
- items: WeightsItems<T>;
258
- weights: number[];
259
-
260
- constructor(seed: string, items: WeightsItems<T>) {
261
- super(seed);
262
-
263
- this.items = items;
264
- this.weights = this.items.map((item) => item.weight);
265
- this.value = this.getValue();
266
- }
267
-
268
- addGUI(gui: Gui) {
269
- this.gui = gui
270
- .add(
271
- this,
272
- 'value',
273
- this.items.map((item) => item.value)
274
- )
275
- .name(this.seed);
276
- return this.gui;
277
- }
278
-
279
- getValue() {
280
- const index = prng.randomIndex(this.seed, this.weights);
281
- this.value = this.items[index].value;
282
- this.gui?.updateDisplay();
283
- return this.value;
284
- }
285
- }
286
-
287
- // *********************
288
- // Group Controllers
289
- // *********************
290
- export class BooleanGroupController extends BasePRNGGroupController<boolean> {
291
- probability: number;
292
- controllers: BooleanController[] = [];
293
-
294
- constructor(seed: string, probability: number) {
295
- super(seed);
296
-
297
- this.probability = probability;
298
- }
299
-
300
- createController(index: number) {
301
- return new BooleanController(`${this.seed}-${index}`, this.probability);
302
- }
303
- }
304
-
305
- export class SignGroupController extends BasePRNGGroupController<number> {
306
- probability: number;
307
- controllers: SignController[] = [];
308
-
309
- constructor(seed: string, probability: number) {
310
- super(seed);
311
-
312
- this.probability = probability;
313
- }
314
-
315
- createController(index: number) {
316
- return new SignController(`${this.seed}-${index}`, this.probability);
317
- }
318
- }
319
-
320
- export class FloatGroupController extends BasePRNGGroupController<number> {
321
- min: number;
322
- max: number;
323
- controllers: FloatController[] = [];
324
-
325
- constructor(seed: string, min: number, max: number) {
326
- super(seed);
327
-
328
- this.min = min;
329
- this.max = max;
330
- }
331
-
332
- createController(index: number) {
333
- return new FloatController(`${this.seed}-${index}`, this.min, this.max);
334
- }
335
- }
336
-
337
- export class IntGroupController extends BasePRNGGroupController<number> {
338
- min: number;
339
- max: number;
340
- controllers: IntController[] = [];
341
-
342
- constructor(seed: string, min: number, max: number) {
343
- super(seed);
344
-
345
- this.min = min;
346
- this.max = max;
347
- }
348
-
349
- createController(index: number) {
350
- return new IntController(`${this.seed}-${index}`, this.min, this.max);
351
- }
352
- }
353
-
354
- export class HexColorGroupController extends BasePRNGGroupController<string> {
355
- controllers: HexColorController[] = [];
356
-
357
- // constructor(seed: string) {
358
- // super(seed);
359
- // }
360
-
361
- createController(index: number) {
362
- return new HexColorController(`${this.seed}-${index}`);
363
- }
364
- }
365
-
366
- export class ItemGroupController<T = unknown> extends BasePRNGGroupController<T> {
367
- items: T[];
368
- controllers: ItemController<T>[] = [];
369
-
370
- constructor(seed: string, items: T[]) {
371
- super(seed);
372
-
373
- this.items = items;
374
- }
375
-
376
- createController(index: number) {
377
- return new ItemController<T>(`${this.seed}-${index}`, this.items);
378
- }
379
- }
380
-
381
- export class ObjectPropertyGroupController<T = unknown> extends BasePRNGGroupController<T> {
382
- object: { [key: string]: T };
383
- controllers: ObjectPropertyController<T>[] = [];
384
-
385
- constructor(seed: string, object: { [key: string]: T }) {
386
- super(seed);
387
-
388
- this.object = object;
389
- }
390
-
391
- createController(index: number) {
392
- return new ObjectPropertyController<T>(`${this.seed}-${index}`, this.object);
393
- }
394
- }
395
-
396
- export class WeightsGroupController<T = unknown> extends BasePRNGGroupController<T> {
397
- items: WeightsItems<T>;
398
- controllers: WeightsController<T>[] = [];
399
-
400
- constructor(seed: string, items: WeightsItems<T>) {
401
- super(seed);
402
-
403
- this.items = items;
404
- }
405
-
406
- createController(index: number) {
407
- return new WeightsController<T>(`${this.seed}-${index}`, this.items);
408
- }
409
- }