コード例 #1
0
ファイル: AStar.cs プロジェクト: faijasmine/PuzzleGameEngine
        List<PathNode> FindAdjacentNodes(PathNode currentNode, PathNode endNode)
        {
            List<PathNode> adjacentNodes = new List<PathNode>();

            int X = currentNode.GridX;
            int Y = currentNode.GridY;

            bool upLeft = true;
            bool upRight = true;
            bool downLeft = true;
            bool downRight = true;

            if ((X > 0) && (TileMap.CellIsPassable(X - 1, Y)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X - 1, Y), CostStraight + currentNode.DirectCost));
            }
            else
            {
                upLeft = false;
                downLeft = false;
            }

            if ((X < TileMap.MapWidth - 1) && (TileMap.CellIsPassable(X + 1, Y)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X + 1, Y), CostStraight + currentNode.DirectCost));
            }
            else
            {
                upRight = false;
                downRight = false;
            }


            if ((Y > 0) && (TileMap.CellIsPassable(X, Y - 1)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X, Y - 1), CostStraight + currentNode.DirectCost));
            }
            else
            {
                upLeft = false;
                upRight = false;
            }

            if ((Y < TileMap.MapHeight - 1) && (TileMap.CellIsPassable(X, Y + 1)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X, Y + 1), CostStraight + currentNode.DirectCost));
            }
            else
            {
                downLeft = false;
                downRight = false;
            }


            if ((upLeft) && (TileMap.CellIsPassable(X - 1, Y - 1)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X - 1, Y - 1), CostDiagonal + currentNode.DirectCost));
            }

            if ((upRight) && (TileMap.CellIsPassable(X + 1, Y - 1)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X + 1, Y - 1), CostDiagonal + currentNode.DirectCost));
            }

            if ((downLeft) && (TileMap.CellIsPassable(X - 1, Y + 1)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X - 1, Y + 1), CostDiagonal + currentNode.DirectCost));
            }

            if ((downRight) && (TileMap.CellIsPassable(X + 1, Y + 1)))
            {
                adjacentNodes.Add(new PathNode(currentNode, endNode, new Vector2(X + 1, Y + 1), CostDiagonal + currentNode.DirectCost));
            }

            return adjacentNodes;
        }
コード例 #2
0
ファイル: AStar.cs プロジェクト: faijasmine/PuzzleGameEngine
        public List<Vector2> FindPath(Vector2 startTile, Vector2 endTile)
        {
            if (!TileMap.CellIsPassable(endTile) || !TileMap.CellIsPassable(startTile))
            {
                return null;
            }

            openList.Clear();
            nodeCosts.Clear();
            nodeStatus.Clear();

            PathNode startNode;
            PathNode endNode;

            endNode = new PathNode(null, null, endTile, 0);
            startNode = new PathNode(null, endNode, startTile, 0);

            AddNodeToOpenList(startNode);

            while (openList.Count > 0)
            {
                PathNode currentNode = openList[openList.Count - 1];

                if (currentNode.IsEqualToNode(endNode))
                {
                    List<Vector2> bestPath = new List<Vector2>();
                    while (currentNode != null)
                    {
                        bestPath.Insert(0, currentNode.GridLocation);
                        currentNode = currentNode.ParentNode;
                    }
                    return bestPath;
                }

                openList.Remove(currentNode);
                nodeCosts.Remove(currentNode.GridLocation);

                foreach (PathNode possibleNode in FindAdjacentNodes(currentNode, endNode))
                {
                    if (nodeStatus.ContainsKey(possibleNode.GridLocation))
                    {
                        if (nodeStatus[possibleNode.GridLocation] == NodeStatus.Closed)
                        {
                            continue;
                        }

                        if (nodeStatus[possibleNode.GridLocation] == NodeStatus.Open)
                        {
                            if (possibleNode.TotalCost >= nodeCosts[possibleNode.GridLocation])
                            {
                                continue;
                            }
                        }
                    }

                    AddNodeToOpenList(possibleNode);
                }

                nodeStatus[currentNode.GridLocation] = NodeStatus.Closed;
            }

            return null;
        }