C# XNA dungeon crawl room gen


SUBMITTED BY: Guest

DATE: April 1, 2013, 8:07 p.m.

FORMAT: Text only

SIZE: 5.9 kB

HITS: 1134

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Xna.Framework;
  6. namespace WindowsGame6
  7. {
  8. class levelgen
  9. {
  10. #region fields
  11. int levelwidth = 70;
  12. int levelheight = 70;
  13. int radius;
  14. Random random = new Random();
  15. int yoffset = 0;
  16. public Vector3 initialPosition;
  17. #endregion
  18. public int[,] generate()
  19. {
  20. int noRooms = random.Next(2, 8);
  21. int[] tile = new int[noRooms * 2];
  22. int[,] levelArray = new int[levelwidth, levelheight];
  23. int roomsGenerated = 0;
  24. int z = 0;
  25. while (roomsGenerated < noRooms)
  26. {
  27. bool wrongRoom = false;
  28. #region placetile
  29. do
  30. {
  31. radius = random.Next(2, 4);
  32. tile[z] = random.Next(radius, levelwidth - radius);
  33. tile[z+1] = random.Next(radius, levelheight - radius);
  34. wrongRoom = false;
  35. for (int i = -(radius - 2); i < radius+1; i++)
  36. {
  37. for (int j = -(radius - 2); j < radius+1; j++)
  38. {
  39. if (levelArray[tile[z] + i, tile[z+1] + j] != 0)
  40. {
  41. wrongRoom = true;
  42. }
  43. }
  44. }
  45. } while (wrongRoom);
  46. #endregion
  47. System.Diagnostics.Debug.WriteLine("{0}, {1}; radius: {2}", tile[z], tile[z+1], radius);
  48. initialPosition = new Vector3((float)(tile[2] * 6.6), 0, (float)(tile[3] * 6.6));
  49. #region fillroom
  50. // levelArray[tile[z], tile[z+1]] = 1;
  51. for (int i = -(radius - 1); i < radius; i++)
  52. {
  53. for (int j = -(radius - 1); j < radius; j++)
  54. {
  55. levelArray[tile[z] + i, tile[z+1] + j] = 1;
  56. }
  57. }
  58. #endregion
  59. #region connectRooms
  60. if (roomsGenerated > 0)
  61. {
  62. //y axis tiles
  63. if ((tile[z] - tile[z - 2]) > 0) //if current row is further down than the old row
  64. {
  65. //starting at the current tile position, go down the row until we are parallel with the old tile
  66. yoffset = 0;
  67. while (tile[z] + yoffset > tile[z - 2]) //whilst we're still above the old tile
  68. {
  69. levelArray[tile[z] + yoffset, tile[z + 1]] = 1; //add a tile
  70. yoffset--; // and move down
  71. }
  72. }
  73. else if ((tile[z] - tile[z - 2]) < 0) //if current row is further up than the old row
  74. {
  75. //starting at the current tile position, go down the row until we are parallel with the old tile
  76. yoffset = 0;
  77. while (tile[z] + yoffset < tile[z - 2] + 1) //(whilst we're below the old tile)
  78. {
  79. levelArray[tile[z] + yoffset, tile[z + 1]] = 1; //add a tile
  80. yoffset++; // and move up
  81. }
  82. }
  83. //x axis tiles
  84. if ((tile[z + 1] - tile[z - 1]) > 0) //if current column is further to the right than the old one
  85. {
  86. int i = 0;
  87. while (tile[z + 1] + i > (tile[z - 1] - 1))
  88. {
  89. levelArray[tile[z] + yoffset, tile[z + 1] + i] = 1; //add a tile
  90. i--; // and move left
  91. }
  92. }
  93. else if ((tile[z + 1] - tile[z - 1]) > 0) //if current column is further to the left than the old one
  94. {
  95. int i = 0;
  96. while (tile[z + 1] + i < (tile[z - 1] + 1))
  97. {
  98. levelArray[tile[z] + yoffset, tile[z + 1] + i] = 1; //add a tile
  99. i++; // and move right
  100. }
  101. }
  102. }
  103. #endregion
  104. #region drawWalls
  105. for (int q = 0; q < levelheight; q++)
  106. {
  107. for (int p = 0; p < levelwidth; p++)
  108. {
  109. if (levelArray[q, p] == 1)
  110. {
  111. for (int i = -1; i < 2; i++)
  112. {
  113. for (int j = -1; j < 2; j++)
  114. {
  115. if(levelArray[q + i, j + p] == 0)
  116. levelArray[q + i, j + p] = 10;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. #endregion
  123. roomsGenerated++;
  124. z+=2;
  125. }
  126. return levelArray;
  127. }
  128. }
  129. }

comments powered by Disqus