AIRoomMemory.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. /// <summary>
  5. /// Tracks which rooms have been visited by agents of a specific character type
  6. /// Shared among all agents of the same type to create a collective memory
  7. /// </summary>
  8. public class AIRoomMemory
  9. {
  10. private string characterType;
  11. private HashSet<int> visitedRoomIds = new();
  12. private Dictionary<int, float> roomLastVisitTime = new();
  13. public AIRoomMemory(string characterType)
  14. {
  15. this.characterType = characterType;
  16. }
  17. /// <summary>
  18. /// Marks a room as visited
  19. /// </summary>
  20. public void VisitRoom(int roomId)
  21. {
  22. if (!visitedRoomIds.Contains(roomId))
  23. {
  24. visitedRoomIds.Add(roomId);
  25. }
  26. roomLastVisitTime[roomId] = Time.time;
  27. }
  28. /// <summary>
  29. /// Checks if a room has been visited
  30. /// </summary>
  31. public bool HasVisited(int roomId)
  32. {
  33. return visitedRoomIds.Contains(roomId);
  34. }
  35. /// <summary>
  36. /// Checks if a room has NOT been visited (convenience method)
  37. /// </summary>
  38. public bool IsRoomUnvisited(int roomId)
  39. {
  40. return !visitedRoomIds.Contains(roomId);
  41. }
  42. /// <summary>
  43. /// Gets all visited room IDs
  44. /// </summary>
  45. public HashSet<int> GetVisitedRooms()
  46. {
  47. return new HashSet<int>(visitedRoomIds);
  48. }
  49. /// <summary>
  50. /// Gets unvisited rooms from a list of options
  51. /// </summary>
  52. public List<MazeRoom> FilterUnvisitedRooms(List<MazeRoom> rooms)
  53. {
  54. return rooms.Where(r => !HasVisited(r.Id)).ToList();
  55. }
  56. /// <summary>
  57. /// Gets the character type this memory is for
  58. /// </summary>
  59. public string CharacterType => characterType;
  60. /// <summary>
  61. /// Gets the number of visited rooms
  62. /// </summary>
  63. public int VisitedCount => visitedRoomIds.Count;
  64. }
  65. /// <summary>
  66. /// Global manager for all agent room memories
  67. /// Allows different character types to have separate memory systems
  68. /// </summary>
  69. public class AIRoomMemoryManager : MonoBehaviour
  70. {
  71. private static AIRoomMemoryManager instance;
  72. private Dictionary<string, AIRoomMemory> memoryByCharacterType = new();
  73. void Awake()
  74. {
  75. if (instance == null)
  76. {
  77. instance = this;
  78. DontDestroyOnLoad(gameObject);
  79. }
  80. else if (instance != this)
  81. {
  82. Destroy(gameObject);
  83. }
  84. }
  85. /// <summary>
  86. /// Gets or creates a room memory for a character type
  87. /// </summary>
  88. public static AIRoomMemory GetMemory(string characterType)
  89. {
  90. if (instance == null)
  91. {
  92. GameObject go = new GameObject("AIRoomMemoryManager");
  93. instance = go.AddComponent<AIRoomMemoryManager>();
  94. }
  95. if (!instance.memoryByCharacterType.ContainsKey(characterType))
  96. {
  97. instance.memoryByCharacterType[characterType] = new AIRoomMemory(characterType);
  98. }
  99. return instance.memoryByCharacterType[characterType];
  100. }
  101. /// <summary>
  102. /// Clears all memory for a character type (useful for new maze)
  103. /// </summary>
  104. public static void ClearMemory(string characterType)
  105. {
  106. if (instance != null && instance.memoryByCharacterType.ContainsKey(characterType))
  107. {
  108. instance.memoryByCharacterType.Remove(characterType);
  109. }
  110. }
  111. /// <summary>
  112. /// Clears all memories
  113. /// </summary>
  114. public static void ClearAllMemories()
  115. {
  116. if (instance != null)
  117. {
  118. instance.memoryByCharacterType.Clear();
  119. }
  120. }
  121. }