Skip to content

Commit

Permalink
fix: elseifs now work with single variables in conditions (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
liana-p committed Apr 13, 2024
1 parent 3f5f9bf commit 2bd479c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 23 deletions.
18 changes: 12 additions & 6 deletions packages/narrat/src/components/debug/debug-menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ import { useInventory } from '../../stores/inventory-store';
import { resetSave } from '@/utils/save-helpers';
import { vm } from '@/vm/vm';
import DebugJumping from './debug-jumping.vue';
import { InputListener } from '@/stores/inputs-store';
import { InputListener, useInputs } from '@/stores/inputs-store';
import { useRenderingStore } from '@/stores/rendering-store';
import { autoSaveGame, resetGlobalSave } from '@/application/saving';
// import { getAllStates, overrideStates } from '@/data/all-stores';
Expand Down Expand Up @@ -157,12 +157,18 @@ export default defineComponent({
const debugHotkey = getCommonConfig().hotkeys.debugMenu ?? 'd';
const jumpHotkey = getCommonConfig().hotkeys.jumpMenu ?? 'j';
rendering.inputsContainer.addEventListener('keydown', (event) => {
if (!this.jumping) {
if (event.key === debugHotkey) {
this.toggle();
if (!useInputs().isTyping) {
if (!this.jumping) {
if (event.key === debugHotkey) {
this.toggle();
}
if (event.key === jumpHotkey) {
this.jump();
}
}
if (event.key === jumpHotkey) {
this.jump();
if (event.key === 'Escape') {
this.closeJumping();
this.close();
}
}
});
Expand Down
6 changes: 5 additions & 1 deletion packages/narrat/src/dialog-box.vue
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ function submitText() {
function createTextFieldListener() {
if (props.options.textField) {
useInputs().startTyping();
timeout.value = setTimeout(() => {
if (canInteract.value) {
textFieldInputGrabber.value = useInputs().registerInputListener(
Expand All @@ -309,6 +310,7 @@ function createTextFieldListener() {
}
function cleanUpTextFieldListener() {
useInputs().stopTyping();
if (textFieldInputGrabber.value) {
useInputs().unregisterInputListener(textFieldInputGrabber.value);
textFieldInputGrabber.value = null;
Expand Down Expand Up @@ -422,7 +424,9 @@ function endTextAnimation({
pressedSpace,
}: { unmounted?: boolean; pressedSpace?: boolean } = {}) {
useVM().endTextAnimation();
createTextFieldListener();
if (!unmounted) {
createTextFieldListener();
}
setTimeout(() => {
if (navigation.value) {
navigation.value.select(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_conditions:
var hello "test"
if (== $hello false):
"false"
elseif $hello:
"true"
else:
"neither"
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dev_test:
run test_conditions
run test_js
run test_arrays
run test_regex
Expand Down
2 changes: 2 additions & 0 deletions packages/narrat/src/examples/default/scripts/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import animations from './animations.narrat';
import macros from './test_macros.narrat';
import scenes from './test_scenes.narrat';
import js from './test_js.narrat';
import conditions from './conditions.narrat';

export default [
game,
Expand All @@ -24,4 +25,5 @@ export default [
macros,
scenes,
js,
conditions,
];
8 changes: 8 additions & 0 deletions packages/narrat/src/stores/inputs-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ export interface InputsStoreState {
baseInputListener: InputListener;
inGameInputListener: InputListener | null;
inputMode: InputMode;
isTyping: boolean;
}

export const useInputs = defineStore('inputs', {
Expand All @@ -204,6 +205,7 @@ export const useInputs = defineStore('inputs', {
inputStack: [],
baseInputListener: null as any,
inputMode: 'mk' as InputMode,
isTyping: false,
inGameInputListener: null,
}) as InputsStoreState,
actions: {
Expand Down Expand Up @@ -248,6 +250,12 @@ export const useInputs = defineStore('inputs', {
},
});
},
startTyping() {
this.isTyping = true;
},
stopTyping() {
this.isTyping = false;
},
removeInGameInputListener() {
if (this.inGameInputListener) {
this.unregisterInputListener(this.inGameInputListener);
Expand Down
24 changes: 18 additions & 6 deletions packages/narrat/src/vm/commands/if.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { getLine, runConditionCommand } from '@/vm/vm-helpers';
import {
getLine,
isExpression,
isVariable,
runConditionCommand,
} from '@/vm/vm-helpers';
import { CommandPlugin, generateParser } from './command-plugin';
import { Parser } from '@/types/parser';
import { MachineBlock, useVM } from '@/stores/vm-store';
import { parseExpression } from '../vm-parser';
import { runExpression } from '../vm';
import { parseArgument, parseExpression } from '../vm-parser';
import { readVariable, runExpression } from '../vm';

export interface IfOptions {
condition: boolean;
}
export interface ElseIfOptions {
branch: Parser.Branch;
condition: Parser.ParsedExpression;
condition: Parser.Arg;
}
export interface IfStaticOptions {
success: Parser.Branch;
Expand All @@ -25,7 +30,14 @@ export const ifCommand = new CommandPlugin<IfOptions, IfStaticOptions>(
const elseIfResults: boolean[] = [];
for (const elseif of cmd.staticOptions.elseifs) {
const condition = elseif.condition;
const finalCondition = await runExpression(condition);
let finalCondition: any;
if (isExpression(condition)) {
finalCondition = await runExpression(condition);
} else if (isVariable(condition)) {
finalCondition = readVariable(condition as string);
} else {
finalCondition = condition;
}
elseIfResults.push(finalCondition);
}
const newBranch = runConditionCommand(cmd, elseIfResults);
Expand Down Expand Up @@ -79,7 +91,7 @@ export const ifCommand = new CommandPlugin<IfOptions, IfStaticOptions>(
nextLine.branch!,
line,
),
condition: parseExpression(
condition: parseArgument(
ctx.parserContext,
nextLine,
expression[1] as Parser.Expression,
Expand Down
13 changes: 11 additions & 2 deletions packages/narrat/src/vm/vm-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,18 @@ export function parseExpression(
line: Parser.Line,
expression: Parser.Expression,
): Parser.ParsedExpression {
if (!Array.isArray(expression)) {
ctx.error(
line.line,
`Expression should be an array. Something is wrong. ${ctx.fileName}:${ctx.currentLine} - ${line.code}`,
);
}
logger.log(expression);
if (typeof expression[0] !== 'string') {
ctx.error(line.line, `Expression operator should be a string`);
ctx.error(
line.line,
`Expression operator should be a string ${ctx.fileName}:${ctx.currentLine} - ${line.code}`,
);
}
const parsed: Parser.ParsedExpression = {
code: line.code,
Expand Down Expand Up @@ -156,7 +165,7 @@ export function parseExpression(
return parsed;
}

function parseArgument(
export function parseArgument(
ctx: ParserContext,
line: Parser.Line,
argument: Parser.Expression | Parser.Primitive,
Expand Down
21 changes: 13 additions & 8 deletions packages/narrat/src/vm/vm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,7 @@ export async function generateCommand(
finalArg = processText(arg.substring(3));
} else if (isVariable(arg)) {
// This is potentially a variable token
const modifiable = getModifiableDataPinia();
const result = findDataHelperWithoutAutoCreate<any>(modifiable, arg);
if (result) {
const [target, key] = result;
finalArg = target[key];
} else {
finalArg = arg;
}
finalArg = readVariable(arg);
} else {
finalArg = arg;
}
Expand All @@ -165,6 +158,18 @@ export async function generateCommand(
}
}

export function readVariable(arg: string) {
// This is potentially a variable token
const modifiable = getModifiableDataPinia();
const result = findDataHelperWithoutAutoCreate<any>(modifiable, arg);
if (result) {
const [target, key] = result;
return target[key];
} else {
return arg;
}
}

export async function runExpression<ReturnType = any>(
expr: Parser.ParsedExpression,
choices?: DialogChoice[],
Expand Down

0 comments on commit 2bd479c

Please sign in to comment.