"use strict"; var text = document.getElementById("textDisplay"); var controls = document.getElementById("controlsDisplay"); var States = { cell0: { text: "You are in a prison cell, and you want to escape. There are some dirty sheets on the bed, a mirror on the wall, and the door is locked from the outside.", controls: "[S to view Sheets, M to view Mirror, L to view Lock]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyM') { CurrentState = States.mirror; } else if (buttonCode == 'KeyS' || buttonCode == 'Space') { CurrentState = States.sheets0; } else if (buttonCode == 'KeyL') { CurrentState = States.lock0; } else if (buttonCode == 'Key1') { // Shhh it's secret CurrentState = States.challenge; } } }, challenge: { text: "Your challenge: Add your own room to this game. Study the javascript above and set up a room for your player to step into.", controls: "[Press E to Enter your room - <i>Make sure you set up the button code first!</i>]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyE') { // These are comments, remove the slashes to let the code run // CurrentState = States.YOURCHALLENGEROOMNAME; } } }, YOURCHALLENGEROOMNAME: { //Describe what is in your room here text: "Describe what is in your room here", //Describe your controls here - use a single letter for simplicity controls: "[Press E to go to next room, R to Return to Challenge]", buttonPress: function(buttonCode) { // Setup keys here, make sure they match your instructions! if (buttonCode == 'KeyE') { //CurrentState = States.AnyOtherRoom; } else if (buttonCode == 'KeyR') { CurrentState = States.challenge; } } }, cell1: { text: "You are still in your cell, and you STILL want to escape! There are some dirty sheets on the bed, a mark where the mirror was, and that pesky door is still there, and firmly locked!", controls: "[S to view Sheets, L to view Lock]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyL') { CurrentState = States.lock1; } else if (buttonCode == 'KeyS') { CurrentState = States.sheets1; } } }, floor: { text: "Rummagaing around on the dirty floor, you find a hairclip.", controls: "[R to Return to standing, or T to Take the hairclip]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.corridor0; } else if (buttonCode == 'KeyT') { CurrentState = States.corridor1; } } }, sheets0: { text: "You can't believe you sleep in these things. Surely it's time somebody changed them. The pleasures of prison life I guess!", controls: "[R to Return to cell]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.cell0; } } }, sheets1: { text: "Holding a mirror in your hand doesn't make the sheets look any better", controls: "[R to Return to cell]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.cell1; } } }, lock0: { text: "This is one of those button locks. You have no idea what the combination is. You wish you could somehow see where the dirty fingerprints were, maybe that would help.", controls: "[R to Return to cell]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.cell0; } } }, lock1: { text: "You carefully put the mirror through the bars, and turn it round so you can see the lock. You can just make out fingerprints around the buttons. You press the dirty buttons, and hear a click.", controls: "[O to Open, R to Return to your cell]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyO') { CurrentState = States.corridor0; } else if (buttonCode == 'KeyR') { CurrentState = States.cell1; } } }, mirror: { text: "The dirty old mirror on the wall seems loose.", controls: "[T to Take mirror, R to Return to cell]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyT') { CurrentState = States.cell1; } else if (buttonCode == 'KeyR') { CurrentState = States.cell0; } } }, corridor0: { text: "You're out of your cell, but not out of trouble. You are in the corridor, there's a closet and some stairs leading to the courtyard. There's also various detritus on the floor", controls: "[C to view the Closet, F to inspect the Floor, S to climb the Stairs]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyC') { CurrentState = States.closetDoor; } else if (buttonCode == 'KeyF') { CurrentState = States.floor; } else if (buttonCode == 'KeyS') { CurrentState = States.stairs0; } } }, corridor1: { text: "Still in the corridor. Floor still dirty. Hairclip in hand. Now what? You wonder if that lock on the closet would succumb to to some lock-picking?", controls: "[P to Pick the lock, C to Climb the stairs]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyP') { CurrentState = States.inCloset; } else if (buttonCode == 'KeyC') { CurrentState = States.stairs1; } } }, corridor2: { text: "Back in the corridor, having declined to dress-up as a cleaner.", controls: "[C to revisit the Closet, S to climb the Stairs]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyC') { CurrentState = States.inCloset; } else if (buttonCode == 'KeyS') { CurrentState = States.stairs2; } } }, corridor3: { text: "You're standing back in the corridor, now convincingly dressed as a cleaner. You strongly consider the run for freedom.", controls: "[C to Climb the stairs, or U to Undress]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyU') { CurrentState = States.inCloset } else if (buttonCode == 'KeyC') { CurrentState = States.courtyard; } } }, stairs0: { text: "You start walking up the stairs towards the outside light. You realise it's not break time, and you'll be caught immediately. You slither back down the stairs and reconsider.", controls: "[R to Return to corridor]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.corridor0; } } }, stairs1: { text: "Unfortunately weilding a puny hairclip hasn't given you the confidence to walk out into a courtyard surrounded by armed guards!", controls: "[R to Retreat down the stairs]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.corridor1; } } }, stairs2: { text: "You feel smug for picking the closet door open, and are still armed with a hairclip (now badly bent). Even these achievements together don't give you the courage to climb up the stairs to your death!", controls: "[R to Return to the corridor]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.corridor2; } } }, courtyard: { text: "You walk through the courtyard dressed as a cleaner. The guard tips his hat at you as you waltz past, claiming your freedom. You heart races as you walk into the sunset.", controls: "[P to Play again, C to try a real Challenge]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyP') { CurrentState = States.cell0; } else if (buttonCode == 'KeyC') { CurrentState = States.challenge; } } }, closetDoor: { text: "You are looking at a closet door, unfortunately it's locked. Maybe you could find something around to help enourage it open?", controls: "[Press R to Return to corridor]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyR') { CurrentState = States.corridor0; } } }, inCloset: { text: "Inside the closet you see a cleaner's uniform that looks about your size! Seems like your day is looking-up", controls: "[D to Dress up, R to Return to the corridor]", buttonPress: function(buttonCode) { if (buttonCode == 'KeyD') { CurrentState = States.corridor3; } else if (buttonCode == 'KeyR') { CurrentState = States.corridor2; } } }, }; // Initial State var CurrentState = States.cell0; updateText(); // Event Handler $(document).keypress(function(e) { var buttonVal = "Key" + String.fromCharCode(e.which).toUpperCase(); CurrentState.buttonPress(buttonVal); updateText(); }); function updateText() { var replacePattern = /\b[A-Z0-9]\b/g; var styledText = CurrentState.controls.replace(replacePattern, function(match, text) { return ('<span class="actionText">' + match + '</span>') }); text.innerHTML = CurrentState.text; controls.innerHTML = styledText; }

!