using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; namespace WindowsGame6 { class levelgen { #region fields int levelwidth = 70; int levelheight = 70; int radius; Random random = new Random(); int yoffset = 0; public Vector3 initialPosition; #endregion public int[,] generate() { int noRooms = random.Next(2, 8); int[] tile = new int[noRooms * 2]; int[,] levelArray = new int[levelwidth, levelheight]; int roomsGenerated = 0; int z = 0; while (roomsGenerated < noRooms) { bool wrongRoom = false; #region placetile do { radius = random.Next(2, 4); tile[z] = random.Next(radius, levelwidth - radius); tile[z+1] = random.Next(radius, levelheight - radius); wrongRoom = false; for (int i = -(radius - 2); i < radius+1; i++) { for (int j = -(radius - 2); j < radius+1; j++) { if (levelArray[tile[z] + i, tile[z+1] + j] != 0) { wrongRoom = true; } } } } while (wrongRoom); #endregion System.Diagnostics.Debug.WriteLine("{0}, {1}; radius: {2}", tile[z], tile[z+1], radius); initialPosition = new Vector3((float)(tile[2] * 6.6), 0, (float)(tile[3] * 6.6)); #region fillroom // levelArray[tile[z], tile[z+1]] = 1; for (int i = -(radius - 1); i < radius; i++) { for (int j = -(radius - 1); j < radius; j++) { levelArray[tile[z] + i, tile[z+1] + j] = 1; } } #endregion #region connectRooms if (roomsGenerated > 0) { //y axis tiles if ((tile[z] - tile[z - 2]) > 0) //if current row is further down than the old row { //starting at the current tile position, go down the row until we are parallel with the old tile yoffset = 0; while (tile[z] + yoffset > tile[z - 2]) //whilst we're still above the old tile { levelArray[tile[z] + yoffset, tile[z + 1]] = 1; //add a tile yoffset--; // and move down } } else if ((tile[z] - tile[z - 2]) < 0) //if current row is further up than the old row { //starting at the current tile position, go down the row until we are parallel with the old tile yoffset = 0; while (tile[z] + yoffset < tile[z - 2] + 1) //(whilst we're below the old tile) { levelArray[tile[z] + yoffset, tile[z + 1]] = 1; //add a tile yoffset++; // and move up } } //x axis tiles if ((tile[z + 1] - tile[z - 1]) > 0) //if current column is further to the right than the old one { int i = 0; while (tile[z + 1] + i > (tile[z - 1] - 1)) { levelArray[tile[z] + yoffset, tile[z + 1] + i] = 1; //add a tile i--; // and move left } } else if ((tile[z + 1] - tile[z - 1]) > 0) //if current column is further to the left than the old one { int i = 0; while (tile[z + 1] + i < (tile[z - 1] + 1)) { levelArray[tile[z] + yoffset, tile[z + 1] + i] = 1; //add a tile i++; // and move right } } } #endregion #region drawWalls for (int q = 0; q < levelheight; q++) { for (int p = 0; p < levelwidth; p++) { if (levelArray[q, p] == 1) { for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if(levelArray[q + i, j + p] == 0) levelArray[q + i, j + p] = 10; } } } } } #endregion roomsGenerated++; z+=2; } return levelArray; } } }