using System.Collections.Generic; using Opsive.BehaviorDesigner.Runtime; using UnityEngine; using UnityEngine.AI; using UniRx; using Cielonos.MainGame.Buffs.Character; namespace Cielonos.MainGame.Characters { public class BehaviorSubcontroller : SubcontrollerBase { public Automata Automata => owner; public BehaviorTree mainBehaviorTree; public NavMeshAgent navMeshAgent; public Dictionary subBehaviorTrees; private System.IDisposable timeScaleDisposable; private bool isTimePaused = false; public override void Initialize() { base.Initialize(); owner.selfTimeSm.timeScaleObservable.Subscribe(OnTimeScaleChanged).AddTo(owner); } private void OnTimeScaleChanged(float timeScale) { if (mainBehaviorTree == null) return; if (timeScale == 0f) { if (!isTimePaused) { isTimePaused = true; mainBehaviorTree.StopBehavior(true); } } else { if (isTimePaused) { isTimePaused = false; mainBehaviorTree.StartBehavior(); } } } public class AutomataEvent { public float Timestamp; public object Arg1; public object Arg2; public object Arg3; public bool IsValid(float expireTime) { return Time.time - Timestamp <= expireTime; } } // 以事件名称作为键值存储 public readonly Dictionary EventMemory = new Dictionary(); /// /// 向自身的 AI 下达上下文事件通知(代替原生的 ExecuteEvent) /// public void DispatchContextEvent(string eventName, object arg1 = null, object arg2 = null, object arg3 = null) { if (!EventMemory.TryGetValue(eventName, out AutomataEvent e)) { e = new AutomataEvent(); EventMemory[eventName] = e; } // 更新该事件发生的最绝对新时间与参数 e.Timestamp = Time.time; e.Arg1 = arg1; e.Arg2 = arg2; e.Arg3 = arg3; } } }