Esempio n. 1
0
        public List<Tile> SearchPath(bool diagonal = true)
        {
            int i;
            for (i = 0; i < tiles.Count; i++)
            {
                if (tiles[i].Type == 1)
                    start = new Node(tiles[i].ID, null);
                if (tiles[i].Type == 2)
                    end = new Node(tiles[i].ID, null);
            }

            openList.Add(start);
            current = start;

            while (true)
            {
                if (openList.Count == 0)
                    break;

                current = FindSmallestF();

                if (current.CellIndex == end.CellIndex)
                    break;

                openList.Remove(current);
                closedList.Add(current);

                if (diagonal)
                {
                    //Diagonal
                    AddAdjacentCellToOpenList(current, 1, -1, 14);
                    AddAdjacentCellToOpenList(current, -1, -1, 14);
                    AddAdjacentCellToOpenList(current, -1, 1, 14);
                    AddAdjacentCellToOpenList(current, 1, 1, 14);
                }
                //Straight
                AddAdjacentCellToOpenList(current, 0, -1, 10);
                AddAdjacentCellToOpenList(current, -1, 0, 10);
                AddAdjacentCellToOpenList(current, 1, 0, 10);
                AddAdjacentCellToOpenList(current, 0, 1, 10);

            }
            while (current != null)
            {
                bool endOnClosed = false;
                for (int v = 0; v < openList.Count; v++)
                    if (openList[v].CellIndex == end.CellIndex)
                        endOnClosed = true;
                if (endOnClosed && tiles[current.CellIndex].Type != 2 && tiles[current.CellIndex].Type != 1)
                    tiles[current.CellIndex].Type = 4;
                current = current.Parent;
            }

            return tiles;
        }
Esempio n. 2
0
        private void AddAdjacentCellToOpenList(Node parentNode, int columnOffset, int rowOffset, int gCost)
        {
            var adjacentCellIndex = GetAdjacentCellIndex(parentNode.CellIndex, columnOffset, rowOffset);

            // ignore unwalkable nodes (or nodes outside the grid)
            if (adjacentCellIndex == -1)
                return;

            // ignore nodes on the closed list
            if (closedList.Any(n => n.CellIndex == adjacentCellIndex))
                return;

            var adjacentNode = openList.SingleOrDefault(n => n.CellIndex == adjacentCellIndex);
            if (adjacentNode != null)
            {
                if (parentNode.G + gCost < adjacentNode.G)
                {
                    adjacentNode.Parent = parentNode;
                    adjacentNode.G = parentNode.G + gCost;
                    adjacentNode.F = adjacentNode.G + adjacentNode.H;
                }

                return;
            }

            var node = new Node(adjacentCellIndex, parentNode) { G = gCost, H = GetDistance(adjacentCellIndex, end.CellIndex) };
            node.F = node.G + node.H;
            openList.Add(node);
        }
Esempio n. 3
0
 public Node(int cellIndex, Node parent)
 {
     CellIndex = cellIndex;
     Parent = parent;
 }