/// <summary> /// To read the existing level file and set the scene for edit it /// </summary> /// <param name="editionInfo"></param> public void readLevelFile(EditionInfo editionInfo) { StreamReader sr = new StreamReader(SettingsManager.LevelsFilesPath + "/" + editionInfo.levelName + ".txt"); string line = sr.ReadLine(); int columns = int.Parse(line.Split('x')[0]); int rows = int.Parse(line.Split('x')[1]);//if needs editionInfo.levelSize = new Vector2(columns, rows); generateBackTiles(editionInfo.levelSize); int row = 0; while (!sr.EndOfStream) { line = sr.ReadLine(); string[] cells = line.Split(';'); for (int colum = 0; colum < columns; colum++) { string tileName; string[] cellsTiles = cells[colum].Split('-');//The two tiles in this position: 0 if there is nothing if (cellsTiles[0] != "0") { tileName = TileCodification.getTileName(cellsTiles[0]); instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); if (cellsTiles[1] != "0") { tileName = TileCodification.getTileName(cellsTiles[1]); instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); } else { //Instantiate nothing } } else { if (cellsTiles[1] != "0") { tileName = TileCodification.getTileName(cellsTiles[1]); instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); } else { //Instantiate nothing } } } row++; } sr.Close(); }
/// <summary> /// Load the world /// </summary> /// <param name="fileName">The world file </param> /// <param name="worldType">the world type (if GameWorld or MyWorld)</param> void readWorldFile(string fileName, WorldType worldType) { GameObject worldGameObject = new GameObject("World"); if (worldType.Equals(WorldType.MyWorld)) { StreamReader sr = new StreamReader(MapEditor.WorldsFilesPath + "/" + fileName); string line = sr.ReadLine(); string[] worldParameters = line.Split(';'); string[] backgrounds = sr.ReadLine().Split(';'); int rows = int.Parse(worldParameters[0].Split('x')[0]);//if needs int columns = int.Parse(worldParameters[0].Split('x')[1]); setBackground(backgrounds[0], backgrounds[1], columns, rows); int row = 0; while (!sr.EndOfStream) { line = sr.ReadLine(); string[] vec = line.Split(';'); for (int colum = 0; colum < columns; colum++) { string tile1Code = vec[colum].Split('-')[0]; string tile2Code = vec[colum].Split('-')[1]; if (!tile1Code.Equals("0")) { //No hay forma que el tileCode sea un spawn point, eso se controla en la creacion del mundo y en el archivo, entonces instacinamos con seguridad string tileName = TileCodification.getTileName(tile1Code); GameObject tilePrefab = Resources.Load("Tiles/" + tileName) as GameObject; GameObject tile = Instantiate(tilePrefab, new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f), Quaternion.identity) as GameObject; tile.name = tileName; tile.transform.SetParent(worldGameObject.transform); if (!tile2Code.Equals("0")) { //Aqui si puede haber spawn Points if (!tile2Code.Equals(TileCodification.getTileCode("Spawn Point"))) { tileName = TileCodification.getTileName(tile2Code); tilePrefab = Resources.Load("Tiles/" + tileName) as GameObject; tile = Instantiate(tilePrefab, new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f), Quaternion.identity) as GameObject; tile.name = tileName; tile.transform.SetParent(worldGameObject.transform); if (tile1Code.Substring(0, 1) == "b" && tile2Code.Substring(0, 1) == "f")//also could be use TileCodification.getTileLayer(TileCodification.getTileName(tile1Code)) to get the Tiles layers { tile.layer = LayerMask.NameToLayer("Ground"); } } else { GameObject sp = new GameObject("Spawn Point " + spawnPointsList.Count); sp.transform.position = new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f); spawnPointsList.Add(sp.transform); } } else { //Instantiate nothing } } else { if (!tile2Code.Equals("0")) { if (!tile2Code.Equals(TileCodification.getTileCode("Spawn Point"))) { string tileName = TileCodification.getTileName(tile2Code); GameObject tilePrefab = Resources.Load("Tiles/" + tileName) as GameObject; GameObject tile = Instantiate(tilePrefab, new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f), Quaternion.identity) as GameObject; tile.name = tileName; tile.transform.SetParent(worldGameObject.transform); } else { GameObject sp = new GameObject("Spawn Point " + spawnPointsList.Count); sp.transform.position = new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f); spawnPointsList.Add(sp.transform); } } else { //Instantiate nothing } } } row++; } sr.Close(); } else { //Reading a txt file inside resources folder of the project in adroid build (Android), also works on editor so no problem TextAsset path = Resources.Load <TextAsset>("Files/World Files/" + fileName); using (StreamReader sr = new StreamReader(new MemoryStream(path.bytes))) { string line = sr.ReadLine(); string[] worldParameters = line.Split(';'); string[] backgrounds = sr.ReadLine().Split(';'); int rows = int.Parse(worldParameters[0].Split('x')[0]);//if needs int columns = int.Parse(worldParameters[0].Split('x')[1]); setBackground(backgrounds[0], backgrounds[1], columns, rows); int row = 0; while (!sr.EndOfStream) { line = sr.ReadLine(); string[] vec = line.Split(';'); for (int colum = 0; colum < columns; colum++) { string tile1Code = vec[colum].Split('-')[0]; string tile2Code = vec[colum].Split('-')[1]; if (!tile1Code.Equals("0")) { //No hay forma que el tileCode sea un spawn point, eso se controla en la creacion del mundo y en el archivo, entonces instacinamos con seguridad string tileName = TileCodification.getTileName(tile1Code); GameObject tilePrefab = Resources.Load("Tiles/" + tileName) as GameObject; GameObject tile = Instantiate(tilePrefab, new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f), Quaternion.identity) as GameObject; tile.name = tileName; tile.transform.SetParent(worldGameObject.transform); if (!tile2Code.Equals("0")) { //Aqui si puede haber spawn Points if (!tile2Code.Equals(TileCodification.getTileCode("Spawn Point"))) { tileName = TileCodification.getTileName(tile2Code); tilePrefab = Resources.Load("Tiles/" + tileName) as GameObject; tile = Instantiate(tilePrefab, new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f), Quaternion.identity) as GameObject; tile.name = tileName; tile.transform.SetParent(worldGameObject.transform); if (tile1Code.Substring(0, 1) == "b" && tile2Code.Substring(0, 1) == "f")//also could be use TileCodification.getTileLayer(TileCodification.getTileName(tile1Code)) to get the Tiles layers { tile.layer = LayerMask.NameToLayer("Ground"); } } else { GameObject sp = new GameObject("Spawn Point " + spawnPointsList.Count); sp.transform.position = new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f); spawnPointsList.Add(sp.transform); } } else { //Instantiate nothing } } else { if (!tile2Code.Equals("0")) { if (!tile2Code.Equals(TileCodification.getTileCode("Spawn Point"))) { string tileName = TileCodification.getTileName(tile2Code); GameObject tilePrefab = Resources.Load("Tiles/" + tileName) as GameObject; GameObject tile = Instantiate(tilePrefab, new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f), Quaternion.identity) as GameObject; tile.name = tileName; tile.transform.SetParent(worldGameObject.transform); } else { GameObject sp = new GameObject("Spawn Point " + spawnPointsList.Count); sp.transform.position = new Vector3(blocksSize * colum, blocksSize * ((rows - 1) - row), 0f); spawnPointsList.Add(sp.transform); } } else { //Instantiate nothing } } } row++; } sr.Close(); } } }
/// <summary> /// Reads level file and load tiles in world space /// </summary> /// <param name="fileName">The level file</param> private void loadLevel(string fileName) { tilesGameObject = new GameObject("Tiles"); if (runningLevel.levelType == LevelType.SokobanLevel) { //worldsFilesPath = Application.dataPath + "/Resources/Files/Level Files"; //Reading a txt file inside resources folder of the project in adroid build (Android), also works on editor so no problem TextAsset path = Resources.Load <TextAsset>("Files/Levels Files/" + fileName); using (StreamReader sr = new StreamReader(new MemoryStream(path.bytes))) { string line = sr.ReadLine(); int columns = int.Parse(line.Split('x')[0]); int rows = int.Parse(line.Split('x')[1]);//if needs int row = 0; while (!sr.EndOfStream) { line = sr.ReadLine(); string[] cells = line.Split(';'); for (int colum = 0; colum < columns; colum++) { string tileName; string[] cellsTiles = cells[colum].Split('-');//The two tiles in this position: 0 if there is nothing if (cellsTiles[0] != "0") { tileName = TileCodification.getTileName(cellsTiles[0]); instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); if (cellsTiles[1] != "0") { tileName = TileCodification.getTileName(cellsTiles[1]); if (tileName != "Spawn Point") { if (tileName.Contains("Crate Point")) { runningLevel.levelCratesPoints++; } instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); } else { spawnPoint = new GameObject("Spawn Point"); spawnPoint.transform.position = new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row)); } } else { //Instantiate nothing } } else { if (cellsTiles[1] != "0") { tileName = TileCodification.getTileName(cellsTiles[1]); if (tileName != "Spawn Point") { if (tileName.Contains("Crate Point")) { runningLevel.levelCratesPoints++; } instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); } else { spawnPoint = new GameObject("Spawn Point"); spawnPoint.transform.position = new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row)); } } else { //Instantiate nothing } } } row++; } sr.Close(); } } else if (runningLevel.levelType == LevelType.MyLevel) { StreamReader sr = new StreamReader(SettingsManager.LevelsFilesPath + "/" + fileName); string line = sr.ReadLine(); int columns = int.Parse(line.Split('x')[0]); int rows = int.Parse(line.Split('x')[1]);//if needs int row = 0; while (!sr.EndOfStream) { line = sr.ReadLine(); string[] cells = line.Split(';'); for (int colum = 0; colum < columns; colum++) { string tileName; string[] cellsTiles = cells[colum].Split('-');//The two tiles in this position: 0 if there is nothing if (cellsTiles[0] != "0") { tileName = TileCodification.getTileName(cellsTiles[0]); instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); if (cellsTiles[1] != "0") { tileName = TileCodification.getTileName(cellsTiles[1]); if (tileName != "Spawn Point") { if (tileName.Contains("Crate Point")) { runningLevel.levelCratesPoints++; } instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); } else { spawnPoint = new GameObject("Spawn Point"); spawnPoint.transform.position = new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row)); } } else { //Instantiate nothing } } else { if (cellsTiles[1] != "0") { tileName = TileCodification.getTileName(cellsTiles[1]); if (tileName != "Spawn Point") { if (tileName.Contains("Crate Point")) { runningLevel.levelCratesPoints++; } instantiateTile(tileName, new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row), 0f)); } else { spawnPoint = new GameObject("Spawn Point"); spawnPoint.transform.position = new Vector3(TileCodification.TileSize * (colum), TileCodification.TileSize * ((rows - 1) - row)); } } else { //Instantiate nothing } } } row++; } sr.Close(); } }