@rpgjs/common 5.0.0-alpha.4 → 5.0.0-alpha.40

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 (62) hide show
  1. package/dist/PerlinNoise.d.ts +167 -0
  2. package/dist/Player.d.ts +92 -89
  3. package/dist/PrebuiltGui.d.ts +3 -1
  4. package/dist/Presets.d.ts +9 -0
  5. package/dist/Shape.d.ts +100 -0
  6. package/dist/Utils.d.ts +1 -0
  7. package/dist/database/Item.d.ts +17 -1
  8. package/dist/database/Skill.d.ts +21 -0
  9. package/dist/database/index.d.ts +1 -0
  10. package/dist/index.d.ts +7 -2
  11. package/dist/index.js +8869 -13605
  12. package/dist/index.js.map +1 -1
  13. package/dist/modules.d.ts +17 -0
  14. package/dist/movement/MovementManager.d.ts +16 -72
  15. package/dist/movement/MovementStrategy.d.ts +1 -39
  16. package/dist/movement/index.d.ts +3 -12
  17. package/dist/rooms/Map.d.ts +498 -21
  18. package/dist/rooms/WorldMaps.d.ts +163 -0
  19. package/dist/services/save.d.ts +12 -0
  20. package/dist/weather.d.ts +27 -0
  21. package/package.json +8 -9
  22. package/src/PerlinNoise.ts +294 -0
  23. package/src/Player.ts +126 -149
  24. package/src/PrebuiltGui.ts +4 -2
  25. package/src/Presets.ts +9 -0
  26. package/src/Shape.ts +149 -0
  27. package/src/Utils.ts +4 -0
  28. package/src/database/Item.ts +27 -6
  29. package/src/database/Skill.ts +35 -0
  30. package/src/database/index.ts +2 -1
  31. package/src/index.ts +8 -3
  32. package/src/modules.ts +29 -1
  33. package/src/movement/MovementManager.ts +38 -119
  34. package/src/movement/MovementStrategy.ts +4 -42
  35. package/src/movement/index.ts +14 -15
  36. package/src/rooms/Map.ts +1505 -85
  37. package/src/rooms/WorldMaps.ts +269 -0
  38. package/src/services/save.ts +14 -0
  39. package/src/weather.ts +29 -0
  40. package/dist/Physic.d.ts +0 -619
  41. package/dist/movement/strategies/CompositeMovement.d.ts +0 -76
  42. package/dist/movement/strategies/Dash.d.ts +0 -52
  43. package/dist/movement/strategies/IceMovement.d.ts +0 -87
  44. package/dist/movement/strategies/Knockback.d.ts +0 -50
  45. package/dist/movement/strategies/LinearMove.d.ts +0 -43
  46. package/dist/movement/strategies/LinearRepulsion.d.ts +0 -55
  47. package/dist/movement/strategies/Oscillate.d.ts +0 -60
  48. package/dist/movement/strategies/PathFollow.d.ts +0 -78
  49. package/dist/movement/strategies/ProjectileMovement.d.ts +0 -138
  50. package/dist/movement/strategies/SeekAvoid.d.ts +0 -27
  51. package/src/Physic.ts +0 -1644
  52. package/src/movement/strategies/CompositeMovement.ts +0 -173
  53. package/src/movement/strategies/Dash.ts +0 -82
  54. package/src/movement/strategies/IceMovement.ts +0 -158
  55. package/src/movement/strategies/Knockback.ts +0 -81
  56. package/src/movement/strategies/LinearMove.ts +0 -58
  57. package/src/movement/strategies/LinearRepulsion.ts +0 -128
  58. package/src/movement/strategies/Oscillate.ts +0 -144
  59. package/src/movement/strategies/PathFollow.ts +0 -156
  60. package/src/movement/strategies/ProjectileMovement.ts +0 -322
  61. package/src/movement/strategies/SeekAvoid.ts +0 -123
  62. package/tests/physic.spec.ts +0 -454
@@ -0,0 +1,163 @@
1
+ /**
2
+ * Interface for world map information
3
+ */
4
+ export interface WorldMapInfo {
5
+ id: string;
6
+ x: number;
7
+ y: number;
8
+ width: number;
9
+ height: number;
10
+ worldX: number;
11
+ worldY: number;
12
+ widthPx: number;
13
+ heightPx: number;
14
+ tileWidth: number;
15
+ tileHeight: number;
16
+ }
17
+ /**
18
+ * Configuration for a world map
19
+ */
20
+ export interface WorldMapConfig {
21
+ id: string;
22
+ worldX: number;
23
+ worldY: number;
24
+ width: number;
25
+ height: number;
26
+ tileWidth?: number;
27
+ tileHeight?: number;
28
+ }
29
+ /**
30
+ * World Maps Manager
31
+ *
32
+ * Manages a collection of interconnected maps and their spatial relationships
33
+ */
34
+ export declare class WorldMapsManager {
35
+ private maps;
36
+ private spatialIndex;
37
+ /**
38
+ * Configure the world maps
39
+ *
40
+ * @param configs - Array of map configurations
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const worldMaps = new WorldMapsManager();
45
+ * worldMaps.configure([
46
+ * { id: "town", worldX: 0, worldY: 0, width: 1024, height: 768 },
47
+ * { id: "forest", worldX: 1024, worldY: 0, width: 1024, height: 768 }
48
+ * ]);
49
+ * ```
50
+ */
51
+ configure(configs: WorldMapConfig[]): void;
52
+ /**
53
+ * Remove a map from the world by its id
54
+ *
55
+ * Deletes the map from the internal registry and spatial index.
56
+ *
57
+ * @param mapId - Map identifier
58
+ * @returns True if a map was removed, false otherwise
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const removed = world.removeMap("forest");
63
+ * ```
64
+ */
65
+ removeMap(mapId: string): boolean;
66
+ /**
67
+ * Find adjacent maps based on various search strategies
68
+ *
69
+ * Supports three search modes:
70
+ * - PositionBox: collect maps intersecting the given box
71
+ * - Direction: collect maps adjacent in the given direction
72
+ * - Point: collect the map containing the given world point
73
+ *
74
+ * The given `map` can be any object exposing `worldX`, `worldY`, `width`, `height` properties
75
+ * (e.g. your `RpgMap` instance or a `WorldMapInfo`).
76
+ *
77
+ * @param map - The source map
78
+ * @param search - Search strategy (box, direction or point)
79
+ * @returns Array of matching adjacent map infos
80
+ *
81
+ * @example
82
+ * ```ts
83
+ * // Point
84
+ * world.getAdjacentMaps(currentMap, { x: 1024, y: 0 });
85
+ *
86
+ * // Direction
87
+ * world.getAdjacentMaps(currentMap, Direction.Up);
88
+ *
89
+ * // Box
90
+ * world.getAdjacentMaps(currentMap, { minX: 0, minY: 0, maxX: 2048, maxY: 1024 });
91
+ * ```
92
+ */
93
+ getAdjacentMaps(map: {
94
+ worldX: number;
95
+ worldY: number;
96
+ widthPx: number;
97
+ heightPx: number;
98
+ }, search: {
99
+ minX: number;
100
+ minY: number;
101
+ maxX: number;
102
+ maxY: number;
103
+ } | {
104
+ x: number;
105
+ y: number;
106
+ } | number): WorldMapInfo[];
107
+ /**
108
+ * Get map information by ID
109
+ *
110
+ * @param mapId - Map ID
111
+ * @returns Map information or null if not found
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * const mapInfo = worldMaps.getMapInfo("forest");
116
+ * ```
117
+ */
118
+ getMapInfo(mapId: string): WorldMapInfo | null;
119
+ /**
120
+ * Get all configured maps
121
+ *
122
+ * @returns Array of all world maps
123
+ */
124
+ getAllMaps(): WorldMapInfo[];
125
+ /**
126
+ * Find map by world coordinates
127
+ *
128
+ * @param worldX - World X coordinate
129
+ * @param worldY - World Y coordinate
130
+ * @returns Map found or null
131
+ */
132
+ getMapByWorldCoordinates(worldX: number, worldY: number): WorldMapInfo | null;
133
+ /**
134
+ * Calculate absolute world position of a player
135
+ *
136
+ * @param map - Current map
137
+ * @param localX - Local X position in the map
138
+ * @param localY - Local Y position in the map
139
+ * @returns Absolute coordinates in the world
140
+ */
141
+ getWorldPosition(map: WorldMapInfo, localX: number, localY: number): {
142
+ x: number;
143
+ y: number;
144
+ };
145
+ /**
146
+ * Calculate local position from world position
147
+ *
148
+ * @param worldX - World X position
149
+ * @param worldY - World Y position
150
+ * @param targetMap - Target map
151
+ * @returns Local position in the target map
152
+ */
153
+ getLocalPosition(worldX: number, worldY: number, targetMap: WorldMapInfo): {
154
+ x: number;
155
+ y: number;
156
+ };
157
+ }
158
+ /**
159
+ * Public alias for the world maps manager
160
+ *
161
+ * This alias is provided for API readability in map methods.
162
+ */
163
+ export type RpgWorldMaps = WorldMapsManager;
@@ -0,0 +1,12 @@
1
+ export interface SaveSlotMeta {
2
+ level?: number;
3
+ exp?: number;
4
+ map?: string;
5
+ date?: string;
6
+ [key: string]: any;
7
+ }
8
+ export interface SaveSlot extends SaveSlotMeta {
9
+ snapshot?: string;
10
+ }
11
+ export type SaveSlotList = Array<SaveSlotMeta | null>;
12
+ export type SaveSlotEntries = Array<SaveSlot | null>;
@@ -0,0 +1,27 @@
1
+ export type WeatherEffect = "rain" | "snow" | "fog" | "cloud";
2
+ export interface WeatherParams {
3
+ speed?: number;
4
+ windDirection?: number;
5
+ windStrength?: number;
6
+ density?: number;
7
+ maxDrops?: number;
8
+ height?: number;
9
+ scale?: number;
10
+ sunIntensity?: number;
11
+ sunAngle?: number;
12
+ raySpread?: number;
13
+ rayTwinkle?: number;
14
+ rayTwinkleSpeed?: number;
15
+ zIndex?: number;
16
+ alpha?: number;
17
+ blendMode?: string;
18
+ }
19
+ export interface WeatherState {
20
+ effect: WeatherEffect;
21
+ preset?: string;
22
+ params?: WeatherParams;
23
+ transitionMs?: number;
24
+ durationMs?: number;
25
+ startedAt?: number;
26
+ seed?: number;
27
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpgjs/common",
3
- "version": "5.0.0-alpha.4",
3
+ "version": "5.0.0-alpha.40",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "publishConfig": {
@@ -11,17 +11,16 @@
11
11
  "license": "MIT",
12
12
  "description": "",
13
13
  "dependencies": {
14
- "@dimforge/rapier2d": "^0.17.3",
15
- "@signe/di": "^2.3.1",
16
- "@signe/reactive": "^2.3.1",
17
- "@signe/sync": "^2.3.1",
18
- "matter-js": "^0.20.0",
14
+ "@rpgjs/physic": "5.0.0-alpha.40",
15
+ "@signe/di": "^2.8.3",
16
+ "@signe/reactive": "^2.8.3",
17
+ "@signe/sync": "^2.8.3",
19
18
  "rxjs": "^7.8.2"
20
19
  },
21
20
  "devDependencies": {
22
- "vite": "^6.2.5",
23
- "vite-plugin-dts": "^4.5.3",
24
- "vitest": "^3.1.1"
21
+ "vite": "^7.3.1",
22
+ "vite-plugin-dts": "^4.5.4",
23
+ "vitest": "^4.0.18"
25
24
  },
26
25
  "type": "module",
27
26
  "scripts": {
@@ -0,0 +1,294 @@
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 class PerlinNoise2D {
32
+ private readonly permutation: number[];
33
+ private readonly p: number[];
34
+
35
+ /**
36
+ * Creates a new Perlin noise generator
37
+ *
38
+ * @param seed - Optional seed for deterministic noise generation. If not provided, uses a default seed.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const noise = new PerlinNoise2D(12345);
43
+ * const value = noise.get(10, 20);
44
+ * ```
45
+ */
46
+ constructor(seed: number = 0) {
47
+ // Create permutation table (256 values)
48
+ this.permutation = this.generatePermutation(seed);
49
+
50
+ // Double the permutation array for easier wrapping
51
+ this.p = [...this.permutation, ...this.permutation];
52
+ }
53
+
54
+ /**
55
+ * Generates a permutation table based on seed
56
+ *
57
+ * @param seed - Seed value for permutation generation
58
+ * @returns Array of 256 shuffled values
59
+ */
60
+ private generatePermutation(seed: number): number[] {
61
+ const p: number[] = [];
62
+
63
+ // Initialize with sequential values
64
+ for (let i = 0; i < 256; i++) {
65
+ p[i] = i;
66
+ }
67
+
68
+ // Simple seeded shuffle using linear congruential generator
69
+ let state = seed;
70
+ const lcg = () => {
71
+ state = (state * 1103515245 + 12345) & 0x7fffffff;
72
+ return state;
73
+ };
74
+
75
+ // Fisher-Yates shuffle with seeded random
76
+ for (let i = 255; i > 0; i--) {
77
+ const j = lcg() % (i + 1);
78
+ [p[i], p[j]] = [p[j], p[i]];
79
+ }
80
+
81
+ return p;
82
+ }
83
+
84
+ /**
85
+ * Fade function for smooth interpolation (ease curve)
86
+ *
87
+ * @param t - Value between 0 and 1
88
+ * @returns Smoothed value between 0 and 1
89
+ */
90
+ private fade(t: number): number {
91
+ return t * t * t * (t * (t * 6 - 15) + 10);
92
+ }
93
+
94
+ /**
95
+ * Linear interpolation
96
+ *
97
+ * @param a - Start value
98
+ * @param b - End value
99
+ * @param t - Interpolation factor (0 to 1)
100
+ * @returns Interpolated value
101
+ */
102
+ private lerp(a: number, b: number, t: number): number {
103
+ return a + t * (b - a);
104
+ }
105
+
106
+ /**
107
+ * Gradient function - generates a pseudo-random gradient vector
108
+ *
109
+ * @param hash - Hash value from permutation table
110
+ * @param x - X component
111
+ * @param y - Y component
112
+ * @returns Dot product of gradient and position
113
+ */
114
+ private grad(hash: number, x: number, y: number): number {
115
+ // Convert hash to one of 4 gradient directions
116
+ const h = hash & 3;
117
+
118
+ // Return dot product with gradient vector
119
+ switch (h) {
120
+ case 0: return x + y; // (1, 1)
121
+ case 1: return -x + y; // (-1, 1)
122
+ case 2: return x - y; // (1, -1)
123
+ case 3: return -x - y; // (-1, -1)
124
+ default: return 0;
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Gets the noise value at the specified 2D coordinates
130
+ *
131
+ * Returns a value between approximately -1 and 1, though values near the edges
132
+ * are less common. For practical use, you may want to clamp or normalize the result.
133
+ *
134
+ * @param x - X coordinate
135
+ * @param y - Y coordinate
136
+ * @param scale - Optional scale factor (default: 0.1). Lower values create smoother, larger patterns.
137
+ * @returns Noise value between approximately -1 and 1
138
+ *
139
+ * @example
140
+ * ```ts
141
+ * const noise = new PerlinNoise2D();
142
+ * const value = noise.get(10, 20); // Basic usage
143
+ * const scaled = noise.get(10, 20, 0.05); // Smoother pattern
144
+ * ```
145
+ */
146
+ get(x: number, y: number, scale: number = 0.1): number {
147
+ // Apply scale
148
+ x *= scale;
149
+ y *= scale;
150
+
151
+ // Find unit grid cell containing point
152
+ const X = Math.floor(x) & 255;
153
+ const Y = Math.floor(y) & 255;
154
+
155
+ // Get relative x,y coordinates within that cell
156
+ x -= Math.floor(x);
157
+ y -= Math.floor(y);
158
+
159
+ // Compute fade curves for x and y
160
+ const u = this.fade(x);
161
+ const v = this.fade(y);
162
+
163
+ // Hash coordinates of the 4 square corners
164
+ const A = this.p[X] + Y;
165
+ const AA = this.p[A];
166
+ const AB = this.p[A + 1];
167
+ const B = this.p[X + 1] + Y;
168
+ const BA = this.p[B];
169
+ const BB = this.p[B + 1];
170
+
171
+ // And add blended results from 4 corners of the square
172
+ return this.lerp(
173
+ this.lerp(
174
+ this.grad(this.p[AA], x, y),
175
+ this.grad(this.p[BA], x - 1, y),
176
+ u
177
+ ),
178
+ this.lerp(
179
+ this.grad(this.p[AB], x, y - 1),
180
+ this.grad(this.p[BB], x - 1, y - 1),
181
+ u
182
+ ),
183
+ v
184
+ );
185
+ }
186
+
187
+ /**
188
+ * Gets a normalized noise value between 0 and 1
189
+ *
190
+ * Convenience method that normalizes the noise output to a 0-1 range.
191
+ *
192
+ * @param x - X coordinate
193
+ * @param y - Y coordinate
194
+ * @param scale - Optional scale factor (default: 0.1)
195
+ * @returns Noise value between 0 and 1
196
+ *
197
+ * @example
198
+ * ```ts
199
+ * const noise = new PerlinNoise2D();
200
+ * const normalized = noise.getNormalized(10, 20);
201
+ * // Returns value between 0 and 1
202
+ * ```
203
+ */
204
+ getNormalized(x: number, y: number, scale: number = 0.1): number {
205
+ return (this.get(x, y, scale) + 1) * 0.5;
206
+ }
207
+
208
+ /**
209
+ * Gets a noise value mapped to a specific range
210
+ *
211
+ * Maps the noise output to a custom min-max range.
212
+ *
213
+ * @param x - X coordinate
214
+ * @param y - Y coordinate
215
+ * @param min - Minimum output value
216
+ * @param max - Maximum output value
217
+ * @param scale - Optional scale factor (default: 0.1)
218
+ * @returns Noise value between min and max
219
+ *
220
+ * @example
221
+ * ```ts
222
+ * const noise = new PerlinNoise2D();
223
+ * const direction = noise.getRange(10, 20, 0, 3); // Returns 0, 1, 2, or 3
224
+ * ```
225
+ */
226
+ getRange(x: number, y: number, min: number, max: number, scale: number = 0.1): number {
227
+ const normalized = this.getNormalized(x, y, scale);
228
+ return min + normalized * (max - min);
229
+ }
230
+
231
+ /**
232
+ * Gets an integer noise value in a specific range (inclusive)
233
+ *
234
+ * Useful for selecting discrete values like array indices or enum values.
235
+ *
236
+ * @param x - X coordinate
237
+ * @param y - Y coordinate
238
+ * @param min - Minimum integer value (inclusive)
239
+ * @param max - Maximum integer value (inclusive)
240
+ * @param scale - Optional scale factor (default: 0.1)
241
+ * @returns Integer noise value between min and max (inclusive)
242
+ *
243
+ * @example
244
+ * ```ts
245
+ * const noise = new PerlinNoise2D();
246
+ * const directionIndex = noise.getInt(10, 20, 0, 3); // Returns 0, 1, 2, or 3
247
+ * ```
248
+ */
249
+ getInt(x: number, y: number, min: number, max: number, scale: number = 0.1): number {
250
+ const value = this.getRange(x, y, min, max + 1, scale);
251
+ return Math.floor(value);
252
+ }
253
+ }
254
+
255
+ /**
256
+ * Creates a shared Perlin noise instance for global use
257
+ *
258
+ * Useful when you want consistent noise across different parts of your application
259
+ * without passing the instance around.
260
+ *
261
+ * @param seed - Optional seed for the shared instance
262
+ * @returns Shared PerlinNoise2D instance
263
+ *
264
+ * @example
265
+ * ```ts
266
+ * const noise = getSharedPerlinNoise(12345);
267
+ * const value = noise.get(10, 20);
268
+ * ```
269
+ */
270
+ let sharedInstance: PerlinNoise2D | null = null;
271
+
272
+ export function getSharedPerlinNoise(seed?: number): PerlinNoise2D {
273
+ if (!sharedInstance) {
274
+ sharedInstance = new PerlinNoise2D(seed);
275
+ }
276
+ return sharedInstance;
277
+ }
278
+
279
+ /**
280
+ * Resets the shared Perlin noise instance
281
+ *
282
+ * Useful for testing or when you need to change the seed.
283
+ *
284
+ * @param seed - Optional new seed for the instance
285
+ *
286
+ * @example
287
+ * ```ts
288
+ * resetSharedPerlinNoise(12345);
289
+ * ```
290
+ */
291
+ export function resetSharedPerlinNoise(seed?: number): void {
292
+ sharedInstance = new PerlinNoise2D(seed);
293
+ }
294
+