예제 #1
0
        //списка соседей для точки:
        private static Collection<PathNode> GetNeighbours(PathNode pathNode,
          Point goal, AreaType[,] field)
        {
            var result = new Collection<PathNode>();

            // Соседними точками являются соседние по стороне клетки.
            Point[] neighbourPoints = new Point[8];
            neighbourPoints[0] = new Point(pathNode.Position.X + 1, pathNode.Position.Y);
            neighbourPoints[1] = new Point(pathNode.Position.X - 1, pathNode.Position.Y);
            neighbourPoints[2] = new Point(pathNode.Position.X, pathNode.Position.Y + 1);
            neighbourPoints[3] = new Point(pathNode.Position.X, pathNode.Position.Y - 1);
            neighbourPoints[4] = new Point(pathNode.Position.X + 1, pathNode.Position.Y+1);
            neighbourPoints[5] = new Point(pathNode.Position.X - 1, pathNode.Position.Y-1);
            neighbourPoints[6] = new Point(pathNode.Position.X-1, pathNode.Position.Y + 1);
            neighbourPoints[7] = new Point(pathNode.Position.X+1, pathNode.Position.Y - 1);
            foreach (var point in neighbourPoints)
            {
                // Проверяем, что не вышли за границы карты.
                if (point.X < 0 || point.X >= field.GetLength(0)*10)
                    continue;
                if (point.Y < 0 || point.Y >= field.GetLength(1)*10)
                    continue;
                // Проверяем, что по клетке можно ходить.
                if ((field[(int)point.X/10, (int)point.Y/10]== AreaType.Water))
                    continue;
                // Заполняем данные для точки маршрута.
                var neighbourNode = new PathNode()
                {
                    Position = point,
                    CameFrom = pathNode,
                    PathLengthFromStart = pathNode.PathLengthFromStart +
                      GetDistanceBetweenNeighbours(),
                    HeuristicEstimatePathLength = GetHeuristicPathLength(point, goal)
                };
                result.Add(neighbourNode);
            }
            return result;
        }