// Method for creating a new blank map. public void CreateNewMap(int x, int y) { map = new Map(x, y); map.RebuildMap(); map.GetImagePalette().PrintImageList(); Refresh(); }
/********************************INPUT*/ /********************************INPUT*/ /********************************INPUT*/ /********************************INPUT*/ // Returns a new map object based on the settings in the passed map path name. public static Map LoadMap(String map) { // Create a new map to build from a local file. Map newMap = new Map(0, 0); // Placeholder for the line being currently ready from file. String line; // Create a file from the supplied map name/file path. System.IO.StreamReader file = new System.IO.StreamReader(map); // Go through each line of the map file and process each command appropriately. while ((line = file.ReadLine()) != null) { /*************************************FILE END*/ if (line.StartsWith(MapIO.CLOSE_FILE)) { break; } /*************************************COMMENTS*/ if (line.StartsWith(MapIO.COMMENT)) { continue; } /************************************LOAD IMAGES*/ if (line.StartsWith(MapIO.IMAGE_LOAD)) { // Cycle to the next line line = file.ReadLine(); // Keep loading each line as an image until you reach the end. while (line != MapIO.IMAGE_CLOSE) { try { Bitmap image = new Bitmap(Image.FromFile(line), ImagePalette.IMAGE_SIZE, ImagePalette.IMAGE_SIZE); newMap.GetImagePalette().AddNewImage(line, image); } catch (Exception e) { Console.WriteLine("Could not load image: " + line); Console.WriteLine("" + e.ToString()); } // Make sure you cycle to the next line in the file before you restart the loop. line = file.ReadLine(); } continue; } /************************************LOAD MAP*/ if (line.StartsWith(MapIO.MAP_LOAD)) { // Cycle to the next line, which is the beginning of the map attributes. line = file.ReadLine(); // Get the map dimentions from the file into local variables. int colomns = Int32.Parse(line); line = file.ReadLine(); int rows = Int32.Parse(line); line = file.ReadLine(); // Set the map dimentions. newMap.SetColumns(colomns); newMap.SetRows(rows); newMap.RebuildMap(); // Loop through every tile and set its images. while (line != MapIO.MAP_CLOSE) { for (int i = 0; i < newMap.GetColumns(); i++) { for (int j = 0; j < newMap.GetRows(); j++) { // Read the tile coordinates and advance the line reader as necessary. int x = Int32.Parse(line); line = file.ReadLine(); int y = Int32.Parse(line); line = file.ReadLine(); // Loop through each layer of the tile and set its image. for (int n = 0; n < Map.NUMBER_OF_LAYERS; n++) { newMap.GetTiles()[x, y].SetTileImage((Map.LAYER)n, line); line = file.ReadLine(); } } } } continue; } } // Shut the door. file.Close(); return(newMap); }
/********************************INPUT*/ /********************************INPUT*/ /********************************INPUT*/ /********************************INPUT*/ // Returns a new map object based on the settings in the passed map path name. public static Map LoadMap(String map) { // Create a new map to build from a local file. Map newMap = new Map(0, 0); // Placeholder for the line being currently ready from file. String line; // Create a file from the supplied map name/file path. System.IO.StreamReader file = new System.IO.StreamReader(map); // Go through each line of the map file and process each command appropriately. while ((line = file.ReadLine()) != null) { /*************************************FILE END*/ if (line.StartsWith(MapIO.CLOSE_FILE)) break; /*************************************COMMENTS*/ if (line.StartsWith(MapIO.COMMENT)) continue; /************************************LOAD IMAGES*/ if(line.StartsWith(MapIO.IMAGE_LOAD)) { // Cycle to the next line line = file.ReadLine(); // Keep loading each line as an image until you reach the end. while (line != MapIO.IMAGE_CLOSE) { try { Bitmap image = new Bitmap(Image.FromFile(line), ImagePalette.IMAGE_SIZE, ImagePalette.IMAGE_SIZE); newMap.GetImagePalette().AddNewImage(line, image); } catch (Exception e) { Console.WriteLine("Could not load image: " + line); Console.WriteLine("" + e.ToString()); } // Make sure you cycle to the next line in the file before you restart the loop. line = file.ReadLine(); } continue; } /************************************LOAD MAP*/ if (line.StartsWith(MapIO.MAP_LOAD)) { // Cycle to the next line, which is the beginning of the map attributes. line = file.ReadLine(); // Get the map dimentions from the file into local variables. int colomns = Int32.Parse(line); line = file.ReadLine(); int rows = Int32.Parse(line); line = file.ReadLine(); // Set the map dimentions. newMap.SetColumns(colomns); newMap.SetRows(rows); newMap.RebuildMap(); // Loop through every tile and set its images. while (line != MapIO.MAP_CLOSE) { for (int i = 0; i < newMap.GetColumns(); i++) { for (int j = 0; j < newMap.GetRows(); j++) { // Read the tile coordinates and advance the line reader as necessary. int x = Int32.Parse(line); line = file.ReadLine(); int y = Int32.Parse(line); line = file.ReadLine(); // Loop through each layer of the tile and set its image. for (int n = 0; n < Map.NUMBER_OF_LAYERS; n++) { newMap.GetTiles()[x, y].SetTileImage((Map.LAYER)n, line); line = file.ReadLine(); } } } } continue; } } // Shut the door. file.Close(); return newMap; }