@react-text-game/core 0.1.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 (178) hide show
  1. package/README.md +744 -0
  2. package/dist/baseGameObject.d.ts +90 -0
  3. package/dist/baseGameObject.d.ts.map +1 -0
  4. package/dist/baseGameObject.js +109 -0
  5. package/dist/baseGameObject.js.map +1 -0
  6. package/dist/constants.d.ts +12 -0
  7. package/dist/constants.d.ts.map +1 -0
  8. package/dist/constants.js +12 -0
  9. package/dist/constants.js.map +1 -0
  10. package/dist/game.d.ts +294 -0
  11. package/dist/game.d.ts.map +1 -0
  12. package/dist/game.js +489 -0
  13. package/dist/game.js.map +1 -0
  14. package/dist/helpers.d.ts +2 -0
  15. package/dist/helpers.d.ts.map +1 -0
  16. package/dist/helpers.js +6 -0
  17. package/dist/helpers.js.map +1 -0
  18. package/dist/hooks/index.d.ts +4 -0
  19. package/dist/hooks/index.d.ts.map +1 -0
  20. package/dist/hooks/index.js +4 -0
  21. package/dist/hooks/index.js.map +1 -0
  22. package/dist/hooks/useCurrentPassage.d.ts +10 -0
  23. package/dist/hooks/useCurrentPassage.d.ts.map +1 -0
  24. package/dist/hooks/useCurrentPassage.js +17 -0
  25. package/dist/hooks/useCurrentPassage.js.map +1 -0
  26. package/dist/hooks/useGameEntity.d.ts +21 -0
  27. package/dist/hooks/useGameEntity.d.ts.map +1 -0
  28. package/dist/hooks/useGameEntity.js +70 -0
  29. package/dist/hooks/useGameEntity.js.map +1 -0
  30. package/dist/hooks/useGameIsStarted.d.ts +12 -0
  31. package/dist/hooks/useGameIsStarted.d.ts.map +1 -0
  32. package/dist/hooks/useGameIsStarted.js +18 -0
  33. package/dist/hooks/useGameIsStarted.js.map +1 -0
  34. package/dist/index.d.ts +12 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +10 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/logger.d.ts +8 -0
  39. package/dist/logger.d.ts.map +1 -0
  40. package/dist/logger.js +36 -0
  41. package/dist/logger.js.map +1 -0
  42. package/dist/options.d.ts +13 -0
  43. package/dist/options.d.ts.map +1 -0
  44. package/dist/options.js +15 -0
  45. package/dist/options.js.map +1 -0
  46. package/dist/passages/interactiveMap/fabric.d.ts +4 -0
  47. package/dist/passages/interactiveMap/fabric.d.ts.map +1 -0
  48. package/dist/passages/interactiveMap/fabric.js +3 -0
  49. package/dist/passages/interactiveMap/fabric.js.map +1 -0
  50. package/dist/passages/interactiveMap/index.d.ts +4 -0
  51. package/dist/passages/interactiveMap/index.d.ts.map +1 -0
  52. package/dist/passages/interactiveMap/index.js +4 -0
  53. package/dist/passages/interactiveMap/index.js.map +1 -0
  54. package/dist/passages/interactiveMap/interactiveMap.d.ts +89 -0
  55. package/dist/passages/interactiveMap/interactiveMap.d.ts.map +1 -0
  56. package/dist/passages/interactiveMap/interactiveMap.js +103 -0
  57. package/dist/passages/interactiveMap/interactiveMap.js.map +1 -0
  58. package/dist/passages/interactiveMap/types.d.ts +822 -0
  59. package/dist/passages/interactiveMap/types.d.ts.map +1 -0
  60. package/dist/passages/interactiveMap/types.js +2 -0
  61. package/dist/passages/interactiveMap/types.js.map +1 -0
  62. package/dist/passages/passage.d.ts +57 -0
  63. package/dist/passages/passage.d.ts.map +1 -0
  64. package/dist/passages/passage.js +64 -0
  65. package/dist/passages/passage.js.map +1 -0
  66. package/dist/passages/story/fabric.d.ts +4 -0
  67. package/dist/passages/story/fabric.d.ts.map +1 -0
  68. package/dist/passages/story/fabric.js +3 -0
  69. package/dist/passages/story/fabric.js.map +1 -0
  70. package/dist/passages/story/index.d.ts +5 -0
  71. package/dist/passages/story/index.d.ts.map +1 -0
  72. package/dist/passages/story/index.js +5 -0
  73. package/dist/passages/story/index.js.map +1 -0
  74. package/dist/passages/story/start.d.ts +14 -0
  75. package/dist/passages/story/start.d.ts.map +1 -0
  76. package/dist/passages/story/start.js +22 -0
  77. package/dist/passages/story/start.js.map +1 -0
  78. package/dist/passages/story/story.d.ts +84 -0
  79. package/dist/passages/story/story.d.ts.map +1 -0
  80. package/dist/passages/story/story.js +88 -0
  81. package/dist/passages/story/story.js.map +1 -0
  82. package/dist/passages/story/types.d.ts +911 -0
  83. package/dist/passages/story/types.d.ts.map +1 -0
  84. package/dist/passages/story/types.js +2 -0
  85. package/dist/passages/story/types.js.map +1 -0
  86. package/dist/passages/types/index.d.ts +3 -0
  87. package/dist/passages/types/index.d.ts.map +1 -0
  88. package/dist/passages/types/index.js +2 -0
  89. package/dist/passages/types/index.js.map +1 -0
  90. package/dist/passages/widget.d.ts +62 -0
  91. package/dist/passages/widget.d.ts.map +1 -0
  92. package/dist/passages/widget.js +66 -0
  93. package/dist/passages/widget.js.map +1 -0
  94. package/dist/saves/constants.d.ts +17 -0
  95. package/dist/saves/constants.d.ts.map +1 -0
  96. package/dist/saves/constants.js +17 -0
  97. package/dist/saves/constants.js.map +1 -0
  98. package/dist/saves/db.d.ts +119 -0
  99. package/dist/saves/db.d.ts.map +1 -0
  100. package/dist/saves/db.js +231 -0
  101. package/dist/saves/db.js.map +1 -0
  102. package/dist/saves/helpers.d.ts +28 -0
  103. package/dist/saves/helpers.d.ts.map +1 -0
  104. package/dist/saves/helpers.js +84 -0
  105. package/dist/saves/helpers.js.map +1 -0
  106. package/dist/saves/hooks/index.d.ts +10 -0
  107. package/dist/saves/hooks/index.d.ts.map +1 -0
  108. package/dist/saves/hooks/index.js +10 -0
  109. package/dist/saves/hooks/index.js.map +1 -0
  110. package/dist/saves/hooks/useDeleteAllSlots.d.ts +18 -0
  111. package/dist/saves/hooks/useDeleteAllSlots.d.ts.map +1 -0
  112. package/dist/saves/hooks/useDeleteAllSlots.js +18 -0
  113. package/dist/saves/hooks/useDeleteAllSlots.js.map +1 -0
  114. package/dist/saves/hooks/useDeleteGame.d.ts +22 -0
  115. package/dist/saves/hooks/useDeleteGame.d.ts.map +1 -0
  116. package/dist/saves/hooks/useDeleteGame.js +33 -0
  117. package/dist/saves/hooks/useDeleteGame.js.map +1 -0
  118. package/dist/saves/hooks/useExportSaves.d.ts +27 -0
  119. package/dist/saves/hooks/useExportSaves.d.ts.map +1 -0
  120. package/dist/saves/hooks/useExportSaves.js +54 -0
  121. package/dist/saves/hooks/useExportSaves.js.map +1 -0
  122. package/dist/saves/hooks/useImportSaves.d.ts +29 -0
  123. package/dist/saves/hooks/useImportSaves.d.ts.map +1 -0
  124. package/dist/saves/hooks/useImportSaves.js +108 -0
  125. package/dist/saves/hooks/useImportSaves.js.map +1 -0
  126. package/dist/saves/hooks/useLastLoadGame.d.ts +39 -0
  127. package/dist/saves/hooks/useLastLoadGame.d.ts.map +1 -0
  128. package/dist/saves/hooks/useLastLoadGame.js +72 -0
  129. package/dist/saves/hooks/useLastLoadGame.js.map +1 -0
  130. package/dist/saves/hooks/useLoadGame.d.ts +22 -0
  131. package/dist/saves/hooks/useLoadGame.d.ts.map +1 -0
  132. package/dist/saves/hooks/useLoadGame.js +40 -0
  133. package/dist/saves/hooks/useLoadGame.js.map +1 -0
  134. package/dist/saves/hooks/useRestartGame.d.ts +20 -0
  135. package/dist/saves/hooks/useRestartGame.d.ts.map +1 -0
  136. package/dist/saves/hooks/useRestartGame.js +29 -0
  137. package/dist/saves/hooks/useRestartGame.js.map +1 -0
  138. package/dist/saves/hooks/useSaveGame.d.ts +22 -0
  139. package/dist/saves/hooks/useSaveGame.d.ts.map +1 -0
  140. package/dist/saves/hooks/useSaveGame.js +34 -0
  141. package/dist/saves/hooks/useSaveGame.js.map +1 -0
  142. package/dist/saves/hooks/useSaveSlots.d.ts +45 -0
  143. package/dist/saves/hooks/useSaveSlots.d.ts.map +1 -0
  144. package/dist/saves/hooks/useSaveSlots.js +42 -0
  145. package/dist/saves/hooks/useSaveSlots.js.map +1 -0
  146. package/dist/saves/index.d.ts +4 -0
  147. package/dist/saves/index.d.ts.map +1 -0
  148. package/dist/saves/index.js +3 -0
  149. package/dist/saves/index.js.map +1 -0
  150. package/dist/saves/types.d.ts +52 -0
  151. package/dist/saves/types.d.ts.map +1 -0
  152. package/dist/saves/types.js +2 -0
  153. package/dist/saves/types.js.map +1 -0
  154. package/dist/storage.d.ts +124 -0
  155. package/dist/storage.d.ts.map +1 -0
  156. package/dist/storage.js +229 -0
  157. package/dist/storage.js.map +1 -0
  158. package/dist/tests/game.test.d.ts +2 -0
  159. package/dist/tests/game.test.d.ts.map +1 -0
  160. package/dist/tests/game.test.js +602 -0
  161. package/dist/tests/game.test.js.map +1 -0
  162. package/dist/tests/interactiveMap.test.d.ts +2 -0
  163. package/dist/tests/interactiveMap.test.d.ts.map +1 -0
  164. package/dist/tests/interactiveMap.test.js +1003 -0
  165. package/dist/tests/interactiveMap.test.js.map +1 -0
  166. package/dist/tests/storage.test.d.ts +2 -0
  167. package/dist/tests/storage.test.d.ts.map +1 -0
  168. package/dist/tests/storage.test.js +328 -0
  169. package/dist/tests/storage.test.js.map +1 -0
  170. package/dist/tests/story.test.d.ts +2 -0
  171. package/dist/tests/story.test.d.ts.map +1 -0
  172. package/dist/tests/story.test.js +698 -0
  173. package/dist/tests/story.test.js.map +1 -0
  174. package/dist/types.d.ts +19 -0
  175. package/dist/types.d.ts.map +1 -0
  176. package/dist/types.js +2 -0
  177. package/dist/types.js.map +1 -0
  178. package/package.json +60 -0
@@ -0,0 +1,90 @@
1
+ import { InitVarsType } from "./types";
2
+ /**
3
+ * Base class for all game entities in the text game engine.
4
+ *
5
+ * Provides automatic registration with the Game registry, reactive state management
6
+ * via Valtio proxies, and persistence through the Storage system using JSONPath.
7
+ *
8
+ * @template VariablesType - The type definition for entity variables, must extend InitVarsType
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * class Player extends BaseGameObject<{ health: number; name: string }> {
13
+ * constructor() {
14
+ * super({
15
+ * id: 'player',
16
+ * variables: { health: 100, name: 'Hero' }
17
+ * });
18
+ * }
19
+ *
20
+ * takeDamage(amount: number) {
21
+ * this._variables.health -= amount;
22
+ * this.save();
23
+ * }
24
+ * }
25
+ *
26
+ * const player = new Player();
27
+ * ```
28
+ */
29
+ export declare class BaseGameObject<VariablesType extends InitVarsType = InitVarsType> {
30
+ private static jsonPath;
31
+ /**
32
+ * Unique identifier for this game object.
33
+ * Used for registry lookup and storage path generation.
34
+ */
35
+ readonly id: string;
36
+ /**
37
+ * Internal storage for entity variables.
38
+ * Protected to allow derived classes to modify state directly.
39
+ */
40
+ protected _variables: VariablesType;
41
+ /**
42
+ * Creates a new game object and automatically registers it with the Game registry.
43
+ *
44
+ * @param props - Configuration object for the game object
45
+ * @param props.id - Unique identifier for this object
46
+ * @param props.variables - Optional initial variables for this object
47
+ */
48
+ constructor(props: {
49
+ id: string;
50
+ variables?: VariablesType;
51
+ });
52
+ /**
53
+ * Read-only accessor for entity variables.
54
+ *
55
+ * @returns The current variables object for this entity
56
+ */
57
+ get variables(): VariablesType;
58
+ /**
59
+ * Generates the JSONPath for this object's storage location.
60
+ *
61
+ * @private
62
+ * @returns The JSONPath string for storage operations
63
+ */
64
+ private get path();
65
+ /**
66
+ * Loads this object's variables from the Storage system.
67
+ *
68
+ * If saved data exists, it merges into the current variables.
69
+ * If no saved data exists, clears all variables.
70
+ *
71
+ * @example
72
+ * ```typescript
73
+ * player.load(); // Restores player state from storage
74
+ * ```
75
+ */
76
+ load(): void;
77
+ /**
78
+ * Saves this object's current variables to the Storage system.
79
+ *
80
+ * Uses Valtio's snapshot to create a plain object copy before saving.
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * player._variables.health = 50;
85
+ * player.save(); // Persists the change
86
+ * ```
87
+ */
88
+ save(): void;
89
+ }
90
+ //# sourceMappingURL=baseGameObject.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"baseGameObject.d.ts","sourceRoot":"","sources":["../src/baseGameObject.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAY,MAAM,QAAQ,CAAC;AAEhD;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,qBAAa,cAAc,CAAC,aAAa,SAAS,YAAY,GAAG,YAAY;IACzE,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAkB;IAEzC;;;OAGG;IACH,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,SAAS,CAAC,UAAU,EAAE,aAAa,CAAC;IAEpC;;;;;;OAMG;gBACS,KAAK,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,SAAS,CAAC,EAAE,aAAa,CAAA;KAAE;IAM5D;;;;OAIG;IACH,IAAI,SAAS,IAAI,aAAa,CAE7B;IAED;;;;;OAKG;IACH,OAAO,KAAK,IAAI,GAEf;IAED;;;;;;;;;;OAUG;IACH,IAAI;IAWJ;;;;;;;;;;OAUG;IACH,IAAI;CAGP"}
@@ -0,0 +1,109 @@
1
+ import { snapshot } from "valtio";
2
+ import { Game } from "./game";
3
+ import { Storage } from "./storage";
4
+ /**
5
+ * Base class for all game entities in the text game engine.
6
+ *
7
+ * Provides automatic registration with the Game registry, reactive state management
8
+ * via Valtio proxies, and persistence through the Storage system using JSONPath.
9
+ *
10
+ * @template VariablesType - The type definition for entity variables, must extend InitVarsType
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * class Player extends BaseGameObject<{ health: number; name: string }> {
15
+ * constructor() {
16
+ * super({
17
+ * id: 'player',
18
+ * variables: { health: 100, name: 'Hero' }
19
+ * });
20
+ * }
21
+ *
22
+ * takeDamage(amount: number) {
23
+ * this._variables.health -= amount;
24
+ * this.save();
25
+ * }
26
+ * }
27
+ *
28
+ * const player = new Player();
29
+ * ```
30
+ */
31
+ export class BaseGameObject {
32
+ static jsonPath = "$.";
33
+ /**
34
+ * Unique identifier for this game object.
35
+ * Used for registry lookup and storage path generation.
36
+ */
37
+ id;
38
+ /**
39
+ * Internal storage for entity variables.
40
+ * Protected to allow derived classes to modify state directly.
41
+ */
42
+ _variables;
43
+ /**
44
+ * Creates a new game object and automatically registers it with the Game registry.
45
+ *
46
+ * @param props - Configuration object for the game object
47
+ * @param props.id - Unique identifier for this object
48
+ * @param props.variables - Optional initial variables for this object
49
+ */
50
+ constructor(props) {
51
+ this.id = props.id;
52
+ this._variables = props.variables || {};
53
+ Game.registerEntity(this);
54
+ }
55
+ /**
56
+ * Read-only accessor for entity variables.
57
+ *
58
+ * @returns The current variables object for this entity
59
+ */
60
+ get variables() {
61
+ return this._variables;
62
+ }
63
+ /**
64
+ * Generates the JSONPath for this object's storage location.
65
+ *
66
+ * @private
67
+ * @returns The JSONPath string for storage operations
68
+ */
69
+ get path() {
70
+ return `${BaseGameObject.jsonPath}${this.id}`;
71
+ }
72
+ /**
73
+ * Loads this object's variables from the Storage system.
74
+ *
75
+ * If saved data exists, it merges into the current variables.
76
+ * If no saved data exists, clears all variables.
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * player.load(); // Restores player state from storage
81
+ * ```
82
+ */
83
+ load() {
84
+ const loadedVariables = Storage.getValue(this.path);
85
+ if (loadedVariables.length > 0) {
86
+ Object.assign(this._variables, loadedVariables[0]);
87
+ }
88
+ else {
89
+ for (const key in this._variables) {
90
+ delete this._variables[key];
91
+ }
92
+ }
93
+ }
94
+ /**
95
+ * Saves this object's current variables to the Storage system.
96
+ *
97
+ * Uses Valtio's snapshot to create a plain object copy before saving.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * player._variables.health = 50;
102
+ * player.save(); // Persists the change
103
+ * ```
104
+ */
105
+ save() {
106
+ Storage.setValue(this.path, snapshot(this._variables));
107
+ }
108
+ }
109
+ //# sourceMappingURL=baseGameObject.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"baseGameObject.js","sourceRoot":"","sources":["../src/baseGameObject.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAElC,OAAO,EAAE,IAAI,EAAE,MAAM,OAAO,CAAC;AAC7B,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGnC;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MA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represents application entry-point
5
+ */
6
+ readonly START_MENU: "start-menu-passage";
7
+ /**
8
+ * represents a game starting point
9
+ */
10
+ readonly START: "start-passage";
11
+ };
12
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB,EAAG,WAAoB,CAAC;AAExD,eAAO,MAAM,oBAAoB;IAC7B;;OAEG;;IAEH;;OAEG;;CAEG,CAAC"}
@@ -0,0 +1,12 @@
1
+ export const STORAGE_SYSTEM_PATH = "$._system";
2
+ export const SYSTEM_PASSAGE_NAMES = {
3
+ /**
4
+ * represents application entry-point
5
+ */
6
+ START_MENU: "start-menu-passage",
7
+ /**
8
+ * represents a game starting point
9
+ */
10
+ START: "start-passage",
11
+ };
12
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,mBAAmB,GAAG,WAAoB,CAAC;AAExD,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAChC;;OAEG;IACH,UAAU,EAAE,oBAAoB;IAChC;;OAEG;IACH,KAAK,EAAE,eAAe;CAChB,CAAC"}
package/dist/game.d.ts ADDED
@@ -0,0 +1,294 @@
1
+ import { BaseGameObject } from "./baseGameObject";
2
+ import { NewOptions, Options } from "./options";
3
+ import { Passage } from "./passages/passage";
4
+ import { GameSaveState } from "./types";
5
+ /**
6
+ * Central orchestrator for the text game engine.
7
+ *
8
+ * The `Game` class manages all core game functionality including:
9
+ * - Entity registration and proxying with Valtio for reactive state
10
+ * - Passage registration and navigation
11
+ * - State serialization and persistence
12
+ * - Auto-save to session storage with debouncing
13
+ *
14
+ * All entities and passages are automatically registered through their constructors,
15
+ * and the Game provides static methods for navigation and state management.
16
+ *
17
+ * @example
18
+ * ```typescript
19
+ * // Create entities and passages (auto-registered)
20
+ * const player = new Player();
21
+ * const intro = newStory('intro', () => [...]);
22
+ *
23
+ * // Navigate
24
+ * Game.jumpTo('intro');
25
+ *
26
+ * // Save/Load
27
+ * const state = Game.getState();
28
+ * Game.setState(state);
29
+ *
30
+ * // Auto-save
31
+ * Game.enableAutoSave();
32
+ * Game.loadFromSessionStorage();
33
+ * ```
34
+ */
35
+ export declare class Game {
36
+ private static state;
37
+ private static initialized;
38
+ private static autoSaveEnabled;
39
+ private static saveTimer;
40
+ private static unsubscribeFunctions;
41
+ private static readonly AUTO_SAVE_KEY;
42
+ private static readonly SAVE_DEBOUNCE_MS;
43
+ /**
44
+ * Ensures the game has been initialized before allowing method calls.
45
+ *
46
+ * @private
47
+ * @throws Error if Game.init() has not been called
48
+ */
49
+ private static ensureInitialized;
50
+ /**
51
+ * Registers and proxies the provided game objects for further use by adding them to the object registry.
52
+ *
53
+ * @param {...BaseGameObject[]} objects - The array of BaseGameObject instances to be registered.
54
+ * @return {void} This method does not return a value.
55
+ */
56
+ static registerEntity(...objects: Array<BaseGameObject>): void;
57
+ /**
58
+ * Registers one or more passages into the passage registry. Each passage must have a unique identifier.
59
+ * Throws an error if a passage with the same id is already registered.
60
+ *
61
+ * @param {...Passage} passages The passages to be registered. Each passage should be an object containing an `id` property.
62
+ * @return {void} Does not return a value.
63
+ * @throws Error if Game.init() has not been called
64
+ */
65
+ static registerPassage(...passages: Array<Passage>): void;
66
+ /**
67
+ * Retrieves all registered passages from the passage registry.
68
+ *
69
+ * @return {IterableIterator<Passage>} An iterator containing all the Passage objects.
70
+ * @throws Error if Game.init() has not been called
71
+ */
72
+ static get registeredPassages(): IterableIterator<Passage>;
73
+ /**
74
+ * Retrieves the current passage from the passage registry based on the current passage ID in the game state.
75
+ * If the current passage ID is null or the passage cannot be found, returns null.
76
+ *
77
+ * @return {Passage | null} The current passage object or null if not available.
78
+ * @throws Error if Game.init() has not been called
79
+ */
80
+ static get currentPassage(): Passage | null;
81
+ /**
82
+ * Retrieves a passage by its unique identifier.
83
+ *
84
+ * @param {string} passageId - The unique ID of the passage to retrieve.
85
+ * @return {Passage|null} The passage object if found, or null if no passage exists with the given ID.
86
+ * @throws Error if Game.init() has not been called
87
+ */
88
+ static getPassageById(passageId: string): Passage | null;
89
+ /**
90
+ * Retrieves all the passages from the passages registry.
91
+ *
92
+ * @return {Array<Passage>} An array containing all the Passage objects.
93
+ * @throws Error if Game.init() has not been called
94
+ */
95
+ static getAllPassages(): Array<Passage>;
96
+ /**
97
+ * Navigates the game to a specified passage.
98
+ *
99
+ * @param {Passage|string} passage - The passage object or identifier of the passage to jump to.
100
+ * @return {void} Does not return any value.
101
+ * @throws {Error} Throws an error if the specified passage is not found or if Game.init() has not been called
102
+ */
103
+ static jumpTo(passage: Passage | string): void;
104
+ /**
105
+ * Sets the current passage in the game state.
106
+ *
107
+ * @param {Passage|string} passage - The passage to be set as current. Can be either a Passage object or a string representing the passage ID.
108
+ * @return {void} This method does not return a value.
109
+ * @throws Error if Game.init() has not been called
110
+ */
111
+ static setCurrent(passage: Passage | string): void;
112
+ /**
113
+ * Retrieves the proxied object from the object registry based on its ID.
114
+ * If the object is not found in the registry, the original object is returned.
115
+ *
116
+ * @internal - Used by hooks for reactive state management
117
+ * @param object - The original object to find in the registry
118
+ * @returns The proxied object from the registry if present, otherwise the original object
119
+ */
120
+ static _getProxiedObject<T extends BaseGameObject>(object: T): T;
121
+ /**
122
+ * Retrieves all proxied objects from the object registry.
123
+ *
124
+ * @internal - Used for batch operations during save/load
125
+ * @returns An array of BaseGameObject instances stored in the object registry
126
+ */
127
+ static _getAllProxiedObjects(): Array<BaseGameObject>;
128
+ /**
129
+ * Provides access to the internal game state for reactive hooks.
130
+ *
131
+ * @returns The game's internal reactive state
132
+ * @throws Error if Game.init() has not been called
133
+ */
134
+ static get selfState(): {
135
+ currentPassageId: string | null;
136
+ };
137
+ /**
138
+ * Saves the current game state, including critical passage information, into storage.
139
+ *
140
+ * This method saves the Game's internal state (current passage) to the Storage system.
141
+ * It does NOT save entity states - use entity.save() or Game.getState() for that.
142
+ *
143
+ * @private - Called internally by Game.getState()
144
+ */
145
+ private static save;
146
+ /**
147
+ * Loads the saved game state from storage and sets the current passage ID in the game state.
148
+ *
149
+ * @private - Called internally by Game.setState()
150
+ * @throws Error if no saved state is found
151
+ */
152
+ private static load;
153
+ /**
154
+ * Captures the complete game state including all entities and passages.
155
+ *
156
+ * This method:
157
+ * 1. Saves the Game's internal state (current passage)
158
+ * 2. Saves all registered entity states
159
+ * 3. Returns the complete state object
160
+ *
161
+ * @returns The complete serializable game state
162
+ * @throws Error if Game.init() has not been called
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * const savedState = Game.getState();
167
+ * localStorage.setItem('save1', JSON.stringify(savedState));
168
+ * ```
169
+ */
170
+ static getState(_fromI?: boolean): GameSaveState;
171
+ /**
172
+ * Restores the complete game state including all entities and passages.
173
+ *
174
+ * This method:
175
+ * 1. Sets the Storage state
176
+ * 2. Loads the Game's internal state (current passage)
177
+ * 3. Loads all registered entity states
178
+ *
179
+ * @param state - The game state to restore
180
+ * @throws Error if Game.init() has not been called
181
+ *
182
+ * @example
183
+ * ```typescript
184
+ * const savedState = JSON.parse(localStorage.getItem('save1'));
185
+ * Game.setState(savedState);
186
+ * ```
187
+ */
188
+ static setState(state: GameSaveState): void;
189
+ /**
190
+ * Saves the current game state to session storage with debouncing.
191
+ *
192
+ * Uses a 500ms debounce to prevent excessive writes during rapid state changes.
193
+ *
194
+ * @private - Called automatically when auto-save is enabled
195
+ */
196
+ private static saveToSessionStorage;
197
+ /**
198
+ * Enables auto-save functionality.
199
+ *
200
+ * Subscribes to all game state changes (Game state and all entity states)
201
+ * and automatically saves to session storage with 500ms debouncing.
202
+ *
203
+ * @throws Error if Game.init() has not been called
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * Game.enableAutoSave();
208
+ * // Now any state change will auto-save after 500ms
209
+ * player.health = 50; // Will trigger auto-save
210
+ * ```
211
+ */
212
+ static enableAutoSave(): void;
213
+ /**
214
+ * Disables auto-save functionality and clears all subscriptions.
215
+ *
216
+ * Cleans up all Valtio subscriptions and cancels any pending debounced saves.
217
+ *
218
+ * @throws Error if Game.init() has not been called
219
+ */
220
+ static disableAutoSave(): void;
221
+ /**
222
+ * Loads game state from session storage if available.
223
+ *
224
+ * @returns True if state was loaded successfully, false otherwise
225
+ * @throws Error if Game.init() has not been called
226
+ *
227
+ * @example
228
+ * ```typescript
229
+ * if (Game.loadFromSessionStorage()) {
230
+ * console.log('Game loaded from auto-save');
231
+ * } else {
232
+ * Game.jumpTo('start');
233
+ * }
234
+ * ```
235
+ */
236
+ static loadFromSessionStorage(): boolean;
237
+ /**
238
+ * Clears the auto-saved state from session storage.
239
+ *
240
+ * @throws Error if Game.init() has not been called
241
+ *
242
+ * @example
243
+ * ```typescript
244
+ * Game.clearAutoSave(); // Remove auto-save data
245
+ * ```
246
+ */
247
+ static clearAutoSave(): void;
248
+ /**
249
+ * Initializes the game engine with the provided options.
250
+ *
251
+ * This method MUST be called before any other Game methods can be used.
252
+ * Calling this method marks the Game as initialized and allows access to all other functionality.
253
+ *
254
+ * Optionally integrates with `@react-text-game/saves` package if installed.
255
+ *
256
+ * @param opts - Configuration options for the game
257
+ * @returns Promise that resolves when initialization is complete
258
+ *
259
+ * @example
260
+ * ```typescript
261
+ * await Game.init({
262
+ * // your options here
263
+ * });
264
+ *
265
+ * // Now you can use other Game methods
266
+ * Game.jumpTo('start');
267
+ * ```
268
+ */
269
+ static init(opts: NewOptions): Promise<void>;
270
+ /**
271
+ * Gets the game options.
272
+ *
273
+ * @returns The current game options
274
+ * @throws Error if Game.init() has not been called
275
+ */
276
+ static get options(): Options;
277
+ /**
278
+ * Updates the game options with the provided settings.
279
+ *
280
+ * @param {NewOptions} options - The new options to update the game configuration.
281
+ * @return {void} No return value.
282
+ */
283
+ static updateOptions(options: NewOptions): void;
284
+ /**
285
+ * Resets the game state for testing purposes.
286
+ *
287
+ * Clears all entity and passage registries, disables auto-save, and resets initialization state.
288
+ * This method is intended for use in test environments only.
289
+ *
290
+ * @internal
291
+ */
292
+ static _resetForTesting(): void;
293
+ }
294
+ //# sourceMappingURL=game.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"game.d.ts","sourceRoot":"","sources":["../src/game.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjD,OAAO,EAAe,UAAU,EAAc,OAAO,EAAE,MAAM,UAAU,CAAC;AACxE,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAG5C,OAAO,EAAE,aAAa,EAAY,MAAM,QAAQ,CAAC;AAWjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,IAAI;IACb,OAAO,CAAC,MAAM,CAAC,KAAK,CAEjB;IAEH,OAAO,CAAC,MAAM,CAAC,WAAW,CAAS;IACnC,OAAO,CAAC,MAAM,CAAC,eAAe,CAAS;IACvC,OAAO,CAAC,MAAM,CAAC,SAAS,CAA8C;IACtE,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAyB;IAC5D,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,aAAa,CAAkB;IACvD,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,gBAAgB,CAAO;IAE/C;;;;;OAKG;IACH,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAQhC;;;;;OAKG;IACH,MAAM,CAAC,cAAc,CAAC,GAAG,OAAO,EAAE,KAAK,CAAC,cAAc,CAAC,GAAG,IAAI;IAa9D;;;;;;;OAOG;IACH,MAAM,CAAC,eAAe,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,OAAO,CAAC,GAAG,IAAI;IAsBzD;;;;;OAKG;IACH,MAAM,KAAK,kBAAkB,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAEzD;IAED;;;;;;OAMG;IACH,MAAM,KAAK,cAAc,IAAI,OAAO,GAAG,IAAI,CAO1C;IAED;;;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