AIAgentExampleSetup.cs 3.2 KB

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