/** * This has to be awake, as the levels have to be parsed before everything else calls the data * from the file. Depending on if the RandomLevel flag is flipped in the @GlobalFlag.cs class, * then a level will be generated randomly and without the use of a file. */ void Awake() { bool randomLevel = GlobalFlags.getRandLevel(); if (!randomLevel) { //TODO Once we have levels being set globally, get the level here from the global variables. try { TextAsset file = (TextAsset)Resources.Load("levels/level" + GlobalFlags.getLevel()); string fileString = file.text; string[] lines = fileString.Split('\n'); //string[] lines = System.IO.File.ReadAllLines("assets/Resources/levels/level" + GlobalFlags.getLevel() + ".txt"); triArray = new TriInfo[lines.Length - 1]; //first line of the level file is the queue for that level queueTris = lines[0].Split(','); for (int i = 1; i < lines.Length; i++) { int commaIndex = lines[i].IndexOf(','); int colonIndex = lines[i].IndexOf(':'); int x = int.Parse(lines[i].Substring(0, commaIndex)); int y = int.Parse(lines[i].Substring(commaIndex + 1, colonIndex - 1 - commaIndex)); string colour = lines[i].Substring(colonIndex + 1, lines[i].Length - 1 - colonIndex); TriInfo triInfo = new TriInfo(x, y, colour.Trim()); triArray[i - 1] = triInfo; // first "i" was for the queue } } catch (UnityException e) { Debug.Log("error reading file level" + GlobalFlags.getLevel() + ".txt"); Debug.Log(e.StackTrace); } } else { //Loads a random level into the Triangle array int negRandY = (int)Random.Range(1, RANDOM_LENGTH) * -1; int randY = (int)Random.Range(1, RANDOM_LENGTH); TriInfo [] temp = new TriInfo[(RANDOM_LENGTH + Mathf.Abs(RANDOM_LENGTH) + 1) * (RANDOM_LENGTH + Mathf.Abs(RANDOM_LENGTH) + 1) - 1]; //used to help make sure there is no complete cluster in the random level string [,] testComplete = new string[RANDOM_LENGTH * 2 + 1, RANDOM_LENGTH * 2 + 1]; int arrLoc = 0; int yLocTri = RANDOM_LENGTH - randY; //y location of the triangle in an array for (int i = randY; i >= negRandY; i--) { int negRandX = (int)Random.Range(1, RANDOM_LENGTH) * -1; int randX = (int)Random.Range(1, RANDOM_LENGTH); int xLocTri = negRandX + RANDOM_LENGTH; //x location of the triangle in an array for (int j = negRandX; j <= randX; j++) { if (i == 0 && j == 0) { testComplete[xLocTri, yLocTri] = "black"; xLocTri++; continue; } string rColour = randomColour(); testComplete[xLocTri, yLocTri] = rColour; while (hasCompleteTriangle(testComplete, false, xLocTri, yLocTri)) { rColour = randomColour(); testComplete[xLocTri, yLocTri] = rColour; } TriInfo triInfo = new TriInfo(j, i, rColour); temp[arrLoc] = triInfo; arrLoc++; xLocTri++; } yLocTri++; } triArray = new TriInfo[arrLoc]; System.Array.Copy(temp, 0, triArray, 0, arrLoc); //loops over arbitrary number (just picked 20), to load the queue with that many random colours. queueTris = new string[40]; for (int i = 0; i < queueTris.Length; i++) { queueTris[i] = randomColour(); } } }
/** * This has to be awake, as the levels have to be parsed before everything else calls the data * from the file. Depending on if the RandomLevel flag is flipped in the @GlobalFlag.cs class, * then a level will be generated randomly and without the use of a file. */ void Awake() { bool randomLevel = GlobalFlags.getRandLevel (); if(!randomLevel) { //TODO Once we have levels being set globally, get the level here from the global variables. try { TextAsset file = (TextAsset) Resources.Load("levels/level" + GlobalFlags.getLevel()); string fileString = file.text; string[] lines = fileString.Split('\n'); //string[] lines = System.IO.File.ReadAllLines("assets/Resources/levels/level" + GlobalFlags.getLevel() + ".txt"); triArray = new TriInfo[lines.Length - 1]; //first line of the level file is the queue for that level queueTris = lines[0].Split(','); for(int i=1; i< lines.Length; i++) { int commaIndex = lines[i].IndexOf(','); int colonIndex = lines[i].IndexOf(':'); int x = int.Parse(lines[i].Substring(0, commaIndex)); int y = int.Parse(lines[i].Substring(commaIndex + 1, colonIndex - 1 - commaIndex)); string colour = lines[i].Substring(colonIndex + 1, lines[i].Length - 1 - colonIndex); TriInfo triInfo = new TriInfo(x, y, colour.Trim()); triArray[i - 1] = triInfo; // first "i" was for the queue } } catch (UnityException e){ Debug.Log("error reading file level" + GlobalFlags.getLevel() + ".txt"); Debug.Log(e.StackTrace); } } else { //Loads a random level into the Triangle array int negRandY = (int) Random.Range(1,RANDOM_LENGTH) * - 1; int randY = (int) Random.Range(1,RANDOM_LENGTH); TriInfo [] temp = new TriInfo[(RANDOM_LENGTH + Mathf.Abs(RANDOM_LENGTH) + 1) * (RANDOM_LENGTH + Mathf.Abs(RANDOM_LENGTH) + 1) - 1]; //used to help make sure there is no complete cluster in the random level string [,] testComplete = new string[RANDOM_LENGTH * 2 + 1, RANDOM_LENGTH * 2 + 1]; int arrLoc = 0; int yLocTri = RANDOM_LENGTH - randY; //y location of the triangle in an array for(int i = randY; i >= negRandY; i--) { int negRandX = (int) Random.Range(1,RANDOM_LENGTH) * - 1; int randX = (int) Random.Range(1,RANDOM_LENGTH); int xLocTri = negRandX + RANDOM_LENGTH; //x location of the triangle in an array for(int j = negRandX; j <= randX; j++) { if(i == 0 && j == 0) { testComplete[xLocTri,yLocTri] = "black"; xLocTri++; continue; } string rColour = randomColour(); testComplete[xLocTri,yLocTri] = rColour; while(hasCompleteTriangle(testComplete, false, xLocTri, yLocTri)){ rColour = randomColour(); testComplete[xLocTri,yLocTri] = rColour; } TriInfo triInfo = new TriInfo(j, i, rColour); temp[arrLoc] = triInfo; arrLoc++; xLocTri++; } yLocTri++; } triArray = new TriInfo[arrLoc]; System.Array.Copy(temp, 0, triArray, 0, arrLoc); //loops over arbitrary number (just picked 20), to load the queue with that many random colours. queueTris = new string[40]; for(int i = 0; i < queueTris.Length; i++) { queueTris[i] = randomColour(); } } }