Beispiel #1
0
        public void GenerateMap()
        {
            int width = Core.XMLProperties.GlobalProperties.mGridWidth;
            int height = Core.XMLProperties.GlobalProperties.mGridHeight;
            int distance = Core.XMLProperties.GlobalProperties.mDistanceBetweenNodes;
            for (int i = 0; i <= width; i += distance)
            {
                for (int j = 0; j <= height; j += distance)
                {
                    PathNode node = new PathNode(mGameWorld.Camera, mGraphics, i, j);
                    mPathNodes.Add(node);
                }
            }

            int distance2 = System.Convert.ToInt32(distance * 1.5);
            foreach (PathNode p1 in mPathNodes)
            {
                foreach (PathNode p2 in mPathNodes)
                {
                    if (p1 != p2 && Vector2.Distance(p1.mPosition, p2.mPosition) < distance2)
                    {
                        p1.mNeighbours.Add(p2);
                    }
                }
            }
        }
Beispiel #2
0
        public List<Vector2> GeneratePath(PathNode startNode, PathNode endNode)
        {
            ResetPathNodes();

            List<Vector2> path = new List<Vector2>();

            if (startNode == null || endNode == null)
                return path;

            List<PathNode> closedList = new List<PathNode>();
            List<PathNode> openList = new List<PathNode>();

            float dist = Vector2.Distance(startNode.mPosition, endNode.mPosition);
            openList.Add(startNode);

            while (openList.Count > 0)
            {
                PathNode current = openList[0];
                openList.RemoveAt(0);

                if (current == endNode)
                {
                    while (current.mCurrentParent != null)
                    {
                        path.Add(current.mPosition);
                        current = current.mCurrentParent;
                    }
                    break;
                }

                foreach (PathNode neighbour in current.mNeighbours)
                {
                    float distFromTarget = Vector2.Distance(current.mPosition, endNode.mPosition);
                    float distFromSource = current.mCurrentCost + Vector2.Distance(current.mPosition, neighbour.mPosition);
                    float heuristic = current.mCurrentCost + distFromTarget;

                    int idxOpen = openList.IndexOf(neighbour);
                    if (idxOpen != -1 && openList[idxOpen].mCurrentCost < distFromSource)
                        continue;

                    int idxClosed = closedList.IndexOf(neighbour);
                    if (idxClosed != -1 && closedList[idxClosed].mCurrentCost < distFromSource)
                        continue;

                    if (idxOpen != -1)
                        openList.RemoveAt(idxOpen);

                    if (idxClosed != -1)
                        closedList.RemoveAt(idxClosed);

                    neighbour.mCurrentParent = current;
                    neighbour.mCurrentCost = distFromSource;
                    neighbour.mCurrentHeuristic = heuristic;

                    int insIdx = -1;
                    for (int i = 0; i < openList.Count; i++)
                    {
                        if (heuristic < openList[i].mCurrentHeuristic)
                        {
                            insIdx = i;
                            break;
                        }
                    }

                    if (insIdx == -1)
                    {
                        openList.Add(neighbour);
                    }
                    else
                        openList.Insert(insIdx, neighbour);
                }

                closedList.Add(current);
            }

            return path;
        }