narraleaf-react 0.2.0 → 0.2.2

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.
@@ -141,5 +141,5 @@ export declare const PersistentActionTypes: {
141
141
  readonly set: "persistent:set";
142
142
  };
143
143
  export type PersistentActionContentType = {
144
- [K in typeof PersistentActionTypes[keyof typeof PersistentActionTypes]]: K extends "persistent:action" ? any : K extends "persistent:set" ? [string, any] : any;
144
+ [K in typeof PersistentActionTypes[keyof typeof PersistentActionTypes]]: K extends "persistent:action" ? any : K extends "persistent:set" ? [string, unknown | ((value: unknown) => unknown)] : any;
145
145
  };
@@ -3,6 +3,7 @@ import { Image } from "../../elements/displayable/image";
3
3
  import { GameState } from "../../../player/gameState";
4
4
  import type { CalledActionResult } from "../../gameTypes";
5
5
  import { Awaitable } from "../../../../util/data";
6
+ import { ContentNode } from "../../action/tree/actionTree";
6
7
  import { TypedAction } from "../../action/actions";
7
8
  export declare class ImageAction<T extends typeof ImageActionTypes[keyof typeof ImageActionTypes] = typeof ImageActionTypes[keyof typeof ImageActionTypes]> extends TypedAction<ImageActionContentType, T, Image> {
8
9
  static ActionTypes: {
@@ -20,5 +21,7 @@ export declare class ImageAction<T extends typeof ImageActionTypes[keyof typeof
20
21
  readonly initWearable: "image:initWearable";
21
22
  readonly setAppearance: "image:setAppearance";
22
23
  };
24
+ type: T;
25
+ contentNode: ContentNode<ImageActionContentType[T]>;
23
26
  executeAction(state: GameState): CalledActionResult | Awaitable<CalledActionResult, any>;
24
27
  }
@@ -1,5 +1,5 @@
1
1
  import { Game } from "../game";
2
2
  import { GameState } from "../../player/gameState";
3
3
  import { Storable, Namespace } from "../elements/persistent/storable";
4
- import { LiveGame } from "../liveGame";
4
+ import { LiveGame } from "../game/liveGame";
5
5
  export { LiveGame, GameState, Game, Storable, Namespace, };
@@ -11,4 +11,5 @@ export declare class Word<T extends string | DynamicWord | Pausing = string | Dy
11
11
  static bold(text: string | Word): Word;
12
12
  static italic(text: string | Word): Word;
13
13
  constructor(text: T, config?: Partial<WordConfig>);
14
+ toString(): string;
14
15
  }
@@ -13,6 +13,7 @@ export declare class Storable {
13
13
  getNamespace<T extends NameSpaceContent<keyof T> = any>(key: string): Namespace<T>;
14
14
  setNamespace<T extends NameSpaceContent<keyof T> = any>(key: string, namespace: Namespace<T>): this;
15
15
  hasNamespace(key: string): boolean;
16
+ removeNamespace(key: string): this;
16
17
  getNamespaces(): {
17
18
  [key: string]: Namespace<any>;
18
19
  };
@@ -8,17 +8,19 @@ import { Word } from "../elements/character/word";
8
8
  import { DynamicWord, DynamicWordResult } from "../elements/character/sentence";
9
9
  import { LambdaHandler } from "../elements/type";
10
10
  type PersistentContent = {
11
- [key: string]: StorableType;
11
+ [K in string]: StorableType;
12
12
  };
13
13
  type ChainedPersistent<T extends PersistentContent> = Proxied<Persistent<T>, Chained<LogicAction.Actions>>;
14
+ type DynamicPersistentData = {
15
+ [K in string]: StorableType;
16
+ };
14
17
  export declare class Persistent<T extends PersistentContent> extends Actionable<null> {
15
- private namespace;
16
- private defaultContent;
17
18
  constructor(namespace: string, defaultContent: T);
18
19
  /**
19
20
  * @chainable
20
21
  */
21
22
  set<K extends StringKeyOf<T>>(key: K, value: T[K]): ChainedPersistent<T>;
23
+ set<K extends StringKeyOf<T>>(key: K, handler: (value: T[K]) => T[K]): ChainedPersistent<T>;
22
24
  /**
23
25
  * Determine whether the values are equal, can be used in {@link Condition}
24
26
  */
@@ -47,6 +49,10 @@ export declare class Persistent<T extends PersistentContent> extends Actionable<
47
49
  * ```
48
50
  */
49
51
  toWord<K extends StringKeyOf<T>>(key: K): Word<DynamicWord>;
52
+ /**
53
+ * Alias of {@link toWord}
54
+ */
55
+ get<K extends StringKeyOf<T>>(key: K): Word<DynamicWord>;
50
56
  /**
51
57
  * Create a conditional word
52
58
  *
@@ -63,5 +69,14 @@ export declare class Persistent<T extends PersistentContent> extends Actionable<
63
69
  * ```
64
70
  */
65
71
  conditional(condition: Lambda<boolean> | LambdaHandler<boolean>, ifTrue: DynamicWordResult, ifFalse: DynamicWordResult): Word;
72
+ /**
73
+ * Evaluate the JavaScript function and determine whether the result is true
74
+ */
75
+ evaluate<K extends StringKeyOf<T>>(key: K, fn: (value: T[K]) => boolean): Lambda<boolean>;
76
+ }
77
+ /**
78
+ * Only for internal use, don't use this class directly
79
+ */
80
+ export declare class DynamicPersistent extends Persistent<DynamicPersistentData> {
66
81
  }
67
82
  export {};
@@ -7,6 +7,7 @@ import { IImageTransition, ITransition } from "../elements/transition/type";
7
7
  import { Sound, SoundDataRaw, VoiceIdMap, VoiceSrcGenerator } from "../elements/sound";
8
8
  import { TransformDefinitions } from "../elements/transform/type";
9
9
  import { ImageDataRaw, VirtualImageProxy } from "../elements/displayable/image";
10
+ import { Persistent } from "../common/core";
10
11
  import { Chained, Proxied } from "../action/chain";
11
12
  import { RGBColor } from "../common/Utils";
12
13
  import Actions = LogicAction.Actions;
@@ -60,6 +61,7 @@ export type SceneEventTypes = {
60
61
  "event:displayable.init": [];
61
62
  };
62
63
  export declare class Scene extends Constructable<Actions, Scene> implements EventfulDisplayable {
64
+ readonly local: Persistent<any>;
63
65
  constructor(name: string, config?: Partial<ISceneConfig>);
64
66
  /**
65
67
  * Activate the scene
@@ -2,7 +2,7 @@ import { Game } from "../game";
2
2
  import { Actionable } from "../action/actionable";
3
3
  import { GameState } from "../../player/gameState";
4
4
  import type { Storable } from "../elements/persistent/storable";
5
- import { LiveGame } from "../liveGame";
5
+ import { LiveGame } from "../game/liveGame";
6
6
  export interface ScriptCtx {
7
7
  gameState: GameState;
8
8
  game: Game;
@@ -18,6 +18,18 @@ export type TransformersMap = {
18
18
  "fontColor": color;
19
19
  };
20
20
  export declare class Transform<T extends TransformDefinitions.Types = object> {
21
+ /**
22
+ * Go to the left side of the stage
23
+ */
24
+ static left(duration: number, easing?: TransformDefinitions.EasingDefinition): Transform<TransformDefinitions.ImageTransformProps>;
25
+ /**
26
+ * Go to the right side of the stage
27
+ */
28
+ static right(duration: number, easing?: TransformDefinitions.EasingDefinition): Transform<TransformDefinitions.ImageTransformProps>;
29
+ /**
30
+ * Go to the center of the stage
31
+ */
32
+ static center(duration: number, easing?: TransformDefinitions.EasingDefinition): Transform<TransformDefinitions.ImageTransformProps>;
21
33
  /**
22
34
  * @example
23
35
  * ```ts
@@ -0,0 +1,84 @@
1
+ import { EventDispatcher, MultiLock } from "../../../util/data";
2
+ import type { SavedGame } from "../gameTypes";
3
+ import { GameState } from "../../player/gameState";
4
+ import { Storable } from "../elements/persistent/storable";
5
+ import { Game } from "../game";
6
+ import { LiveGameEventHandler, LiveGameEventToken } from "../types";
7
+ import { Character } from "../elements/character";
8
+ import { Sentence } from "../elements/character/sentence";
9
+ type LiveGameEvent = {
10
+ "event:character.prompt": [
11
+ {
12
+ /**
13
+ * The character who says the sentence
14
+ */
15
+ character: Character | null;
16
+ /**
17
+ * The sentence said by the character
18
+ */
19
+ sentence: Sentence;
20
+ /**
21
+ * The text of the sentence
22
+ */
23
+ text: string;
24
+ }
25
+ ];
26
+ "event:menu.choose": [
27
+ {
28
+ /**
29
+ * The sentence selected by the player
30
+ */
31
+ sentence: Sentence;
32
+ /**
33
+ * The text of the sentence
34
+ */
35
+ text: string;
36
+ }
37
+ ];
38
+ };
39
+ export declare class LiveGame {
40
+ static DefaultNamespaces: {
41
+ game: {};
42
+ };
43
+ static GameSpacesKey: {
44
+ readonly game: "game";
45
+ };
46
+ static EventTypes: {
47
+ readonly "event:character.prompt": "event:character.prompt";
48
+ readonly "event:menu.choose": "event:menu.choose";
49
+ };
50
+ game: Game;
51
+ gameLock: MultiLock;
52
+ events: EventDispatcher<LiveGameEvent>;
53
+ getStorable(): Storable;
54
+ /**
55
+ * Serialize the current game state
56
+ *
57
+ * You can use this to save the game state to a file or a database
58
+ *
59
+ * Note: even if you change just a single line of script, the saved game might not be compatible with the new version
60
+ */
61
+ serialize(): SavedGame;
62
+ /**
63
+ * Load a saved game
64
+ *
65
+ * Note: even if you change just a single line of script, the saved game might not be compatible with the new version
66
+ *
67
+ * After calling this method, the current game state will be lost, and the stage will trigger force reset
68
+ */
69
+ deserialize(savedGame: SavedGame): void;
70
+ /**
71
+ * When a character says something
72
+ */
73
+ onCharacterPrompt(fc: LiveGameEventHandler<LiveGameEvent["event:character.prompt"]>): LiveGameEventToken;
74
+ /**
75
+ * When a player chooses a menu
76
+ */
77
+ onMenuChoose(fc: LiveGameEventHandler<LiveGameEvent["event:menu.choose"]>): LiveGameEventToken;
78
+ /**
79
+ * Start a new game
80
+ */
81
+ newGame(): this;
82
+ getGameState(): GameState | undefined;
83
+ }
84
+ export {};
@@ -2,7 +2,7 @@ import type { GameConfig } from "./gameTypes";
2
2
  import { DeepPartial } from "../../util/data";
3
3
  import { LogicAction } from "./action/logicAction";
4
4
  import { ComponentsTypes } from "../player/elements/type";
5
- import { LiveGame } from "./liveGame";
5
+ import { LiveGame } from "./game/liveGame";
6
6
  import { Preference } from "./game/preference";
7
7
  declare enum GameSettingsNamespace {
8
8
  game = "game"
@@ -93,6 +93,18 @@ export type GameConfig = {
93
93
  * The number of actions will be predicted and preloaded
94
94
  */
95
95
  maxPreloadActions: number;
96
+ /**
97
+ * Src of the cursor image, if null, the game will show the default cursor
98
+ */
99
+ cursor: string | null;
100
+ /**
101
+ * Cursor width in pixels
102
+ */
103
+ cursorWidth: number;
104
+ /**
105
+ * Cursor height in pixels
106
+ */
107
+ cursorHeight: number;
96
108
  };
97
109
  elements: {
98
110
  say: {
@@ -62,3 +62,7 @@ export type EventfulDisplayableEvents = {
62
62
  };
63
63
  export interface EventfulDisplayable {
64
64
  }
65
+ export type LiveGameEventHandler<T extends Array<any>> = (...args: T) => void;
66
+ export type LiveGameEventToken = {
67
+ cancel(): void;
68
+ };
@@ -1,3 +1,3 @@
1
1
  import React from "react";
2
2
  import { MenuElementProps } from "../../elements/menu/type";
3
- export default function Menu({ prompt, choices, afterChoose, state, }: Readonly<MenuElementProps>): React.JSX.Element;
3
+ export default function Menu({ prompt, choices, afterChoose, state, words, }: Readonly<MenuElementProps>): React.JSX.Element;
@@ -1,9 +1,13 @@
1
1
  import type { Choice } from "../../../nlcore/elements/menu";
2
2
  import type { GameState } from "../../gameState";
3
3
  import { Sentence } from "../../../nlcore/elements/character/sentence";
4
+ import { Word } from "../../../nlcore/elements/character/word";
5
+ import { Pausing } from "../../../nlcore/elements/character/pause";
6
+ import { Chosen } from "../../type";
4
7
  export interface MenuElementProps {
5
8
  prompt: Sentence;
6
9
  choices: Choice[];
7
- afterChoose: (choice: Choice) => void;
10
+ afterChoose: (choice: Chosen) => void;
8
11
  state: GameState;
12
+ words: Word<Pausing | string>[];
9
13
  }
@@ -1,7 +1,9 @@
1
1
  import React from "react";
2
2
  import { GameState } from "../../gameState";
3
3
  import { Sentence as GameSentence } from "../../../nlcore/elements/character/sentence";
4
- export default function Sentence({ sentence, gameState, useTypeEffect, onCompleted, finished, count, className, }: Readonly<{
4
+ import { Word } from "../../../nlcore/elements/character/word";
5
+ import { Pausing } from "../../../nlcore/elements/character/pause";
6
+ export default function Sentence({ sentence, gameState, useTypeEffect, onCompleted, finished, count, className, words: w, }: Readonly<{
5
7
  sentence: GameSentence;
6
8
  gameState: GameState;
7
9
  useTypeEffect?: boolean;
@@ -9,4 +11,5 @@ export default function Sentence({ sentence, gameState, useTypeEffect, onComplet
9
11
  finished?: boolean;
10
12
  count?: number;
11
13
  className?: string;
14
+ words?: Word<string | Pausing>[];
12
15
  }>): React.JSX.Element;
@@ -1,13 +1,16 @@
1
1
  import type { Character } from "../../../nlcore/elements/character";
2
2
  import type { GameState } from "../../gameState";
3
3
  import { Sentence } from "../../../nlcore/elements/character/sentence";
4
+ import { Word } from "../../../nlcore/elements/character/word";
5
+ import { Pausing } from "../../../nlcore/elements/character/pause";
4
6
  export interface SayElementProps {
5
7
  action: {
6
8
  sentence: Sentence;
7
9
  character: Character | null;
10
+ words: Word<Pausing | string>[];
8
11
  };
9
12
  /**
10
- * Callback function to be called when player triggers the next action
13
+ * Callback function to be called when the player triggers the next action
11
14
  */
12
15
  onClick?: () => void;
13
16
  useTypeEffect?: boolean;
@@ -6,7 +6,7 @@ import clsx from "clsx";
6
6
  import { Game } from "../../nlcore/game";
7
7
  import { GameState } from "../gameState";
8
8
  import { Storable } from "../../nlcore/elements/persistent/storable";
9
- import { LiveGame } from "../../nlcore/liveGame";
9
+ import { LiveGame } from "../../nlcore/game/liveGame";
10
10
  export type Components<T extends Record<string, any>> = (props: Readonly<T>) => React.JSX.Element;
11
11
  export type SayComponent = Components<SayElementProps>;
12
12
  export type MenuComponent = Components<MenuElementProps>;
@@ -15,9 +15,10 @@ import { Sentence } from "../nlcore/elements/character/sentence";
15
15
  import { Text, TextEventTypes } from "../nlcore/elements/displayable/text";
16
16
  import { Logger } from "../../util/logger";
17
17
  import { Story } from "../nlcore/elements/story";
18
+ import { Chosen } from "./type";
18
19
  type PlayerStateElement = {
19
20
  texts: Clickable<TextElement>[];
20
- menus: Clickable<MenuElement, Choice>[];
21
+ menus: Clickable<MenuElement, Chosen>[];
21
22
  displayable: LogicAction.DisplayableElements[];
22
23
  };
23
24
  export type PlayerState = {
@@ -107,16 +108,15 @@ export declare class GameState {
107
108
  getSceneByName(name: string): Scene | null;
108
109
  getStory(): Story;
109
110
  /**
110
- * Dispose the game state
111
+ * Dispose of the game state
111
112
  *
112
- * This is an irreversible action, once disposed, the game state cannot be used again.
113
+ * This is an irreversible action; once disposed of, the game state can't be used again.
113
114
  *
114
- * Do not call this method directly
115
+ * Don't call this method directly
115
116
  */
116
117
  dispose(): void;
117
118
  toData(): PlayerStateData;
118
119
  loadData(data: PlayerStateData, elementMap: Map<string, LogicAction.GameElement>): void;
119
- initScenes(): void;
120
120
  getLastSceneIfNot(scene: Scene | null | void): Scene;
121
121
  private getElementMap;
122
122
  private removeElements;
@@ -1,6 +1,8 @@
1
1
  import { Character } from "../nlcore/elements/character";
2
2
  import { Choice } from "../nlcore/elements/menu";
3
3
  import { Sentence } from "../nlcore/elements/character/sentence";
4
+ import { Word } from "../nlcore/elements/character/word";
5
+ import { Pausing } from "../nlcore/elements/character/pause";
4
6
  export type Clickable<T, U = undefined> = {
5
7
  action: T;
6
8
  onClick: U extends undefined ? () => void : (arg0: U) => void;
@@ -9,8 +11,10 @@ export type TextElement = {
9
11
  character: Character | null;
10
12
  sentence: Sentence;
11
13
  id: string;
14
+ words: Word<Pausing | string>[];
12
15
  };
13
16
  export type MenuElement = {
14
17
  prompt: Sentence;
15
18
  choices: Choice[];
19
+ words: Word<Pausing | string>[];
16
20
  };
@@ -0,0 +1,6 @@
1
+ import React from "react";
2
+ export default function Cursor({ src, width, height, }: Readonly<{
3
+ src: string;
4
+ width: number;
5
+ height: number;
6
+ }>): React.JSX.Element;
@@ -1,7 +1,8 @@
1
1
  import React, { ReactNode } from "react";
2
- export default function Isolated({ children, className, props, style }: Readonly<{
2
+ export default function Isolated({ children, className, props, style, $ref }: Readonly<{
3
3
  children: ReactNode;
4
4
  className?: string;
5
5
  props?: Record<any, any>;
6
6
  style?: React.CSSProperties;
7
+ $ref?: React.RefObject<HTMLDivElement>;
7
8
  }>): React.JSX.Element;
@@ -1 +1,5 @@
1
+ import { Choice } from "../nlcore/elements/menu";
1
2
  export * from "./elements/type";
3
+ export type Chosen = Choice & {
4
+ evaluated: string;
5
+ };