Exemple #1
0
        private static Collection <PathNode> GetNeighbours(PathNode pathNode,
                                                           Vector3Int goal)
        {
            var result = new Collection <PathNode>();

            // Соседними точками являются соседние по стороне клетки.
            Vector3Int[] neighbourPoints = new Vector3Int[4];
            neighbourPoints[0] = new Vector3Int(pathNode.Position.x + 1, pathNode.Position.y, pathNode.Position.z);
            neighbourPoints[1] = new Vector3Int(pathNode.Position.x - 1, pathNode.Position.y, pathNode.Position.z);
            neighbourPoints[2] = new Vector3Int(pathNode.Position.x, pathNode.Position.y + 1, pathNode.Position.z);
            neighbourPoints[3] = new Vector3Int(pathNode.Position.x, pathNode.Position.y - 1, pathNode.Position.z);

            var chunk = ChunkManager.CurrentChunk;

            foreach (var point in neighbourPoints)
            {
                // Проверяем, что не вышли за границы карты.
                if (point.x < 0 || point.x >= chunk.MapSize)
                {
                    continue;
                }
                if (point.y < 0 || point.y >= chunk.MapSize)
                {
                    continue;
                }

                if (ChunkUtil.IsStairsIndex(chunk.ChunkNumber, ChunkUtil.GetUpper(point)))
                {
                    var p = ChunkUtil.GetUpper(point);
                    point.Set(p.x, p.y, p.z);
                }
                else if (!ChunkUtil.IsAnyEntity(chunk.ChunkNumber, point) &&
                         ChunkUtil.IsStairsIndex(chunk.ChunkNumber, pathNode.Position))
                {
                    var p = ChunkUtil.GetDovvner(point);
                    point.Set(p.x, p.y, p.z);
                }


                if (point.z < 0 || point.z >= ChunkManager.MaxGroundsLvls)
                {
                    continue;
                }


                // Проверяем, что по клетке можно ходить.
                if (IsInvalidPath(point)
                    )
                {
                    continue;
                }
                // Заполняем данные для точки маршрута.
                var neighbourNode = new PathNode()
                {
                    Position            = point,
                    CameFrom            = pathNode,
                    PathLengthFromStart = pathNode.PathLengthFromStart +
                                          GetDistanceBetweenNeighbours(),
                    HeuristicEstimatePathLength = GetHeuristicPathLength(point, goal)
                };
                result.Add(neighbourNode);
            }
            return(result);
        }