| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- using UnityEngine;
- using System.Collections.Generic;
- /// <summary>
- /// Example usage of the maze generation system
- /// Shows how to generate, query, and navigate the maze
- /// </summary>
- 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<MazeController>();
- }
- }
- void Start()
- {
- // Wait a frame for maze to generate
- Invoke(nameof(PrintMazeInfo), 0.1f);
- }
- /// <summary>
- /// Example: Print information about the generated maze
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// Example: Print information about all rooms
- /// </summary>
- private void PrintRoomInfo(MazeData maze)
- {
- Debug.Log("\n=== ROOM INFORMATION ===");
- var roomsByType = new Dictionary<MazeRoom.RoomType, List<MazeRoom>>();
- foreach (var room in maze.Rooms)
- {
- if (!roomsByType.ContainsKey(room.Type))
- {
- roomsByType[room.Type] = new List<MazeRoom>();
- }
- 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);
- }
- }
- }
- /// <summary>
- /// Example: Print terrain distribution
- /// </summary>
- private void PrintTerrainInfo(MazeData maze)
- {
- Debug.Log("\n=== TERRAIN DISTRIBUTION ===");
- var terrainCounts = new Dictionary<MazeTile.TerrainType, int>();
- 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");
- }
- }
- /// <summary>
- /// Example: Test pathfinding between start and end
- /// </summary>
- 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!");
- }
- }
- /// <summary>
- /// Example: Find all reachable areas from a position
- /// </summary>
- 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}");
- }
- /// <summary>
- /// Example: Query a specific position in the maze
- /// </summary>
- 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})");
- }
- }
- /// <summary>
- /// Example: Regenerate maze with different settings
- /// </summary>
- public void RegenerateMaze()
- {
- Debug.Log("Regenerating maze...");
- mazeController.GenerateMaze();
- Invoke(nameof(PrintMazeInfo), 0.1f);
- }
- }
|