MazeData.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. /// <summary>
  5. /// Holds all data for a generated maze
  6. /// This is the core representation that can be used for rendering, pathfinding, and AI
  7. /// </summary>
  8. public class MazeData
  9. {
  10. public int Width { get; private set; }
  11. public int Height { get; private set; }
  12. public MazeTile[,] Tiles { get; private set; }
  13. public List<MazeRoom> Rooms { get; private set; } = new();
  14. public List<Vector2Int> StartPoints { get; private set; } = new();
  15. public List<Vector2Int> ExitPoints { get; private set; } = new();
  16. private int nextRoomId = 0;
  17. public MazeData(int width, int height)
  18. {
  19. Width = width;
  20. Height = height;
  21. Tiles = new MazeTile[width, height];
  22. // Initialize all tiles as walls
  23. for (int x = 0; x < width; x++)
  24. {
  25. for (int y = 0; y < height; y++)
  26. {
  27. Tiles[x, y] = new MazeTile(x, y);
  28. }
  29. }
  30. }
  31. /// <summary>
  32. /// Sets a tile at the given position
  33. /// </summary>
  34. public void SetTile(int x, int y, MazeTile tile)
  35. {
  36. if (IsInBounds(x, y))
  37. {
  38. Tiles[x, y] = tile;
  39. }
  40. }
  41. /// <summary>
  42. /// Gets a tile at the given position
  43. /// </summary>
  44. public MazeTile GetTile(int x, int y)
  45. {
  46. if (IsInBounds(x, y))
  47. {
  48. return Tiles[x, y];
  49. }
  50. return null;
  51. }
  52. /// <summary>
  53. /// Checks if coordinates are within maze bounds
  54. /// </summary>
  55. public bool IsInBounds(int x, int y)
  56. {
  57. return x >= 0 && x < Width && y >= 0 && y < Height;
  58. }
  59. /// <summary>
  60. /// Checks if a tile is walkable
  61. /// </summary>
  62. public bool IsWalkable(int x, int y)
  63. {
  64. var tile = GetTile(x, y);
  65. return tile != null && tile.IsWalkable();
  66. }
  67. /// <summary>
  68. /// Fills a rectangular area with floor tiles and assigns to a room
  69. /// </summary>
  70. public int FillRoom(int minX, int minY, int maxX, int maxY, int roomId, MazeTile.TerrainType terrain = MazeTile.TerrainType.Normal)
  71. {
  72. int changedTiles = 0;
  73. for (int x = minX; x <= maxX; x++)
  74. {
  75. for (int y = minY; y <= maxY; y++)
  76. {
  77. if (IsInBounds(x, y))
  78. {
  79. var tile = GetTile(x, y);
  80. if (tile.Type != MazeTile.TileType.Floor)
  81. {
  82. changedTiles++;
  83. }
  84. tile.Type = MazeTile.TileType.Floor;
  85. tile.Terrain = terrain;
  86. tile.RoomId = roomId;
  87. }
  88. }
  89. }
  90. return changedTiles;
  91. }
  92. /// <summary>
  93. /// Creates floor tiles along a path (for hallways/corridors)
  94. /// </summary>
  95. public int DrawPath(List<Vector2Int> path, int hallwayWidth, int roomId = -1)
  96. {
  97. int changedTiles = 0;
  98. foreach (var point in path)
  99. {
  100. for (int dx = -hallwayWidth / 2; dx <= hallwayWidth / 2; dx++)
  101. {
  102. for (int dy = -hallwayWidth / 2; dy <= hallwayWidth / 2; dy++)
  103. {
  104. int x = point.x + dx;
  105. int y = point.y + dy;
  106. if (IsInBounds(x, y))
  107. {
  108. var tile = GetTile(x, y);
  109. if (tile.Type != MazeTile.TileType.Floor) // Don't overwrite existing floors
  110. {
  111. changedTiles++;
  112. tile.Type = MazeTile.TileType.Floor;
  113. tile.Terrain = MazeTile.TerrainType.Normal;
  114. if (tile.RoomId == -1)
  115. {
  116. tile.RoomId = roomId;
  117. }
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return changedTiles;
  124. }
  125. /// <summary>
  126. /// Adds a room to the maze
  127. /// </summary>
  128. public MazeRoom AddRoom(int minX, int minY, int maxX, int maxY, MazeRoom.RoomType roomType = MazeRoom.RoomType.Normal)
  129. {
  130. var room = new MazeRoom(nextRoomId++, minX, minY, maxX, maxY, roomType);
  131. Rooms.Add(room);
  132. return room;
  133. }
  134. /// <summary>
  135. /// Gets all rooms of a specific type
  136. /// </summary>
  137. public List<MazeRoom> GetRoomsByType(MazeRoom.RoomType type)
  138. {
  139. return Rooms.Where(r => r.Type == type).ToList();
  140. }
  141. /// <summary>
  142. /// Gets the room at a specific tile position
  143. /// </summary>
  144. public MazeRoom GetRoomAtTile(int x, int y)
  145. {
  146. var tile = GetTile(x, y);
  147. if (tile != null && tile.RoomId >= 0)
  148. {
  149. return Rooms.FirstOrDefault(r => r.Id == tile.RoomId);
  150. }
  151. return null;
  152. }
  153. /// <summary>
  154. /// Adds a start point
  155. /// </summary>
  156. public void AddStartPoint(Vector2Int point)
  157. {
  158. if (IsWalkable(point.x, point.y))
  159. {
  160. StartPoints.Add(point);
  161. }
  162. }
  163. /// <summary>
  164. /// Adds an exit point
  165. /// </summary>
  166. public void AddExitPoint(Vector2Int point)
  167. {
  168. if (IsWalkable(point.x, point.y))
  169. {
  170. ExitPoints.Add(point);
  171. }
  172. }
  173. /// <summary>
  174. /// Gets all adjacent walkable tiles
  175. /// </summary>
  176. public List<Vector2Int> GetAdjacentWalkable(int x, int y)
  177. {
  178. var adjacent = new List<Vector2Int>();
  179. Vector2Int[] directions = new[]
  180. {
  181. new Vector2Int(x + 1, y),
  182. new Vector2Int(x - 1, y),
  183. new Vector2Int(x, y + 1),
  184. new Vector2Int(x, y - 1),
  185. };
  186. foreach (var dir in directions)
  187. {
  188. if (IsWalkable(dir.x, dir.y))
  189. {
  190. adjacent.Add(dir);
  191. }
  192. }
  193. return adjacent;
  194. }
  195. /// <summary>
  196. /// Gets statistics about the maze
  197. /// </summary>
  198. public string GetStatistics()
  199. {
  200. int floorTiles = 0;
  201. int wallTiles = 0;
  202. for (int x = 0; x < Width; x++)
  203. {
  204. for (int y = 0; y < Height; y++)
  205. {
  206. if (GetTile(x, y).IsWalkable())
  207. floorTiles++;
  208. else
  209. wallTiles++;
  210. }
  211. }
  212. return $"Maze Statistics:\n" +
  213. $" Size: {Width}x{Height}\n" +
  214. $" Rooms: {Rooms.Count}\n" +
  215. $" Floor Tiles: {floorTiles}\n" +
  216. $" Wall Tiles: {wallTiles}\n" +
  217. $" Start Points: {StartPoints.Count}\n" +
  218. $" Exit Points: {ExitPoints.Count}";
  219. }
  220. }