The AI Agent System allows you to spawn intelligent agents that navigate the maze with limited knowledge, just like a real player would. Each agent only knows about:
This creates a realistic exploration experience where agents must be smart about which paths to take based on shared information from other agents of the same type.
AIAgent.cs)Key Features:
Inspector Properties:
agentCharacterType: Type/name of the agent (e.g., "Warrior", "Scout")movementSpeed: How fast the agent moves (default: 2)pathUpdateInterval: How often to recalculate path (default: 0.5s)stoppingDistance: Distance to stop at waypoint (default: 0.1)showPath: Draw path visualization (default: true)pathColor: Color of path visualizationAIAgentManager.cs)Key Features:
Inspector Properties:
initialAgentCount: How many agents to spawn at start (default: 10)agentCharacterType: Default character type for agents (default: "Default")spawnDelay: Delay between spawning each agent (default: 0.1s)agentPrefab: Prefab to use for agent (auto-created if not set)showAgentPaths: Show paths for all agents (default: true)agentPathColor: Color for agent paths (default: yellow)Key Methods:
// Spawn a single agent
AIAgent agent = agentManager.SpawnAgent();
// Spawn multiple agents
agentManager.SpawnAgents(5);
// Get agent counts
int total = agentManager.GetAgentCount();
int byType = agentManager.GetAgentCountByType("Warrior");
// Get stats for display
string stats = agentManager.GetAgentStats();
// Reset for new maze
agentManager.ResetForNewMaze();
AIRoomMemory.cs)Key Features:
Usage:
// Get or create memory for a character type
AIRoomMemory memory = AIRoomMemoryManager.GetMemory("Warrior");
// Mark a room as visited
memory.VisitRoom(roomId);
// Check if room was visited
if (memory.HasVisited(roomId)) { ... }
// Get all visited rooms
HashSet<int> visited = memory.GetVisitedRooms();
// Filter unvisited from a list
var unvisited = memory.FilterUnvisitedRooms(roomList);
AIAgentUIDisplay.cs) - OptionalFeatures:
Keyboard Controls:
A: Spawn 1 agentS: Spawn 5 agentsC: Clear all agentsAttach to MazeController:
That's it! Agents will spawn automatically when the maze generates
Create Agent Manager:
AIAgentManager componentagentManager fieldAdd UI Display (Optional):
AIAgentUIDisplay componentIn MazeController:
spawnAIAgents is checkedpublic class GameManager : MonoBehaviour
{
void Start()
{
MazeController maze = FindFirstObjectByType<MazeController>();
AIAgentManager agentManager = maze.GetAgentManager();
// Agents already spawned if spawnAIAgents is enabled
Debug.Log($"Spawned {agentManager.GetAgentCount()} agents");
}
}
public class AISpawner : MonoBehaviour
{
private AIAgentManager agentManager;
void Start()
{
MazeController maze = FindFirstObjectByType<MazeController>();
agentManager = maze.GetAgentManager();
}
void Update()
{
// Spawn 3 agents every 10 seconds
if (Input.GetKeyDown(KeyCode.E))
{
agentManager.SpawnAgents(3);
}
}
}
public class MultiTypeAgentSpawner : MonoBehaviour
{
void Start()
{
MazeController maze = FindFirstObjectByType<MazeController>();
AIAgentManager agentManager = maze.GetAgentManager();
// Current agents are "Default" type
// To spawn other types, you would need to:
// 1. Create new managers for each type
// 2. Or modify agentCharacterType and spawn
// Example: Manually spawn agents of different types
SpawnAgentType("Warrior", 5);
SpawnAgentType("Scout", 3);
SpawnAgentType("Rogue", 2);
}
void SpawnAgentType(string type, int count)
{
for (int i = 0; i < count; i++)
{
// This would require extending AIAgentManager or creating type-specific managers
}
}
}
public class AgentProgressMonitor : MonoBehaviour
{
private AIAgentManager agentManager;
private float checkInterval = 2f;
private float lastCheck = 0f;
void Start()
{
MazeController maze = FindFirstObjectByType<MazeController>();
agentManager = maze.GetAgentManager();
}
void Update()
{
if (Time.time - lastCheck > checkInterval)
{
string stats = agentManager.GetAgentStats();
Debug.Log(stats);
lastCheck = Time.time;
}
}
}
pathUpdateInterval for more agents (0.5s → 1.0s)spawnDelay for faster agent creationshowPath for better performanceThe system is designed to be extensible:
spawnAIAgents is enabled on MazeControllerinitialAgentCount > 0movementSpeed on AIAgent or prefabautoCreateUI and assign UI elements manuallypublic class AIAgent : MonoBehaviour
{
public int AgentId { get; }
public string CharacterType { get; }
public Vector2Int CurrentRoom { get; }
public AIRoomMemory RoomMemory { get; }
}
public class AIAgentManager : MonoBehaviour
{
public AIAgent SpawnAgent();
public void SpawnAgents(int count);
public void RemoveAgent(AIAgent agent);
public List<AIAgent> GetActiveAgents();
public List<AIAgent> GetAgentsByType(string characterType);
public int GetAgentCount();
public int GetAgentCountByType(string characterType);
public void ClearAllAgents();
public void ResetForNewMaze();
public string GetAgentStats();
public int InitialAgentCount { get; }
public void SetInitialAgentCount(int count);
}
public class AIRoomMemory
{
public void VisitRoom(int roomId);
public bool HasVisited(int roomId);
public HashSet<int> GetVisitedRooms();
public List<MazeRoom> FilterUnvisitedRooms(List<MazeRoom> rooms);
public string CharacterType { get; }
public int VisitedCount { get; }
}
public class AIRoomMemoryManager : MonoBehaviour
{
public static AIRoomMemory GetMemory(string characterType);
public static void ClearMemory(string characterType);
public static void ClearAllMemories();
}
Happy maze exploring! 🎮