I'm making something like a text adventure game in Javascript, in a browser.
I've made almost all of it except the bit of logic that actually handles user input and the programme's response to said input. Currently, I'm a bit stumped on the best way to go about this.
My current idea is a recursive switch
function:
mainLoop("Introduction");
function mainLoop(section) {
switch(section) {
case "Introduction":
// Do stuff with the introductory scene
// Await input
mainLoop("Gather supplies");
break;
case "Gather Supplies":
// Do stuff
mainLoop("Make a decision");
break;
case "Make a decision":
// Do stuff
// Await decision (userDecision)
if(userDecision == "Left") {
mainLoop("Ending 1");
} else {
mainLoop("Ending 2");
}
break;
case "Ending 1":
// Do stuff
break;
case "Ending 2":
// Do stuff
break;
}
}
My issue is that I have no idea how to await user input.
I've considered having a while
loop run forever and only progress when it receives a signal from some handleUserInput()
, but as I understand it, that would take up the whole thread - I couldn't run another secondaryLoop()
with its own while
s alongside the mainLoop()
.
What's the best way to go about this?
EDIT: I should specify that detecting the input itself is already set up. The user types their sentence/command, it's passed to handleUserInput()
which (in this example) would then generate a string corresponding to the relevant case
in the mainloop. If I were to not use this technique, then handleUserInput()
would do whatever the mainloop requires it to do.