示例#1
0
    List <GameObject> SplitStrand(Strand str, Node[] n, bool hidden = false)
    {
        List <GameObject> splits = new List <GameObject>();
        int strandIndex          = strandList.IndexOf(str);

        if (strandIndex < 4)
        {
            return(splits);
        }
        float startX, startY, endX, endY;

        GameObject strandGO = GameObject.Find(string.Format("Strand{0}", strandIndex - 3));
        Node       startN = str.GetStartNode(), endN = str.GetEndNode();

        strandList.RemoveAt(strandIndex);
        Destroy(strandGO);
        strandGO = null;

        for (int i = strandIndex; i < strandList.Count; i++)
        {
            GameObject go = GameObject.Find(string.Format("Strand{0}", i - 2));
            go.name = string.Format("Strand{0}", i - 3);
        }

        startX = startN.xPos();
        startY = startN.yPos();
        endX   = endN.xPos();
        endY   = endN.yPos();

        GameObject ds;

        ds = DrawScaledStrand(startX, startY, n[0].xPos(), n[0].yPos());
        ds.GetComponent <Renderer>().enabled = !hidden;
        splits.Add(ds);

        for (int i = 0; i < n.Length - 1; i++)
        {
            ds = DrawScaledStrand(n[i].xPos(), n[i].yPos(), n[i + 1].xPos(), n[i + 1].yPos());
            ds.GetComponent <Renderer>().enabled = !hidden;
            splits.Add(ds);
        }

        ds = DrawScaledStrand(n[n.Length - 1].xPos(), n[n.Length - 1].yPos(), endX, endY);
        ds.GetComponent <Renderer>().enabled = !hidden;
        splits.Add(ds);

        return(splits);
    }
示例#2
0
    public void nameTBD(Vector3 begin, Vector3 finish)
    {
        drawnStrand = drawStrandBetween(begin, finish, true);

        int strandIndex;

        string[] splitName = drawnStrand.name.Split(new string[] { "d" }, StringSplitOptions.RemoveEmptyEntries);
        // foreach(string s in splitName){
        //  Debug.Log(s);
        // }
        strandIndex = Int32.Parse(splitName[1]) + 3;
        // Debug.Log(string.Format("strandList.Count = {0}, strandIndex = {1}", strandList.Count, strandIndex));

        if (strandIndex > strandList.Count)
        {
            return;
        }
        Strand addedStrand = strandList[strandIndex];

        Strand            startStrand, endStrand;
        bool              startStrandValid = StrandListContainsPoint(begin, out startStrand);
        bool              endStrandValid = StrandListContainsPoint(finish, out endStrand);
        List <GameObject> intersectNodes = new List <GameObject>(), splitStrands = null;

        bool existingStartNodeCopy = existingStartNode, existingEndNodeCopy = existingEndNode;

        Vector3 intersectPt = Vector3.zero;

        // bool drawnStrandIsSplit = false;
        for (int i = strandList.Count - 1; i >= 0; i--)
        {
            Strand str = strandList[i];
            // Skipping self
            if (str.Equals(addedStrand))
            {
                continue;
            }
            ;
            if (str.Equals(startStrand) && !existingStartNodeCopy)
            {
                splitStrands = SplitStrand(str, new Node[] { addedStrand.GetStartNode() });
            }
            if (str.Equals(endStrand) && !existingEndNodeCopy)
            {
                splitStrands = SplitStrand(str, new Node[] { addedStrand.GetEndNode() });
            }
            if (addedStrand.Intersects(str, out intersectPt))
            {
                splitStrands = SplitStrand(str, new Node[] { new Node(intersectPt.x, intersectPt.y) });
                GameObject newNode = Instantiate(nodePrefab, intersectPt, Quaternion.identity) as GameObject;
                // newNode.GetComponent<Renderer>().enabled = false;
                intersectNodes.Add(newNode);

                newNode.name = string.Format("Node{0}", nodeList.Count - 3);
                nodeList.Add(new Node(intersectPt.x, intersectPt.y));
            }
        }

        if (intersectNodes.Count > 0)
        {
            // drawnStrandIsSplit = true;
            Node[] iNodes = new Node[intersectNodes.Count];
            // Debug.Log(string.Format("{0} intersection points", iNodes.Length));
            for (int j = 0; j < intersectNodes.Count; j++)
            {
                GameObject goTemp = intersectNodes[j];

                iNodes[j] = new Node(goTemp.transform.position.x, goTemp.transform.position.y);
            }
            // string debugString = "Split strands are: ";
            splitStrands = SplitStrand(addedStrand, iNodes, false);
            // foreach(var split in splitStrands){
            //  debugString +=  string.Format("{0} at {1}", split.name, split.transform.position.ToString());
            // }
            // Debug.Log(debugString);

            // string dString = "";
            // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
            //  dString += string.Format("{0}, ", g.name);
            // }
            // Debug.Log(dString);
            // Destroy(drawnStrand);
        }
        // Debug.Log("after destroying original");
        // string debugString = "";
        // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
        //  debugString += string.Format("{0}, ", g.name);
        // }
        // Debug.Log(debugString);

        drawnStrand = null;
        UpdateAdjacencyMatrix();
    }
示例#3
0
    List <GameObject> SplitStrand(Strand str, Node[] n, bool hidden = false)
    {
        List <GameObject> splits = new List <GameObject>();
        int strandIndex          = strandList.IndexOf(str);

        if (strandIndex < 4)
        {
            return(splits);
        }
        // float startX, startY, endX, endY;

        GameObject strandGO = GameObject.Find(string.Format("Strand{0}", strandIndex - 3));

        // Debug.Log(string.Format("Found {0}", strandGO.name));
        strandGO.name = "Delete_Me";
        Node startN = str.GetStartNode(), endN = str.GetEndNode();

        // string debugString = "Before delete\n";
        // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
        //  debugString += string.Format("{0}, ", g.name);
        // }
        // Debug.Log(debugString);

        strandList.RemoveAt(strandIndex);
        UnityEngine.Object.Destroy(strandGO);
        strandGO = null;

        // debugString = "After delete\n";
        // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
        //  debugString += string.Format("{0}, ", g.name);
        // }
        // Debug.Log(debugString);

        for (int i = strandIndex; i < strandList.Count; i++)
        {
            GameObject go = GameObject.Find(string.Format("Strand{0}", i - 2));
            go.name = string.Format("Strand{0}", i - 3);
        }

        // debugString = "After Rename\n";
        // foreach (var g in GameObject.FindGameObjectsWithTag("Edge")) {
        //  debugString += string.Format("{0}, ", g.name);
        // }
        // Debug.Log(debugString);

        // GameObject[] renamedStrands = GameObject.FindGameObjectsWithTag("Edge");
        // foreach(GameObject rgo in renamedStrands){
        //  Debug.Log(rgo.name);
        // }

        // startX = startN.xPos();
        // startY = startN.yPos();
        // endX = endN.xPos();
        // endY = endN.yPos();

        GameObject ds;

        ds = drawStrandBetween(startN.Position(), n[0].Position(), false);
        ds.GetComponent <Renderer>().enabled = !hidden;
        splits.Add(ds);

        for (int i = 0; i < n.Length - 1; i++)
        {
            ds = drawStrandBetween(n[i].Position(), n[i + 1].Position(), false);
            ds.GetComponent <Renderer>().enabled = !hidden;
            splits.Add(ds);
        }

        ds = drawStrandBetween(n[n.Length - 1].Position(), endN.Position(), false);
        ds.GetComponent <Renderer>().enabled = !hidden;
        splits.Add(ds);

        return(splits);
    }
示例#4
0
    void CalculatePlayerMovePath(Vector3 playerStart, Vector3 playerDestination)
    {
        Strand      startStrand, endStrand;
        Node        s1, s2, e1, e2;
        int         startIndex, finishIndex;
        List <Node> path1, path2, path3, path4;

        path1 = new List <Node>();
        path2 = new List <Node>();
        path3 = new List <Node>();
        path4 = new List <Node>();

        int matrixSize = adjacencyMatrix.GetLength(0);

        Dictionary <Node, Dictionary <Node, float> > vertexList = new Dictionary <Node, Dictionary <Node, float> >();
        Dictionary <Node, float> nodeMap = new Dictionary <Node, float>();

        for (int i = 0; i < matrixSize; i++)
        {
            for (int j = 0; j < matrixSize; j++)
            {
                if (adjacencyMatrix[i, j] > 0)
                {
                    nodeMap.Add(nodeList[j], adjacencyMatrix[i, j]);
                }
            }
            vertexList[nodeList[i]] = new Dictionary <Node, float>(nodeMap);
            nodeMap.Clear();
        }

        if (StrandListContainsPoint(playerStart, out startStrand) &&
            StrandListContainsPoint(playerDestination, out endStrand))
        {
            if (startStrand.Equals(endStrand))
            {
                return;
            }

            s1 = startStrand.GetStartNode();
            s2 = startStrand.GetEndNode();
            e1 = endStrand.GetStartNode();
            e2 = endStrand.GetEndNode();

            bool startOnFrame, endOnFrame, startStrandIsFrame = false, endStrandIsFrame = false;

            for (int i = 0; i < 4; i++)
            {
                startOnFrame = strandList[i].ContainsPointWithEnds(s1.xPos(), s1.yPos());
                endOnFrame   = strandList[i].ContainsPointWithEnds(s2.xPos(), s2.yPos());
                if (startOnFrame && endOnFrame)
                {
                    startStrandIsFrame = true;
                }
                startOnFrame = strandList[i].ContainsPointWithEnds(e1.xPos(), e1.yPos());
                endOnFrame   = strandList[i].ContainsPointWithEnds(e2.xPos(), e2.yPos());
                if (startOnFrame && endOnFrame)
                {
                    endStrandIsFrame = true;
                }
            }

            Node temp;
            if (startStrandIsFrame && nodeList.Count > 4)
            {
                // Debug.Log("start is frame");
                temp = startStrand.GetClosestNodeOnStrand(playerStart, nodeList);
                if (temp.xPos() != float.MaxValue && temp.yPos() != float.MaxValue)
                {
                    s1 = temp;
                    s2 = startStrand.GetClosestEnd(playerStart);
                }
            }
            if (endStrandIsFrame && nodeList.Count > 4)
            {
                // Debug.Log("end is frame");
                temp = endStrand.GetClosestNodeOnStrand(playerDestination, nodeList);
                if (temp.xPos() != float.MaxValue && temp.yPos() != float.MaxValue)
                {
                    e1 = temp;
                    e2 = endStrand.GetClosestEnd(playerDestination);
                }
            }

            Node playerStartNode       = new Node(playerStart.x, playerStart.y),
                 playerDestinationNode = new Node(playerDestination.x, playerDestination.y);

            if (nodeListContainsAt(playerStartNode, out startIndex))
            {
                // Debug.Log(string.Format("Player position on existing node at {0}", playerStartNode.PositionString()));
                s1 = playerStartNode;
                s2 = playerStartNode;
            }
            // end node is always existing!
            if (nodeListContainsAt(playerDestinationNode, out finishIndex))
            {
                // Debug.Log(string.Format("Destination is existing node at {0}", playerDestinationNode.PositionString()));
                e1 = playerDestinationNode;
                e2 = playerDestinationNode;
            }

            graph = new Graph(vertexList);

            if (nodeListContainsAt(s1, out startIndex) && nodeListContainsAt(e1, out finishIndex))
            {
                if (s1.Equals(e1))
                {
                    path1.Add(s1);
                }
                else
                {
                    path1 = graph.ShortestPath(s1, e1);
                }
            }

            if (nodeListContainsAt(s1, out startIndex) && nodeListContainsAt(e2, out finishIndex))
            {
                if (s1.Equals(e2))
                {
                    path2.Add(s1);
                }
                else
                {
                    path2 = graph.ShortestPath(s1, e2);
                }
            }

            if (nodeListContainsAt(s2, out startIndex) && nodeListContainsAt(e1, out finishIndex))
            {
                if (s2.Equals(e1))
                {
                    path3.Add(s2);
                }
                else
                {
                    path3 = graph.ShortestPath(s2, e1);
                }
            }

            if (nodeListContainsAt(s2, out startIndex) && nodeListContainsAt(e2, out finishIndex))
            {
                if (s2.Equals(e2))
                {
                    path4.Add(s2);
                }
                else
                {
                    path4 = graph.ShortestPath(s2, e2);
                }
            }

            List <Node>[] paths = new List <Node>[] { path1, path2, path3, path4 };
            playerMovePath = ShortestDistance(paths);

            // string pathlist = "";
            // foreach(var moveNode in playerMovePath){
            //   if(playerMovePath.FindIndex(a => a.Equals(moveNode)) == playerMovePath.Count-1){
            //     pathlist += string.Format("{0}", moveNode.PositionString());
            //   } else {
            //     pathlist += string.Format("{0} => ", moveNode.PositionString());
            //   }
            // }
            // Debug.Log(pathlist);
        }
    }