//Method for generating the new map public void GenerateMap( Image tileset, int tileSize, int tilesPerScreenWidth, int tilesPerScreenHeight, int screensHorizontal, int screensVertical) { //Instructions text goes away labelInstructions.Visible = false; labelInstructions.Enabled = false; //Tilesize variable saved for saving the file this.tileSize = tileSize; //Crops supplied image into a list of images (tileset) listTiles = CroppedImage(tileset, tileSize); //Total array sizes int gridPanelWidth = tilesPerScreenWidth * screensHorizontal; int gridPanelHeight = tilesPerScreenHeight * screensVertical; //Generate the map mapPanels = new MapPanel[gridPanelWidth, gridPanelHeight]; for (int j = 0; j < gridPanelHeight; j++) { for (int i = 0; i < gridPanelWidth; i++) { mapPanels[i, j] = new MapPanel(); mapPanels[i, j].Location = new Point(i * tileSize, j * tileSize + toolbarMain.Height); mapPanels[i, j].Size = new Size(tileSize, tileSize); mapPanels[i, j].BackgroundImage = listTiles[0]; mapPanels[i, j].tileID = 0; mapPanels[i, j].MouseClick += new MouseEventHandler(ClickTile); mapPanels[i, j].BorderStyle = BorderStyle.FixedSingle; Controls.Add(mapPanels[i, j]); } } //Create a paintbox- the tile-selector, essentially if (paintbox != null) { paintbox.Close(); } paintbox = new Paintbox(this, listTiles, tileset, tileset.Width, tileset.Height, tileSize); paintbox.Show(); paintbox.TopMost = true; }
//User pressed load- load a file private void toolStripButtonLoad_onClick(object sender, EventArgs e) { //user prompted to open a file OpenFileDialog dlg = new OpenFileDialog(); dlg.Title = "Open Map File"; dlg.Filter = "png files (*.txt)|*.txt"; //If that works, load the text file. if (dlg.ShowDialog() == DialogResult.OK) { var filePath = dlg.FileName; StreamReader input = new StreamReader(filePath); //Read header values int levelWidth = int.Parse(input.ReadLine()); int levelHeight = int.Parse(input.ReadLine()); tileSize = int.Parse(input.ReadLine()); int[,] tileIDArray = new int[levelWidth, levelHeight]; bool[,] collideArray = new bool[levelWidth, levelHeight]; //Nested for loop will read each line and parse it into the 2D array of tile IDs and collisions string fullLine; string[] splitLine; for (int j = 0; j < levelHeight; j++) { //Reads a row and saves it into a 1D array. fullLine = input.ReadLine(); splitLine = fullLine.Split(','); for (int i = 0; i < levelWidth; i++) { //Stores each value in the 1D array into the tilemap 2D array. tileIDArray[i, j] = int.Parse(splitLine[i]); } } for (int j = 0; j < levelHeight; j++) { //Reads a row and saves it into a 1D array. fullLine = input.ReadLine(); splitLine = fullLine.Split(','); for (int i = 0; i < levelWidth; i++) { //Stores each value in the 1D array into the tilemap 2D array. collideArray[i, j] = bool.Parse(splitLine[i]); } } input.Close(); dlg.Dispose(); Image tileSet; //Now, open the tileset dlg = new OpenFileDialog(); dlg.Title = "Open Image"; dlg.Filter = "png files (*.png)|*.png"; if (dlg.ShowDialog() == DialogResult.OK) { tileSet = Image.FromFile(dlg.FileName); //Crops supplied image into a list of images (tileset) listTiles = CroppedImage(tileSet, tileSize); //Total array sizes int gridPanelWidth = levelWidth; int gridPanelHeight = levelHeight; //Generate the map mapPanels = new MapPanel[gridPanelWidth, gridPanelHeight]; for (int j = 0; j < gridPanelHeight; j++) { for (int i = 0; i < gridPanelWidth; i++) { mapPanels[i, j] = new MapPanel(); mapPanels[i, j].Location = new Point(i * tileSize, j * tileSize + toolbarMain.Height); mapPanels[i, j].Size = new Size(tileSize, tileSize); mapPanels[i, j].BackgroundImage = listTiles[tileIDArray[i, j]]; mapPanels[i, j].tileID = tileIDArray[i, j]; //Sets collide status, changes color if it's true if (mapPanels[i, j].isCollision = collideArray[i, j]) { mapPanels[i, j].BackColor = Color.Blue; } mapPanels[i, j].MouseClick += new MouseEventHandler(ClickTile); mapPanels[i, j].BorderStyle = BorderStyle.FixedSingle; Controls.Add(mapPanels[i, j]); } } //Instructions text goes away labelInstructions.Visible = false; labelInstructions.Enabled = false; //Create a paintbox- the tile-selector, essentially if (paintbox != null) { paintbox.Close(); } paintbox = new Paintbox(this, listTiles, tileSet, tileSet.Width, tileSet.Height, tileSize); paintbox.Show(); paintbox.TopMost = true; } dlg.Dispose(); } dlg.Dispose(); }