MazeData.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. // O(1) room lookup by ID – avoids scanning Rooms list every call
  18. private readonly Dictionary<int, MazeRoom> _roomById = new();
  19. // Cached typed-room lists – invalidated when a room is added
  20. private readonly Dictionary<MazeRoom.RoomType, List<MazeRoom>> _roomsByType = new();
  21. public MazeData(int width, int height)
  22. {
  23. Width = width;
  24. Height = height;
  25. Tiles = new MazeTile[width, height];
  26. // Initialize all tiles as walls
  27. for (int x = 0; x < width; x++)
  28. {
  29. for (int y = 0; y < height; y++)
  30. {
  31. Tiles[x, y] = new MazeTile(x, y);
  32. }
  33. }
  34. }
  35. /// <summary>
  36. /// Sets a tile at the given position
  37. /// </summary>
  38. public void SetTile(int x, int y, MazeTile tile)
  39. {
  40. if (IsInBounds(x, y))
  41. {
  42. Tiles[x, y] = tile;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets a tile at the given position
  47. /// </summary>
  48. public MazeTile GetTile(int x, int y)
  49. {
  50. if (IsInBounds(x, y))
  51. {
  52. return Tiles[x, y];
  53. }
  54. return null;
  55. }
  56. /// <summary>
  57. /// Checks if coordinates are within maze bounds
  58. /// </summary>
  59. public bool IsInBounds(int x, int y)
  60. {
  61. return x >= 0 && x < Width && y >= 0 && y < Height;
  62. }
  63. /// <summary>
  64. /// Checks if a tile is walkable
  65. /// </summary>
  66. public bool IsWalkable(int x, int y)
  67. {
  68. var tile = GetTile(x, y);
  69. return tile != null && tile.IsWalkable();
  70. }
  71. /// <summary>
  72. /// Fills a rectangular area with floor tiles and assigns to a room
  73. /// </summary>
  74. public int FillRoom(int minX, int minY, int maxX, int maxY, int roomId, MazeTile.TerrainType terrain = MazeTile.TerrainType.Normal)
  75. {
  76. int changedTiles = 0;
  77. for (int x = minX; x <= maxX; x++)
  78. {
  79. for (int y = minY; y <= maxY; y++)
  80. {
  81. if (IsInBounds(x, y))
  82. {
  83. var tile = GetTile(x, y);
  84. if (tile.Type != MazeTile.TileType.Floor)
  85. {
  86. changedTiles++;
  87. }
  88. tile.Type = MazeTile.TileType.Floor;
  89. tile.Terrain = terrain;
  90. tile.RoomId = roomId;
  91. }
  92. }
  93. }
  94. return changedTiles;
  95. }
  96. /// <summary>
  97. /// Creates floor tiles along a path (for hallways/corridors)
  98. /// </summary>
  99. public int DrawPath(List<Vector2Int> path, int hallwayWidth, int roomId = -1)
  100. {
  101. int changedTiles = 0;
  102. foreach (var point in path)
  103. {
  104. for (int dx = -hallwayWidth / 2; dx <= hallwayWidth / 2; dx++)
  105. {
  106. for (int dy = -hallwayWidth / 2; dy <= hallwayWidth / 2; dy++)
  107. {
  108. int x = point.x + dx;
  109. int y = point.y + dy;
  110. if (IsInBounds(x, y))
  111. {
  112. var tile = GetTile(x, y);
  113. if (tile.Type != MazeTile.TileType.Floor) // Don't overwrite existing floors
  114. {
  115. changedTiles++;
  116. tile.Type = MazeTile.TileType.Floor;
  117. tile.Terrain = MazeTile.TerrainType.Normal;
  118. if (tile.RoomId == -1)
  119. {
  120. tile.RoomId = roomId;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. return changedTiles;
  128. }
  129. /// <summary>
  130. /// Adds a room to the maze and registers it in the fast-lookup dictionary.
  131. /// </summary>
  132. public MazeRoom AddRoom(int minX, int minY, int maxX, int maxY, MazeRoom.RoomType roomType = MazeRoom.RoomType.Normal)
  133. {
  134. var room = new MazeRoom(nextRoomId++, minX, minY, maxX, maxY, roomType);
  135. Rooms.Add(room);
  136. _roomById[room.Id] = room;
  137. _roomsByType.Clear(); // invalidate type cache
  138. return room;
  139. }
  140. /// <summary>
  141. /// Gets all rooms of a specific type. Always queries fresh since room types
  142. /// can be changed after AddRoom() (e.g. exit rooms assigned later by generator).
  143. /// </summary>
  144. public List<MazeRoom> GetRoomsByType(MazeRoom.RoomType type)
  145. {
  146. return Rooms.Where(r => r.Type == type).ToList();
  147. }
  148. /// <summary>
  149. /// Gets a room by its ID in O(1).
  150. /// </summary>
  151. public MazeRoom GetRoomById(int id)
  152. => _roomById.TryGetValue(id, out var r) ? r : null;
  153. /// <summary>
  154. /// Gets the room at a specific tile position. O(1) via dictionary lookup.
  155. /// </summary>
  156. public MazeRoom GetRoomAtTile(int x, int y)
  157. {
  158. var tile = GetTile(x, y);
  159. if (tile != null && tile.RoomId >= 0 && _roomById.TryGetValue(tile.RoomId, out var room))
  160. return room;
  161. return null;
  162. }
  163. /// <summary>
  164. /// Adds a start point
  165. /// </summary>
  166. public void AddStartPoint(Vector2Int point)
  167. {
  168. if (IsWalkable(point.x, point.y))
  169. {
  170. StartPoints.Add(point);
  171. }
  172. }
  173. /// <summary>
  174. /// Adds an exit point
  175. /// </summary>
  176. public void AddExitPoint(Vector2Int point)
  177. {
  178. if (IsWalkable(point.x, point.y))
  179. {
  180. ExitPoints.Add(point);
  181. }
  182. }
  183. /// <summary>
  184. /// Gets all adjacent walkable tiles
  185. /// </summary>
  186. public List<Vector2Int> GetAdjacentWalkable(int x, int y)
  187. {
  188. var adjacent = new List<Vector2Int>();
  189. Vector2Int[] directions = new[]
  190. {
  191. new Vector2Int(x + 1, y),
  192. new Vector2Int(x - 1, y),
  193. new Vector2Int(x, y + 1),
  194. new Vector2Int(x, y - 1),
  195. };
  196. foreach (var dir in directions)
  197. {
  198. if (IsWalkable(dir.x, dir.y))
  199. {
  200. adjacent.Add(dir);
  201. }
  202. }
  203. return adjacent;
  204. }
  205. /// <summary>
  206. /// Gets statistics about the maze
  207. /// </summary>
  208. public string GetStatistics()
  209. {
  210. int floorTiles = 0;
  211. int wallTiles = 0;
  212. for (int x = 0; x < Width; x++)
  213. {
  214. for (int y = 0; y < Height; y++)
  215. {
  216. if (GetTile(x, y).IsWalkable())
  217. floorTiles++;
  218. else
  219. wallTiles++;
  220. }
  221. }
  222. return $"Maze Statistics:\n" +
  223. $" Size: {Width}x{Height}\n" +
  224. $" Rooms: {Rooms.Count}\n" +
  225. $" Floor Tiles: {floorTiles}\n" +
  226. $" Wall Tiles: {wallTiles}\n" +
  227. $" Start Points: {StartPoints.Count}\n" +
  228. $" Exit Points: {ExitPoints.Count}";
  229. }
  230. }