| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using UnityEngine;
- using System.Collections.Generic;
- using System.Linq;
- /// <summary>
- /// Tracks which rooms have been visited by agents of a specific character type
- /// Shared among all agents of the same type to create a collective memory
- /// </summary>
- public class AIRoomMemory
- {
- private string characterType;
- private HashSet<int> visitedRoomIds = new();
- private Dictionary<int, float> roomLastVisitTime = new();
- public AIRoomMemory(string characterType)
- {
- this.characterType = characterType;
- }
- /// <summary>
- /// Marks a room as visited
- /// </summary>
- public void VisitRoom(int roomId)
- {
- if (!visitedRoomIds.Contains(roomId))
- {
- visitedRoomIds.Add(roomId);
- }
- roomLastVisitTime[roomId] = Time.time;
- }
- /// <summary>
- /// Checks if a room has been visited
- /// </summary>
- public bool HasVisited(int roomId)
- {
- return visitedRoomIds.Contains(roomId);
- }
- /// <summary>
- /// Checks if a room has NOT been visited (convenience method)
- /// </summary>
- public bool IsRoomUnvisited(int roomId)
- {
- return !visitedRoomIds.Contains(roomId);
- }
- /// <summary>
- /// Gets all visited room IDs
- /// </summary>
- public HashSet<int> GetVisitedRooms()
- {
- return new HashSet<int>(visitedRoomIds);
- }
- /// <summary>
- /// Gets unvisited rooms from a list of options
- /// </summary>
- public List<MazeRoom> FilterUnvisitedRooms(List<MazeRoom> rooms)
- {
- return rooms.Where(r => !HasVisited(r.Id)).ToList();
- }
- /// <summary>
- /// Gets the character type this memory is for
- /// </summary>
- public string CharacterType => characterType;
- /// <summary>
- /// Gets the number of visited rooms
- /// </summary>
- public int VisitedCount => visitedRoomIds.Count;
- }
- /// <summary>
- /// Global manager for all agent room memories
- /// Allows different character types to have separate memory systems
- /// </summary>
- public class AIRoomMemoryManager : MonoBehaviour
- {
- private static AIRoomMemoryManager instance;
- private Dictionary<string, AIRoomMemory> memoryByCharacterType = new();
- void Awake()
- {
- if (instance == null)
- {
- instance = this;
- DontDestroyOnLoad(gameObject);
- }
- else if (instance != this)
- {
- Destroy(gameObject);
- }
- }
- /// <summary>
- /// Gets or creates a room memory for a character type
- /// </summary>
- public static AIRoomMemory GetMemory(string characterType)
- {
- if (instance == null)
- {
- GameObject go = new GameObject("AIRoomMemoryManager");
- instance = go.AddComponent<AIRoomMemoryManager>();
- }
- if (!instance.memoryByCharacterType.ContainsKey(characterType))
- {
- instance.memoryByCharacterType[characterType] = new AIRoomMemory(characterType);
- }
- return instance.memoryByCharacterType[characterType];
- }
- /// <summary>
- /// Clears all memory for a character type (useful for new maze)
- /// </summary>
- public static void ClearMemory(string characterType)
- {
- if (instance != null && instance.memoryByCharacterType.ContainsKey(characterType))
- {
- instance.memoryByCharacterType.Remove(characterType);
- }
- }
- /// <summary>
- /// Clears all memories
- /// </summary>
- public static void ClearAllMemories()
- {
- if (instance != null)
- {
- instance.memoryByCharacterType.Clear();
- }
- }
- }
|