Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2016 11:58
Show Gist options
  • Save anonymous/07c8b470d78585faf4610ffd8379edc8 to your computer and use it in GitHub Desktop.
Save anonymous/07c8b470d78585faf4610ffd8379edc8 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class GameDirector : MonoBehaviour
{
//variables
public bool canControl = true; //control lock, if false, player can't click game buttons
public int buttonPressed = 0;
public int gameSpeed = 1; //currently unused, in future simon's playback will get faster
public int level = 1; //current level, also used to dictate how many numbers will be generated
public List<int> simon = new List<int>(); //simon's list, sequence is stored here every level
//Game Loop Variables
public int playerInput = 0; //button's change this when they are clicked
public int inputCounter = 0; // goes up every time player input gets changed
public int inputCounterCheck = 0; //used to check if this input has been check by the game director
public int count = 0; // number of successfull inputs this level
private int replay = 3; // number of replays remaining
//GameObjects
public GameObject mainMenuCanvas;
//UI Text
public Text levelCounter;
public Text replaysRemaining;
//scripts
public buttonOne buttonOne;
public buttonTwo buttonTwo;
public buttonThree buttonThree;
public buttonFour buttonFour;
void Start()
{
mainMenu(); //go to main menu when game starts
}
void Update()
{
//GAME LOOP
if (level < 10) {
levelCounter.text = "0" + level.ToString (); // this if statement is just for the formatting of the level counter (always 2 digits)
}
else
{
levelCounter.text = level.ToString ();
}
replaysRemaining.text = "0" + replay.ToString (); //print the number of replays left in the replaysRemaining text field
if (playerInput > 0 && inputCounter != inputCounterCheck && count != simon.Count)
//if the player has clicked one of the buttons AND the current input hasn't been checked AND the player hasn't reached the end of the sequence
{
if (playerInput == simon [count]) //if the input is equal to this unchecked element in the list
{
count++; // increase the value of count by 1, so next time it checks the next element in the list
inputCounterCheck++; // increase the countercheck by 1 so we know this input has been checked
}
else if (playerInput != simon [count]) //if the input is NOT equal to this element in the list
{
loseGame (); //the player loses
}
}
if (count == simon.Count && simon.Count > 0) //if the player has reached the end of this levels sequence
{
nextLevel();
}
}
void mainMenu()
{
mainMenuCanvas.SetActive (true); // enable gui elements
lockInput(); //disable player control
Debug.Log ("Main Menu");
}
public void gameStart()
{
//start the game, set all the game veriables to their default possition, and clear the sequence list.
mainMenuCanvas.SetActive (false);
playerInput = 0;
inputCounter = 0;
inputCounterCheck = 0;
count = 0;
level = 10;
replay = 3;
simon.Clear();
simonThinks();
}
void simonThinks() // number generation
{
lockInput(); //disable player control
for (int i = 0; i < level; i++) //for every int in the range of 'level'
{
//generate n random numbers between 1 and 4 (n = level) and store them in the list 'simon'
simon.Add(Random.Range(1, 5));
//Debug.Log("Random Number Generated");
}
//Debug.Log(simon.Count);
StartCoroutine("simonSays"); //run the function that displays the sequence to the player
}
IEnumerator simonSays()
{
lockInput ();
foreach (int number in simon)
{
yield return new WaitForSeconds(0.4f);
//Walk through each number in the list simon, do something depending on which number it is
if (number == 1) {
buttonOne.StartCoroutine("buttonFlash");
}
else if (number == 2) {
buttonTwo.StartCoroutine("buttonFlash");
}
else if (number == 3) {
buttonThree.StartCoroutine("buttonFlash");
}
else if (number == 4) {
buttonFour.StartCoroutine("buttonFlash");
}
yield return new WaitForSeconds(0.1f);
yield return null;
//Debug.Log ("coroutine running");
}
unlockInput ();
}
void nextLevel ()
{
playerInput = 0; //reset input
inputCounter = 0; //reset input counter
inputCounterCheck = 0; //reset input counter check
count = 0; // reset count
level++; // increase the level by 1
simon.Clear(); //clear the sequence list
simonThinks (); // run the thinking function to generate a new sequence based on the new level number
}
void loseGame() //player lost the game, reset all values and go back to the main menu
{
playerInput = 0;
inputCounter = 0;
inputCounterCheck = 0;
count = 0;
level = 1;
replay = 3;
simon.Clear();
mainMenu ();
}
void lockInput()
{
Debug.Log("Controls Locked");
canControl = false;
}
void unlockInput()
{
Debug.Log ("Controls Unlocked");
canControl = true;
}
public void replaySequence()
{
if (replay > 0 && canControl == true)
{
StartCoroutine("simonSays"); //run the function that displays the sequence to the player
replay--;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment