コード例 #1
0
ファイル: AStar.cs プロジェクト: pampersrocker/STAR
        private void MapToArray(ref PathNode[,] searchmap)
        {
            Node[,] map = AStarMap.Mapnodes;
            //searchmap = new PathNode[AStarMap.GetLength(0),AStarMap.GetLength(1)];
            int ymax = searchmap.GetLength(0);
            int xmax = searchmap.GetLength(1);
            for (int y = 0; y < ymax; y++)
            {
                for (int x = 0; x < xmax; x++)
                {
                    searchmap[y, x].FromNode(map[y, x]);
                }
            }

            //return searchmap;
        }
コード例 #2
0
ファイル: AStar.cs プロジェクト: pampersrocker/STAR
        private void GetNewNode(PathNode rootNode, int xOffset, int yOffset,PathNode[,] searchMap)
        {
            //Check if xOffset is in Bounds
            if(rootNode.MapXPosition + xOffset >= 0 && rootNode.MapXPosition + xOffset < searchMap.GetLength(1))
                if (rootNode.MapYPosition + yOffset >= 0 && rootNode.MapYPosition + yOffset < searchMap.GetLength(0))
                {
                    PathNode newNode = searchMap[rootNode.MapYPosition + yOffset, rootNode.MapXPosition + xOffset];
                    //Check if its not the RootNode
                    if (newNode != rootNode)
                        //Exclude Closed Nodes
                        if (newNode.State == NodeState.Unknown)
                        {
                            if (newNode.Walkable != Walkable.Blocked)
                            {
                                if (!(newNode.Walkable == Walkable.NotReachable && yOffset < 0) && !(newNode.Walkable == Walkable.Platform && yOffset > 0))
                                {
                                    newNode.RootNode = rootNode;
                                    newNode.State = NodeState.Known;
                                    newNode.CalculateFCost(aimglobal.Rectangle.CenterToVector2());
                                    //openList.Add(newNode);
                                    try
                                    {
                                        LinkedListNode<PathNode> smaller = openLinkedList.First;
                                        while (newNode.FCost > smaller.Value.FCost)
                                        {
                                            smaller = smaller.Next;
                                        }
                                        openLinkedList.AddBefore(smaller, newNode);
                                    }
                                    catch (Exception)
                                    {

                                        openLinkedList.AddLast(newNode);
                                    }
                                }
                            }
                        }
                        else if (newNode.State == NodeState.Known)
                        {
                            float oldFCost = newNode.FCostNoAim;
                            //Calculate new F Cost
                            float newFCost = newNode.CalculateFCost(aimglobal.Rectangle.CenterToVector2(), rootNode);
                            //If newFcost ist smaller than the old F Cost, we found a shorter way to this node
                            if (newFCost < oldFCost)
                            {
                                openLinkedList.Remove(newNode);
                                newNode.RootNode = rootNode;
                                newNode.CalculateFCost(aimglobal.Rectangle.CenterToVector2());
                                try
                                {
                                    LinkedListNode<PathNode> smaller = openLinkedList.First;
                                    while (newNode.FCost > smaller.Value.FCost)
                                    {
                                        smaller = smaller.Next;
                                    }
                                    openLinkedList.AddBefore(smaller, newNode);
                                }
                                catch (Exception)
                                {

                                    openLinkedList.AddLast(newNode);
                                }
                            }
                            //openList.Add(newNode);
                        }
                }
        }