Example #1
0
        private Cord MergeJunction()
        {
            List <Vector3> mergedPath = new List <Vector3>();

            //If the branch cord is collapsing then we merge the two cords in the junction
            mergedPath.AddRange(cordJunction.A.Cord.Path);
            mergedPath.AddRange(cordJunction.B.Cord.Path);

            cordJunction.A.Cord.Connect(cordJunction.A.Cord.StartNode, cordJunction.B.Cord.EndNode);
            cordJunction.A.Cord.Path = mergedPath;

            if (cordJunction.B.Cord.EndNode.GetComponent <BranchHandle>())
            {
                BranchHandle endHandle = cordJunction.B.Cord.EndNode.GetComponent <BranchHandle>();
                endHandle.ReplaceCord(cordJunction.B.Cord, cordJunction.A.Cord);
            }

            cordJunction.B.Cord.DestroyCord();

            return(cordJunction.A.Cord);
        }
Example #2
0
        /// <summary>
        /// This method searches the network of cords connected to this cord in the direction determined by reverseFlow.
        ///
        /// For a given cord, flow is positive if the cord flows from A -> B (i.e. A is the output and B is the input)
        /// Flow is negative if the cords flows from B -> A
        ///
        /// When searching, we look for nodes that are 'downstream' from the start point. This means we only take paths with positive flow during our search.
        /// This is reversed when searching for output jacks
        ///
        /// </summary>
        /// <param name="reverseFlow"></param>
        /// <param name="cordPos"></param>
        /// <returns></returns>
        public HashSet <PhysicalDataEndpoint> GetConnectedEndpoints(bool reverseFlow, Transform searchStartNode)
        {
            HashSet <PhysicalDataEndpoint> results = new HashSet <PhysicalDataEndpoint>();

            float workingFlow = Flow;

            if (reverseFlow)
            {
                workingFlow = -workingFlow;
            }

            if (workingFlow > 0 && !B.Equals(searchStartNode))
            {
                BranchHandle handleB = B.GetComponent <BranchHandle>();
                results.UnionWith(GetEndpointsConnectedToHandle(reverseFlow, handleB));

                Plug plugB = B.GetComponentInActor <Plug>();
                PhysicalDataEndpoint connectedReceptacle = GetConnectedEndpoint(plugB);

                if (connectedReceptacle != null)
                {
                    results.Add(connectedReceptacle);
                }
            }
            else if (workingFlow < 0 && !A.Equals(searchStartNode))
            {
                BranchHandle handleA = A.GetComponent <BranchHandle>();
                results.UnionWith(GetEndpointsConnectedToHandle(reverseFlow, handleA));

                Plug plugA = A.GetComponentInActor <Plug>();
                PhysicalDataEndpoint connectedReceptacle = GetConnectedEndpoint(plugA);

                if (connectedReceptacle != null)
                {
                    results.Add(connectedReceptacle);
                }
            }

            return(results);
        }
Example #3
0
        /// <summary>
        /// Splits the cord so that this cord contains the points before splitPoint and splitCord contains the points after splitPoint
        /// </summary>
        /// <param name="handle"></param>
        /// <param name="splitPointIndex"></param>
        /// <param name="splitCord"></param>
        /// <returns></returns>
        public CordJunction SplitByBranchHandle(BranchHandle handle, int splitPointIndex, Cord splitCord)
        {
            //Get the points after the splitPoint and assign them to the splitCord
            List <Vector3> splitPath = new List <Vector3>();

            for (int i = splitPointIndex; i < path.Count; i++)
            {
                splitPath.Add(path[i]);
            }

            splitCord.Color = Color;
            splitCord.Flow  = Flow;
            splitCord.Connect(handle.transform, B);
            if (B.GetComponent <BranchHandle>() != null)
            {
                B.GetComponent <BranchHandle>().ReplaceCord(this, splitCord);
            }

            splitCord.Path = splitPath;
            splitCord.AllowBranching(true);

            int combinedPathLength = path.Count;

            //Exclude the points in the splitCord from this cord
            for (int i = 0; i < combinedPathLength - splitPointIndex; i++)
            {
                path.RemoveAt(path.Count - 1);
            }
            B = handle.transform;

            branchHandles.Remove(handle);
            CreateBranchHandles(handle.TrackedController);
            UpdateBoundingBox();

            CordNode JunctionA = new CordNode(this, handle.transform);
            CordNode JunctionB = new CordNode(splitCord, handle.transform);

            return(new CordJunction(JunctionA, JunctionB, Flow));
        }
Example #4
0
        /// <summary>
        /// A BranchHandle exists at the intersection of three cords:
        /// The two cords in the CordJunction that was formed by splitting the source cord as well as the newly created branch cord
        ///
        /// This method takes a cord that is about to collapse as the parameter and merges the other two cords of the handle together.
        /// </summary>
        /// <param name="collapsedCord"></param>
        public void MergeRemainingCords(Cord collapsedCord)
        {
            if (collapsedCord.Equals(branchNode.Cord))
            {
                //If the branch cord has collapsed we merge the junction and end up with the same cord that was split by the BranchHandle
                MergeJunction();
            }
            else
            {
                //Otherwise one side of the junction has collapsed so we merge the branch cord with the other side of the junction
                List <Vector3> mergedPath = new List <Vector3>();
                CordNode       toMerge    = collapsedCord.Equals(cordJunction.A.Cord) ? cordJunction.B : cordJunction.A;

                Transform branchEnd = branchNode.transform.Equals(branchNode.Cord.StartNode) ? branchNode.Cord.EndNode : branchNode.Cord.StartNode;

                if (toMerge.transform.Equals(toMerge.Cord.StartNode))
                {
                    toMerge.Cord.Connect(branchEnd, toMerge.Cord.EndNode);
                    mergedPath.AddRange(branchNode.Cord.Path);
                    mergedPath.AddRange(toMerge.Cord.Path);
                }
                else
                {
                    toMerge.Cord.Connect(toMerge.Cord.StartNode, branchEnd);
                    mergedPath.AddRange(toMerge.Cord.Path);
                    mergedPath.AddRange(branchNode.Cord.Path);
                }

                toMerge.Cord.Path = mergedPath;

                if (branchEnd.GetComponent <BranchHandle>())
                {
                    BranchHandle startHandle = branchEnd.GetComponent <BranchHandle>();
                    startHandle.ReplaceCord(branchNode.Cord, toMerge.Cord);
                }

                branchNode.Cord.DestroyCord();
            }
        }
Example #5
0
        private HashSet <PhysicalDataEndpoint> GetEndpointsConnectedToHandle(bool reverseFlow, BranchHandle handle)
        {
            HashSet <PhysicalDataEndpoint> results = new HashSet <PhysicalDataEndpoint>();

            if (handle != null)
            {
                CordNode  downstreamJunctionNode = handle.GetDownstreamJunctionNode(reverseFlow);
                Transform other = downstreamJunctionNode.Cord.GetOppositeEnd(downstreamJunctionNode.transform);

                results.UnionWith(GetEndpointsConnectedToNode(reverseFlow, downstreamJunctionNode));

                CordNode branchNode = handle.BranchNode;
                results.UnionWith(GetEndpointsConnectedToNode(reverseFlow, branchNode));
            }

            return(results);
        }