Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;using System.Collections.Generic;
- using UnityEngine.SceneManagement;
- using UnityEngine;
- using System.IO;
- public class Grid : MonoBehaviour {
- [SerializeField]
- private GameObject SquareEmpty, SquareFilled, outLine;
- [SerializeField]
- private GameObject[,] array;
- [SerializeField]
- private AudioSource ASstep, ASgood, ASbad, ASQuestionInput;
- [SerializeField]
- private AudioClip recapClip;
- [SerializeField]
- private TextAsset[] textFiles;
- [HideInInspector]
- public int thisTextFile;
- private int selectX, selectY;
- [SerializeField]
- private AudioSource ACgood, ACbad, ACQuestion;
- [SerializeField]
- private AudioClip introToNextLevel;
- [HideInInspector]
- public bool inputEnabled = true;
- [SerializeField]
- private string nextLevel;
- private KeyCode correctKey;
- private KeyCode[] keys = {
- KeyCode.Alpha1,
- KeyCode.Alpha2,
- KeyCode.Alpha3,
- KeyCode.Alpha4
- };
- void Start() {
- selectX = 0;
- selectY = 0;
- inputEnabled = true;
- thisTextFile = Random.Range(0, textFiles.Length - 1);
- correctKey = keys[thisTextFile];
- int[,] textArray = FileToArray(textFiles[thisTextFile].name + ".txt");
- array = new GameObject[textArray.GetLength(0), textArray.GetLength(1)];
- for (int y = 0; y < textArray.GetLength(1); ++y)
- {
- for (int x = 0; x < textArray.GetLength(0); ++x)
- {
- if (textArray[x, y] == 1)
- {
- GameObject t = Instantiate(SquareFilled, new Vector3Int(y, -x, 0), Quaternion.identity);
- array[x, y] = t;
- }
- else
- {
- GameObject t = Instantiate(SquareEmpty, new Vector3Int(y, -x, 0), Quaternion.identity);
- array[x, y] = t;
- }
- }
- }
- }
- void Update() {
- int oldX = selectX;
- int oldY = selectY;
- if (inputEnabled)
- {
- if (selectY != -(array.GetLength(0) - 1) && Input.GetKeyUp(KeyCode.DownArrow))
- selectY--;
- if (selectY != 0 && Input.GetKeyUp(KeyCode.UpArrow))
- selectY++;
- if (selectX != 0 && Input.GetKeyUp(KeyCode.LeftArrow))
- selectX--;
- if (selectX != array.GetLength(1) - 1 && Input.GetKeyUp(KeyCode.RightArrow))
- selectX++;
- if (Input.GetKeyDown(correctKey))
- {
- if (SceneManager.GetActiveScene().name == "Level3")
- {
- StartCoroutine(GoodEndSequence());
- }
- else {
- inputEnabled = false;
- StartCoroutine(GoToNextScene());
- }
- }
- else if (Input.GetKeyDown(keys[0]) || Input.GetKeyDown(keys[1]) || Input.GetKeyDown(keys[2]) || Input.GetKeyDown(keys[3])) {
- StartCoroutine(BadEndSequence());
- }
- }
- outLine.transform.position = new Vector3(selectX, selectY, 0);
- if (oldX != selectX || oldY != selectY)
- {
- if (array[-selectY, selectX].GetComponent<SpriteRenderer>().color.r < 1)
- {
- ASgood.Play();
- }
- else
- {
- ASbad.pitch = Random.Range(0.8f, 1.2f);
- ASbad.Play();
- }
- //topleft corner try input and go next scene
- if (selectX == 0 && selectY == 0 && !ASQuestionInput.isPlaying)
- {
- ASQuestionInput.clip = recapClip;
- ASQuestionInput.Play();
- }
- }
- }
- public int[,] FileToArray(string msg)
- {
- string input = File.ReadAllText("Assets/TextFiles/" + SceneManager.GetActiveScene().name + "/" + msg);
- int i = 0, j = 0;
- int[,] result = new int[10, 10];
- foreach (var row in input.Split('\n'))
- {
- j = 0;
- foreach (var col in row.Trim().Split(' '))
- {
- result[i, j] = int.Parse(col.Trim());
- j++;
- }
- i++;
- }
- int[,] result2 = new int[i, j];
- for (int x = 0; x < i; ++x)
- {
- for (int y = 0; y < j; ++y)
- {
- result2[x, y] = result[x, y];
- }
- }
- return result2;
- }
- IEnumerator GoToNextScene() {
- ACQuestion.Stop();
- yield return loadClip(introToNextLevel);
- SceneManager.LoadScene(nextLevel);
- }
- IEnumerator loadClip(AudioClip audioclip)
- {
- ACQuestion.clip = audioclip;
- ACQuestion.Play();
- yield return new WaitForSeconds(ACQuestion.clip.length);
- }
- IEnumerator GoodEndSequence()
- {
- ACQuestion.Stop();
- yield return null;
- SceneManager.LoadScene("GoodEnd");
- }
- IEnumerator BadEndSequence()
- {
- ACQuestion.Stop();
- yield return null;
- SceneManager.LoadScene("BadEnd");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement