/// <summary> /// 重置执行 /// </summary> /// <param name="unit"></param> protected void UndoExecuteChildGroups(ExecuteUnit unit) { var childUnits = unit.childUnits.ToArray(); Array.Reverse(childUnits); foreach (var list in childUnits) { foreach (var item in list) { if (statusDic.ContainsKey(item)) { if (statusDic[item].statu != ExecuteStatu.UnStarted) { stateMechine.UnDo(item); } stateMechine.UnDo(item); statusDic[item].Clear(); } else { if (log) { Debug.Log("ignore:" + item.node); } } } } }
protected override void ExecuteOnUnStarted(ExecuteUnit unit) { base.ExecuteOnUnStarted(unit); var logicNode = unit.node as Graph.LogicNode; switch (logicNode.logicType) { case Graph.LogicType.And: if (AllParentCompleted(unit)) { statusDic[unit].statu = ExecuteStatu.Executing; StartExecuteChildGroups(unit); } break; case Graph.LogicType.Or: statusDic[unit].statu = ExecuteStatu.Executing; StartExecuteChildGroups(unit); break; case Graph.LogicType.ExclusiveOr: break; default: break; } }
protected virtual void ExecuteOnCompleted(ExecuteUnit unit) { if (log) { Debug.Log("ExecuteCompleted:" + unit.node.name); } }
public virtual void UnDo(ExecuteUnit unit) { if (!statusDic.ContainsKey(unit)) { statusDic[unit] = new UnitStatus(); } }
public void AddParentUnit(ExecuteUnit parentUnit) { if (!parentUnits.Contains(parentUnit)) { parentUnits.Add(parentUnit); } }
protected virtual void ExecuteOnExecuting(ExecuteUnit unit) { if (log) { Debug.Log("ExecuteExecuting:" + unit.node.name); } }
/// <summary> /// 首次执行 /// </summary> /// <param name="unit"></param> protected override void ExecuteOnUnStarted(ExecuteUnit unit) { base.ExecuteOnUnStarted(unit); statusDic[unit].statu = ExecuteStatu.Executing; var operateNode = unit.node as OperaterNode; //判断是不是叶节点 var leaf = unit.childUnits.Count == 0; if (!leaf) { for (int i = 0; i < unit.childUnits.Count; i++) { statusDic[unit].waitUnits.Enqueue(unit.childUnits[i]); } } operateNode.onEndExecute = () => { stateMechine.OnStopAction(unit.node as Graph.OperaterNode); if (leaf || statusDic[unit].waitUnits.Count == 0) { statusDic[unit].statu = ExecuteStatu.Completed; } stateMechine.Execute(unit); }; operateNode.OnStartExecute(stateMechine.IsAuto); stateMechine.OnStartAction(unit.node as Graph.OperaterNode); }
public virtual void OnUnDoExecute(bool all) { StopUpdateAction(true); if (all) { UnDo(root); } else { if (activedUnits.Count > 0) { var unit = activedUnits.Pop(); if (unit == currentUnit) { redoUnits.Push(unit); UnDo(unit); unit = activedUnits.Pop(); } currentUnit = unit; redoUnits.Push(currentUnit); Debug.Log("UnDo:" + currentUnit.node); UnDo(currentUnit); Execute(currentUnit); } } }
public virtual void OnEndExecute(bool all) { StopUpdateAction(false); if (all) { Complete(root); } else { if (redoUnits.Count > 0) { currentUnit = redoUnits.Pop(); Complete(currentUnit); Execute(currentUnit); } else { if (startedActions.Count > 0) { startedActions[0].OnEndExecute(true); OnStopAction(startedActions[0]); } } } }
public virtual void Execute(ExecuteUnit unit) { if (!statusDic.ContainsKey(unit)) { statusDic[unit] = new UnitStatus(); } switch (statusDic[unit].statu) { case ExecuteStatu.UnStarted: ExecuteOnUnStarted(unit); break; case ExecuteStatu.Executing: ExecuteOnExecuting(unit); break; case ExecuteStatu.Completed: ExecuteOnCompleted(unit); break; default: break; } }
protected virtual void ExecuteOnUnStarted(ExecuteUnit unit) { stateMechine.activedUnits.Push(unit); if (log) { Debug.Log("ExecuteUnStarted:" + unit.node.name); } }
public void Complete(ExecuteUnit unit) { if (unit != null) { SwitchState(unit); currentState.Complete(unit); } }
public void UnDo(ExecuteUnit unit) { if (unit != null) { SwitchState(unit); currentState.UnDo(unit); } }
public override void UnDo(ExecuteUnit unit) { base.UnDo(unit); if (statusDic[unit].statu != ExecuteStatu.UnStarted) { UndoExecuteChildGroups(unit); statusDic[unit].statu = ExecuteStatu.UnStarted; } }
/// <summary> /// 结束 /// </summary> /// <param name="unit"></param> public override void Complete(ExecuteUnit unit) { base.Complete(unit); if (statusDic[unit].statu != ExecuteStatu.Completed) { CompleteExecuteChildGroups(unit); statusDic[unit].statu = ExecuteStatu.Completed; } }
/// <summary> /// 结束执行 /// </summary> /// <param name="unit"></param> protected override void ExecuteOnCompleted(ExecuteUnit unit) { base.ExecuteOnCompleted(unit); if (unit.parentUnits.Count == 0) { Debug.LogError(unit.node + "have no parent!"); } stateMechine.Execute(unit.parentUnits[0]); }
public override void Complete(ExecuteUnit unit) { base.Complete(unit); CompleteExecuteChildGroups(unit); statusDic[unit].statu = ExecuteStatu.Completed; var operateNode = unit.node as OperaterNode; operateNode.onEndExecute = null; operateNode.OnEndExecute(true); stateMechine.OnStopAction(unit.node as Graph.OperaterNode); }
private static ExecuteUnit CreateOringalUnit(Graph.ActionNode node, List <ExecuteUnit> copyEnviroment) { var unit = copyEnviroment.Find(x => x.node == node); if (unit == null) { unit = new ExecuteUnit(node); copyEnviroment.Add(unit); } return(unit); }
public override void UnDo(ExecuteUnit unit) { base.UnDo(unit); UndoExecuteChildGroups(unit); if (statusDic[unit].statu != ExecuteStatu.UnStarted) { statusDic[unit].statu = ExecuteStatu.UnStarted; var operateNode = unit.node as OperaterNode; operateNode.OnUnDoExecute(); stateMechine.OnStopAction(unit.node as Graph.OperaterNode); } }
/// <summary> /// 判读执行单元当前开启的列表是否执行完成 /// </summary> /// <param name="unit"></param> /// <returns></returns> protected bool HaveUnitNotComplete(ExecuteUnit unit) { if (statusDic[unit].workUnits.Count > 0) { var lastworking = statusDic[unit].workUnits.Peek(); if (!IsGroupCompleted(lastworking)) { return(true); } } return(false); }
//再次执行 protected override void ExecuteOnExecuting(ExecuteUnit unit) { if (HaveUnitNotComplete(unit)) { return; } if (!LunchStackGroup(unit)) { statusDic[unit].statu = ExecuteStatu.Completed; stateMechine.Complete(); } }
protected bool LunchStackGroup(ExecuteUnit unit) { if (statusDic[unit].waitUnits.Count > 0) { var units = statusDic[unit].waitUnits.Dequeue(); statusDic[unit].workUnits.Push(units); stateMechine.ExecuteGroup(units); return(true); } else { return(false); } }
private bool AllParentCompleted(ExecuteUnit unit) { var parentList = unit.parentUnits; Debug.Log(parentList.Count); foreach (var item in parentList) { if (item.node is Graph.OperaterNode) { var node = item.node as Graph.OperaterNode; Debug.Log(node + ": statu:" + node.Statu); if (node.Statu != ExecuteStatu.Completed) { return(false); } } } return(true); }
public void SwitchState(ExecuteUnit unit) { if (unit.node is Graph.StartNode) { SetState(State.Start); } else if (unit.node is Graph.EndNode) { SetState(State.End); } else if (unit.node is Graph.LogicNode) { SetState(State.Logic); } else if (unit.node is Graph.OperaterNode) { SetState(State.Operate); } }
//private static ExecuteUnit CreateCopyExecuteUnit(ExecuteUnit unit, int count) //{ // var copyUnit = new ExecuteUnit(ScriptableObject.Instantiate(unit.node)); // for (int i = 0; i < unit.parentUnits.Count; i++) // { // var parent = unit.parentUnits[i]; // var orignalList = parent.GetPositon(unit); // orignalList.Add(copyUnit); // copyUnit.AddParentUnit(unit.parentUnits[i]); // } // return copyUnit; //} private static ExecuteUnit CreateOriginalUnit(ActionCommand graphObj) { if (graphObj == null || graphObj.Nodes == null) { return(null); } NodeData startNodeData = graphObj.Nodes.Where(node => node.Object is Graph.StartNode).FirstOrDefault(); if (startNodeData == null) { return(null); } Debug.Assert(startNodeData != null, "this is no start node!"); orignalUnits.Clear(); copyDic.Clear(); var executeUnit = new ExecuteUnit(startNodeData.Object as Graph.StartNode); RetiveChildNode(graphObj, startNodeData, executeUnit); return(executeUnit); }
/// <summary> /// 结束一组执行 /// </summary> /// <param name="unit"></param> protected void CompleteExecuteChildGroups(ExecuteUnit unit) { var childs = unit.childUnits.ToArray(); foreach (var list in childs) { foreach (var item in list) { if (!statusDic.ContainsKey(item)) { statusDic[item] = new UnitStatus(); } if (statusDic[item].statu != ExecuteStatu.Completed) { stateMechine.Complete(item); } } } }
/// <summary> /// 开启执行 /// </summary> /// <param name="unit"></param> protected void StartExecuteChildGroups(ExecuteUnit unit) { for (int i = 1; i < unit.childUnits.Count; i++) { statusDic[unit].waitUnits.Enqueue(unit.childUnits[i]); } if (unit.childUnits.Count > 0) { var firstGroup = unit.childUnits[0]; stateMechine.ExecuteGroup(firstGroup); statusDic[unit].workUnits.Push(firstGroup); } else { //结束执行 Debug.LogError("结束执行"); statusDic[unit].statu = ExecuteStatu.Completed; stateMechine.Execute(unit); } }
/// <summary> /// 将copyDic中的unit进行反向复制 /// </summary> /// <param name="unit"></param> /// <returns></returns> private static ExecuteUnit DeepCopy(ExecuteUnit unit) { copyDic.Reverse(); foreach (var item in copyDic) { var count = item.Value; var copyOriginal = item.Key; enviroment[copyOriginal] = new List <ExecuteUnit>(); for (int z = 0; z < count; z++) { var newUnit = MakeCopy(copyOriginal, enviroment[copyOriginal]); for (int i = 0; i < copyOriginal.parentUnits.Count; i++) { var position = copyOriginal.parentUnits[i].GetPositon(copyOriginal); position.Add(newUnit); newUnit.parentUnits.Add(copyOriginal.parentUnits[i]); } } } return(unit); }
public static ExecuteUnit MakeCopy(ExecuteUnit original, List <ExecuteUnit> enviroment) { var node = ScriptableObject.Instantiate(original.node) as Graph.ActionNode; (node as Graph.ActionNode).SetContext(original.node.Command);//设置上下文 var unit = CreateOringalUnit(node, enviroment); if (log) { Debug.Log("MakeCopy:" + unit); } for (int i = 0; i < original.childUnits.Count; i++) { var childList = new List <ExecuteUnit>(); for (int j = 0; j < original.childUnits[i].Count; j++) { var childUnit = MakeCopy(original.childUnits[i][j], enviroment); childList.Add(childUnit); } unit.AppendChildNodes(childList); } return(unit); }
private static void RetiveChildNode(ActionCommand graphObj, NodeData parentNode, ExecuteUnit unit) { //按连接点分组 var connectionGroup = graphObj.Connections.Where(connection => connection.FromNodeId == parentNode.Id).GroupBy( x => { var port = parentNode.OutputPoints.Find(y => y.Id == x.FromNodeConnectionPointId); var id = parentNode.OutputPoints.IndexOf(port); return(id); } ).OrderBy(x => x.Key).ToArray(); foreach (var connections in connectionGroup) { if (log) { Debug.Log("connection:" + connections.Key); } var childNodes = new List <ExecuteUnit>(); foreach (var connection in connections) { var node = graphObj.Nodes.Find(x => x.Id == connection.ToNodeId); (node.Object as Graph.ActionNode).SetContext(graphObj);//设置上下文 var childUnit = CreateOringalUnit(node.Object as Graph.ActionNode, orignalUnits); RetiveChildNode(graphObj, node, childUnit); childNodes.Add(childUnit); var copyCount = (connection.Object as Graph.ActionConnection).count - 1; if (copyCount > 0) { copyDic.Add(new KeyValuePair <ExecuteUnit, int>(childUnit, copyCount)); } } unit.AppendChildNodes(childNodes); } }