using System; using System.Collections.Generic; using UnityEngine; namespace BehaviorTreeSlayer { /// /// Agent of behavior tree, /// This class is not required. /// You only need to call Entry.update in the right place to drive the behavior tree to run /// use event drive method to communicate data with outside /// public class BehaviorTree : MonoBehaviour { System.Random rd = new System.Random(); public System.Random MyRandom => rd;//random inside behavior tree call public bool AutoRun; Dictionary> actions = new Dictionary>(); public void Regist(string key, Action onEvent) { if (actions.ContainsKey(key)) { actions[key] = onEvent; } else { actions.Add(key, onEvent); } } public void UnRegist(string key) { actions.Remove(key); } public void Dispatch(string key, object obj) { if (actions.ContainsKey(key)) { actions[key].Invoke(obj); } } public TextAsset config; Entry Entry; public List Obj = new List(); Dictionary blackBoard = new Dictionary(); public object this[string key] { get { if (blackBoard.TryGetValue(key, out object v)) { return v; } GameObject obj = Obj.Find(o => o.name.Equals(key)); return obj; } set { if (blackBoard.ContainsKey(key)) { blackBoard[key] = value; } else { blackBoard.Add(key, value); } } } public void Remove(string key) { blackBoard.Remove(key); } private void Start() { Load(); if (AutoRun) Entry?.Enter(this); } public Entry Load() { if (Entry == null && config != null) { Entry = XmlUtils.DeSerialize(config.text); } return Entry; } private void Update() { Entry?.Tick(Time.deltaTime, this); } } }