/// <summary> /// ツリーの状態をUpdate /// </summary> public void Update() { if (_isCompleted) { return; } int abortIndex = ReevaluateConditionalTasks(); if (abortIndex != -1) { // 中断したノードと現在実行中のノードの共通祖先を見つける int caIndex = CommonAncestorNode(abortIndex, _activeNodeIndex); Node activeNode = _nodeList[_activeNodeIndex]; activeNode.OnAbort(); while (_activeStack.Count != 0) { PopNode(); activeNode = _nodeList[_activeNodeIndex]; activeNode.OnAbort(); if (_activeNodeIndex == caIndex) { break; } ConditionalReevaluate cr = _reevaluateList.FirstOrDefault(r => r.Index == _activeNodeIndex); if (cr != null) { _reevaluateList.Remove(cr); } } } BehaviorStatus status = BehaviorStatus.Inactive; if (_activeNodeIndex == -1) { status = Execute(_rootNode); } else { Node node = _nodeList[_activeNodeIndex]; status = Execute(node); } if (status == BehaviorStatus.Completed) { Debug.Log("Behavior Tree has been completed."); _isCompleted = true; } }
/// <summary> /// 再評価が必要なノードを再評価する /// </summary> /// <returns>再評価して変化のあったノードのIndex</returns> private int ReevaluateConditionalTasks() { for (int i = 0; i < _reevaluateList.Count; i++) { ConditionalReevaluate cr = _reevaluateList[i]; BehaviorStatus status = _nodeList[cr.Index].OnUpdate(); // 前回の状態と変化していたら、祖先まで遡って処理を停止する if (cr.Status != status) { CompositeNode cnode = _nodeList[cr.CompositeIndex] as CompositeNode; if (cnode != null) { cnode.OnConditionalAbort(cr.Index); } _reevaluateList.Remove(cr); return(cr.Index); } } return(-1); }