using System.Collections; using System.Collections.Generic; using UnityEngine; using Sirenix.OdinInspector; using QFSW.QC; using KairoEngine.Core; namespace KairoEngine.CharacterSystem { public class BehaviorDebuger : MonoBehaviour { public static BehaviorDebuger Instance { get; private set; } public GameObject debugWindowPrefab; public Transform windowContainer; public bool showDebugger = false; private bool debugerInitialized = false; public List debugerWindows = new List(); private void Start() { if (Instance == null) Instance = this; } void Update() { for (int i = 0; i < debugerWindows.Count; i++) { var window = debugerWindows[i]; if(window == null) { debugerWindows.RemoveAt(i); break; } if(window.character == null) { Destroy(window.gameObject); debugerWindows.RemoveAt(i); break; } } if (debugerInitialized = true && showDebugger == false) StopDebuger(); if (showDebugger == false) return; if (debugerInitialized == false) StartDebuger(); } [Command("ai-debuger", "Show de AI behavior debuger windows")] public static string ToogleDebuger() { if(Instance == null) return "AI Debuger not loaded yet."; if (Instance.showDebugger == true) { Instance.StopDebuger(); return "Debuger is OFF"; } else { Instance.StartDebuger(); return "Debuger is ON"; } } public void StartDebuger() { if (debugerInitialized == true) return; debugerInitialized = true; showDebugger = true; //Debug.Log($"Initializing debuger windows ({CharacterManager.instance.sceneCharacters.Count.ToString()})"); for (int i = 0; i < CharacterManager.instance.sceneCharacters.Count; i++) { CharacterController character = CharacterManager.instance.sceneCharacters[i]; if(character != null) CreateDebugWindow(character); } RegisterEvents.OnRegisterCharacter += CreateDebugWindow; } public void StopDebuger() { for (int i = 0; i < debugerWindows.Count; i++) { if(debugerWindows[i] != null) Destroy(debugerWindows[i].gameObject); } debugerWindows.Clear(); RegisterEvents.OnRegisterCharacter -= CreateDebugWindow; debugerInitialized = false; showDebugger = false; } private void CreateDebugWindow(CharacterController targetCharacter) { for (int i = 0; i < debugerWindows.Count; i++) { if(debugerWindows[i].character.unique_name == targetCharacter.unique_name) { //Debug.Log($"Character already registered ({targetCharacter.unique_name})"); return; } } EventResponse response = EventManager.request.GetCharacterController("PlayerCharacter"); if(response.status != EventResponseStatus.OK) return; if(targetCharacter.unique_name == response.value.unique_name) return; GameObject window = Instantiate(debugWindowPrefab, windowContainer); BehaviorDebugerUi debuger = window.GetComponent(); debuger.Initialize(targetCharacter); debugerWindows.Add(debuger); Debug.Log("Created AI Debug window for character " + targetCharacter.unique_name); } } }