using UnityEngine;
///
/// Example scene setup for AI agents
/// Shows how to configure and use the AI agent system
///
public class AIAgentExampleSetup : MonoBehaviour
{
[Header("Configuration")]
[SerializeField] private int initialAgentCount = 10;
[SerializeField] private string agentCharacterType = "Default";
[Header("Debug")]
[SerializeField] private bool logStats = true;
[SerializeField] private float statsLogInterval = 5f;
private MazeController mazeController;
private AIAgentManager agentManager;
private float lastStatsLog = 0f;
void Start()
{
mazeController = FindAnyObjectByType();
if (mazeController == null)
{
Debug.LogError("AIAgentExampleSetup: MazeController not found in scene!");
return;
}
// Get the agent manager
agentManager = mazeController.GetAgentManager();
if (agentManager == null)
{
Debug.LogError("AIAgentExampleSetup: Agent manager not found! Make sure spawnAIAgents is enabled on MazeController.");
return;
}
// Configure agent count if not already set
if (initialAgentCount > 0)
{
agentManager.SetInitialAgentCount(initialAgentCount);
}
Debug.Log($"AI Agent Example Setup initialized. Agents will spawn when maze is generated.");
Debug.Log($"Configuration: {initialAgentCount} agents of type '{agentCharacterType}'");
Debug.Log("Controls:");
Debug.Log(" A - Spawn 1 agent");
Debug.Log(" S - Spawn 5 agents");
Debug.Log(" C - Clear all agents");
}
void Update()
{
if (agentManager == null) return;
// Log statistics periodically
if (logStats && Time.time - lastStatsLog > statsLogInterval)
{
LogAgentStats();
lastStatsLog = Time.time;
}
// Handle example keyboard input
HandleExampleInput();
}
///
/// Logs agent statistics to console
///
private void LogAgentStats()
{
string stats = agentManager.GetAgentStats();
Debug.Log("=== Agent Statistics ===\n" + stats);
}
///
/// Handles example keyboard input for agent management
///
private void HandleExampleInput()
{
// Spawn 1 agent
if (Input.GetKeyDown(KeyCode.A))
{
var newAgent = agentManager.SpawnAgent();
if (newAgent != null)
{
Debug.Log($"Spawned agent {newAgent.AgentId}");
}
}
// Spawn 5 agents
if (Input.GetKeyDown(KeyCode.S))
{
agentManager.SpawnAgents(5);
Debug.Log("Spawning 5 agents...");
}
// Clear all agents
if (Input.GetKeyDown(KeyCode.C))
{
agentManager.ClearAllAgents();
Debug.Log("Cleared all agents");
}
// Log stats
if (Input.GetKeyDown(KeyCode.L))
{
LogAgentStats();
}
// Regenerate maze
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log("Regenerating maze...");
mazeController.GenerateMaze();
}
}
}