You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

1 month ago
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<int> 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<BehaviorTree>();
if (componentsInChildren.Length > 0)
bt = componentsInChildren[0];
else
bt = go.AddComponent<BehaviorTree>();
if (string.IsNullOrEmpty(btTreePath))
btTreePath = BlackboardSrv.GetBehaviourTreePath(ent.creationIndex);
if (string.IsNullOrEmpty(btTreePath))
return;
ebt = SyncAssetLoader.GetAssetObject<ExternalBehaviorTree>("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;
}
}
}