BehaviorDebuger.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Sirenix.OdinInspector;
  5. using QFSW.QC;
  6. using KairoEngine.Core;
  7. namespace KairoEngine.CharacterSystem
  8. {
  9. public class BehaviorDebuger : MonoBehaviour
  10. {
  11. public static BehaviorDebuger Instance { get; private set; }
  12. public GameObject debugWindowPrefab;
  13. public Transform windowContainer;
  14. public bool showDebugger = false;
  15. private bool debugerInitialized = false;
  16. public List<BehaviorDebugerUi> debugerWindows = new List<BehaviorDebugerUi>();
  17. private void Start()
  18. {
  19. if (Instance == null) Instance = this;
  20. }
  21. void Update()
  22. {
  23. for (int i = 0; i < debugerWindows.Count; i++)
  24. {
  25. var window = debugerWindows[i];
  26. if(window == null)
  27. {
  28. debugerWindows.RemoveAt(i);
  29. break;
  30. }
  31. if(window.character == null)
  32. {
  33. Destroy(window.gameObject);
  34. debugerWindows.RemoveAt(i);
  35. break;
  36. }
  37. }
  38. if (debugerInitialized = true && showDebugger == false) StopDebuger();
  39. if (showDebugger == false) return;
  40. if (debugerInitialized == false) StartDebuger();
  41. }
  42. [Command("ai-debuger", "Show de AI behavior debuger windows")]
  43. public static string ToogleDebuger()
  44. {
  45. if(Instance == null) return "AI Debuger not loaded yet.";
  46. if (Instance.showDebugger == true)
  47. {
  48. Instance.StopDebuger();
  49. return "Debuger is OFF";
  50. }
  51. else
  52. {
  53. Instance.StartDebuger();
  54. return "Debuger is ON";
  55. }
  56. }
  57. public void StartDebuger()
  58. {
  59. if (debugerInitialized == true) return;
  60. debugerInitialized = true;
  61. showDebugger = true;
  62. //Debug.Log($"Initializing debuger windows ({CharacterManager.instance.sceneCharacters.Count.ToString()})");
  63. for (int i = 0; i < CharacterManager.instance.sceneCharacters.Count; i++)
  64. {
  65. CharacterController character = CharacterManager.instance.sceneCharacters[i];
  66. if(character != null) CreateDebugWindow(character);
  67. }
  68. RegisterEvents.OnRegisterCharacter += CreateDebugWindow;
  69. }
  70. public void StopDebuger()
  71. {
  72. for (int i = 0; i < debugerWindows.Count; i++)
  73. {
  74. if(debugerWindows[i] != null) Destroy(debugerWindows[i].gameObject);
  75. }
  76. debugerWindows.Clear();
  77. RegisterEvents.OnRegisterCharacter -= CreateDebugWindow;
  78. debugerInitialized = false;
  79. showDebugger = false;
  80. }
  81. private void CreateDebugWindow(CharacterController targetCharacter)
  82. {
  83. for (int i = 0; i < debugerWindows.Count; i++)
  84. {
  85. if(debugerWindows[i].character.unique_name == targetCharacter.unique_name)
  86. {
  87. //Debug.Log($"Character already registered ({targetCharacter.unique_name})");
  88. return;
  89. }
  90. }
  91. EventResponse<CharacterController> response = EventManager.request.GetCharacterController("PlayerCharacter");
  92. if(response.status != EventResponseStatus.OK) return;
  93. if(targetCharacter.unique_name == response.value.unique_name) return;
  94. GameObject window = Instantiate(debugWindowPrefab, windowContainer);
  95. BehaviorDebugerUi debuger = window.GetComponent<BehaviorDebugerUi>();
  96. debuger.Initialize(targetCharacter);
  97. debugerWindows.Add(debuger);
  98. Debug.Log("Created AI Debug window for character " + targetCharacter.unique_name);
  99. }
  100. }
  101. }