Compacted version of the script

I found that the original source code had some similar structured functions, so I have made the following change to make it easier to add stories to the game.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class TextController : MonoBehaviour {

public Text text;
private enum States { cell, sheets_0, mirror, lock_0, cell_mirror, sheets_1, lock_1, freedom };
private States myState;

private Dictionary<States, string> gameText = new Dictionary<States, string>() {
	{States.cell, "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.\n\n" +
		"Press S to view Sheets, M to view Mirror or L to view Lock"},
	{States.sheets_0, "You can't believe you sleep in these things. " +
		"Surely it's time somebody changed them. " +
		"The pleasures of prison life I guess!\n\n" +
		"Press R to Return to roaming your cell"},
	{States.sheets_1, "You can't believe you sleep in these things. " +
		"Surely it's time somebody changed them. " +
		"The pleasures of prison life I guess!\n\n" +
		"Press R to Return to roaming your cell"},
	{States.mirror, "The dirty old mirror on the wall seems loose.\n\n" +
		"Press T to Take the mirror, or R to return to roaming your cell"},
	{States.lock_0, "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, " +
		"may be that would help!\n\n" +
		"Press R to Return to roaming your cell"},
	{States.lock_1, "You carefully put the mirror through the bars, " +
		"and turn it round so that you can see the lock. " +
		"You can just make out fingerprints around the buttons. " +
		"You press the dirty buttons, and hear a click.\n\n" +
		"Press O to Open, or R to Return roaming your cell"},
	{States.cell_mirror, "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 till there, and firmly locked!\n\n" +
		"Press S to view Sheets, or L to view Lock"},
	{States.freedom, "Yuu are FREE!\n\n" +
		"Press P to Play again"}
};

private Dictionary<States, Dictionary<KeyCode, States>> possibleMoves = new Dictionary<States, Dictionary<KeyCode, States>>() {
	{States.cell, new Dictionary<KeyCode, States>() {
			{KeyCode.S, States.sheets_0}, 
			{KeyCode.M, States.mirror},
			{KeyCode.L, States.lock_0} } },
	{States.sheets_0, new Dictionary<KeyCode, States>() {
			{KeyCode.R, States.cell} } },
	{States.mirror, new Dictionary<KeyCode, States>() {
			{KeyCode.T, States.cell_mirror},
			{KeyCode.R, States.cell} } },
	{States.lock_0, new Dictionary<KeyCode, States>() {
			{KeyCode.R, States.cell} } },
	{States.sheets_1, new Dictionary<KeyCode, States>() {
			{KeyCode.R, States.cell_mirror} } },
	{States.cell_mirror, new Dictionary<KeyCode, States>() {
			{KeyCode.S, States.sheets_1}, 
			{KeyCode.L, States.lock_1} } },
	{States.lock_1, new Dictionary<KeyCode, States>() {
			{KeyCode.O, States.freedom}, 
			{KeyCode.R, States.cell_mirror} } },
	{States.freedom, new Dictionary<KeyCode, States>() {
			{KeyCode.P, States.cell} } }
};

// Use this for initialization
void Start () {
	myState = States.cell;
}

// Update is called once per frame
void Update () {
	text.text = gameText[myState];
	foreach (KeyCode key in possibleMoves[myState].Keys) {
		if (Input.GetKeyDown(key)) {
			myState = possibleMoves[myState][key];
			return;
		}
	}
}
}

Privacy & Terms