@rpgjs/common 5.0.0-alpha.26 → 5.0.0-alpha.27
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/dist/PerlinNoise.d.ts +167 -0
- package/dist/Player.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +281 -27
- package/dist/index.js.map +1 -1
- package/dist/rooms/Map.d.ts +43 -0
- package/package.json +6 -6
- package/src/PerlinNoise.ts +294 -0
- package/src/Player.ts +2 -1
- package/src/index.ts +1 -0
- package/src/rooms/Map.ts +81 -38
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Perlin Noise 2D Generator
|
|
3
|
+
*
|
|
4
|
+
* A simple, efficient, and performant implementation of 2D Perlin noise.
|
|
5
|
+
* Perlin noise generates smooth, natural-looking random values that are coherent
|
|
6
|
+
* across space, making it ideal for procedural generation and smooth random movements.
|
|
7
|
+
*
|
|
8
|
+
* ## Features
|
|
9
|
+
* - **Deterministic**: Same seed and coordinates always produce the same value
|
|
10
|
+
* - **Smooth**: Values change gradually, creating natural-looking patterns
|
|
11
|
+
* - **Performant**: Optimized for real-time use in movement systems
|
|
12
|
+
* - **Seeded**: Optional seed for reproducible results
|
|
13
|
+
*
|
|
14
|
+
* ## Usage
|
|
15
|
+
* ```ts
|
|
16
|
+
* const noise = new PerlinNoise2D();
|
|
17
|
+
* const value = noise.get(x, y); // Returns value between -1 and 1
|
|
18
|
+
*
|
|
19
|
+
* // With seed for deterministic results
|
|
20
|
+
* const seededNoise = new PerlinNoise2D(12345);
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* // Use in movement system for smooth random directions
|
|
26
|
+
* const noise = new PerlinNoise2D();
|
|
27
|
+
* const time = Date.now() * 0.001;
|
|
28
|
+
* const direction = Math.floor(noise.get(player.x(), player.y(), time) * 4) % 4;
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare class PerlinNoise2D {
|
|
32
|
+
private readonly permutation;
|
|
33
|
+
private readonly p;
|
|
34
|
+
/**
|
|
35
|
+
* Creates a new Perlin noise generator
|
|
36
|
+
*
|
|
37
|
+
* @param seed - Optional seed for deterministic noise generation. If not provided, uses a default seed.
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* const noise = new PerlinNoise2D(12345);
|
|
42
|
+
* const value = noise.get(10, 20);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
constructor(seed?: number);
|
|
46
|
+
/**
|
|
47
|
+
* Generates a permutation table based on seed
|
|
48
|
+
*
|
|
49
|
+
* @param seed - Seed value for permutation generation
|
|
50
|
+
* @returns Array of 256 shuffled values
|
|
51
|
+
*/
|
|
52
|
+
private generatePermutation;
|
|
53
|
+
/**
|
|
54
|
+
* Fade function for smooth interpolation (ease curve)
|
|
55
|
+
*
|
|
56
|
+
* @param t - Value between 0 and 1
|
|
57
|
+
* @returns Smoothed value between 0 and 1
|
|
58
|
+
*/
|
|
59
|
+
private fade;
|
|
60
|
+
/**
|
|
61
|
+
* Linear interpolation
|
|
62
|
+
*
|
|
63
|
+
* @param a - Start value
|
|
64
|
+
* @param b - End value
|
|
65
|
+
* @param t - Interpolation factor (0 to 1)
|
|
66
|
+
* @returns Interpolated value
|
|
67
|
+
*/
|
|
68
|
+
private lerp;
|
|
69
|
+
/**
|
|
70
|
+
* Gradient function - generates a pseudo-random gradient vector
|
|
71
|
+
*
|
|
72
|
+
* @param hash - Hash value from permutation table
|
|
73
|
+
* @param x - X component
|
|
74
|
+
* @param y - Y component
|
|
75
|
+
* @returns Dot product of gradient and position
|
|
76
|
+
*/
|
|
77
|
+
private grad;
|
|
78
|
+
/**
|
|
79
|
+
* Gets the noise value at the specified 2D coordinates
|
|
80
|
+
*
|
|
81
|
+
* Returns a value between approximately -1 and 1, though values near the edges
|
|
82
|
+
* are less common. For practical use, you may want to clamp or normalize the result.
|
|
83
|
+
*
|
|
84
|
+
* @param x - X coordinate
|
|
85
|
+
* @param y - Y coordinate
|
|
86
|
+
* @param scale - Optional scale factor (default: 0.1). Lower values create smoother, larger patterns.
|
|
87
|
+
* @returns Noise value between approximately -1 and 1
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const noise = new PerlinNoise2D();
|
|
92
|
+
* const value = noise.get(10, 20); // Basic usage
|
|
93
|
+
* const scaled = noise.get(10, 20, 0.05); // Smoother pattern
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
get(x: number, y: number, scale?: number): number;
|
|
97
|
+
/**
|
|
98
|
+
* Gets a normalized noise value between 0 and 1
|
|
99
|
+
*
|
|
100
|
+
* Convenience method that normalizes the noise output to a 0-1 range.
|
|
101
|
+
*
|
|
102
|
+
* @param x - X coordinate
|
|
103
|
+
* @param y - Y coordinate
|
|
104
|
+
* @param scale - Optional scale factor (default: 0.1)
|
|
105
|
+
* @returns Noise value between 0 and 1
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* const noise = new PerlinNoise2D();
|
|
110
|
+
* const normalized = noise.getNormalized(10, 20);
|
|
111
|
+
* // Returns value between 0 and 1
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
getNormalized(x: number, y: number, scale?: number): number;
|
|
115
|
+
/**
|
|
116
|
+
* Gets a noise value mapped to a specific range
|
|
117
|
+
*
|
|
118
|
+
* Maps the noise output to a custom min-max range.
|
|
119
|
+
*
|
|
120
|
+
* @param x - X coordinate
|
|
121
|
+
* @param y - Y coordinate
|
|
122
|
+
* @param min - Minimum output value
|
|
123
|
+
* @param max - Maximum output value
|
|
124
|
+
* @param scale - Optional scale factor (default: 0.1)
|
|
125
|
+
* @returns Noise value between min and max
|
|
126
|
+
*
|
|
127
|
+
* @example
|
|
128
|
+
* ```ts
|
|
129
|
+
* const noise = new PerlinNoise2D();
|
|
130
|
+
* const direction = noise.getRange(10, 20, 0, 3); // Returns 0, 1, 2, or 3
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
getRange(x: number, y: number, min: number, max: number, scale?: number): number;
|
|
134
|
+
/**
|
|
135
|
+
* Gets an integer noise value in a specific range (inclusive)
|
|
136
|
+
*
|
|
137
|
+
* Useful for selecting discrete values like array indices or enum values.
|
|
138
|
+
*
|
|
139
|
+
* @param x - X coordinate
|
|
140
|
+
* @param y - Y coordinate
|
|
141
|
+
* @param min - Minimum integer value (inclusive)
|
|
142
|
+
* @param max - Maximum integer value (inclusive)
|
|
143
|
+
* @param scale - Optional scale factor (default: 0.1)
|
|
144
|
+
* @returns Integer noise value between min and max (inclusive)
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* const noise = new PerlinNoise2D();
|
|
149
|
+
* const directionIndex = noise.getInt(10, 20, 0, 3); // Returns 0, 1, 2, or 3
|
|
150
|
+
* ```
|
|
151
|
+
*/
|
|
152
|
+
getInt(x: number, y: number, min: number, max: number, scale?: number): number;
|
|
153
|
+
}
|
|
154
|
+
export declare function getSharedPerlinNoise(seed?: number): PerlinNoise2D;
|
|
155
|
+
/**
|
|
156
|
+
* Resets the shared Perlin noise instance
|
|
157
|
+
*
|
|
158
|
+
* Useful for testing or when you need to change the seed.
|
|
159
|
+
*
|
|
160
|
+
* @param seed - Optional new seed for the instance
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* resetSharedPerlinNoise(12345);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
export declare function resetSharedPerlinNoise(seed?: number): void;
|
package/dist/Player.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2619,6 +2619,9 @@ __decorateClass([
|
|
|
2619
2619
|
__decorateClass([
|
|
2620
2620
|
sync()
|
|
2621
2621
|
], RpgCommonPlayer.prototype, "_gold");
|
|
2622
|
+
__decorateClass([
|
|
2623
|
+
sync()
|
|
2624
|
+
], RpgCommonPlayer.prototype, "animationName");
|
|
2622
2625
|
__decorateClass([
|
|
2623
2626
|
sync()
|
|
2624
2627
|
], RpgCommonPlayer.prototype, "hpSignal");
|
|
@@ -9488,6 +9491,11 @@ class RpgCommonMap {
|
|
|
9488
9491
|
this.tileHeight = 32;
|
|
9489
9492
|
this.physicsAccumulatorMs = 0;
|
|
9490
9493
|
this.physicsSyncDepth = 0;
|
|
9494
|
+
/**
|
|
9495
|
+
* Whether to automatically subscribe to tick$ for physics updates
|
|
9496
|
+
* Set to false in test environments for manual control with nextTick()
|
|
9497
|
+
*/
|
|
9498
|
+
this.autoTickEnabled = true;
|
|
9491
9499
|
/**
|
|
9492
9500
|
* Observable representing the game loop tick
|
|
9493
9501
|
*
|
|
@@ -9721,9 +9729,11 @@ class RpgCommonMap {
|
|
|
9721
9729
|
this.updateCharacterHitbox(event);
|
|
9722
9730
|
}
|
|
9723
9731
|
});
|
|
9724
|
-
|
|
9725
|
-
this.
|
|
9726
|
-
|
|
9732
|
+
if (this.autoTickEnabled) {
|
|
9733
|
+
this.tickSubscription = this.tick$.subscribe(({ delta }) => {
|
|
9734
|
+
this.runFixedTicks(delta);
|
|
9735
|
+
});
|
|
9736
|
+
}
|
|
9727
9737
|
}
|
|
9728
9738
|
async movePlayer(player, direction) {
|
|
9729
9739
|
const currentX = player.x();
|
|
@@ -9745,12 +9755,8 @@ class RpgCommonMap {
|
|
|
9745
9755
|
nextY = currentY + speed;
|
|
9746
9756
|
break;
|
|
9747
9757
|
}
|
|
9748
|
-
|
|
9749
|
-
|
|
9750
|
-
} else if (typeof player.changeDirection === "function") {
|
|
9751
|
-
player.changeDirection(direction);
|
|
9752
|
-
}
|
|
9753
|
-
if (typeof player.autoChangeMap === "function") {
|
|
9758
|
+
player.changeDirection(direction);
|
|
9759
|
+
if (typeof player.autoChangeMap === "function" && !player.isEvent()) {
|
|
9754
9760
|
const mapChanged = await player.autoChangeMap({ x: nextX, y: nextY }, direction);
|
|
9755
9761
|
if (mapChanged) {
|
|
9756
9762
|
this.stopMovement(player);
|
|
@@ -9834,6 +9840,46 @@ class RpgCommonMap {
|
|
|
9834
9840
|
}
|
|
9835
9841
|
return executed;
|
|
9836
9842
|
}
|
|
9843
|
+
/**
|
|
9844
|
+
* Manually trigger a single game tick
|
|
9845
|
+
*
|
|
9846
|
+
* This method allows you to manually advance the game by one tick (16ms at 60fps).
|
|
9847
|
+
* It's primarily useful for testing where you need precise control over when
|
|
9848
|
+
* physics updates occur, rather than relying on the automatic tick$ subscription.
|
|
9849
|
+
*
|
|
9850
|
+
* ## Use Cases
|
|
9851
|
+
*
|
|
9852
|
+
* - **Testing**: Control exactly when physics steps occur in unit tests
|
|
9853
|
+
* - **Manual control**: Step through game state manually for debugging
|
|
9854
|
+
* - **Deterministic testing**: Ensure consistent timing in test scenarios
|
|
9855
|
+
*
|
|
9856
|
+
* ## Important
|
|
9857
|
+
*
|
|
9858
|
+
* This method should NOT be used in production code alongside the automatic `tick$`
|
|
9859
|
+
* subscription, as it will cause double-stepping. Use either:
|
|
9860
|
+
* - Automatic ticks (via `loadPhysic()` which subscribes to `tick$`)
|
|
9861
|
+
* - Manual ticks (via `nextTick()` without `loadPhysic()` subscription)
|
|
9862
|
+
*
|
|
9863
|
+
* @param deltaMs - Optional delta time in milliseconds (default: 16ms for 60fps)
|
|
9864
|
+
* @returns Number of physics ticks executed
|
|
9865
|
+
*
|
|
9866
|
+
* @example
|
|
9867
|
+
* ```ts
|
|
9868
|
+
* // In tests: manually advance game by one tick
|
|
9869
|
+
* map.nextTick(); // Advances by 16ms (one frame at 60fps)
|
|
9870
|
+
*
|
|
9871
|
+
* // With custom delta
|
|
9872
|
+
* map.nextTick(32); // Advances by 32ms (two frames at 60fps)
|
|
9873
|
+
*
|
|
9874
|
+
* // In a test loop
|
|
9875
|
+
* for (let i = 0; i < 60; i++) {
|
|
9876
|
+
* map.nextTick(); // Simulate 1 second of game time
|
|
9877
|
+
* }
|
|
9878
|
+
* ```
|
|
9879
|
+
*/
|
|
9880
|
+
nextTick(deltaMs = 16) {
|
|
9881
|
+
return this.runFixedTicks(deltaMs);
|
|
9882
|
+
}
|
|
9837
9883
|
/**
|
|
9838
9884
|
* Force a single physics tick outside of the normal game loop
|
|
9839
9885
|
*
|
|
@@ -9883,19 +9929,12 @@ class RpgCommonMap {
|
|
|
9883
9929
|
const hitbox = typeof owner.hitbox === "function" ? owner.hitbox() : owner.hitbox;
|
|
9884
9930
|
const width = hitbox?.w ?? 32;
|
|
9885
9931
|
const height = hitbox?.h ?? 32;
|
|
9886
|
-
const topLeftX = this.resolveNumeric(owner.x);
|
|
9887
|
-
const topLeftY = this.resolveNumeric(owner.y);
|
|
9888
|
-
const centerX = topLeftX + width / 2;
|
|
9889
|
-
const centerY = topLeftY + height / 2;
|
|
9890
9932
|
const radius = Math.max(width, height) / 2;
|
|
9891
|
-
const speedValue = typeof owner.speed === "function" ? owner.speed() : typeof owner.speed === "number" ? owner.speed : void 0;
|
|
9892
9933
|
this.addCharacter({
|
|
9893
9934
|
owner,
|
|
9894
|
-
x: centerX,
|
|
9895
|
-
y: centerY,
|
|
9896
9935
|
radius,
|
|
9897
9936
|
kind,
|
|
9898
|
-
maxSpeed:
|
|
9937
|
+
maxSpeed: owner.speed(),
|
|
9899
9938
|
collidesWithCharacters: !this.shouldDisableCharacterCollisions(owner),
|
|
9900
9939
|
isStatic: options?.isStatic,
|
|
9901
9940
|
mass: options?.mass
|
|
@@ -10267,11 +10306,24 @@ class RpgCommonMap {
|
|
|
10267
10306
|
owner2.changeDirection(cardinalDirection);
|
|
10268
10307
|
});
|
|
10269
10308
|
entity.onMovementChange(({ isMoving, intensity }) => {
|
|
10309
|
+
const owner2 = entity.owner;
|
|
10310
|
+
if (!owner2) return;
|
|
10270
10311
|
const LOW_INTENSITY_THRESHOLD = 10;
|
|
10312
|
+
const hasSetAnimation = typeof owner2.setAnimation === "function";
|
|
10313
|
+
const animationNameSignal = owner2.animationName;
|
|
10314
|
+
const ownerHasAnimationName = animationNameSignal && typeof animationNameSignal === "object" && typeof animationNameSignal.set === "function";
|
|
10271
10315
|
if (isMoving && intensity > LOW_INTENSITY_THRESHOLD) {
|
|
10272
|
-
|
|
10316
|
+
if (hasSetAnimation) {
|
|
10317
|
+
owner2.setAnimation("walk");
|
|
10318
|
+
} else if (ownerHasAnimationName) {
|
|
10319
|
+
animationNameSignal.set("walk");
|
|
10320
|
+
}
|
|
10273
10321
|
} else if (!isMoving) {
|
|
10274
|
-
|
|
10322
|
+
if (hasSetAnimation) {
|
|
10323
|
+
owner2.setAnimation("stand");
|
|
10324
|
+
} else if (ownerHasAnimationName) {
|
|
10325
|
+
animationNameSignal.set("stand");
|
|
10326
|
+
}
|
|
10275
10327
|
}
|
|
10276
10328
|
});
|
|
10277
10329
|
const entityWidth = width;
|
|
@@ -10346,10 +10398,7 @@ class RpgCommonMap {
|
|
|
10346
10398
|
moveBody(player, direction) {
|
|
10347
10399
|
const entity = this.physic.getEntityByUUID(player.id);
|
|
10348
10400
|
if (!entity) return false;
|
|
10349
|
-
const speedValue =
|
|
10350
|
-
if (typeof player.setIntendedDirection === "function") {
|
|
10351
|
-
player.setIntendedDirection(direction);
|
|
10352
|
-
}
|
|
10401
|
+
const speedValue = player.speed();
|
|
10353
10402
|
let vx = 0, vy = 0;
|
|
10354
10403
|
switch (direction) {
|
|
10355
10404
|
case Direction.Left:
|
|
@@ -10401,9 +10450,6 @@ class RpgCommonMap {
|
|
|
10401
10450
|
const entity = this.physic.getEntityByUUID(player.id);
|
|
10402
10451
|
if (!entity) return false;
|
|
10403
10452
|
this.moveManager.stopMovement(player.id);
|
|
10404
|
-
if (typeof player.setIntendedDirection === "function") {
|
|
10405
|
-
player.setIntendedDirection(null);
|
|
10406
|
-
}
|
|
10407
10453
|
player.pendingInputs = [];
|
|
10408
10454
|
return true;
|
|
10409
10455
|
}
|
|
@@ -11173,5 +11219,213 @@ var PrebuiltGui = /* @__PURE__ */ ((PrebuiltGui2) => {
|
|
|
11173
11219
|
return PrebuiltGui2;
|
|
11174
11220
|
})(PrebuiltGui || {});
|
|
11175
11221
|
|
|
11176
|
-
|
|
11222
|
+
class PerlinNoise2D {
|
|
11223
|
+
/**
|
|
11224
|
+
* Creates a new Perlin noise generator
|
|
11225
|
+
*
|
|
11226
|
+
* @param seed - Optional seed for deterministic noise generation. If not provided, uses a default seed.
|
|
11227
|
+
*
|
|
11228
|
+
* @example
|
|
11229
|
+
* ```ts
|
|
11230
|
+
* const noise = new PerlinNoise2D(12345);
|
|
11231
|
+
* const value = noise.get(10, 20);
|
|
11232
|
+
* ```
|
|
11233
|
+
*/
|
|
11234
|
+
constructor(seed = 0) {
|
|
11235
|
+
this.permutation = this.generatePermutation(seed);
|
|
11236
|
+
this.p = [...this.permutation, ...this.permutation];
|
|
11237
|
+
}
|
|
11238
|
+
/**
|
|
11239
|
+
* Generates a permutation table based on seed
|
|
11240
|
+
*
|
|
11241
|
+
* @param seed - Seed value for permutation generation
|
|
11242
|
+
* @returns Array of 256 shuffled values
|
|
11243
|
+
*/
|
|
11244
|
+
generatePermutation(seed) {
|
|
11245
|
+
const p = [];
|
|
11246
|
+
for (let i = 0; i < 256; i++) {
|
|
11247
|
+
p[i] = i;
|
|
11248
|
+
}
|
|
11249
|
+
let state = seed;
|
|
11250
|
+
const lcg = () => {
|
|
11251
|
+
state = state * 1103515245 + 12345 & 2147483647;
|
|
11252
|
+
return state;
|
|
11253
|
+
};
|
|
11254
|
+
for (let i = 255; i > 0; i--) {
|
|
11255
|
+
const j = lcg() % (i + 1);
|
|
11256
|
+
[p[i], p[j]] = [p[j], p[i]];
|
|
11257
|
+
}
|
|
11258
|
+
return p;
|
|
11259
|
+
}
|
|
11260
|
+
/**
|
|
11261
|
+
* Fade function for smooth interpolation (ease curve)
|
|
11262
|
+
*
|
|
11263
|
+
* @param t - Value between 0 and 1
|
|
11264
|
+
* @returns Smoothed value between 0 and 1
|
|
11265
|
+
*/
|
|
11266
|
+
fade(t) {
|
|
11267
|
+
return t * t * t * (t * (t * 6 - 15) + 10);
|
|
11268
|
+
}
|
|
11269
|
+
/**
|
|
11270
|
+
* Linear interpolation
|
|
11271
|
+
*
|
|
11272
|
+
* @param a - Start value
|
|
11273
|
+
* @param b - End value
|
|
11274
|
+
* @param t - Interpolation factor (0 to 1)
|
|
11275
|
+
* @returns Interpolated value
|
|
11276
|
+
*/
|
|
11277
|
+
lerp(a, b, t) {
|
|
11278
|
+
return a + t * (b - a);
|
|
11279
|
+
}
|
|
11280
|
+
/**
|
|
11281
|
+
* Gradient function - generates a pseudo-random gradient vector
|
|
11282
|
+
*
|
|
11283
|
+
* @param hash - Hash value from permutation table
|
|
11284
|
+
* @param x - X component
|
|
11285
|
+
* @param y - Y component
|
|
11286
|
+
* @returns Dot product of gradient and position
|
|
11287
|
+
*/
|
|
11288
|
+
grad(hash, x, y) {
|
|
11289
|
+
const h = hash & 3;
|
|
11290
|
+
switch (h) {
|
|
11291
|
+
case 0:
|
|
11292
|
+
return x + y;
|
|
11293
|
+
// (1, 1)
|
|
11294
|
+
case 1:
|
|
11295
|
+
return -x + y;
|
|
11296
|
+
// (-1, 1)
|
|
11297
|
+
case 2:
|
|
11298
|
+
return x - y;
|
|
11299
|
+
// (1, -1)
|
|
11300
|
+
case 3:
|
|
11301
|
+
return -x - y;
|
|
11302
|
+
// (-1, -1)
|
|
11303
|
+
default:
|
|
11304
|
+
return 0;
|
|
11305
|
+
}
|
|
11306
|
+
}
|
|
11307
|
+
/**
|
|
11308
|
+
* Gets the noise value at the specified 2D coordinates
|
|
11309
|
+
*
|
|
11310
|
+
* Returns a value between approximately -1 and 1, though values near the edges
|
|
11311
|
+
* are less common. For practical use, you may want to clamp or normalize the result.
|
|
11312
|
+
*
|
|
11313
|
+
* @param x - X coordinate
|
|
11314
|
+
* @param y - Y coordinate
|
|
11315
|
+
* @param scale - Optional scale factor (default: 0.1). Lower values create smoother, larger patterns.
|
|
11316
|
+
* @returns Noise value between approximately -1 and 1
|
|
11317
|
+
*
|
|
11318
|
+
* @example
|
|
11319
|
+
* ```ts
|
|
11320
|
+
* const noise = new PerlinNoise2D();
|
|
11321
|
+
* const value = noise.get(10, 20); // Basic usage
|
|
11322
|
+
* const scaled = noise.get(10, 20, 0.05); // Smoother pattern
|
|
11323
|
+
* ```
|
|
11324
|
+
*/
|
|
11325
|
+
get(x, y, scale = 0.1) {
|
|
11326
|
+
x *= scale;
|
|
11327
|
+
y *= scale;
|
|
11328
|
+
const X = Math.floor(x) & 255;
|
|
11329
|
+
const Y = Math.floor(y) & 255;
|
|
11330
|
+
x -= Math.floor(x);
|
|
11331
|
+
y -= Math.floor(y);
|
|
11332
|
+
const u = this.fade(x);
|
|
11333
|
+
const v = this.fade(y);
|
|
11334
|
+
const A = this.p[X] + Y;
|
|
11335
|
+
const AA = this.p[A];
|
|
11336
|
+
const AB = this.p[A + 1];
|
|
11337
|
+
const B = this.p[X + 1] + Y;
|
|
11338
|
+
const BA = this.p[B];
|
|
11339
|
+
const BB = this.p[B + 1];
|
|
11340
|
+
return this.lerp(
|
|
11341
|
+
this.lerp(
|
|
11342
|
+
this.grad(this.p[AA], x, y),
|
|
11343
|
+
this.grad(this.p[BA], x - 1, y),
|
|
11344
|
+
u
|
|
11345
|
+
),
|
|
11346
|
+
this.lerp(
|
|
11347
|
+
this.grad(this.p[AB], x, y - 1),
|
|
11348
|
+
this.grad(this.p[BB], x - 1, y - 1),
|
|
11349
|
+
u
|
|
11350
|
+
),
|
|
11351
|
+
v
|
|
11352
|
+
);
|
|
11353
|
+
}
|
|
11354
|
+
/**
|
|
11355
|
+
* Gets a normalized noise value between 0 and 1
|
|
11356
|
+
*
|
|
11357
|
+
* Convenience method that normalizes the noise output to a 0-1 range.
|
|
11358
|
+
*
|
|
11359
|
+
* @param x - X coordinate
|
|
11360
|
+
* @param y - Y coordinate
|
|
11361
|
+
* @param scale - Optional scale factor (default: 0.1)
|
|
11362
|
+
* @returns Noise value between 0 and 1
|
|
11363
|
+
*
|
|
11364
|
+
* @example
|
|
11365
|
+
* ```ts
|
|
11366
|
+
* const noise = new PerlinNoise2D();
|
|
11367
|
+
* const normalized = noise.getNormalized(10, 20);
|
|
11368
|
+
* // Returns value between 0 and 1
|
|
11369
|
+
* ```
|
|
11370
|
+
*/
|
|
11371
|
+
getNormalized(x, y, scale = 0.1) {
|
|
11372
|
+
return (this.get(x, y, scale) + 1) * 0.5;
|
|
11373
|
+
}
|
|
11374
|
+
/**
|
|
11375
|
+
* Gets a noise value mapped to a specific range
|
|
11376
|
+
*
|
|
11377
|
+
* Maps the noise output to a custom min-max range.
|
|
11378
|
+
*
|
|
11379
|
+
* @param x - X coordinate
|
|
11380
|
+
* @param y - Y coordinate
|
|
11381
|
+
* @param min - Minimum output value
|
|
11382
|
+
* @param max - Maximum output value
|
|
11383
|
+
* @param scale - Optional scale factor (default: 0.1)
|
|
11384
|
+
* @returns Noise value between min and max
|
|
11385
|
+
*
|
|
11386
|
+
* @example
|
|
11387
|
+
* ```ts
|
|
11388
|
+
* const noise = new PerlinNoise2D();
|
|
11389
|
+
* const direction = noise.getRange(10, 20, 0, 3); // Returns 0, 1, 2, or 3
|
|
11390
|
+
* ```
|
|
11391
|
+
*/
|
|
11392
|
+
getRange(x, y, min, max, scale = 0.1) {
|
|
11393
|
+
const normalized = this.getNormalized(x, y, scale);
|
|
11394
|
+
return min + normalized * (max - min);
|
|
11395
|
+
}
|
|
11396
|
+
/**
|
|
11397
|
+
* Gets an integer noise value in a specific range (inclusive)
|
|
11398
|
+
*
|
|
11399
|
+
* Useful for selecting discrete values like array indices or enum values.
|
|
11400
|
+
*
|
|
11401
|
+
* @param x - X coordinate
|
|
11402
|
+
* @param y - Y coordinate
|
|
11403
|
+
* @param min - Minimum integer value (inclusive)
|
|
11404
|
+
* @param max - Maximum integer value (inclusive)
|
|
11405
|
+
* @param scale - Optional scale factor (default: 0.1)
|
|
11406
|
+
* @returns Integer noise value between min and max (inclusive)
|
|
11407
|
+
*
|
|
11408
|
+
* @example
|
|
11409
|
+
* ```ts
|
|
11410
|
+
* const noise = new PerlinNoise2D();
|
|
11411
|
+
* const directionIndex = noise.getInt(10, 20, 0, 3); // Returns 0, 1, 2, or 3
|
|
11412
|
+
* ```
|
|
11413
|
+
*/
|
|
11414
|
+
getInt(x, y, min, max, scale = 0.1) {
|
|
11415
|
+
const value = this.getRange(x, y, min, max + 1, scale);
|
|
11416
|
+
return Math.floor(value);
|
|
11417
|
+
}
|
|
11418
|
+
}
|
|
11419
|
+
let sharedInstance = null;
|
|
11420
|
+
function getSharedPerlinNoise(seed) {
|
|
11421
|
+
if (!sharedInstance) {
|
|
11422
|
+
sharedInstance = new PerlinNoise2D(seed);
|
|
11423
|
+
}
|
|
11424
|
+
return sharedInstance;
|
|
11425
|
+
}
|
|
11426
|
+
function resetSharedPerlinNoise(seed) {
|
|
11427
|
+
sharedInstance = new PerlinNoise2D(seed);
|
|
11428
|
+
}
|
|
11429
|
+
|
|
11430
|
+
export { AABB, AABBCollider, AnchorConstraint, Animation, BVH, CapsuleCollider, CircleCollider, CollisionResolver, CompositeMovement, Control, Dash, DeterministicInputBuffer, Direction, DistanceConstraint, EPSILON$1 as EPSILON, Entity, EntityMovementBody, EntityState, EventSystem, Hooks, IceMovement, IntegrationMethod, Integrator, Item, Knockback, LinearMove, LinearRepulsion, Matrix2, ModulesToken, MovementManager$1 as MovementManager, ObjectPool, Oscillate, PathFollow, PerlinNoise2D, PhysicsEngine, PolygonCollider, PrebuiltGui, PredictionController, ProjectileMovement, ProjectileType, Quadtree, Ray, Region, RegionManager, RpgCommonMap, RpgCommonPlayer, RpgModule, RpgShape, SeekAvoid, Side, SpatialHash, SpringConstraint, UpdateMapService, UpdateMapToken, Vector2, World, WorldMapsManager, ZoneManager, angleBetween, angularDistance, applyAttraction, applyConstantForce, applyDirectionalForce, applyExplosion, applyMixins, applyRepulsion, applySpring, approximatelyEqual, arrayEquals, arrayFlat, arrayUniq, assignPolygonCollider, basename, camelToKebab, capitalize, clamp, closestPointOnAABB, combineMixins, createCollider, createConstructor, createModule, defineModule, degToRad, distance, distanceSquared, elementToPositionAbsolute, extractId, findCollisions, findModules, fps2ms, generateDeterministicUUID, generateUID, generateUUID, getSharedPerlinNoise, hexaToNumber, intersection, isArray, isBrowser, isClass, isFunction, isInstanceOf, isObject, isPromise, isString, lerp, mergeObjectWithMethods, migrateEntities, migrateEntity, normalizeAngle, pointInCircle, preciseNow, projectPointOnLineSegment, provideModules, radToDeg, random, raycast, resetSharedPerlinNoise, round, set, sharedArrayBuffer, sweepEntities, testCollision, toRadians };
|
|
11177
11431
|
//# sourceMappingURL=index.js.map
|