const textElement = document.getElementById('text') const optionButtonsElement = document.getElementById('option-buttons') let state = {} function startGame() { state = {} showTextNode(1) } function showTextNode(textNodeIndex) { const textNode = textNodes.find(textNode => textNode.id === textNodeIndex) textElement.innerText = textNode.text while (optionButtonsElement.firstChild) { optionButtonsElement.removeChild(optionButtonsElement.firstChild) } textNode.options.forEach(option => { if (showOption(option)) { const button = document.createElement('button') button.innerText = option.text button.classList.add('btn') button.addEventListener('click', () => selectOption(option)) optionButtonsElement.appendChild(button) } }) } function showOption(option) { return option.requiredState == null || option.requiredState(state) } function selectOption(option) { const nextTextNodeId = option.nextText if (nextTextNodeId <= 0) { return startGame() } state = Object.assign(state, option.setState) showTextNode(nextTextNodeId) } const textNodes = [ { id: 1, text: "You find yourself on the foot of an active volcano. You look down and see a cardboard sign lying by your feet with the message 'We are the grandchildren of the witches you were not able to burn.' Hm. What do you do?", options: [ { text: "become and ally", setState: { blueGoo: true, ally: true }, nextText: 2 }, { text: "doubt the kind", setState: { blueGoo: true, ally: false }, nextText: 3 }, ] }, { id: 2, text: 'You set out to find the people who wrote the sign. You follow a narrow path into a dense forest, where you see a group of elderly women weaving.', options: [ { text: 'Ask them what they are weaving.', requiredState: (currentState) => currentState.blueGoo, setState: { blueGoo: false, sword: true }, nextText: 4 }, { text: "Keep on walking.", requiredState: (currentState) => currentState.blueGoo, setState: { blueGoo: false}, nextText: 5 } ] }, { id: 3, text: "You walk on a concrete road towards a large building. It has 'FACTS ONLY' engraved on top of a massive wooden door", options: [ { text: 'You ring the bell, waiting for someone to let you in.', nextText: 31 }, { text: 'Go right', nextText: 32 }, { text: 'Keep on walking straight forwards', nextText: 33 } ] }, { id: 100, text: 'You are getting more tired by the minute. Why were you here again?', options: [ { text: 'Restart', nextText: -1 } ] }, { id: 5, text: 'As you are walking, the fauna is getting denser and wilder by the minute. Suddenly, a gate made of white stone catches your attention', options: [ { text: 'Restart', nextText: 51 } ] }, { id: 6, text: 'You awake in front of a well.', options: [ { text: 'Nothing matters so you jump.', nextText: 7 } ] }, { id: 7, text: 'At the bottom of the well, there are two doors. The doors are guarded by dogs with eyes the size of windmills.', options: [ { text: 'Try to run', nextText: 8 }, { text: 'Shoot at it with your gun', requiredState: (currentState) => currentState.sword, nextText: 9 }, { text: 'Hide behind your shield', requiredState: (currentState) => currentState.shield, nextText: 10 }, { text: 'You calmy approach it, trying to pet his head', requiredState: (currentState) => currentState.blueGoo, nextText: 11 } ] }, { id: 8, text: 'Your attempts to run are in vain and the monster easily catches.', options: [ { text: 'Restart', nextText: -1 } ] }, { id: 9, text: 'You foolishly thought this monster could be slain with the shot of a gun. The doors turn into stonewall in front of your eyes.', options: [ { text: 'Restart', nextText: -1 } ] }, { id: 10, text: 'The monster laughed as you hid behind your shield and ate you.', options: [ { text: 'Restart', nextText: -1 } ] }, { id: 11, text: 'The tension rises as the monster stares back at you, growling and drooling. It smell your hand and with a big POOF, it turns into a fluffy key.', options: [ { text: 'Congratulations. Play Again.', nextText: -1 } ] } ] startGame()