BehaviorDebugerUi.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. //using KairoEngine.Utilities;
  5. using TMPro;
  6. using Sirenix.OdinInspector;
  7. using NodeCanvas.Framework;
  8. using KairoEngine.Core;
  9. namespace KairoEngine.CharacterSystem
  10. {
  11. public class BehaviorDebugerUi : MonoBehaviour
  12. {
  13. public Transform windowTransform;
  14. public Transform dataContainer;
  15. public TextMeshProUGUI titleUi;
  16. public GameObject behaviorDataUiPrefab;
  17. public float windowDistance = 3.5f;
  18. public Color whiteColor;
  19. public Color redColor;
  20. public Color blueColor;
  21. public Color greenColor;
  22. public Color grayColor;
  23. public Color magentaColor;
  24. [ReadOnly] public KairoEngine.CharacterSystem.CharacterController character;
  25. [ReadOnly] public Blackboard blackboard;
  26. [ReadOnly] public KairoEngine.CharacterSystem.CharacterController targetCharacter;
  27. public List<BehaviorDebugerDataUi> dataSlots;
  28. private bool initialized = false;
  29. public void Initialize(KairoEngine.CharacterSystem.CharacterController debugCharacter)
  30. {
  31. if(debugCharacter == null)
  32. {
  33. Destroy(this.gameObject);
  34. return;
  35. }
  36. initialized = true;
  37. character = debugCharacter;
  38. blackboard = debugCharacter.GetComponent<Blackboard>();
  39. if(blackboard == null)
  40. {
  41. Debug.LogError("Could not find Blackboard component in " + character.unique_name);
  42. //Destroy(this.gameObject);
  43. //return;
  44. }
  45. titleUi.text = character.unique_name;
  46. for (int i = 0; i < 4; i++)
  47. {
  48. GameObject slot = Instantiate(behaviorDataUiPrefab, dataContainer);
  49. BehaviorDebugerDataUi slotData = slot.GetComponent<BehaviorDebugerDataUi>();
  50. if(slotData != null) dataSlots.Add(slotData);
  51. else Debug.LogError("Could not find component BehaviorDebugerDataUi in " + slot.name);
  52. }
  53. UpdateData();
  54. UpdatePanelPosition(character.transform, windowTransform);
  55. }
  56. void Update()
  57. {
  58. if(initialized == false) return;
  59. UpdateData();
  60. if(character != null) UpdatePanelPosition(character.transform, windowTransform);
  61. }
  62. private void UpdatePanelPosition(Transform target, Transform targetPanel)
  63. {
  64. Vector3 pos = new Vector3(target.position.x, target.position.y + windowDistance, target.position.z);
  65. Vector3 screenPos = Camera.main.WorldToScreenPoint(pos);
  66. targetPanel.GetComponent<RectTransform>().position = screenPos;
  67. }
  68. private void UpdateData()
  69. {
  70. if(character == null)
  71. {
  72. Destroy(this.gameObject);
  73. return;
  74. }
  75. if(blackboard == null)
  76. {
  77. blackboard = character.GetComponent<Blackboard>();
  78. if(blackboard == null) return;
  79. }
  80. string characterState = "";
  81. Color stateColor = greenColor;
  82. //Debug.Log(behavior.stateMachine.currentState.GetType().ToString());
  83. string currentState = blackboard.GetVariable<string>("currentState").value;
  84. switch (currentState)
  85. {
  86. case "Idle":
  87. characterState = "Idle";
  88. break;
  89. case "Shooting":
  90. characterState = "Shoot";
  91. break;
  92. case "Moving":
  93. characterState = "Move";
  94. break;
  95. case "Aiming":
  96. characterState = "Aim";
  97. break;
  98. case "Melee Attack":
  99. characterState = "Melee Attack";
  100. break;
  101. case "Unarmed Attack":
  102. characterState = "Unarmed Attack";
  103. break;
  104. case "Reloading":
  105. characterState = "Reload";
  106. break;
  107. case "Equiping Weapon":
  108. characterState = "Equip Weapon";
  109. break;
  110. case "Dead":
  111. characterState = "Dead";
  112. stateColor = redColor;
  113. break;
  114. default:
  115. characterState = "Undefined State";
  116. stateColor = redColor;
  117. break;
  118. }
  119. if(characterState == "")
  120. {
  121. characterState = "NULL";
  122. stateColor = grayColor;
  123. }
  124. string target = "";
  125. string distance = "";
  126. Color targetColor = greenColor;
  127. Color distanceColor = whiteColor;
  128. Transform currentTarget = blackboard.GetVariable<Transform>("target").value;
  129. float currentAttackDisntace = blackboard.GetVariable<float>("attackDistance").value;
  130. Vector3 currentTargetPosition = blackboard.GetVariable<Vector3>("targetPosition").value;
  131. if(currentTarget != null)
  132. {
  133. if(targetCharacter == null) targetCharacter = currentTarget.root.GetComponentInChildren<CharacterController>();
  134. if(targetCharacter != null) target = targetCharacter.unique_name;
  135. else target = currentTarget.name;
  136. float d = Vector3.Distance(currentTarget.position, character.transform.position);
  137. d = KairoEngine.Core.Utilities.Round(d, 1);
  138. if(d <= currentAttackDisntace) distanceColor = blueColor;
  139. else distanceColor = redColor;
  140. distance = d.ToString() + "m/" + KairoEngine.Core.Utilities.Round(currentAttackDisntace, 1) + "m";
  141. }
  142. else if(currentTargetPosition != new Vector3())
  143. {
  144. targetCharacter = null;
  145. target = currentTargetPosition.ToString();
  146. targetColor = grayColor;
  147. float d = Vector3.Distance(currentTargetPosition, character.transform.position);
  148. d = KairoEngine.Core.Utilities.Round(d, 1);
  149. if(d <= currentAttackDisntace) distanceColor = blueColor;
  150. else distanceColor = redColor;
  151. distance = d.ToString() + "m/" + KairoEngine.Core.Utilities.Round(currentAttackDisntace, 1) + "m";
  152. }
  153. else
  154. {
  155. targetCharacter = null;
  156. target = "NULL";
  157. targetColor = character.HasEquipedItem() ? redColor : grayColor;
  158. distance = "NULL/" + KairoEngine.Core.Utilities.Round(currentAttackDisntace, 1) + "m";
  159. distanceColor = grayColor;
  160. }
  161. string weapon = "";
  162. Color weaponColor = magentaColor;
  163. if(character.HasEquipedItem())
  164. {
  165. weapon = character.GetEquipedItem().item.title;
  166. }
  167. else
  168. {
  169. weapon = "NULL";
  170. weaponColor = grayColor;
  171. }
  172. if(currentState != "Dead")
  173. {
  174. dataSlots[0].Setup("State:", characterState, stateColor);
  175. dataSlots[1].Setup("Target:", target, targetColor);
  176. dataSlots[2].Setup("Distance:", distance, distanceColor);
  177. dataSlots[3].Setup("Weapon:", weapon, weaponColor);
  178. }
  179. else
  180. {
  181. dataSlots[0].Setup("State:", characterState, stateColor);
  182. dataSlots[1].Setup("Target:", "NULL", grayColor);
  183. dataSlots[2].Setup("Distance:", "NULL", grayColor);
  184. dataSlots[3].Setup("Weapon:", "NULL", grayColor);
  185. }
  186. }
  187. }
  188. }