@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.
- package/README.md +744 -0
- package/dist/baseGameObject.d.ts +90 -0
- package/dist/baseGameObject.d.ts.map +1 -0
- package/dist/baseGameObject.js +109 -0
- package/dist/baseGameObject.js.map +1 -0
- package/dist/constants.d.ts +12 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +12 -0
- package/dist/constants.js.map +1 -0
- package/dist/game.d.ts +294 -0
- package/dist/game.d.ts.map +1 -0
- package/dist/game.js +489 -0
- package/dist/game.js.map +1 -0
- package/dist/helpers.d.ts +2 -0
- package/dist/helpers.d.ts.map +1 -0
- package/dist/helpers.js +6 -0
- package/dist/helpers.js.map +1 -0
- package/dist/hooks/index.d.ts +4 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +4 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/useCurrentPassage.d.ts +10 -0
- package/dist/hooks/useCurrentPassage.d.ts.map +1 -0
- package/dist/hooks/useCurrentPassage.js +17 -0
- package/dist/hooks/useCurrentPassage.js.map +1 -0
- package/dist/hooks/useGameEntity.d.ts +21 -0
- package/dist/hooks/useGameEntity.d.ts.map +1 -0
- package/dist/hooks/useGameEntity.js +70 -0
- package/dist/hooks/useGameEntity.js.map +1 -0
- package/dist/hooks/useGameIsStarted.d.ts +12 -0
- package/dist/hooks/useGameIsStarted.d.ts.map +1 -0
- package/dist/hooks/useGameIsStarted.js +18 -0
- package/dist/hooks/useGameIsStarted.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +8 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +36 -0
- package/dist/logger.js.map +1 -0
- package/dist/options.d.ts +13 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +15 -0
- package/dist/options.js.map +1 -0
- package/dist/passages/interactiveMap/fabric.d.ts +4 -0
- package/dist/passages/interactiveMap/fabric.d.ts.map +1 -0
- package/dist/passages/interactiveMap/fabric.js +3 -0
- package/dist/passages/interactiveMap/fabric.js.map +1 -0
- package/dist/passages/interactiveMap/index.d.ts +4 -0
- package/dist/passages/interactiveMap/index.d.ts.map +1 -0
- package/dist/passages/interactiveMap/index.js +4 -0
- package/dist/passages/interactiveMap/index.js.map +1 -0
- package/dist/passages/interactiveMap/interactiveMap.d.ts +89 -0
- package/dist/passages/interactiveMap/interactiveMap.d.ts.map +1 -0
- package/dist/passages/interactiveMap/interactiveMap.js +103 -0
- package/dist/passages/interactiveMap/interactiveMap.js.map +1 -0
- package/dist/passages/interactiveMap/types.d.ts +822 -0
- package/dist/passages/interactiveMap/types.d.ts.map +1 -0
- package/dist/passages/interactiveMap/types.js +2 -0
- package/dist/passages/interactiveMap/types.js.map +1 -0
- package/dist/passages/passage.d.ts +57 -0
- package/dist/passages/passage.d.ts.map +1 -0
- package/dist/passages/passage.js +64 -0
- package/dist/passages/passage.js.map +1 -0
- package/dist/passages/story/fabric.d.ts +4 -0
- package/dist/passages/story/fabric.d.ts.map +1 -0
- package/dist/passages/story/fabric.js +3 -0
- package/dist/passages/story/fabric.js.map +1 -0
- package/dist/passages/story/index.d.ts +5 -0
- package/dist/passages/story/index.d.ts.map +1 -0
- package/dist/passages/story/index.js +5 -0
- package/dist/passages/story/index.js.map +1 -0
- package/dist/passages/story/start.d.ts +14 -0
- package/dist/passages/story/start.d.ts.map +1 -0
- package/dist/passages/story/start.js +22 -0
- package/dist/passages/story/start.js.map +1 -0
- package/dist/passages/story/story.d.ts +84 -0
- package/dist/passages/story/story.d.ts.map +1 -0
- package/dist/passages/story/story.js +88 -0
- package/dist/passages/story/story.js.map +1 -0
- package/dist/passages/story/types.d.ts +911 -0
- package/dist/passages/story/types.d.ts.map +1 -0
- package/dist/passages/story/types.js +2 -0
- package/dist/passages/story/types.js.map +1 -0
- package/dist/passages/types/index.d.ts +3 -0
- package/dist/passages/types/index.d.ts.map +1 -0
- package/dist/passages/types/index.js +2 -0
- package/dist/passages/types/index.js.map +1 -0
- package/dist/passages/widget.d.ts +62 -0
- package/dist/passages/widget.d.ts.map +1 -0
- package/dist/passages/widget.js +66 -0
- package/dist/passages/widget.js.map +1 -0
- package/dist/saves/constants.d.ts +17 -0
- package/dist/saves/constants.d.ts.map +1 -0
- package/dist/saves/constants.js +17 -0
- package/dist/saves/constants.js.map +1 -0
- package/dist/saves/db.d.ts +119 -0
- package/dist/saves/db.d.ts.map +1 -0
- package/dist/saves/db.js +231 -0
- package/dist/saves/db.js.map +1 -0
- package/dist/saves/helpers.d.ts +28 -0
- package/dist/saves/helpers.d.ts.map +1 -0
- package/dist/saves/helpers.js +84 -0
- package/dist/saves/helpers.js.map +1 -0
- package/dist/saves/hooks/index.d.ts +10 -0
- package/dist/saves/hooks/index.d.ts.map +1 -0
- package/dist/saves/hooks/index.js +10 -0
- package/dist/saves/hooks/index.js.map +1 -0
- package/dist/saves/hooks/useDeleteAllSlots.d.ts +18 -0
- package/dist/saves/hooks/useDeleteAllSlots.d.ts.map +1 -0
- package/dist/saves/hooks/useDeleteAllSlots.js +18 -0
- package/dist/saves/hooks/useDeleteAllSlots.js.map +1 -0
- package/dist/saves/hooks/useDeleteGame.d.ts +22 -0
- package/dist/saves/hooks/useDeleteGame.d.ts.map +1 -0
- package/dist/saves/hooks/useDeleteGame.js +33 -0
- package/dist/saves/hooks/useDeleteGame.js.map +1 -0
- package/dist/saves/hooks/useExportSaves.d.ts +27 -0
- package/dist/saves/hooks/useExportSaves.d.ts.map +1 -0
- package/dist/saves/hooks/useExportSaves.js +54 -0
- package/dist/saves/hooks/useExportSaves.js.map +1 -0
- package/dist/saves/hooks/useImportSaves.d.ts +29 -0
- package/dist/saves/hooks/useImportSaves.d.ts.map +1 -0
- package/dist/saves/hooks/useImportSaves.js +108 -0
- package/dist/saves/hooks/useImportSaves.js.map +1 -0
- package/dist/saves/hooks/useLastLoadGame.d.ts +39 -0
- package/dist/saves/hooks/useLastLoadGame.d.ts.map +1 -0
- package/dist/saves/hooks/useLastLoadGame.js +72 -0
- package/dist/saves/hooks/useLastLoadGame.js.map +1 -0
- package/dist/saves/hooks/useLoadGame.d.ts +22 -0
- package/dist/saves/hooks/useLoadGame.d.ts.map +1 -0
- package/dist/saves/hooks/useLoadGame.js +40 -0
- package/dist/saves/hooks/useLoadGame.js.map +1 -0
- package/dist/saves/hooks/useRestartGame.d.ts +20 -0
- package/dist/saves/hooks/useRestartGame.d.ts.map +1 -0
- package/dist/saves/hooks/useRestartGame.js +29 -0
- package/dist/saves/hooks/useRestartGame.js.map +1 -0
- package/dist/saves/hooks/useSaveGame.d.ts +22 -0
- package/dist/saves/hooks/useSaveGame.d.ts.map +1 -0
- package/dist/saves/hooks/useSaveGame.js +34 -0
- package/dist/saves/hooks/useSaveGame.js.map +1 -0
- package/dist/saves/hooks/useSaveSlots.d.ts +45 -0
- package/dist/saves/hooks/useSaveSlots.d.ts.map +1 -0
- package/dist/saves/hooks/useSaveSlots.js +42 -0
- package/dist/saves/hooks/useSaveSlots.js.map +1 -0
- package/dist/saves/index.d.ts +4 -0
- package/dist/saves/index.d.ts.map +1 -0
- package/dist/saves/index.js +3 -0
- package/dist/saves/index.js.map +1 -0
- package/dist/saves/types.d.ts +52 -0
- package/dist/saves/types.d.ts.map +1 -0
- package/dist/saves/types.js +2 -0
- package/dist/saves/types.js.map +1 -0
- package/dist/storage.d.ts +124 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +229 -0
- package/dist/storage.js.map +1 -0
- package/dist/tests/game.test.d.ts +2 -0
- package/dist/tests/game.test.d.ts.map +1 -0
- package/dist/tests/game.test.js +602 -0
- package/dist/tests/game.test.js.map +1 -0
- package/dist/tests/interactiveMap.test.d.ts +2 -0
- package/dist/tests/interactiveMap.test.d.ts.map +1 -0
- package/dist/tests/interactiveMap.test.js +1003 -0
- package/dist/tests/interactiveMap.test.js.map +1 -0
- package/dist/tests/storage.test.d.ts +2 -0
- package/dist/tests/storage.test.d.ts.map +1 -0
- package/dist/tests/storage.test.js +328 -0
- package/dist/tests/storage.test.js.map +1 -0
- package/dist/tests/story.test.d.ts +2 -0
- package/dist/tests/story.test.d.ts.map +1 -0
- package/dist/tests/story.test.js +698 -0
- package/dist/tests/story.test.js.map +1 -0
- package/dist/types.d.ts +19 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- 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,MAAM,OAAO,cAAc;IACf,MAAM,CAAC,QAAQ,GAAa,IAAI,CAAC;IAEzC;;;OAGG;IACM,EAAE,CAAS;IAEpB;;;OAGG;IACO,UAAU,CAAgB;IAEpC;;;;;;OAMG;IACH,YAAY,KAAgD;QACxD,IAAI,CAAC,EAAE,GAAG,KAAK,CAAC,EAAE,CAAC;QACnB,IAAI,CAAC,UAAU,GAAG,KAAK,CAAC,SAAS,IAAK,EAAoB,CAAC;QAC3D,IAAI,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC;IAC9B,CAAC;IAED;;;;OAIG;IACH,IAAI,SAAS;QACT,OAAO,IAAI,CAAC,UAAU,CAAC;IAC3B,CAAC;IAED;;;;;OAKG;IACH,IAAY,IAAI;QACZ,OAAO,GAAG,cAAc,CAAC,QAAQ,GAAG,IAAI,CAAC,EAAE,EAAc,CAAC;IAC9D,CAAC;IAED;;;;;;;;;;OAUG;IACH,IAAI;QACA,MAAM,eAAe,GAAG,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACpD,IAAI,eAAe,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC7B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,CAAkB,CAAC,CAAC;QACxE,CAAC;aAAM,CAAC;YACJ,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,UAAU,EAAE,CAAC;gBAChC,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;YAChC,CAAC;QACL,CAAC;IACL,CAAC;IAED;;;;;;;;;;OAUG;IACH,IAAI;QACA,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC;IAC3D,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export declare const STORAGE_SYSTEM_PATH: "$._system";
|
|
2
|
+
export declare const SYSTEM_PASSAGE_NAMES: {
|
|
3
|
+
/**
|
|
4
|
+
* 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,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