@react-text-game/core 0.4.0 → 0.5.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 (41) hide show
  1. package/README.md +389 -0
  2. package/dist/audio/audioTrack.d.ts +407 -0
  3. package/dist/audio/audioTrack.d.ts.map +1 -0
  4. package/dist/audio/audioTrack.js +656 -0
  5. package/dist/audio/audioTrack.js.map +1 -0
  6. package/dist/audio/constants.d.ts +19 -0
  7. package/dist/audio/constants.d.ts.map +1 -0
  8. package/dist/audio/constants.js +20 -0
  9. package/dist/audio/constants.js.map +1 -0
  10. package/dist/audio/fabric.d.ts +35 -0
  11. package/dist/audio/fabric.d.ts.map +1 -0
  12. package/dist/audio/fabric.js +36 -0
  13. package/dist/audio/fabric.js.map +1 -0
  14. package/dist/audio/index.d.ts +33 -0
  15. package/dist/audio/index.d.ts.map +1 -0
  16. package/dist/audio/index.js +32 -0
  17. package/dist/audio/index.js.map +1 -0
  18. package/dist/audio/types.d.ts +105 -0
  19. package/dist/audio/types.d.ts.map +1 -0
  20. package/dist/audio/types.js +2 -0
  21. package/dist/audio/types.js.map +1 -0
  22. package/dist/hooks/index.d.ts +2 -0
  23. package/dist/hooks/index.d.ts.map +1 -1
  24. package/dist/hooks/index.js +2 -0
  25. package/dist/hooks/index.js.map +1 -1
  26. package/dist/hooks/useAudio.d.ts +50 -0
  27. package/dist/hooks/useAudio.d.ts.map +1 -0
  28. package/dist/hooks/useAudio.js +42 -0
  29. package/dist/hooks/useAudio.js.map +1 -0
  30. package/dist/hooks/useAudioManager.d.ts +66 -0
  31. package/dist/hooks/useAudioManager.d.ts.map +1 -0
  32. package/dist/hooks/useAudioManager.js +70 -0
  33. package/dist/hooks/useAudioManager.js.map +1 -0
  34. package/dist/i18n/utils.d.ts.map +1 -1
  35. package/dist/i18n/utils.js +6 -0
  36. package/dist/i18n/utils.js.map +1 -1
  37. package/dist/tests/audio.test.d.ts +2 -0
  38. package/dist/tests/audio.test.d.ts.map +1 -0
  39. package/dist/tests/audio.test.js +605 -0
  40. package/dist/tests/audio.test.js.map +1 -0
  41. package/package.json +6 -2
@@ -0,0 +1,407 @@
1
+ import type { AudioOptions, AudioState } from "./types";
2
+ /**
3
+ * Individual audio track with reactive state management.
4
+ *
5
+ * AudioTrack wraps the HTMLAudioElement API with Valtio reactive state,
6
+ * making it easy to integrate with React components and the game's save system.
7
+ *
8
+ * Features:
9
+ * - Reactive state updates via Valtio
10
+ * - Automatic persistence with save/load
11
+ * - Volume, loop, and playback rate controls
12
+ * - Fade in/out effects
13
+ * - Event-driven state synchronization
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const music = new AudioTrack('assets/music.mp3', {
18
+ * id: 'bg-music',
19
+ * loop: true,
20
+ * volume: 0.7,
21
+ * });
22
+ *
23
+ * await music.play();
24
+ * music.setVolume(0.5);
25
+ * ```
26
+ */
27
+ export declare class AudioTrack {
28
+ readonly id: string;
29
+ readonly src: string;
30
+ private readonly audioElement;
31
+ private readonly state;
32
+ private readonly jsonPath;
33
+ private readonly unsubscribe?;
34
+ constructor(src: string, options?: AudioOptions);
35
+ /**
36
+ * Attaches event listeners to sync HTMLAudioElement state with reactive state.
37
+ * @private
38
+ */
39
+ private attachEventListeners;
40
+ /**
41
+ * Plays the audio track.
42
+ *
43
+ * @returns Promise that resolves when playback starts
44
+ * @throws Error if playback fails
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * try {
49
+ * await audio.play();
50
+ * } catch (error) {
51
+ * console.error('Playback failed:', error);
52
+ * }
53
+ * ```
54
+ */
55
+ play(): Promise<void>;
56
+ /**
57
+ * Pauses the audio track.
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * audio.pause();
62
+ * ```
63
+ */
64
+ pause(): void;
65
+ /**
66
+ * Resumes playback if the audio is paused.
67
+ *
68
+ * @example
69
+ * ```typescript
70
+ * audio.resume();
71
+ * ```
72
+ */
73
+ resume(): void;
74
+ /**
75
+ * Stops the audio track and resets to the beginning.
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * audio.stop();
80
+ * ```
81
+ */
82
+ stop(): void;
83
+ /**
84
+ * Sets the volume level.
85
+ *
86
+ * @param volume - Volume level (0.0 to 1.0), clamped to valid range
87
+ *
88
+ * @example
89
+ * ```typescript
90
+ * audio.setVolume(0.5); // 50% volume
91
+ * ```
92
+ */
93
+ setVolume(volume: number): void;
94
+ /**
95
+ * Applies the effective volume (track volume * master volume) to the audio element.
96
+ * Internal method used by AudioManager to apply master volume changes.
97
+ *
98
+ * @internal
99
+ */
100
+ _applyEffectiveVolume(): void;
101
+ /**
102
+ * Sets whether the audio should loop.
103
+ *
104
+ * @param loop - True to enable looping, false to disable
105
+ *
106
+ * @example
107
+ * ```typescript
108
+ * audio.setLoop(true);
109
+ * ```
110
+ */
111
+ setLoop(loop: boolean): void;
112
+ /**
113
+ * Sets the playback rate.
114
+ *
115
+ * @param rate - Playback rate multiplier (0.5 = half speed, 2.0 = double speed)
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * audio.setPlaybackRate(1.5); // 1.5x speed
120
+ * ```
121
+ */
122
+ setPlaybackRate(rate: number): void;
123
+ /**
124
+ * Sets whether the audio is muted.
125
+ *
126
+ * @param muted - True to mute, false to unmute
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * audio.setMuted(true);
131
+ * ```
132
+ */
133
+ setMuted(muted: boolean): void;
134
+ /**
135
+ * Seeks to a specific time in the audio.
136
+ *
137
+ * @param time - Time in seconds, clamped to [0, duration]
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * audio.seek(30); // Seek to 30 seconds
142
+ * ```
143
+ */
144
+ seek(time: number): void;
145
+ /**
146
+ * Fades in the audio over a specified duration.
147
+ *
148
+ * Starts at volume 0 and gradually increases to the target volume.
149
+ *
150
+ * @param duration - Fade duration in milliseconds
151
+ * @returns Promise that resolves when fade completes
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * await audio.fadeIn(2000); // Fade in over 2 seconds
156
+ * ```
157
+ */
158
+ fadeIn(duration?: number): Promise<void>;
159
+ /**
160
+ * Fades out the audio over a specified duration and stops.
161
+ *
162
+ * @param duration - Fade duration in milliseconds
163
+ * @returns Promise that resolves when fade completes
164
+ *
165
+ * @example
166
+ * ```typescript
167
+ * await audio.fadeOut(1500); // Fade out over 1.5 seconds
168
+ * ```
169
+ */
170
+ fadeOut(duration?: number): Promise<void>;
171
+ /**
172
+ * Fades to a target volume over a specified duration.
173
+ *
174
+ * @private
175
+ * @param targetVolume - Target volume level (0.0 to 1.0)
176
+ * @param duration - Fade duration in milliseconds
177
+ * @returns Promise that resolves when fade completes
178
+ */
179
+ private fadeTo;
180
+ /**
181
+ * Gets the reactive audio state.
182
+ *
183
+ * This state is a Valtio proxy and can be used with useSnapshot() in React.
184
+ *
185
+ * @returns The reactive audio state
186
+ *
187
+ * @example
188
+ * ```typescript
189
+ * const state = audio.getState();
190
+ * console.log(state.isPlaying); // true/false
191
+ * ```
192
+ */
193
+ getState(): AudioState;
194
+ /**
195
+ * Saves the current audio state to storage.
196
+ *
197
+ * Called automatically when auto-save is enabled.
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * audio.save();
202
+ * ```
203
+ */
204
+ save(): void;
205
+ /**
206
+ * Loads the audio state from storage.
207
+ *
208
+ * Restores volume, loop, playback rate, muted status, and playback position.
209
+ * Optionally resumes playback if the audio was playing when saved.
210
+ *
211
+ * @example
212
+ * ```typescript
213
+ * audio.load();
214
+ * ```
215
+ */
216
+ load(): void;
217
+ /**
218
+ * Cleans up the audio track and removes all listeners.
219
+ *
220
+ * Should be called when the audio track is no longer needed.
221
+ *
222
+ * @example
223
+ * ```typescript
224
+ * audio.dispose();
225
+ * ```
226
+ */
227
+ dispose(): void;
228
+ }
229
+ /**
230
+ * Global audio manager for controlling all audio tracks.
231
+ *
232
+ * AudioManager provides a singleton interface for game-wide audio operations
233
+ * like master volume control, muting all tracks, or pausing all audio.
234
+ *
235
+ * @example
236
+ * ```typescript
237
+ * // Global volume control
238
+ * AudioManager.setMasterVolume(0.5);
239
+ *
240
+ * // Mute all audio
241
+ * AudioManager.muteAll();
242
+ *
243
+ * // Pause all playing tracks
244
+ * AudioManager.pauseAll();
245
+ * ```
246
+ */
247
+ declare class AudioManagerClass {
248
+ private tracks;
249
+ private state;
250
+ /**
251
+ * Internal method to register a track.
252
+ * Called automatically by AudioTrack constructor.
253
+ *
254
+ * @internal
255
+ * @param track - The audio track to register
256
+ */
257
+ _registerTrack(track: AudioTrack): void;
258
+ /**
259
+ * Internal method to unregister a track.
260
+ * Called automatically by AudioTrack.dispose().
261
+ *
262
+ * @internal
263
+ * @param track - The audio track to unregister
264
+ */
265
+ _unregisterTrack(track: AudioTrack): void;
266
+ /**
267
+ * Sets the master volume for all audio tracks.
268
+ *
269
+ * The master volume is multiplied with each track's individual volume.
270
+ * This does not modify individual track volumes - it applies a global multiplier.
271
+ *
272
+ * @param volume - Master volume level (0.0 to 1.0), clamped to valid range
273
+ *
274
+ * @example
275
+ * ```typescript
276
+ * AudioManager.setMasterVolume(0.5); // 50% master volume
277
+ * ```
278
+ */
279
+ setMasterVolume(volume: number): void;
280
+ /**
281
+ * Gets the current master volume.
282
+ *
283
+ * @returns The master volume level (0.0 to 1.0)
284
+ *
285
+ * @example
286
+ * ```typescript
287
+ * const volume = AudioManager.getMasterVolume();
288
+ * ```
289
+ */
290
+ getMasterVolume(): number;
291
+ /**
292
+ * Mutes all audio tracks.
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * AudioManager.muteAll();
297
+ * ```
298
+ */
299
+ muteAll(): void;
300
+ /**
301
+ * Unmutes all audio tracks.
302
+ *
303
+ * @example
304
+ * ```typescript
305
+ * AudioManager.unmuteAll();
306
+ * ```
307
+ */
308
+ unmuteAll(): void;
309
+ /**
310
+ * Pauses all currently playing audio tracks.
311
+ *
312
+ * @example
313
+ * ```typescript
314
+ * AudioManager.pauseAll();
315
+ * ```
316
+ */
317
+ pauseAll(): void;
318
+ /**
319
+ * Resumes all paused audio tracks.
320
+ *
321
+ * @example
322
+ * ```typescript
323
+ * AudioManager.resumeAll();
324
+ * ```
325
+ */
326
+ resumeAll(): void;
327
+ /**
328
+ * Stops all audio tracks.
329
+ *
330
+ * @example
331
+ * ```typescript
332
+ * AudioManager.stopAll();
333
+ * ```
334
+ */
335
+ stopAll(): void;
336
+ /**
337
+ * Gets all registered audio tracks.
338
+ *
339
+ * @returns Array of all audio tracks
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * const tracks = AudioManager.getAllTracks();
344
+ * console.log(`Total tracks: ${tracks.length}`);
345
+ * ```
346
+ */
347
+ getAllTracks(): Array<AudioTrack>;
348
+ /**
349
+ * Gets an audio track by its ID.
350
+ *
351
+ * @param id - The unique identifier of the audio track
352
+ * @returns The audio track, or undefined if not found
353
+ *
354
+ * @example
355
+ * ```typescript
356
+ * const music = AudioManager.getTrackById('bg-music');
357
+ * if (music) {
358
+ * music.play();
359
+ * }
360
+ * ```
361
+ */
362
+ getTrackById(id: string): AudioTrack | undefined;
363
+ /**
364
+ * Gets the reactive audio manager state.
365
+ *
366
+ * This state is a Valtio proxy and can be used with useSnapshot() in React.
367
+ *
368
+ * @returns The reactive audio manager state
369
+ *
370
+ * @example
371
+ * ```typescript
372
+ * const state = AudioManager.getState();
373
+ * console.log(state.masterVolume); // 1.0
374
+ * ```
375
+ */
376
+ getState(): {
377
+ masterVolume: number;
378
+ isMuted: boolean;
379
+ };
380
+ /**
381
+ * Disposes all audio tracks.
382
+ *
383
+ * Useful for cleanup when shutting down the game.
384
+ *
385
+ * @example
386
+ * ```typescript
387
+ * AudioManager.disposeAll();
388
+ * ```
389
+ */
390
+ disposeAll(): void;
391
+ }
392
+ /**
393
+ * Singleton instance of the AudioManager.
394
+ *
395
+ * Use this to control all audio tracks globally.
396
+ *
397
+ * @example
398
+ * ```typescript
399
+ * import { AudioManager } from '@react-text-game/core/audio';
400
+ *
401
+ * AudioManager.setMasterVolume(0.8);
402
+ * AudioManager.muteAll();
403
+ * ```
404
+ */
405
+ export declare const AudioManager: AudioManagerClass;
406
+ export {};
407
+ //# sourceMappingURL=audioTrack.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"audioTrack.d.ts","sourceRoot":"","sources":["../../src/audio/audioTrack.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,YAAY,EAAkB,UAAU,EAAE,MAAM,SAAS,CAAC;AAExE;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,UAAU;IACnB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB,OAAO,CAAC,QAAQ,CAAC,YAAY,CAAmB;IAChD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAa;IACnC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;IACpC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAa;gBAE9B,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB;IAgEnD;;;OAGG;IACH,OAAO,CAAC,oBAAoB;IAwC5B;;;;;;;;;;;;;;OAcG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAS3B;;;;;;;OAOG;IACH,KAAK,IAAI,IAAI;IAIb;;;;;;;OAOG;IACH,MAAM,IAAI,IAAI;IAQd;;;;;;;OAOG;IACH,IAAI,IAAI,IAAI;IAQZ;;;;;;;;;OASG;IACH,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAM/B;;;;;OAKG;IACH,qBAAqB,IAAI,IAAI;IAM7B;;;;;;;;;OASG;IACH,OAAO,CAAC,IAAI,EAAE,OAAO,GAAG,IAAI;IAK5B;;;;;;;;;OASG;IACH,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI;IAKnC;;;;;;;;;OASG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI;IAK9B;;;;;;;;;OASG;IACH,IAAI,CAAC,IAAI,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