Advertisement
Guest User

meh

a guest
Oct 21st, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System.Collections;using System.Collections.Generic;
  2. using UnityEngine.SceneManagement;
  3. using UnityEngine;
  4. using System.IO;
  5.  
  6. public class Grid : MonoBehaviour {
  7.     [SerializeField]
  8.     private GameObject SquareEmpty, SquareFilled, outLine;
  9.     [SerializeField]
  10.     private GameObject[,] array;
  11.     [SerializeField]
  12.     private AudioSource ASstep, ASgood, ASbad, ASQuestionInput;
  13.     [SerializeField]
  14.     private AudioClip recapClip;
  15.     [SerializeField]
  16.     private TextAsset[] textFiles;
  17.     [HideInInspector]
  18.     public int thisTextFile;
  19.  
  20.     private int selectX, selectY;
  21.  
  22.     [SerializeField]
  23.     private AudioSource ACgood, ACbad, ACQuestion;
  24.     [SerializeField]
  25.     private AudioClip introToNextLevel;
  26.     [HideInInspector]
  27.     public bool inputEnabled = true;
  28.     [SerializeField]
  29.     private string nextLevel;
  30.  
  31.     private KeyCode correctKey;
  32.  
  33.     private KeyCode[] keys = {
  34.         KeyCode.Alpha1,
  35.         KeyCode.Alpha2,
  36.         KeyCode.Alpha3,
  37.         KeyCode.Alpha4
  38.     };
  39.  
  40.     void Start()  {
  41.         selectX = 0;
  42.         selectY = 0;
  43.         inputEnabled = true;
  44.  
  45.         thisTextFile = Random.Range(0, textFiles.Length - 1);
  46.         correctKey = keys[thisTextFile];
  47.  
  48.         int[,] textArray = FileToArray(textFiles[thisTextFile].name + ".txt");
  49.         array = new GameObject[textArray.GetLength(0), textArray.GetLength(1)];
  50.  
  51.         for (int y = 0; y < textArray.GetLength(1); ++y)
  52.         {
  53.             for (int x = 0; x < textArray.GetLength(0); ++x)
  54.             {
  55.                 if (textArray[x, y] == 1)
  56.                 {
  57.                     GameObject t = Instantiate(SquareFilled, new Vector3Int(y, -x, 0), Quaternion.identity);
  58.                     array[x, y] = t;
  59.                 }
  60.                 else
  61.                 {
  62.                     GameObject t = Instantiate(SquareEmpty, new Vector3Int(y, -x, 0), Quaternion.identity);
  63.                     array[x, y] = t;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.  
  69.     void Update() {
  70.         int oldX = selectX;
  71.         int oldY = selectY;
  72.  
  73.         if (inputEnabled)
  74.         {
  75.             if (selectY != -(array.GetLength(0) - 1) && Input.GetKeyUp(KeyCode.DownArrow))
  76.                 selectY--;
  77.             if (selectY != 0 && Input.GetKeyUp(KeyCode.UpArrow))
  78.                 selectY++;
  79.             if (selectX != 0 && Input.GetKeyUp(KeyCode.LeftArrow))
  80.                 selectX--;
  81.             if (selectX != array.GetLength(1) - 1 && Input.GetKeyUp(KeyCode.RightArrow))
  82.                 selectX++;
  83.  
  84.             if (Input.GetKeyDown(correctKey))
  85.             {
  86.                 if (SceneManager.GetActiveScene().name == "Level3")
  87.                 {
  88.                     StartCoroutine(GoodEndSequence());
  89.                 }
  90.                 else {
  91.                     inputEnabled = false;
  92.                     StartCoroutine(GoToNextScene());
  93.                 }
  94.             }
  95.             else if (Input.GetKeyDown(keys[0]) || Input.GetKeyDown(keys[1]) || Input.GetKeyDown(keys[2]) || Input.GetKeyDown(keys[3])) {
  96.                 StartCoroutine(BadEndSequence());
  97.             }
  98.         }
  99.  
  100.         outLine.transform.position = new Vector3(selectX, selectY, 0);
  101.  
  102.         if (oldX != selectX || oldY != selectY)
  103.         {
  104.             if (array[-selectY, selectX].GetComponent<SpriteRenderer>().color.r < 1)
  105.             {
  106.                 ASgood.Play();
  107.             }
  108.             else
  109.             {
  110.                 ASbad.pitch = Random.Range(0.8f, 1.2f);
  111.                 ASbad.Play();
  112.             }
  113.  
  114.             //topleft corner try input and go next scene
  115.             if (selectX == 0 && selectY == 0 && !ASQuestionInput.isPlaying)
  116.             {
  117.                 ASQuestionInput.clip = recapClip;
  118.                 ASQuestionInput.Play();
  119.             }
  120.         }
  121.  
  122.     }
  123.  
  124.     public int[,] FileToArray(string msg)
  125.     {
  126.         string input = File.ReadAllText("Assets/TextFiles/" + SceneManager.GetActiveScene().name + "/" + msg);
  127.         int i = 0, j = 0;
  128.         int[,] result = new int[10, 10];
  129.         foreach (var row in input.Split('\n'))
  130.         {
  131.             j = 0;
  132.             foreach (var col in row.Trim().Split(' '))
  133.             {
  134.                 result[i, j] = int.Parse(col.Trim());
  135.                 j++;
  136.             }
  137.             i++;
  138.         }
  139.         int[,] result2 = new int[i, j];
  140.         for (int x = 0; x < i; ++x)
  141.         {
  142.             for (int y = 0; y < j; ++y)
  143.             {
  144.                 result2[x, y] = result[x, y];
  145.             }
  146.         }
  147.         return result2;
  148.     }
  149.  
  150.     IEnumerator GoToNextScene() {
  151.         ACQuestion.Stop();
  152.         yield return loadClip(introToNextLevel);
  153.         SceneManager.LoadScene(nextLevel);
  154.     }
  155.  
  156.     IEnumerator loadClip(AudioClip audioclip)
  157.     {
  158.         ACQuestion.clip = audioclip;
  159.         ACQuestion.Play();
  160.         yield return new WaitForSeconds(ACQuestion.clip.length);
  161.     }
  162.  
  163.     IEnumerator GoodEndSequence()
  164.     {
  165.         ACQuestion.Stop();
  166.         yield return null;
  167.         SceneManager.LoadScene("GoodEnd");
  168.     }
  169.  
  170.     IEnumerator BadEndSequence()
  171.     {
  172.         ACQuestion.Stop();
  173.         yield return null;
  174.         SceneManager.LoadScene("BadEnd");
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement