| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using System;
- using System.Collections.Generic;
- namespace MazeGen
- {
- /// <summary>
- /// Tracks one agent's progress through the maze: current room, ordered path history
- /// (for visualization/backtracking), and O(1) sets for "have I been here" / "have I
- /// used this exit" checks. Plain C# class - no MonoBehaviour/scene dependency, so it's
- /// cheap to run many of these (e.g. for future autonomous multi-agent scenarios).
- /// </summary>
- public class MazeAgent
- {
- public readonly string Name;
- public Room CurrentRoom { get; private set; }
- public int NewRoomsVisited { get; private set; }
- private readonly List<Room> _path = new List<Room>();
- private readonly HashSet<int> _visitedRoomIds = new HashSet<int>();
- private readonly HashSet<int> _usedExitIds = new HashSet<int>();
- public IReadOnlyList<Room> PathHistory => _path;
- public event Action<Room> RoomEntered;
- public event Action<Room> ReachedExitRoom;
- public MazeAgent(string name, Room startRoom)
- {
- Name = name;
- EnterRoom(startRoom, null);
- }
- public bool HasVisited(int roomId) => _visitedRoomIds.Contains(roomId);
- public bool HasUsedExit(Exit exit) => _usedExitIds.Contains(exit.Id);
- public void EnterRoom(Room room, Exit viaExit)
- {
- if (viaExit != null)
- _usedExitIds.Add(viaExit.Id);
- if (_visitedRoomIds.Add(room.Id))
- NewRoomsVisited++;
- CurrentRoom = room;
- _path.Add(room);
- RoomEntered?.Invoke(room);
- if (room.Kind == RoomKind.Exit)
- ReachedExitRoom?.Invoke(room);
- }
- }
- }
|