MazeExample.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. /// <summary>
  4. /// Example usage of the maze generation system
  5. /// Shows how to generate, query, and navigate the maze
  6. /// </summary>
  7. public class MazeExample : MonoBehaviour
  8. {
  9. [SerializeField] private MazeController mazeController;
  10. [SerializeField] private bool logRoomInfo = true;
  11. [SerializeField] private bool logTerrainInfo = true;
  12. [SerializeField] private bool performPathfindingTest = true;
  13. void Awake()
  14. {
  15. if (mazeController == null)
  16. {
  17. mazeController = GetComponent<MazeController>();
  18. }
  19. }
  20. void Start()
  21. {
  22. // Wait a frame for maze to generate
  23. Invoke(nameof(PrintMazeInfo), 0.1f);
  24. }
  25. /// <summary>
  26. /// Example: Print information about the generated maze
  27. /// </summary>
  28. public void PrintMazeInfo()
  29. {
  30. var maze = mazeController.GetCurrentMaze();
  31. if (maze == null)
  32. {
  33. Debug.LogWarning("No maze generated yet");
  34. return;
  35. }
  36. Debug.Log("=== MAZE INFORMATION ===");
  37. Debug.Log(maze.GetStatistics());
  38. if (logRoomInfo)
  39. {
  40. PrintRoomInfo(maze);
  41. }
  42. if (logTerrainInfo)
  43. {
  44. PrintTerrainInfo(maze);
  45. }
  46. if (performPathfindingTest)
  47. {
  48. PerformPathfindingTest(maze);
  49. }
  50. }
  51. /// <summary>
  52. /// Example: Print information about all rooms
  53. /// </summary>
  54. private void PrintRoomInfo(MazeData maze)
  55. {
  56. Debug.Log("\n=== ROOM INFORMATION ===");
  57. var roomsByType = new Dictionary<MazeRoom.RoomType, List<MazeRoom>>();
  58. foreach (var room in maze.Rooms)
  59. {
  60. if (!roomsByType.ContainsKey(room.Type))
  61. {
  62. roomsByType[room.Type] = new List<MazeRoom>();
  63. }
  64. roomsByType[room.Type].Add(room);
  65. }
  66. foreach (var kvp in roomsByType)
  67. {
  68. Debug.Log($"{kvp.Key} Rooms ({kvp.Value.Count}):");
  69. foreach (var room in kvp.Value)
  70. {
  71. string info = $" Room {room.Id}: {room.Width}x{room.Height} at ({room.MinX}, {room.MinY})";
  72. if (room.IsStart) info += " [START]";
  73. if (room.IsEnd) info += " [END]";
  74. Debug.Log(info);
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// Example: Print terrain distribution
  80. /// </summary>
  81. private void PrintTerrainInfo(MazeData maze)
  82. {
  83. Debug.Log("\n=== TERRAIN DISTRIBUTION ===");
  84. var terrainCounts = new Dictionary<MazeTile.TerrainType, int>();
  85. for (int x = 0; x < maze.Width; x++)
  86. {
  87. for (int y = 0; y < maze.Height; y++)
  88. {
  89. var tile = maze.GetTile(x, y);
  90. if (tile.Type == MazeTile.TileType.Floor || tile.Type == MazeTile.TileType.Terrain)
  91. {
  92. if (!terrainCounts.ContainsKey(tile.Terrain))
  93. {
  94. terrainCounts[tile.Terrain] = 0;
  95. }
  96. terrainCounts[tile.Terrain]++;
  97. }
  98. }
  99. }
  100. foreach (var kvp in terrainCounts)
  101. {
  102. Debug.Log($" {kvp.Key}: {kvp.Value} tiles");
  103. }
  104. }
  105. /// <summary>
  106. /// Example: Test pathfinding between start and end
  107. /// </summary>
  108. private void PerformPathfindingTest(MazeData maze)
  109. {
  110. if (maze.StartPoints.Count == 0 || maze.ExitPoints.Count == 0)
  111. {
  112. Debug.LogWarning("No start or exit points to test pathfinding");
  113. return;
  114. }
  115. var pathfinder = new MazePathfinder(maze);
  116. var start = maze.StartPoints[0];
  117. var goal = maze.ExitPoints[0];
  118. Debug.Log("\n=== PATHFINDING TEST ===");
  119. Debug.Log($"Finding path from {start} to {goal}...");
  120. var path = pathfinder.FindPath(start, goal);
  121. if (path.Count > 0)
  122. {
  123. Debug.Log($"Path found! Length: {path.Count} tiles");
  124. // Calculate total movement cost
  125. float totalCost = 0f;
  126. for (int i = 0; i < path.Count; i++)
  127. {
  128. var tile = maze.GetTile(path[i].x, path[i].y);
  129. totalCost += tile.GetMovementCost();
  130. }
  131. Debug.Log($"Total movement cost: {totalCost:F2}x");
  132. // Show path preview (first 20 tiles)
  133. string pathPreview = "Path: ";
  134. for (int i = 0; i < Mathf.Min(20, path.Count); i++)
  135. {
  136. pathPreview += path[i] + " ";
  137. }
  138. if (path.Count > 20)
  139. pathPreview += "...";
  140. Debug.Log(pathPreview);
  141. }
  142. else
  143. {
  144. Debug.LogWarning("No path found between start and goal!");
  145. }
  146. }
  147. /// <summary>
  148. /// Example: Find all reachable areas from a position
  149. /// </summary>
  150. public void TestReachableAreas()
  151. {
  152. var maze = mazeController.GetCurrentMaze();
  153. if (maze == null || maze.StartPoints.Count == 0)
  154. {
  155. Debug.LogWarning("No maze or start points");
  156. return;
  157. }
  158. var pathfinder = new MazePathfinder(maze);
  159. var start = maze.StartPoints[0];
  160. var reachable = pathfinder.FindReachableTiles(start, 50f);
  161. Debug.Log($"Found {reachable.Count} tiles reachable within distance 50 from {start}");
  162. }
  163. /// <summary>
  164. /// Example: Query a specific position in the maze
  165. /// </summary>
  166. public void QueryPosition(int x, int y)
  167. {
  168. var maze = mazeController.GetCurrentMaze();
  169. if (maze == null)
  170. {
  171. Debug.LogWarning("No maze generated");
  172. return;
  173. }
  174. var tile = maze.GetTile(x, y);
  175. if (tile == null)
  176. {
  177. Debug.Log($"Position ({x}, {y}) is out of bounds");
  178. return;
  179. }
  180. Debug.Log($"\n=== POSITION ({x}, {y}) ===");
  181. Debug.Log($"Tile Type: {tile.Type}");
  182. Debug.Log($"Terrain: {tile.Terrain}");
  183. Debug.Log($"Walkable: {tile.IsWalkable()}");
  184. Debug.Log($"Movement Cost: {tile.GetMovementCost()}x");
  185. Debug.Log($"Room ID: {tile.RoomId}");
  186. var room = maze.GetRoomAtTile(x, y);
  187. if (room != null)
  188. {
  189. Debug.Log($"Room: {room.Type} (ID: {room.Id})");
  190. }
  191. }
  192. /// <summary>
  193. /// Example: Regenerate maze with different settings
  194. /// </summary>
  195. public void RegenerateMaze()
  196. {
  197. Debug.Log("Regenerating maze...");
  198. mazeController.GenerateMaze();
  199. Invoke(nameof(PrintMazeInfo), 0.1f);
  200. }
  201. }