NarraLeaf

ストーリー

ゲームのエントリーポイントとなる `Story` クラス。シーンのエントリー、永続化データとサービスの登録、セーブ互換性のためのハッシュ計算を扱う

Story はストーリーを作成するための主要なエントリーポイントです。

import { Story } from 'narraleaf-react';

const story = new Story("story name");
const scene1 = new Scene("scene 1");

story.entry(scene1);

公開メソッド

constructor

  • name: string - ストーリーの名前
  • config: IStoryConfig - IStoryConfig を参照

entry

  • scene: Scene - 最初に実行するシーン
  • 戻り値: this

registerPersistent

永続化オブジェクトをストーリー内で使用できるようにするには、登録する必要があります。

const story = new Story("story name");
const persis = new Persistent</* ... */>("persis", /* ... */);

story.registerPersistent(persistent);

const scene1 = new Scene("scene 1");
scene1.action([
    persis.set(/* ... */, /* ... */)
]);
  • persistent: Persistent<any> - 登録する Persistent
  • 戻り値: this

registerService

サービスを登録します。詳細は Service を参照してください。

const story = new Story(/* ... */);
const gallery = new Gallery(/* ... */);

story.registerService("gallery", gallery);
  • name: string - サービスの名前
  • service: Service<any> - 登録する Service
  • 戻り値: this

getService<T extends Service>

名前を指定してサービスを取得します。サービスが見つからない場合はエラーをスローします。

const game = useGame();
const liveGame = game.getLiveGame();

const gallery = liveGame.story?.getService<Gallery>("gallery");
  • name: string - サービスの名前
  • 戻り値: T

createPersistent<T extends PersistentContent>

Persistent を作成し、ストーリーに登録します。

const persistent = story.createPersistent("playerData", {
  name: "persistent",
});

// これは次と等価
const persistent = new Persistent("playerData", {
  name: "persistent",
});
story.registerPersistent(persistent);
  • namespace: string - 永続化オブジェクトの名前
  • defaultContent: T
  • 戻り値: Persistent<T>

hash

ストーリーの 64 ビットハッシュを返します。

このハッシュは、テキスト内容ではなく、ストーリーのロジックと操作の順序に基づいて計算されます。具体的には次の通りです:

  • ハッシュは、ストーリー内の操作の論理構造と順序を反映します
  • 操作の順序(アニメーションやジャンプなど)を変更すると、ハッシュ値が変わります
  • テキスト内容(セリフや説明文など)を変更しても、ハッシュ値には影響しません
  • stricttrue の場合、ハッシュには文字列化されたラムダ関数も含まれます

このハッシュは主に、セーブデータ間のストーリー互換性を確認するために使用されます。

const story = new Story("story name");
const hash = story.hash(); // 基本のハッシュ
const strictHash = story.hash(true); // 厳格モードのハッシュ
  • strict: boolean - ハッシュ計算に厳格モードを使用するかどうか(デフォルト: false
  • 戻り値: string - ストーリーの 64 ビットハッシュ

stringify

ストーリーを文字列表現に変換します。これは主に、内部でのハッシュ計算に使用されます。

文字列化の挙動は strict パラメーターによって変わります:

  • strictfalse(デフォルト)の場合、基本的な文字列表現を返します
  • stricttrue の場合、出力に文字列化されたラムダ関数が含まれます
const story = new Story("story name");
const str = story.stringify(); // 基本的な文字列表現
const strictStr = story.stringify(true); // 厳格モードの文字列表現
  • strict: boolean - 文字列化に厳格モードを使用するかどうか(デフォルト: false
  • 戻り値: string - ストーリーの文字列表現

このページの目次