c# - Getting data out of a textfile and feeding it into a 2d array -
i have want make levels in game load textfile , load 2d array, level textfile looks:
0,0,0,0,0,0,0,0,0,0 1,1,1,1,1,1,1,1,1,1 2,2,2,2,2,2,2,2,2,2 3,3,3,3,3,3,3,3,3,3 4,4,4,4,4,4,4,4,4,4 5,5,5,5,5,5,5,5,5,5 6,6,6,6,6,6,6,6,6,6 7,7,7,7,7,7,7,7,7,7 8,8,8,8,8,8,8,8,8,8 9,9,9,9,9,9,9,9,9,9
i want each number seperate tile ingame, commas act seperator, have no idea how data out of 2d array. how far i've got:
tile[,] tiles; string[] mapdata; public void loadmap(string path) { if (file.exists(path)) { mapdata = file.readalllines(path); var width = mapdata[0].length; var height = mapdata.length; tiles = new tile[width, height]; using (streamreader reader = new streamreader(path)) { (int y = 0; y < height; y++) { (int x = 0; x < width; x++) { tiles[x, y] = new tile(spritesheet, 5, 3, new vector2(x * 64, y * 64)); } } } } }
the numbers 5 , 3 in line tiles[x, y] = new tile() represent position of texture in textureatlas. want add if statement if number in file 0 @ topleft, want tiles[0, 0] set specific row , column in textureatlas. on appreciated, i'm not seeing it!
first, var width = mapdata[0].length;
going return length of character array, including commas, 19. looks not want return commas. so, should split string so:
tile[,] tiles; string[] mapdata; public void loadmap(string path) { if (file.exists(path)) { mapdata = file.readalllines(path); var width = mapdata[0].split(',').length; var height = mapdata.length; tiles = new tile[width, height]; using (streamreader reader = new streamreader(path)) { (int y = 0; y < height; y++) { string[] chararray = mapdata[y].split(','); (int x = 0; x < chararray.length; x++) { int value = int.parse(chararray[x]); ... tiles[x, y] = new tile(spritesheet, 5, 3, new vector2(x * 64, y * 64)); } } } } }
Comments
Post a Comment