コードフレームワーク
レシピ: リソースを消費する
リソースがなくなったら何もしないアクションを書き、両方のケースをテストします。
目標
Keeper が火花を 3 回打てるようにします。4 回目は何も起こりません。
変更内容
rules/spark.ts
export function spark(self: ElementRef): void {
const charges: number = element.var(self, "charges") as number;
if (charges > 0) {
ledger.add(self, "charges", -1);
}
}- 2 行目 リソースを読みます。
- 3-5 行目 残っている間だけ 1 つ消費します。残っていなければ、アクションは何も書き込みません。
スキーマでアクションを宣言します。
schemas/keeper.json
"actions": [
{ "id": "hurt", "name": "Take damage", "source": "rules/hurt.ts" },
{ "id": "heal", "name": "Heal", "source": "rules/heal.ts" },
{ "id": "spark", "name": "Strike a spark", "source": "rules/spark.ts" }
],- 13 行目 新しいアクションです。
コンストラクタは、開始値 3 の charges を返します。
rules/keeper.ts
export function construct(
name: string,
max_hp: number,
): { name: string; max_hp: number; hp: number; charges: number } {
return { name: name, max_hp: max_hp, hp: max_hp, charges: 3 };
}- 5-6 行目 charges は、開始値 3 の保存フィールドです。
フレームの dataContract にアクションのエイリアスを加え、それを使うボタンも加えます。そうしないと、プレイヤーはシートからアクションを使えません。
layouts/keeper.json
{ "name": "spark", "functionRef": "spark" }- 20 行目 アクションのエイリアスです。
layouts/keeper.json
{ "kind": "button", "label": "Strike a spark", "actionRef": "spark" }- 34 行目 それを呼び出すボタンです。
テストする
scenarios/spark.json
{
"id": "spend-all-charges",
"framework": "keeper",
"parameters": { "name": "Mira", "max_hp": 10 },
"expectedValues": { "name": "Mira", "max_hp": 10, "hp": 10, "charges": 3 },
"actions": [
{
"action": "spark",
"parameters": {},
"expectedWrites": [
{ "op": "ledger.add", "key": "charges", "value": -1 }
]
},
{
"action": "spark",
"parameters": {},
"expectedWrites": [
{ "op": "ledger.add", "key": "charges", "value": -1 }
]
},
{
"action": "spark",
"parameters": {},
"expectedWrites": [
{ "op": "ledger.add", "key": "charges", "value": -1 }
]
},
{ "action": "spark", "parameters": {}, "expectedWrites": [] }
]
}- 7-27 行目 3 回の火花で、それぞれ 1 チャージを消費します。
- 28 行目 4 回目はチャージがないので、書き込みなしを期待します。
manifest/project.json
{
"formatVersion": "fabled.game-project/v1",
"projectId": "game-your-game-id",
"projectKind": "GAME",
"compilerProfile": "native-game-v1",
"name": "Lantern Keepers",
"entryFramework": "keeper",
"frameworks": ["schemas/keeper.json"],
"scenarios": [
"scenarios/new-keeper.json",
"scenarios/heal.json",
"scenarios/spark.json"
]
}- 12 行目 新しいシナリオファイルです。
新しいシナリオファイルをマニフェストに載せます。ほかの 2 つのシナリオの期待値にも charges を加えます。

