| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- 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).
- ///
- /// Also tracks the agent's combat state (character sheet, health, equipment).
- /// </summary>
- public class MazeAgent
- {
- public readonly string Name;
- public Room CurrentRoom { get; private set; }
- public int NewRoomsVisited { get; private set; }
- public bool IsFinished { get; private set; }
- /// <summary>The exit the agent walked through to reach CurrentRoom, if any (null for the starting room). Used by AutonomousAgentBrain to avoid immediately reversing.</summary>
- public Exit LastUsedExit { get; private set; }
- /// <summary>This agent's character sheet (stats, equipment, inventory).</summary>
- public CharacterSheet CharacterSheet { get; private set; }
- /// <summary>Track if this agent died (different from IsFinished, which means reached the exit).</summary>
- public bool IsDead => !CharacterSheet.Stats.IsAlive;
- private readonly List<Room> _path = new List<Room>();
- private readonly HashSet<int> _visitedRoomIds = new HashSet<int>();
- private readonly HashSet<int> _usedExitIds = new HashSet<int>();
- private readonly Dictionary<int, int> _visitCounts = new Dictionary<int, int>();
- public IReadOnlyList<Room> PathHistory => _path;
- public event Action<Room> RoomEntered;
- public event Action<Room> ReachedExitRoom;
- public event Action Died;
- public MazeAgent(string name, Room startRoom, CharacterSheet characterSheet = null)
- {
- Name = name;
- CharacterSheet = characterSheet ?? new CharacterSheet(new CharacterStats());
- EnterRoom(startRoom, null);
- }
- public bool HasVisited(int roomId) => _visitedRoomIds.Contains(roomId);
- public bool HasUsedExit(Exit exit) => _usedExitIds.Contains(exit.Id);
- /// <summary>How many times this agent has personally entered the given room (0 if never).</summary>
- public int VisitCount(int roomId) => _visitCounts.TryGetValue(roomId, out var count) ? count : 0;
- public void EnterRoom(Room room, Exit viaExit)
- {
- // Check if agent is already dead before entering
- if (IsDead)
- return;
- if (viaExit != null)
- _usedExitIds.Add(viaExit.Id);
- if (_visitedRoomIds.Add(room.Id))
- NewRoomsVisited++;
- _visitCounts[room.Id] = VisitCount(room.Id) + 1;
- CurrentRoom = room;
- LastUsedExit = viaExit;
- _path.Add(room);
- RoomEntered?.Invoke(room);
- if (room.Kind == RoomKind.Exit)
- {
- IsFinished = true;
- ReachedExitRoom?.Invoke(room);
- }
- }
- /// <summary>Called when the agent dies (defeated in combat or other hazard). Stops them from moving.</summary>
- public void Die()
- {
- if (IsDead) return;
- CharacterSheet.Stats.CurrentHealth = 0;
- Died?.Invoke();
- }
- }
- }
|