boardgame-core/src/samples/slay-the-spire-like/system/combat/prompts.ts

37 lines
1.3 KiB
TypeScript

import { createPromptDef } from "@/core/game";
import {CombatGameContext} from "./types";
export const prompts = {
mainAction: createPromptDef<[string, string?]>(
"main-action <cardId:string> [targetId:string]",
"选择卡牌并指定目标"
),
};
export async function promptMainAction(game: CombatGameContext){
return await game.prompt(prompts.mainAction, (cardId, targetId) => {
if(cardId === 'end-turn') return {
action: 'end-turn' as 'end-turn'
};
const exists = game.value.player.deck.hand.includes(cardId);
if(!exists) throw `卡牌"${cardId}"不在手牌中`;
const card = game.value.player.deck.cards[cardId];
const {targetType} = card.cardData;
if(targetType === 'single'){
if(!targetId) throw `请指定目标`;
const target = game.value.enemies.find(e => e.id === targetId);
if(!target) throw `目标"${targetId}"不存在`;
if(!target.isAlive) throw `目标"${targetId}"已死亡`;
}else if(targetType === 'none'){
if(targetId) throw `目标"${targetId}"无效`;
}
return {
action: 'play' as 'play',
cardId,
targetId
};
});
}