コード例 #1
0
    void FindPath()
    {
        var s      = flag.Split('.');
        int rowLen = s.Length / lineLen;

        bool[,] f = new bool[lineLen, rowLen];
        for (int i = 0; i < lineLen; i++)
        {
            for (int j = 0; j < rowLen; j++)
            {
                f[i, j] = s[i + j * lineLen] == "1" ? true : false;
            }
        }
        var grid = new Jps_Grid(f);

        jps = new Jps(grid);
        var paths = jps.FindPath(start, end);

        if (paths == null)
        {
            return;
        }
        _line.positionCount = paths.Count;

        Vector3[] poses = new Vector3[paths.Count];
        for (int i = 0; i < paths.Count; i++)
        {
            poses[i] = new Vector3(paths[i].x, 0, -paths[i].y);
        }
        _line.SetPositions(poses);
    }
コード例 #2
0
        /// <inheritdoc/>
        public PathfindingResponse FindJumpPointsWithJps(Position startPosition, Position targetPosition, JpsMode mode = JpsMode.AllowDiagonalBetweenWalls)
        {
            if (!_ready)
            {
                InitializeAlgorithms(_contextManager.Current);
            }

            Point startPoint  = _contextManager.Current.PathfindingData.PositionToZeroBasedPoint(startPosition);
            Point targetPoint = _contextManager.Current.PathfindingData.PositionToZeroBasedPoint(targetPosition);

            bool[,] wallMatrix = _contextManager.Current.PathfindingData.WallMatrixForJps;
            if (targetPoint.X >= wallMatrix.GetLength(0) || targetPoint.Y >= wallMatrix.GetLength(1) || wallMatrix[targetPoint.X, targetPoint.Y])
            {
                return(new PathfindingResponse(PathfindingResult.FailureTargetUnreachable));
            }

            List <Point> path;
            int          nodesOpen, nodesClosed;

            if (mode == JpsMode.AllowDiagonalBetweenWalls)
            {
                path = _jpsTightDiagonal.FindPath(startPoint, targetPoint, out nodesOpen, out nodesClosed);
            }
            else if (mode == JpsMode.Normal)
            {
                path = _jps.FindPath(startPoint, targetPoint, out nodesOpen, out nodesClosed);
            }
            else
            {
                path = _jpsStrictWalk.FindPath(startPoint, targetPoint, out nodesOpen, out nodesClosed);
            }

            //Debug.Log($"{(path.Any() ? "" : "Failed ")} Path from {startPoint} to {targetPoint} with {path.Count} steps, {nodesOpen} open nodes, {nodesClosed} closed nodes.");
            if (!path.Any())
            {
                return(new PathfindingResponse(PathfindingResult.FailureTargetUnreachable));
            }

            List <Position> resultJumpPointsInGrid = path.Select(_contextManager.Current.PathfindingData.PointToNonZeroBasedPosition).ToList();

            List <Position> naturalJumpPoints = _naturalLineCalculator.GetNaturalJumpPoints(resultJumpPointsInGrid).ToList();

            naturalJumpPoints.Reverse();
            return(new PathfindingResponse(naturalJumpPoints));
        }