using UnityEngine; using System.Collections.Generic; /// /// Example usage of the maze generation system /// Shows how to generate, query, and navigate the maze /// public class MazeExample : MonoBehaviour { [SerializeField] private MazeController mazeController; [SerializeField] private bool logRoomInfo = true; [SerializeField] private bool logTerrainInfo = true; [SerializeField] private bool performPathfindingTest = true; void Awake() { if (mazeController == null) { mazeController = GetComponent(); } } void Start() { // Wait a frame for maze to generate Invoke(nameof(PrintMazeInfo), 0.1f); } /// /// Example: Print information about the generated maze /// public void PrintMazeInfo() { var maze = mazeController.GetCurrentMaze(); if (maze == null) { Debug.LogWarning("No maze generated yet"); return; } Debug.Log("=== MAZE INFORMATION ==="); Debug.Log(maze.GetStatistics()); if (logRoomInfo) { PrintRoomInfo(maze); } if (logTerrainInfo) { PrintTerrainInfo(maze); } if (performPathfindingTest) { PerformPathfindingTest(maze); } } /// /// Example: Print information about all rooms /// private void PrintRoomInfo(MazeData maze) { Debug.Log("\n=== ROOM INFORMATION ==="); var roomsByType = new Dictionary>(); foreach (var room in maze.Rooms) { if (!roomsByType.ContainsKey(room.Type)) { roomsByType[room.Type] = new List(); } roomsByType[room.Type].Add(room); } foreach (var kvp in roomsByType) { Debug.Log($"{kvp.Key} Rooms ({kvp.Value.Count}):"); foreach (var room in kvp.Value) { string info = $" Room {room.Id}: {room.Width}x{room.Height} at ({room.MinX}, {room.MinY})"; if (room.IsStart) info += " [START]"; if (room.IsEnd) info += " [END]"; Debug.Log(info); } } } /// /// Example: Print terrain distribution /// private void PrintTerrainInfo(MazeData maze) { Debug.Log("\n=== TERRAIN DISTRIBUTION ==="); var terrainCounts = new Dictionary(); for (int x = 0; x < maze.Width; x++) { for (int y = 0; y < maze.Height; y++) { var tile = maze.GetTile(x, y); if (tile.Type == MazeTile.TileType.Floor || tile.Type == MazeTile.TileType.Terrain) { if (!terrainCounts.ContainsKey(tile.Terrain)) { terrainCounts[tile.Terrain] = 0; } terrainCounts[tile.Terrain]++; } } } foreach (var kvp in terrainCounts) { Debug.Log($" {kvp.Key}: {kvp.Value} tiles"); } } /// /// Example: Test pathfinding between start and end /// private void PerformPathfindingTest(MazeData maze) { if (maze.StartPoints.Count == 0 || maze.ExitPoints.Count == 0) { Debug.LogWarning("No start or exit points to test pathfinding"); return; } var pathfinder = new MazePathfinder(maze); var start = maze.StartPoints[0]; var goal = maze.ExitPoints[0]; Debug.Log("\n=== PATHFINDING TEST ==="); Debug.Log($"Finding path from {start} to {goal}..."); var path = pathfinder.FindPath(start, goal); if (path.Count > 0) { Debug.Log($"Path found! Length: {path.Count} tiles"); // Calculate total movement cost float totalCost = 0f; for (int i = 0; i < path.Count; i++) { var tile = maze.GetTile(path[i].x, path[i].y); totalCost += tile.GetMovementCost(); } Debug.Log($"Total movement cost: {totalCost:F2}x"); // Show path preview (first 20 tiles) string pathPreview = "Path: "; for (int i = 0; i < Mathf.Min(20, path.Count); i++) { pathPreview += path[i] + " "; } if (path.Count > 20) pathPreview += "..."; Debug.Log(pathPreview); } else { Debug.LogWarning("No path found between start and goal!"); } } /// /// Example: Find all reachable areas from a position /// public void TestReachableAreas() { var maze = mazeController.GetCurrentMaze(); if (maze == null || maze.StartPoints.Count == 0) { Debug.LogWarning("No maze or start points"); return; } var pathfinder = new MazePathfinder(maze); var start = maze.StartPoints[0]; var reachable = pathfinder.FindReachableTiles(start, 50f); Debug.Log($"Found {reachable.Count} tiles reachable within distance 50 from {start}"); } /// /// Example: Query a specific position in the maze /// public void QueryPosition(int x, int y) { var maze = mazeController.GetCurrentMaze(); if (maze == null) { Debug.LogWarning("No maze generated"); return; } var tile = maze.GetTile(x, y); if (tile == null) { Debug.Log($"Position ({x}, {y}) is out of bounds"); return; } Debug.Log($"\n=== POSITION ({x}, {y}) ==="); Debug.Log($"Tile Type: {tile.Type}"); Debug.Log($"Terrain: {tile.Terrain}"); Debug.Log($"Walkable: {tile.IsWalkable()}"); Debug.Log($"Movement Cost: {tile.GetMovementCost()}x"); Debug.Log($"Room ID: {tile.RoomId}"); var room = maze.GetRoomAtTile(x, y); if (room != null) { Debug.Log($"Room: {room.Type} (ID: {room.Id})"); } } /// /// Example: Regenerate maze with different settings /// public void RegenerateMaze() { Debug.Log("Regenerating maze..."); mazeController.GenerateMaze(); Invoke(nameof(PrintMazeInfo), 0.1f); } }