public override void OnInput(NetworkNode.Direction from, bool isActive) { base.OnInput(from, isActive); int i = (int)from; if (InputLockSetting[i]) { // OnLockChanged(from, isActive); NetworkNode.SetSpriteActiveColor(InputLockSprites[i], isActive); } CheckLocks(); }
protected virtual void DrawSceneGUI() { for (int i = 0; i < NetworkNode.NeighborNum; i++) { var neighbor = self.Neighbors[i]; if (neighbor != null) { NetworkNode.Direction direction = (NetworkNode.Direction)i; Vector3 offset1 = NetworkNode.GetDrawLineOffset(self, direction); Vector3 offset2 = NetworkNode.GetDrawLineOffset(neighbor, direction); Handles.DrawLine(self.transform.localPosition + offset1, neighbor.transform.localPosition - offset2); } } }
private NetworkNode.Direction FindRepeatNodeDirection(NetworkNode newValue, NetworkNode.Direction direction) { for (int i = 0; i < NetworkNode.NeighborNum; i++) { // 跳过自身 if (i == (int)direction) { continue; } if (self.Neighbors[i] == newValue) { return((NetworkNode.Direction)i); } } return(NetworkNode.Direction.Unknown); }
private void DrawNode(NetworkNode.Direction direction) { NetworkNode neighbor = self.Neighbors[(int)direction]; NetworkNode newValue = EditorGUILayout.ObjectField( direction.ToString(), neighbor, typeof(NetworkNode), true) as NetworkNode; if (newValue == self) { ShowTips("不能将自己设为目标"); return; } if (newValue == neighbor) { return; } if (newValue == null) { neighbor.SetNeighbor(NetworkNode.GetOppositeDirection(direction), null); self.SetNeighbor(direction, null); EditorUtility.SetDirty(neighbor); EditorUtility.SetDirty(self); } else { NetworkNode.Direction repeatDirection = FindRepeatNodeDirection(newValue, direction); if (repeatDirection != NetworkNode.Direction.Unknown) { ShowTips(string.Format("节点{0}不能应用在多个出口,已调整到新的出口", newValue.name)); self.GetNeighbor(repeatDirection).SetNeighbor(NetworkNode.GetOppositeDirection(repeatDirection), null); self.SetNeighbor(repeatDirection, null); } // 如果原来有值,又换了新值,应该清除原来的引用 if (neighbor != null) { neighbor.SetNeighbor(NetworkNode.GetOppositeDirection(direction), null); EditorUtility.SetDirty(neighbor); } newValue.SetNeighbor(NetworkNode.GetOppositeDirection(direction), self); self.SetNeighbor(direction, newValue); Debug.Log(string.Format("Set {0} to {1}", direction, newValue)); EditorUtility.SetDirty(newValue); EditorUtility.SetDirty(self); } }
private void CreateFlowBetween(NetworkNode first, NetworkNode second, NetworkNode.Direction direction) { NetworkFlow existFlow = activeFlowList.Find(x => x.FirstNode == second && x.SecondNode == first); // 两条有向边共享同一个flow GameObject GameObject flowObject = existFlow == null? CreateFlowGameObject(first, second, direction) : existFlow.FlowObject; NetworkFlow newFlow = new NetworkFlow(flowObject) { FirstNode = first, SecondNode = second, Direction = direction }; first.SetFlow(direction, newFlow); activeFlowList.Add(newFlow); }
public virtual void OnInput(NetworkNode.Direction from, bool isActive) { }
private void TraverseNetworkFlow() { // 清理工作:将所有节点和流都关闭 foreach (var node in activeNodeList) { if (!(node is StartNode)) { node.Clear(); } } foreach (var flow in activeFlowList) { flow.Clear(); } visitedFlowList.Clear(); // 从起点开始,查找周边的通路 for (int i = 0; i < NetworkNode.NeighborNum; i++) { var flow = startNode.GetFlow(i); if (flow != null && startNode.IsReachableTo(flow.Direction)) { flowQueue.Enqueue(flow); Log(string.Format("Enqueue: {0}", flow)); } } // 开始遍历网络图 while (flowQueue.Count > 0) { NetworkFlow flow = flowQueue.Dequeue(); NetworkNode node = flow.FirstNode; NetworkNode neighbor = flow.SecondNode; visitedFlowList.Add(flow); Log(string.Format("Visit {0}", flow)); // 激活流和输入状态 NetworkNode.Direction direction = node.GetNeighborDirection(neighbor); flow.Activate(node, neighbor); neighbor.SetInput(NetworkNode.GetOppositeDirection(direction), true); bool nextFlowFound = false; for (int i = 0; i < NetworkNode.NeighborNum; i++) { var nextFlow = neighbor.GetFlow(i); if (nextFlow == null || visitedFlowList.Contains(nextFlow) || flowQueue.Contains(nextFlow) || // 第一趟查找,不包含那些已经走过一遍的通路 IsVisitedFlowExist(nextFlow.SecondNode, nextFlow.FirstNode)) { continue; } if (neighbor.IsReachableTo(nextFlow.Direction)) { Log(string.Format("Enqueue: {0}", nextFlow)); flowQueue.Enqueue(nextFlow); nextFlowFound = true; } } // 第二趟查找,可能包含反向的通路 if (!nextFlowFound) { for (int i = 0; i < NetworkNode.NeighborNum; i++) { var nextFlow = neighbor.GetFlow(i); if (nextFlow == null || visitedFlowList.Contains(nextFlow) || flowQueue.Contains(nextFlow)) { continue; } if (neighbor.IsReachableTo(nextFlow.Direction)) { Log(string.Format("Enqueue2: {0}", nextFlow)); flowQueue.Enqueue(nextFlow); } } } } }
private GameObject CreateFlowGameObject(NetworkNode first, NetworkNode second, NetworkNode.Direction direction) { Vector3 offset1 = NetworkNode.GetDrawLineOffset(first, direction); Vector3 offset2 = NetworkNode.GetDrawLineOffset(second, direction); GameObject flowGo = Instantiate(NetworkFlowPrefab, NetworkFlows); LineRenderer lineRenderer = flowGo.GetComponent <LineRenderer>(); lineRenderer.SetPositions(new Vector3[] { first.transform.localPosition + offset1, second.transform.localPosition - offset2 }); return(flowGo); }