Example #1
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);
        }
    }