123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- 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<BehaviorDebugerDataUi> 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<Blackboard>();
- 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<BehaviorDebugerDataUi>();
- 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<RectTransform>().position = screenPos;
- }
- private void UpdateData()
- {
- if(character == null)
- {
- Destroy(this.gameObject);
- return;
- }
- if(blackboard == null)
- {
- blackboard = character.GetComponent<Blackboard>();
- if(blackboard == null) return;
- }
- string characterState = "";
- Color stateColor = greenColor;
- //Debug.Log(behavior.stateMachine.currentState.GetType().ToString());
- string currentState = blackboard.GetVariable<string>("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<Transform>("target").value;
- float currentAttackDisntace = blackboard.GetVariable<float>("attackDistance").value;
- Vector3 currentTargetPosition = blackboard.GetVariable<Vector3>("targetPosition").value;
- if(currentTarget != null)
- {
- if(targetCharacter == null) targetCharacter = currentTarget.root.GetComponentInChildren<CharacterController>();
- 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);
- }
-
- }
- }
- }
|