com.wallstop-studios.unity-helpers 2.0.0-rc07 → 2.0.0-rc09

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.
@@ -0,0 +1,369 @@
1
+ namespace UnityHelpers.Core.Random
2
+ {
3
+ using System;
4
+ using Extension;
5
+ using UnityEngine;
6
+
7
+ public sealed class PerlinNoise
8
+ {
9
+ // Permutation array. This is a standard permutation of numbers from 0 to 255.
10
+ private static readonly int[] DefaultPermutations =
11
+ {
12
+ 151,
13
+ 160,
14
+ 137,
15
+ 91,
16
+ 90,
17
+ 15,
18
+ 131,
19
+ 13,
20
+ 201,
21
+ 95,
22
+ 96,
23
+ 53,
24
+ 194,
25
+ 233,
26
+ 7,
27
+ 225,
28
+ 140,
29
+ 36,
30
+ 103,
31
+ 30,
32
+ 69,
33
+ 142,
34
+ 8,
35
+ 99,
36
+ 37,
37
+ 240,
38
+ 21,
39
+ 10,
40
+ 23,
41
+ 190,
42
+ 6,
43
+ 148,
44
+ 247,
45
+ 120,
46
+ 234,
47
+ 75,
48
+ 0,
49
+ 26,
50
+ 197,
51
+ 62,
52
+ 94,
53
+ 252,
54
+ 219,
55
+ 203,
56
+ 117,
57
+ 35,
58
+ 11,
59
+ 32,
60
+ 57,
61
+ 177,
62
+ 33,
63
+ 88,
64
+ 237,
65
+ 149,
66
+ 56,
67
+ 87,
68
+ 174,
69
+ 20,
70
+ 125,
71
+ 136,
72
+ 171,
73
+ 168,
74
+ 68,
75
+ 175,
76
+ 74,
77
+ 165,
78
+ 71,
79
+ 134,
80
+ 139,
81
+ 48,
82
+ 27,
83
+ 166,
84
+ 77,
85
+ 146,
86
+ 158,
87
+ 231,
88
+ 83,
89
+ 111,
90
+ 229,
91
+ 122,
92
+ 60,
93
+ 211,
94
+ 133,
95
+ 230,
96
+ 220,
97
+ 105,
98
+ 92,
99
+ 41,
100
+ 55,
101
+ 46,
102
+ 245,
103
+ 40,
104
+ 244,
105
+ 102,
106
+ 143,
107
+ 54,
108
+ 65,
109
+ 25,
110
+ 63,
111
+ 161,
112
+ 1,
113
+ 216,
114
+ 80,
115
+ 73,
116
+ 209,
117
+ 76,
118
+ 132,
119
+ 187,
120
+ 208,
121
+ 89,
122
+ 18,
123
+ 169,
124
+ 200,
125
+ 196,
126
+ 135,
127
+ 130,
128
+ 116,
129
+ 188,
130
+ 159,
131
+ 86,
132
+ 164,
133
+ 100,
134
+ 109,
135
+ 198,
136
+ 173,
137
+ 186,
138
+ 3,
139
+ 64,
140
+ 52,
141
+ 217,
142
+ 226,
143
+ 250,
144
+ 124,
145
+ 123,
146
+ 5,
147
+ 202,
148
+ 38,
149
+ 147,
150
+ 118,
151
+ 126,
152
+ 255,
153
+ 82,
154
+ 85,
155
+ 212,
156
+ 207,
157
+ 206,
158
+ 59,
159
+ 227,
160
+ 47,
161
+ 16,
162
+ 58,
163
+ 17,
164
+ 182,
165
+ 189,
166
+ 28,
167
+ 42,
168
+ 223,
169
+ 183,
170
+ 170,
171
+ 213,
172
+ 119,
173
+ 248,
174
+ 152,
175
+ 2,
176
+ 44,
177
+ 154,
178
+ 163,
179
+ 70,
180
+ 221,
181
+ 153,
182
+ 101,
183
+ 155,
184
+ 167,
185
+ 43,
186
+ 172,
187
+ 9,
188
+ 129,
189
+ 22,
190
+ 39,
191
+ 253,
192
+ 19,
193
+ 98,
194
+ 108,
195
+ 110,
196
+ 79,
197
+ 113,
198
+ 224,
199
+ 232,
200
+ 178,
201
+ 185,
202
+ 112,
203
+ 104,
204
+ 218,
205
+ 246,
206
+ 97,
207
+ 228,
208
+ 251,
209
+ 34,
210
+ 242,
211
+ 193,
212
+ 238,
213
+ 210,
214
+ 144,
215
+ 12,
216
+ 191,
217
+ 179,
218
+ 162,
219
+ 241,
220
+ 81,
221
+ 51,
222
+ 145,
223
+ 235,
224
+ 249,
225
+ 14,
226
+ 239,
227
+ 107,
228
+ 49,
229
+ 192,
230
+ 214,
231
+ 31,
232
+ 181,
233
+ 199,
234
+ 106,
235
+ 157,
236
+ 184,
237
+ 84,
238
+ 204,
239
+ 176,
240
+ 115,
241
+ 121,
242
+ 50,
243
+ 45,
244
+ 127,
245
+ 4,
246
+ 150,
247
+ 254,
248
+ 138,
249
+ 236,
250
+ 205,
251
+ 93,
252
+ 222,
253
+ 114,
254
+ 67,
255
+ 29,
256
+ 24,
257
+ 72,
258
+ 243,
259
+ 141,
260
+ 128,
261
+ 195,
262
+ 78,
263
+ 66,
264
+ 215,
265
+ 61,
266
+ 156,
267
+ 180,
268
+ };
269
+
270
+ public static readonly PerlinNoise Instance = new();
271
+
272
+ private readonly int[] _permutations = new int[DefaultPermutations.Length];
273
+
274
+ // Doubled permutation to avoid overflow
275
+ private readonly int[] _doubledPermutations = new int[DefaultPermutations.Length * 2];
276
+
277
+ public PerlinNoise()
278
+ : this(null) { }
279
+
280
+ // Static constructor to initialize the doubled permutation array
281
+ public PerlinNoise(IRandom random)
282
+ {
283
+ Array.Copy(DefaultPermutations, 0, _permutations, 0, DefaultPermutations.Length);
284
+ if (random != null)
285
+ {
286
+ _permutations.Shuffle(random);
287
+ }
288
+ for (int i = 0; i < _doubledPermutations.Length; ++i)
289
+ {
290
+ _doubledPermutations[i] = _permutations[i % _permutations.Length];
291
+ }
292
+ }
293
+
294
+ // Fade function as defined by Ken Perlin. This eases coordinate values
295
+ // so that they will "ease" towards integral values. This ends up smoothing the final output.
296
+ public static float Fade(float t)
297
+ {
298
+ return t * t * t * (t * (t * 6 - 15) + 10);
299
+ }
300
+
301
+ // Linear interpolation function
302
+ public static float Lerp(float t, float a, float b)
303
+ {
304
+ return a + t * (b - a);
305
+ }
306
+
307
+ // Gradient function calculates the dot product between a pseudorandom gradient vector and the vector from the input coordinate to the grid coordinate
308
+ public static float Grad(int hash, float x, float y)
309
+ {
310
+ int h = hash & 7; // Convert low 3 bits of hash code
311
+ float u = h < 4 ? x : y; // If h < 4, use x, else use y
312
+ float v = h < 4 ? y : x; // If h < 4, use y, else use x
313
+ return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v);
314
+ }
315
+
316
+ // The Perlin noise function
317
+ public float Noise(float x, float y)
318
+ {
319
+ // Find unit grid cell containing point
320
+ int clampedX = (int)Mathf.Floor(x) & 255;
321
+ int clampedY = (int)Mathf.Floor(y) & 255;
322
+
323
+ // Get relative xy coordinates inside the cell
324
+ x -= Mathf.Floor(x);
325
+ y -= Mathf.Floor(y);
326
+
327
+ // Compute fade curves for x and y
328
+ float u = Fade(x);
329
+ float v = Fade(y);
330
+
331
+ // Hash coordinates of the square's corners
332
+ int aa = _doubledPermutations[_doubledPermutations[clampedX] + clampedY];
333
+ int ab = _doubledPermutations[_doubledPermutations[clampedX] + clampedY + 1];
334
+ int ba = _doubledPermutations[_doubledPermutations[clampedX + 1] + clampedY];
335
+ int bb = _doubledPermutations[_doubledPermutations[clampedX + 1] + clampedY + 1];
336
+
337
+ // Add blended results from the corners
338
+ float res = Lerp(
339
+ v,
340
+ Lerp(u, Grad(aa, x, y), Grad(ba, x - 1, y)),
341
+ Lerp(u, Grad(ab, x, y - 1), Grad(bb, x - 1, y - 1))
342
+ );
343
+
344
+ // Optional: Scale result to [0,1]
345
+ return (res + 1.0f) / 2.0f;
346
+ }
347
+
348
+ // Optional: Generate noise with multiple octaves for more complexity
349
+ public float OctaveNoise(float x, float y, int octaves, float persistence)
350
+ {
351
+ float total = 0;
352
+ float frequency = 1;
353
+ float amplitude = 1;
354
+ float maxValue = 0; // Used for normalizing result to [0,1]
355
+
356
+ for (int i = 0; i < octaves; ++i)
357
+ {
358
+ total += Noise(x * frequency, y * frequency) * amplitude;
359
+
360
+ maxValue += amplitude;
361
+
362
+ amplitude *= persistence;
363
+ frequency *= 2;
364
+ }
365
+
366
+ return total / maxValue;
367
+ }
368
+ }
369
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 9798fa1ddadd48958dacd90cfa254962
3
+ timeCreated: 1732743633
@@ -1,15 +1,18 @@
1
1
  namespace UnityHelpers.Utils
2
2
  {
3
3
  using System;
4
+ using Core.Extension;
5
+ using Core.Helper;
4
6
  using UnityEngine;
5
7
 
6
8
  [DisallowMultipleComponent]
7
- public abstract class RuntimeSingleton<T> : MonoBehaviour where T : RuntimeSingleton<T>
9
+ public abstract class RuntimeSingleton<T> : MonoBehaviour
10
+ where T : RuntimeSingleton<T>
8
11
  {
9
12
  public static bool HasInstance => _instance != null;
10
-
13
+
11
14
  protected static T _instance;
12
-
15
+
13
16
  protected virtual bool Preserve => true;
14
17
 
15
18
  public static T Instance
@@ -31,5 +34,24 @@
31
34
  return _instance;
32
35
  }
33
36
  }
37
+
38
+ protected virtual void Awake()
39
+ {
40
+ if (_instance == null)
41
+ {
42
+ _instance = this as T;
43
+ }
44
+ }
45
+
46
+ protected virtual void Start()
47
+ {
48
+ if (_instance != null && _instance != this)
49
+ {
50
+ this.LogError(
51
+ $"Double singleton detected, {_instance.name} conflicts with {name}."
52
+ );
53
+ gameObject.Destroy();
54
+ }
55
+ }
34
56
  }
35
- }
57
+ }
@@ -0,0 +1,27 @@
1
+ namespace UnityHelpers.Tests.Extensions
2
+ {
3
+ using System.Collections.Generic;
4
+ using Core.Extension;
5
+ using Core.Random;
6
+ using NUnit.Framework;
7
+ using UnityEngine;
8
+
9
+ public sealed class RandomExtensionTests
10
+ {
11
+ [Test]
12
+ public void NextVector2InRange()
13
+ {
14
+ HashSet<float> seenAngles = new();
15
+ for (int i = 0; i < 1_000; ++i)
16
+ {
17
+ Vector2 vector = PRNG.Instance.NextVector2(-100, 100);
18
+ float range = PRNG.Instance.NextFloat(100f);
19
+ Vector2 inRange = PRNG.Instance.NextVector2InRange(range, vector);
20
+ Assert.LessOrEqual(Vector2.Distance(vector, inRange), range);
21
+ seenAngles.Add(Vector2.SignedAngle(vector, inRange));
22
+ }
23
+
24
+ Assert.LessOrEqual(3, seenAngles.Count);
25
+ }
26
+ }
27
+ }
@@ -0,0 +1,3 @@
1
+ fileFormatVersion: 2
2
+ guid: 610d56fdeadc4caf9d50c75574db4204
3
+ timeCreated: 1732732306