Skip to content

Commit

Permalink
feat(choices): choices with both skill checks and ifs working (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
liana-p committed Jun 11, 2023
1 parent 0c54914 commit 227d054
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
17 changes: 15 additions & 2 deletions packages/narrat/examples/games/default/scripts/default.nar
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ main:
// if (== $global.counter 2):
// unlock_achievement win_game
// talk player idle "Global counter is %{$global.counter}"
set data.n 5
jump quest_demo

test_roll_if:
set data.n 2
choice:
talk player idle "what will u do"
roll someTest agility 50 "test roll" repeatable if (== $data.n 2):
success:
talk player idle "success"
failure:
talk player idle "failure"
"other option":
talk player idle "other option"

test_elseif:
if (== $data.n 1):
"Test 1"
elseif (== $data.n 2):
Expand All @@ -17,7 +31,6 @@ main:
"test 5"
else:
"test else"
jump quest_demo

achievements_demo:
talk helper idle "Let's play rock paper scissors!"
Expand Down
42 changes: 40 additions & 2 deletions packages/narrat/src/vm/commands/choice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
import { ParserContext } from '../vm-parser';
import { MachineBlock, useVM } from '@/stores/vm-store';
import { useSkills } from '@/stores/skills';
import { commandRuntimeError } from './command-helpers';

export const runChoice: CommandRunner<
ChoiceOptions,
Expand Down Expand Up @@ -257,8 +258,45 @@ export const choicePromptCommandPlugin = new CommandPlugin<ChoicePromptOptions>(
const difficulty = args[3] as number;
const skillText = args[4] as string;
let mode: any = false;
let hasCondition: any = false;
let condition: undefined | boolean;
if (args.length > 5) {
mode = args[5] as string;
const nextArg = args[5];
if (nextArg === 'if') {
// There's an if but no optional mode.
hasCondition = true;
if (args.length < 7) {
// Not enough arguments
commandRuntimeError(
cmd,
`Missing condition argument after "if" in choice with a skill check`,
);
}
condition = args[6] as boolean;
} else {
// There's an optional mode
mode = args[5] as string;
if (args.length > 6) {
if (args[6] === 'if') {
// There's an optional mode and also an if
hasCondition = true;
if (args.length < 8) {
// optional mode + if but not enough arguments
commandRuntimeError(
cmd,
`Missing condition argument after "if" in choice with a skill check`,
);
}
condition = args[7] as boolean;
} else {
// There's some unknown argument after the optional mode
commandRuntimeError(
cmd,
`Invalid argument after skill check mode: ${args[6]}. The next argument can only be an if condition.`,
);
}
}
}
}
let hideAfterRoll = false;
let repeatable = false;
Expand All @@ -269,7 +307,7 @@ export const choicePromptCommandPlugin = new CommandPlugin<ChoicePromptOptions>(
repeatable = true;
}
const state = useSkills().getSkillCheck(skillCheckId);
if (state.hidden) {
if (state.hidden || (hasCondition && !condition)) {
return {
text: null,
};
Expand Down

0 comments on commit 227d054

Please sign in to comment.