MazeAgent.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. namespace MazeGen
  4. {
  5. /// <summary>
  6. /// Tracks one agent's progress through the maze: current room, ordered path history
  7. /// (for visualization/backtracking), and O(1) sets for "have I been here" / "have I
  8. /// used this exit" checks. Plain C# class - no MonoBehaviour/scene dependency, so it's
  9. /// cheap to run many of these (e.g. for future autonomous multi-agent scenarios).
  10. /// </summary>
  11. public class MazeAgent
  12. {
  13. public readonly string Name;
  14. public Room CurrentRoom { get; private set; }
  15. public int NewRoomsVisited { get; private set; }
  16. private readonly List<Room> _path = new List<Room>();
  17. private readonly HashSet<int> _visitedRoomIds = new HashSet<int>();
  18. private readonly HashSet<int> _usedExitIds = new HashSet<int>();
  19. public IReadOnlyList<Room> PathHistory => _path;
  20. public event Action<Room> RoomEntered;
  21. public event Action<Room> ReachedExitRoom;
  22. public MazeAgent(string name, Room startRoom)
  23. {
  24. Name = name;
  25. EnterRoom(startRoom, null);
  26. }
  27. public bool HasVisited(int roomId) => _visitedRoomIds.Contains(roomId);
  28. public bool HasUsedExit(Exit exit) => _usedExitIds.Contains(exit.Id);
  29. public void EnterRoom(Room room, Exit viaExit)
  30. {
  31. if (viaExit != null)
  32. _usedExitIds.Add(viaExit.Id);
  33. if (_visitedRoomIds.Add(room.Id))
  34. NewRoomsVisited++;
  35. CurrentRoom = room;
  36. _path.Add(room);
  37. RoomEntered?.Invoke(room);
  38. if (room.Kind == RoomKind.Exit)
  39. ReachedExitRoom?.Invoke(room);
  40. }
  41. }
  42. }