AIAgentExampleSetup.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using UnityEngine;
  2. /// <summary>
  3. /// Example scene setup for AI agents
  4. /// Shows how to configure and use the AI agent system
  5. /// </summary>
  6. public class AIAgentExampleSetup : MonoBehaviour
  7. {
  8. [Header("Configuration")]
  9. [SerializeField] private int initialAgentCount = 10;
  10. [SerializeField] private string agentCharacterType = "Default";
  11. [SerializeField] private bool showUIDisplay = true;
  12. [Header("Debug")]
  13. [SerializeField] private bool logStats = true;
  14. [SerializeField] private float statsLogInterval = 5f;
  15. private MazeController mazeController;
  16. private AIAgentManager agentManager;
  17. private float lastStatsLog = 0f;
  18. void Start()
  19. {
  20. mazeController = FindAnyObjectByType<MazeController>();
  21. if (mazeController == null)
  22. {
  23. Debug.LogError("AIAgentExampleSetup: MazeController not found in scene!");
  24. return;
  25. }
  26. // Get the agent manager
  27. agentManager = mazeController.GetAgentManager();
  28. if (agentManager == null)
  29. {
  30. Debug.LogError("AIAgentExampleSetup: Agent manager not found! Make sure spawnAIAgents is enabled on MazeController.");
  31. return;
  32. }
  33. // Configure agent count if not already set
  34. if (initialAgentCount > 0)
  35. {
  36. agentManager.SetInitialAgentCount(initialAgentCount);
  37. }
  38. Debug.Log($"AI Agent Example Setup initialized. Agents will spawn when maze is generated.");
  39. Debug.Log($"Configuration: {initialAgentCount} agents of type '{agentCharacterType}'");
  40. Debug.Log("Controls:");
  41. Debug.Log(" A - Spawn 1 agent");
  42. Debug.Log(" S - Spawn 5 agents");
  43. Debug.Log(" C - Clear all agents");
  44. }
  45. void Update()
  46. {
  47. if (agentManager == null) return;
  48. // Log statistics periodically
  49. if (logStats && Time.time - lastStatsLog > statsLogInterval)
  50. {
  51. LogAgentStats();
  52. lastStatsLog = Time.time;
  53. }
  54. // Handle example keyboard input
  55. HandleExampleInput();
  56. }
  57. /// <summary>
  58. /// Logs agent statistics to console
  59. /// </summary>
  60. private void LogAgentStats()
  61. {
  62. string stats = agentManager.GetAgentStats();
  63. Debug.Log("=== Agent Statistics ===\n" + stats);
  64. }
  65. /// <summary>
  66. /// Handles example keyboard input for agent management
  67. /// </summary>
  68. private void HandleExampleInput()
  69. {
  70. // Spawn 1 agent
  71. if (Input.GetKeyDown(KeyCode.A))
  72. {
  73. var newAgent = agentManager.SpawnAgent();
  74. if (newAgent != null)
  75. {
  76. Debug.Log($"Spawned agent {newAgent.AgentId}");
  77. }
  78. }
  79. // Spawn 5 agents
  80. if (Input.GetKeyDown(KeyCode.S))
  81. {
  82. agentManager.SpawnAgents(5);
  83. Debug.Log("Spawning 5 agents...");
  84. }
  85. // Clear all agents
  86. if (Input.GetKeyDown(KeyCode.C))
  87. {
  88. agentManager.ClearAllAgents();
  89. Debug.Log("Cleared all agents");
  90. }
  91. // Log stats
  92. if (Input.GetKeyDown(KeyCode.L))
  93. {
  94. LogAgentStats();
  95. }
  96. // Regenerate maze
  97. if (Input.GetKeyDown(KeyCode.R))
  98. {
  99. Debug.Log("Regenerating maze...");
  100. mazeController.GenerateMaze();
  101. }
  102. }
  103. }