using System.Collections; using System.Collections.Generic; using UnityEngine; //using KairoEngine.Utilities; using TMPro; using Sirenix.OdinInspector; using NodeCanvas.Framework; using KairoEngine.Core; namespace KairoEngine.CharacterSystem { public class BehaviorDebugerUi : MonoBehaviour { public Transform windowTransform; public Transform dataContainer; public TextMeshProUGUI titleUi; public GameObject behaviorDataUiPrefab; public float windowDistance = 3.5f; public Color whiteColor; public Color redColor; public Color blueColor; public Color greenColor; public Color grayColor; public Color magentaColor; [ReadOnly] public KairoEngine.CharacterSystem.CharacterController character; [ReadOnly] public Blackboard blackboard; [ReadOnly] public KairoEngine.CharacterSystem.CharacterController targetCharacter; public List dataSlots; private bool initialized = false; public void Initialize(KairoEngine.CharacterSystem.CharacterController debugCharacter) { if(debugCharacter == null) { Destroy(this.gameObject); return; } initialized = true; character = debugCharacter; blackboard = debugCharacter.GetComponent(); if(blackboard == null) { Debug.LogError("Could not find Blackboard component in " + character.unique_name); //Destroy(this.gameObject); //return; } titleUi.text = character.unique_name; for (int i = 0; i < 4; i++) { GameObject slot = Instantiate(behaviorDataUiPrefab, dataContainer); BehaviorDebugerDataUi slotData = slot.GetComponent(); if(slotData != null) dataSlots.Add(slotData); else Debug.LogError("Could not find component BehaviorDebugerDataUi in " + slot.name); } UpdateData(); UpdatePanelPosition(character.transform, windowTransform); } void Update() { if(initialized == false) return; UpdateData(); if(character != null) UpdatePanelPosition(character.transform, windowTransform); } private void UpdatePanelPosition(Transform target, Transform targetPanel) { Vector3 pos = new Vector3(target.position.x, target.position.y + windowDistance, target.position.z); Vector3 screenPos = Camera.main.WorldToScreenPoint(pos); targetPanel.GetComponent().position = screenPos; } private void UpdateData() { if(character == null) { Destroy(this.gameObject); return; } if(blackboard == null) { blackboard = character.GetComponent(); if(blackboard == null) return; } string characterState = ""; Color stateColor = greenColor; //Debug.Log(behavior.stateMachine.currentState.GetType().ToString()); string currentState = blackboard.GetVariable("currentState").value; switch (currentState) { case "Idle": characterState = "Idle"; break; case "Shooting": characterState = "Shoot"; break; case "Moving": characterState = "Move"; break; case "Aiming": characterState = "Aim"; break; case "Melee Attack": characterState = "Melee Attack"; break; case "Unarmed Attack": characterState = "Unarmed Attack"; break; case "Reloading": characterState = "Reload"; break; case "Equiping Weapon": characterState = "Equip Weapon"; break; case "Dead": characterState = "Dead"; stateColor = redColor; break; default: characterState = "Undefined State"; stateColor = redColor; break; } if(characterState == "") { characterState = "NULL"; stateColor = grayColor; } string target = ""; string distance = ""; Color targetColor = greenColor; Color distanceColor = whiteColor; Transform currentTarget = blackboard.GetVariable("target").value; float currentAttackDisntace = blackboard.GetVariable("attackDistance").value; Vector3 currentTargetPosition = blackboard.GetVariable("targetPosition").value; if(currentTarget != null) { if(targetCharacter == null) targetCharacter = currentTarget.root.GetComponentInChildren(); if(targetCharacter != null) target = targetCharacter.unique_name; else target = currentTarget.name; float d = Vector3.Distance(currentTarget.position, character.transform.position); d = KairoEngine.Core.Utilities.Round(d, 1); if(d <= currentAttackDisntace) distanceColor = blueColor; else distanceColor = redColor; distance = d.ToString() + "m/" + KairoEngine.Core.Utilities.Round(currentAttackDisntace, 1) + "m"; } else if(currentTargetPosition != new Vector3()) { targetCharacter = null; target = currentTargetPosition.ToString(); targetColor = grayColor; float d = Vector3.Distance(currentTargetPosition, character.transform.position); d = KairoEngine.Core.Utilities.Round(d, 1); if(d <= currentAttackDisntace) distanceColor = blueColor; else distanceColor = redColor; distance = d.ToString() + "m/" + KairoEngine.Core.Utilities.Round(currentAttackDisntace, 1) + "m"; } else { targetCharacter = null; target = "NULL"; targetColor = character.HasEquipedItem() ? redColor : grayColor; distance = "NULL/" + KairoEngine.Core.Utilities.Round(currentAttackDisntace, 1) + "m"; distanceColor = grayColor; } string weapon = ""; Color weaponColor = magentaColor; if(character.HasEquipedItem()) { weapon = character.GetEquipedItem().item.title; } else { weapon = "NULL"; weaponColor = grayColor; } if(currentState != "Dead") { dataSlots[0].Setup("State:", characterState, stateColor); dataSlots[1].Setup("Target:", target, targetColor); dataSlots[2].Setup("Distance:", distance, distanceColor); dataSlots[3].Setup("Weapon:", weapon, weaponColor); } else { dataSlots[0].Setup("State:", characterState, stateColor); dataSlots[1].Setup("Target:", "NULL", grayColor); dataSlots[2].Setup("Distance:", "NULL", grayColor); dataSlots[3].Setup("Weapon:", "NULL", grayColor); } } } }