/// <summary> /// 状态通过当前节点的子节点状态来判断,不能使用叶节点判断 /// running时,只负责把自身状态改为running并返回runningNode /// fail时,不返回自身 /// succ时,绝不返回自身 /// </summary> /// <returns></returns> public override JBehaviorNode DFS() { state = JBTNodeState.fail; // 当当前运行节点不为空时,处理当前节点 if (runningNode != null) { _dfsNode = runningNode.DFS(); // 如果成功,则枚举下一节点 if (runningNode.state == JBTNodeState.succ) { runningIndex++; } // 如果状态为失败,则退出该节点 else if (runningNode.state == JBTNodeState.fail) { state = JBTNodeState.fail; Exit(); return(_dfsNode); } else //如果是运行中 保持运行 { state = JBTNodeState.running; return(_dfsNode); } } if (runningIndex < children.Count) { for (int i = runningIndex; i < children.Count; i++) { node = children[i]; _dfsNode = node.DFS(); if (node.state == JBTNodeState.fail) { state = JBTNodeState.fail; Exit(); return(_dfsNode); } if (node.state == JBTNodeState.running) { state = JBTNodeState.running; runningNode = node; runningIndex = i; return(_dfsNode); } } } //全部完成时,则定义为succ state = JBTNodeState.succ; runningNode = children[children.Count - 1]; Exit(); return(_dfsNode); }
public void Update() { //查询当前可执行的节点 curr = root.DFS(); //当当前可执行的节点不是上次执行的节点是 Trans(); curr.Update(); pre = curr; }
/// <summary> /// 状态通过当前节点的子节点状态来判断,不能使用叶节点判断 /// 获取到succ状态时,自身为succ并返回,不再执行接下来的子节点 /// 取到failure状态时,继续枚举下一个子节点 /// 取到running状态时,设置自身为running并种植枚举 /// 子节点全部取完切没有找到succ,则返回fail /// </summary> /// <returns></returns> public override JBehaviorNode DFS() { state = JBTNodeState.fail; if (runningNode != null) { _dfsNode = runningNode.DFS(); if (runningNode.state == JBTNodeState.succ) { Exit(); state = JBTNodeState.succ; return(_dfsNode); } else if (runningNode.state == JBTNodeState.running) { state = JBTNodeState.running; return(_dfsNode); } else if (runningNode.state == JBTNodeState.fail) { runningIndex++; } } for (int i = runningIndex; i < children.Count; i++) { node = children[i]; _dfsNode = node.DFS(); ///选择节点,获取到成功时则退出 if (node.state == JBTNodeState.succ) { state = JBTNodeState.succ; Exit(); return(_dfsNode); } //当前节点为运行中,则可以运行 if (node.state == JBTNodeState.running) { state = JBTNodeState.running; runningNode = node; runningIndex = i; return(_dfsNode); } //否则,如果时失败,则继续前行 } //全部运行完毕,是指一个没有选择出来,就是fail state = JBTNodeState.fail; Exit(); return(_dfsNode); }