private TileProperties.Type[] InsertItemToArray(TileProperties.Type[] array, TileProperties.Type item) { int length = array.Length; for (int i = 0; i < length; i++) { if (array[i] == TileProperties.Type.None) { array[i] = item; break; } } return(array); }
private void ReadFile(string path) { string name; TileProperties.Floor[] floor = new TileProperties.Floor[3]; TileProperties.Type[] type = new TileProperties.Type[3]; int numDoors; TileProperties.Door[] mydoor = new TileProperties.Door[4]; string additionalEffect; int tileNum = 0; #region Reset Variables // Reset name name = ""; // Reset floor for (int i = 0; i < floor.Length; i++) { floor[i] = TileProperties.Floor.None; } // Reset type for (int i = 0; i < type.Length; i++) { type[i] = TileProperties.Type.None; } // Reset door numDoors = 0; for (int i = 0; i < mydoor.Length; i++) { mydoor[i] = TileProperties.Door.None; } // Rest effects additionalEffect = ""; #endregion StreamReader reader = new StreamReader(path); text = reader.ReadLine(); text = reader.ReadLine(); while (text != null) { #region Reset Variables // Reset parsing variables name = ""; for (int i = 0; i < floor.Length; i++) { floor[i] = TileProperties.Floor.None; } for (int i = 0; i < type.Length; i++) { type[i] = TileProperties.Type.None; } numDoors = 0; for (int i = 0; i < mydoor.Length; i++) { mydoor[i] = TileProperties.Door.None; } additionalEffect = ""; #endregion oneDatum = text.Split(','); // Assign name name = oneDatum[0]; // Assign floors for (int i = 0; i < 3; i++) { TileProperties.Floor currFloor = TileProperties.Floor.None; if (oneDatum[i + 1] == "X") { if (i == 0) { currFloor = TileProperties.Floor.Basement; } else if (i == 1) { currFloor = TileProperties.Floor.Ground; } else if (i == 2) { currFloor = TileProperties.Floor.Upper; } floor = InsertItemToArray(floor, currFloor); } } // Assign type for (int i = 0; i < 3; i++) { TileProperties.Type currType = TileProperties.Type.None; if (oneDatum[i + 4] == "X" || oneDatum[i + 4] == "XX") { if (i == 0) { currType = TileProperties.Type.Event; } else if (i == 1) { currType = TileProperties.Type.Omen; } else if (i == 2) { currType = TileProperties.Type.Item; } if (oneDatum[i + 4] == "XX") { type = InsertItemToArray(type, currType); type = InsertItemToArray(type, currType); } else { type = InsertItemToArray(type, currType); } } } // Set door location and number if (Int32.TryParse(oneDatum[7], out int num)) { numDoors = num; } for (int i = 0; i < 4; i++) { TileProperties.Door currDoor = TileProperties.Door.None; if (oneDatum[i + 8] == "X") { if (i == 0) { currDoor = TileProperties.Door.Top; } else if (i == 1) { currDoor = TileProperties.Door.Bottom; } else if (i == 2) { currDoor = TileProperties.Door.Left; } else if (i == 3) { currDoor = TileProperties.Door.Right; } mydoor = InsertItemToArray(mydoor, currDoor); } } // Set additional effect additionalEffect = oneDatum[12]; // Instantiate tile GameObject tile = tilePrefab; tile.GetComponent <Tile>().name = name; tile.GetComponent <Tile>().floor = floor; tile.GetComponent <Tile>().type = type; tile.GetComponent <Tile>().numDoors = numDoors; tile.GetComponent <Tile>().door = mydoor; tile.GetComponent <Tile>().additionalEffect = additionalEffect; GameObject newTile = Instantiate(tile); newTile.transform.parent = gameObject.transform; // Add this tile to the tile group allTiles[tileNum] = newTile; tileNum++; // Read next line text = reader.ReadLine(); } reader.Close(); }