public void StartNode(Node node) { _aliveBehavior = node; if (node.decorators.Length > 0) { for (int i = 0; i < node.decorators.Length; i++) { RuntimeDecorator rd = GetRuntimeDecorator(node.decorators[i]); #if UNITY_EDITOR rd.closed = false; #endif if (!StartDecorator(rd)) { FinishExecute(false); return; } } } if (node is Task) { StartTask(node); } else { if (node is Composite) { if ((node as Composite).services.Length > 0) { for (int i = 0; i < _runtimeServices.Count; i++) { if (_runtimeServices[i].parent == node as Composite) { StartService(_runtimeServices[i]); } } } if (node.childNodes.Length == 0) { if (node is Selector) { FinishExecute(true); } else if (node is Sequence) { FinishExecute(false); } } } if (_currentChildNodeIndex[node] < node.childNodes.Length) { StartNode(node.childNodes[_currentChildNodeIndex[node]]); } } }
private bool StartDecorator(RuntimeDecorator runtimeDecorator) { bool value = runtimeDecorator.inversed ? runtimeDecorator.decoratorFunc() : !runtimeDecorator.decoratorFunc(); if (value) { #if UNITY_EDITOR runtimeDecorator.closed = true; #endif return(false); } #if UNITY_EDITOR runtimeDecorator.closed = false; #endif if (runtimeDecorator.tick > 0f) { if (runtimeDecorator.subscription != null) { runtimeDecorator.subscription.Dispose(); } runtimeDecorator.activeSelf = true; runtimeDecorator.subscription = Observable.Interval(TimeSpan.FromSeconds(runtimeDecorator.tick)) .Select(_ => runtimeDecorator.activeSelf) .TakeWhile(x => x) .Subscribe(_ => { if (!runtimeDecorator.activeSelf) { return; } bool _value = runtimeDecorator.inversed ? runtimeDecorator.decoratorFunc() : !runtimeDecorator.decoratorFunc(); if (_value) { #if UNITY_EDITOR runtimeDecorator.closed = true; #endif runtimeDecorator.activeSelf = false; runtimeDecorator.subscription = null; while (true) { if (_aliveBehavior != runtimeDecorator.parent) { FinishExecuteImmediate(); } else if (_aliveBehavior == runtimeDecorator.parent) { FinishExecute(false); break; } } } }); } return(true); }
private void FinishDecorators() { if (_aliveBehavior.decorators.Length > 0) { for (int i = 0; i < _aliveBehavior.decorators.Length; i++) { RuntimeDecorator rd = GetRuntimeDecorator(_aliveBehavior.decorators[i]); if (rd.activeSelf) { rd.activeSelf = false; } } } }
private void InitializeDecorator(Node node) { if (node.decorators.Length > 0) { for (int i = 0; i < node.decorators.Length; i++) { Decorator dc = node.decorators[i]; RuntimeDecorator rd = new RuntimeDecorator(node, dc, dc.observerAbortsType, dc.tick, dc.inversed); MonoBehaviour comp = GetEqualTypeComponent(dc.targetScript.GetType()) as MonoBehaviour; if (comp == null) { comp = gameObject.AddComponent(dc.targetScript.GetType()) as MonoBehaviour; IInitializable initializable = comp as IInitializable; initializable.Initialize(); } rd.decoratorFunc = Delegate.CreateDelegate(typeof(Func <bool>), comp, dc.targetMethod) as Func <bool>; _runtimeDecorators.Add(rd); } } }