Skip to content

Instantly share code, notes, and snippets.

@CAD97
Created May 15, 2015 12:28
Show Gist options
  • Save CAD97/80ac9c7687ccd13967a3 to your computer and use it in GitHub Desktop.
Save CAD97/80ac9c7687ccd13967a3 to your computer and use it in GitHub Desktop.
My implementation of the Udemy Unity Course "Text101" game.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class TextController : MonoBehaviour {
#region Unity editor fields
[SerializeField]
private State startingState;
#endregion
#region Components
private Text text;
#endregion
#region Local variables
private enum State {cell, sheets_0, mirror, lock_0, sheets_1, cell_mirror, lock_1,
corridor_0, stairs_0, floor, closet_door, stairs_1, corridor_1,
in_closet, stairs_2, corridor_2, courtyard, corridor_3};
private State state;
#endregion
void Awake () {
text = GetComponent<Text>();
}
void Start () {
state = startingState;
}
void Update () {
switch(state) {
case State.cell: state_cell(); break;
case State.sheets_0: state_sheets_0(); break;
case State.mirror: state_mirror(); break;
case State.lock_0: state_lock_0(); break;
case State.sheets_1: state_sheets_1(); break;
case State.cell_mirror: state_cell_mirror(); break;
case State.lock_1: state_lock_1(); break;
case State.corridor_0: state_corridor_0(); break;
case State.stairs_0: state_stairs_0(); break;
case State.floor: state_floor(); break;
case State.closet_door: state_closet_door(); break;
case State.stairs_1: state_stairs_1(); break;
case State.corridor_1: state_corridor_1(); break;
case State.in_closet: state_in_closet(); break;
case State.stairs_2: state_stairs_2(); break;
case State.corridor_2: state_corridor_2(); break;
case State.courtyard: state_courtyard(); break;
case State.corridor_3: state_corridor_3(); break;
default: throw new MissingReferenceException("State not implemented");
}
}
#region State handler methods
void state_cell() {
text.text = "You wake up in a cell, with no idea how you got there. " +
"The only interesting things in the room are the bed's dirty " +
"sheets, a mirror on the wall, and the door, which will " +
"definately be locked." +
"\n\n" +
"Press 'S' to examine the Sheets, 'M' to examine the Mirror, " +
"or 'L' to try the Locked door.";
if (Input.GetKeyDown(KeyCode.S)) {state = State.sheets_0;}
else if (Input.GetKeyDown(KeyCode.M)) {state = State.mirror;}
else if (Input.GetKeyDown(KeyCode.L)) {state = State.lock_0;}
}
void state_sheets_0() {
text.text = "There isn't anything special about these sheets. They're " +
"standard issue, after all." +
"\n\n" +
"Press 'R' to Return.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.cell;}
}
void state_mirror() {
text.text = "There isn't anything special about the mirror -- wait! " +
"Pulling on the side of the mirror, it swings into the room, " +
"revealing a small hole in the wall with a key in it. " +
"This may come in handy." +
"\n\n" +
"Press 'R' to Return or 'T' to Take the key.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.cell;}
else if (Input.GetKeyDown(KeyCode.T)) {state = State.cell_mirror;}
}
void state_lock_0() {
text.text = "There is no handle on the door. However, the keyhole is " +
"visible from inside. If only you had some way to " +
"find a key..." +
"\n\n" +
"Press 'R' to Return.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.cell;}
}
void state_sheets_1() {
text.text = "How do you expect to use a key on bedclothes? A key is for " +
"doors, not linens." +
"\n\n" +
"Press 'R' to Return.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.cell_mirror;}
}
void state_cell_mirror() {
text.text = "You are in your cell, the mirror has fallen off its hinges, " +
"and you have a small key." +
"\n\n" +
"Press 'S' to examine your Sheets or 'L' to try the Locked door.";
if (Input.GetKeyDown(KeyCode.S)) {state = State.sheets_1;}
else if (Input.GetKeyDown(KeyCode.L)) {state = State.lock_1;}
}
void state_lock_1() {
text.text = "You stick your key in the keyhole and -- this just might work! " +
"However, you suddenly feel like this might be a bad idea." +
"\n\n" +
"Press 'R' to Return to your room or 'O' to Open the door.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.cell_mirror;}
else if (Input.GetKeyDown(KeyCode.O)) {state = State.corridor_0;}
}
void state_corridor_0() {
text.text = "You've escaped from your room, but you're not out of the fire " +
"yet. You see a glint of light bounce off the floor, a stairwell on your " +
"left, and a closet door." +
"\n\n" +
"Press 'S' to go to the Stairs, 'F' to inspect the Floor, or 'C' to " +
"check out the Closet.";
if (Input.GetKeyDown(KeyCode.S)) {state = State.stairs_0;}
else if (Input.GetKeyDown(KeyCode.F)) {state = State.floor;}
else if (Input.GetKeyDown(KeyCode.C)) {state = State.closet_door;}
}
void state_stairs_0() {
text.text = "These stairs go up, but if you go up you are going away from " +
"your intended target -- freedom. You don't really want to go up the " +
"stairs." +
"\n\n" +
"Press 'R' to Return to the hallway.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.corridor_0;}
}
void state_floor() {
text.text = "That glint of light was a hairclip on the ground. Might as well " +
"take it, it's not like someone needs it." +
"\n\n" +
"Press 'R' to Return or 'H' to take the Hairclip.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.corridor_0;}
else if (Input.GetKeyDown(KeyCode.H)) {state = State.corridor_1;}
}
void state_closet_door() {
text.text = "There's a closet door, probably a janitor closet, but it's " +
"locked. You seem to have a problem with locked doors. Possibly you " +
"could open the door if you had something to pick the lock with." +
"\n\n" +
"Press 'R' to Return to the hallway.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.corridor_0;}
}
void state_stairs_1() {
text.text = "You see the stairs go up. However, the voice of your " +
"subconsious tells you \"This isn't a good idea, You still have work " +
"to do here.\"" +
"\n\n" +
"Press 'R' to Return to the hallway.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.corridor_1;}
}
void state_corridor_1() {
text.text = "You're still stranded, but now you have a women's fashion " +
"accessory. Whoop-de-do." +
"\n\n" +
"Press 'S' to check the Stairwell or 'C' to try the Closet.";
if (Input.GetKeyDown(KeyCode.S)) {state = State.stairs_1;}
else if (Input.GetKeyDown(KeyCode.C)) {state = State.in_closet;}
}
void state_in_closet() {
text.text = "You managed to pick the lock and now can enter the closet. " +
"There's not much in there but a janitor uniform. It looks to be " +
"about your size." +
"\n\n" +
"Press 'R' to Return to the hallway, or 'D' to Dress in the uniform.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.corridor_2;}
else if (Input.GetKeyDown(KeyCode.D)) {state = State.corridor_3;}
}
void state_stairs_2() {
text.text = "You are in the stairwell. You could go down, but you might " +
"fare better if you looked like you belong here rather than still " +
"dressed as a prisoner." +
"\n\n" +
"Press 'R' to Return to the hallway.";
if (Input.GetKeyDown(KeyCode.R)) {state = State.corridor_2;}
}
void state_corridor_2() {
text.text = "You stand in a mostly featureless hallway, except for a " +
"mostly featureless stairwell, the door to your old cell, and the " +
"mostly featureless janitor's closet. You wonder how many different " +
"flavor texts there are for this hallway." +
"\n\n" +
"Press 'S' to check out the Stairs, or 'R' to Return to the closet.";
if (Input.GetKeyDown(KeyCode.S)) {state = State.stairs_2;}
else if (Input.GetKeyDown(KeyCode.R)) {state = State.in_closet;}
}
void state_courtyard() {
text.text = "You safely exit the building undetected. Now you are free! " +
"You think." +
"\n\n" +
"Thank you for playing this game. Built as part of the Udemy Unity " +
"Course." +
"\n\n" +
"Press 'P' to Play again.";
if (Input.GetKeyDown(KeyCode.P)) {state = startingState;}
}
void state_corridor_3() {
text.text = "You enter the corridor and notice the stairwell once more. " +
"You should be able to exit now that you look like an everyday worker." +
"\n\n" +
"Press 'S' to take the Stairs or 'U' to Undress from the uniform.";
if (Input.GetKeyDown(KeyCode.S)) {state = State.courtyard;}
else if (Input.GetKeyDown(KeyCode.U)) {state = State.in_closet;}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment