using System.Collections.Generic; using BehaviorDesigner.Runtime; using UnityEngine; namespace CoreGame.Render { [Combat] public class AIComponent : ProxyBase { public string btTreePath; // 一个实体只规划一棵树 internal BehaviorTree bt; internal ExternalBehaviorTree ebt; internal readonly List targetList = new(); // 临时存储目标列表 internal int abilitySeq = 0; // 主动技 public CombatEntity TargetEnt => targetList.Count > 0 ? Contexts.Combat.GetEntity(targetList[0]) : default; protected override void Initialize(GameObject go, CombatEntity ent) { var componentsInChildren = go.GetComponentsInChildren(); if (componentsInChildren.Length > 0) bt = componentsInChildren[0]; else bt = go.AddComponent(); if (string.IsNullOrEmpty(btTreePath)) btTreePath = BlackboardSrv.GetBehaviourTreePath(ent.creationIndex); if (string.IsNullOrEmpty(btTreePath)) return; ebt = SyncAssetLoader.GetAssetObject("ExternalBehaviorTree", btTreePath); if (ebt == null) return; bt.ExternalBehavior = ebt; bt.StartWhenEnabled = false; bt.RestartWhenComplete = true; bt.ownerEnt = ent; // mono的 EnableBehavior(); } public void EnableBehavior() { if (bt != null) bt.EnableBehavior(); } public void DisableBehavior() { if (bt != null) bt.DisableBehavior(); } public void SendEvent(string key) { if (bt != null) bt.SendEvent(key); } protected override void OnReset() { targetList.Clear(); // m_Blackboard.Clear(); DisableBehavior(); bt = null; btTreePath = null; } } }