Java 类com.badlogic.gdx.ai.pfa.GraphPath 实例源码

项目:conquest    文件:Troop.java   
public Troop(int troops, GraphPath<Tile> path) {
    this.troops = troops;
    this.path = new DefaultGraphPath<>();

    for (Tile tile : path) {
        this.path.add(tile);
    }

    texture = Assets.TROOP;
    setSize(texture.getRegionWidth(), texture.getRegionHeight());

    label = new ConquestLabel(this.troops, getX(), getY(), getWidth(), getHeight());

    setOrigin(Align.center);
    createActions();
}
项目:Inspiration    文件:IndexedAStarPathFinderTest.java   
@Test
public void searchNodePath_WhenDestinationUnreachable_ExpectedNoOuputPathFound () {
    // @off - disable libgdx formatter
    final String graphDrawing =
        ".....#....\n" +
        ".....#....\n" +
        ".....#....";
    // @on - enable libgdx formatter

    final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

    final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<>(graph);

    final GraphPath<MyNode> outPath = new DefaultGraphPath<>();

    // @off - disable libgdx formatter
    // 0123456789
    // S....#...E 0
    // .....#.... 10
    // .....#.... 20
    // @on - enable libgdx formatter
    final boolean searchResult = pathfinder.searchNodePath(graph.nodes.get(0), graph.nodes.get(9), new ManhattanDistance(),
        outPath);

    Assert.assertFalse("Unexpected search result", searchResult);
}
项目:ns2-scc-profiler    文件:RouteCalculationSystem.java   
private void generatePath(GraphPath<GridNode> resultPath) {

            // @TODO replace legacy usage of GridCell.
            final LinkedList<GridNode> cells = new LinkedList<>();
            for (Object node : resultPath) {
                cells.add(new GridNode(((GridNode) node).x, ((GridNode) node).y));
            }
            ArrayList<GridNode> reversedCells = new ArrayList<>(cells);
            Collections.reverse(reversedCells);

            final Path toDestination = new Path(new SafeEntityReference(b), cells, team, false);
            final Path toSource = new Path(new SafeEntityReference(a), reversedCells, team, true);

            entityA().paths.get(team).add(toDestination);
            entityB().paths.get(team).add(toSource);
        }
项目:ns2-scc-profiler    文件:RouteCalculationSystem.java   
private void generatePath(GraphPath<GridNode> resultPath) {

            // @TODO replace legacy usage of GridCell.
            final LinkedList<GridNode> cells = new LinkedList<>();
            for (Object node : resultPath) {
                cells.add(new GridNode(((GridNode) node).x, ((GridNode) node).y));
            }
            ArrayList<GridNode> reversedCells = new ArrayList<>(cells);
            Collections.reverse(reversedCells);

            final Path toDestination = new Path(new SafeEntityReference(b), cells, team, false);
            final Path toSource = new Path(new SafeEntityReference(a), reversedCells, team, true);

            entityA().paths.get(team).add(toDestination);
            entityB().paths.get(team).add(toSource);
        }
项目:gdx-ai    文件:IndexedAStarPathFinderTest.java   
@Test
public void searchNodePath_WhenDestinationUnreachable_ExpectedNoOuputPathFound () {
    // @off - disable libgdx formatter
    final String graphDrawing =
        ".....#....\n" +
        ".....#....\n" +
        ".....#....";
    // @on - enable libgdx formatter

    final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

    final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<>(graph);

    final GraphPath<MyNode> outPath = new DefaultGraphPath<>();

    // @off - disable libgdx formatter
    // 0123456789
    // S....#...E 0
    // .....#.... 10
    // .....#.... 20
    // @on - enable libgdx formatter
    final boolean searchResult = pathfinder.searchNodePath(graph.nodes.get(0), graph.nodes.get(9), new ManhattanDistance(),
        outPath);

    Assert.assertFalse("Unexpected search result", searchResult);
}
项目:jrpg-engine    文件:PathFinder.java   
public TileCoordinate getNextTarget(final TileCoordinate location, final TileCoordinate destination) {
    final GraphPath<Connection<TileCoordinate>> path = new DefaultGraphPath<>();
    pathFinder.searchConnectionPath(
            graph.getCachedNode(location),
            graph.getCachedNode(destination),
            heuristic,
            path
    );

    return path.getCount() == 0 ? null : path.get(0).getToNode();
}
项目:Inspiration    文件:IndexedAStarPathFinder.java   
@Override
public boolean searchConnectionPath (N startNode, N endNode, Heuristic<N> heuristic, GraphPath<Connection<N>> outPath) {

    // Perform AStar
    boolean found = search(startNode, endNode, heuristic);

    if (found) {
        // Create a path made of connections
        generateConnectionPath(startNode, outPath);
    }

    return found;
}
项目:Inspiration    文件:IndexedAStarPathFinder.java   
@Override
public boolean searchNodePath (N startNode, N endNode, Heuristic<N> heuristic, GraphPath<N> outPath) {

    // Perform AStar
    boolean found = search(startNode, endNode, heuristic);

    if (found) {
        // Create a path made of nodes
        generateNodePath(startNode, outPath);
    }

    return found;
}
项目:Inspiration    文件:IndexedAStarPathFinder.java   
protected void generateConnectionPath (N startNode, GraphPath<Connection<N>> outPath) {

        // Work back along the path, accumulating connections
        // outPath.clear();
        while (current.node != startNode) {
            outPath.add(current.connection);
            current = nodeRecords[graph.getIndex(current.connection.getFromNode())];
        }

        // Reverse the path
        outPath.reverse();
    }
项目:Inspiration    文件:IndexedAStarPathFinder.java   
protected void generateNodePath (N startNode, GraphPath<N> outPath) {

        // Work back along the path, accumulating nodes
        // outPath.clear();
        while (current.connection != null) {
            outPath.add(current.node);
            current = nodeRecords[graph.getIndex(current.connection.getFromNode())];
        }
        outPath.add(startNode);

        // Reverse the path
        outPath.reverse();
    }
项目:Inspiration    文件:GraphGenerator.java   
public static void main(String[] args) {
    // @off - disable libgdx formatter
            final String graphDrawing =
                ".....#....\n" +
                ".....#....\n" +
                ".....#....";
            // @on - enable libgdx formatter

            final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

            final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<MyNode>(graph);

            final GraphPath<MyNode> outPath = new DefaultGraphPath<MyNode>();

            // @off - disable libgdx formatter
            // 0123456789
            // S....#...E 0
            // .....#.... 10
            // .....#.... 20
            // @on - enable libgdx formatter
            final boolean searchResult = pathfinder.searchNodePath(graph.getNodes().get(0), graph.getNodes().get(20), new ManhattanDistance(),
                outPath);

            System.out.println(""+searchResult);
            System.out.println(""+outPath.getCount());
            for(int i=0;i<outPath.getCount();i++){
                System.out.println(""+outPath.get(i));
            }
}
项目:gdx-ai    文件:IndexedAStarPathFinder.java   
@Override
public boolean searchConnectionPath (N startNode, N endNode, Heuristic<N> heuristic, GraphPath<Connection<N>> outPath) {

    // Perform AStar
    boolean found = search(startNode, endNode, heuristic);

    if (found) {
        // Create a path made of connections
        generateConnectionPath(startNode, outPath);
    }

    return found;
}
项目:gdx-ai    文件:IndexedAStarPathFinder.java   
@Override
public boolean searchNodePath (N startNode, N endNode, Heuristic<N> heuristic, GraphPath<N> outPath) {

    // Perform AStar
    boolean found = search(startNode, endNode, heuristic);

    if (found) {
        // Create a path made of nodes
        generateNodePath(startNode, outPath);
    }

    return found;
}
项目:gdx-ai    文件:IndexedAStarPathFinder.java   
protected void generateConnectionPath (N startNode, GraphPath<Connection<N>> outPath) {

        // Work back along the path, accumulating connections
        // outPath.clear();
        while (current.node != startNode) {
            outPath.add(current.connection);
            current = nodeRecords[graph.getIndex(current.connection.getFromNode())];
        }

        // Reverse the path
        outPath.reverse();
    }
项目:gdx-ai    文件:IndexedAStarPathFinder.java   
protected void generateNodePath (N startNode, GraphPath<N> outPath) {

        // Work back along the path, accumulating nodes
        // outPath.clear();
        while (current.connection != null) {
            outPath.add(current.node);
            current = nodeRecords[graph.getIndex(current.connection.getFromNode())];
        }
        outPath.add(startNode);

        // Reverse the path
        outPath.reverse();
    }
项目:Inspiration    文件:MyPathSmoother.java   
/** Smoothes the given path in place.
 * @param outPath the path to smooth
 * @return the number of nodes removed from the path. */
public int smoothPath (GraphPath<MyNode> outPath) {
    int inputPathLength = outPath.getCount();

    // If the path is two nodes long or less, then we can't smooth it
    if (inputPathLength <= 2) return 0;

    // Make sure the ray is instantiated
    if (this.ray == null) {
        this.ray = new MyRay(outPath.get(0), outPath.get(0));
    }

    // Keep track of where we are in the smoothed path.
    // We start at 1, because we must always include the start node in the smoothed path.
    int outputIndex = 1;

    // Keep track of where we are in the input path
    // We start at 2, because we assume two adjacent
    // nodes will pass the ray cast
    int inputIndex = 2;

    // Loop until we find the last item in the input
    while (inputIndex < inputPathLength) {
        // Set the ray
        ray.start = (outPath.get(outputIndex - 1));
        ray.end = (outPath.get(inputIndex));

        // Do the ray cast
        boolean collides = raycastCollisionDetector.collides(ray);

        if (collides) {
            // The ray test failed, swap nodes and consider the next output node
            outPath.swapNodes(outputIndex, inputIndex - 1);
            outputIndex++;
        }

        // Consider the next input node
        inputIndex++;
    }

    // Reached the last input node, always add it to the smoothed path.
    outPath.swapNodes(outputIndex, inputIndex - 1);
    outPath.truncatePath(outputIndex + 1);

    // Return the number of removed nodes
    return inputIndex - outputIndex - 1;
}
项目:Inspiration    文件:WorldController.java   
public void touchToMove(int screenX, int screenY) {
        Vector3 input = new Vector3(screenX, screenY, 0);
        camera.unproject(input);
        int x = MathUtils.floor(input.x / 32);
        int y = MathUtils.floor(input.y / 32);
        Gdx.app.debug(TAG, "clicked # (x:" + x + ",y:" + y + " )");
        //we click not npc or block,set aim to move
        if (!isCollisionWithNpc(x, y) && !isCollisionWithBlock(x, y)) {
            //A* path finding
            path.clear();
            Vector2 start = new Vector2(MathUtils.round(player.getX() / 32), MathUtils.round(player.getY() / 32));
            //we need set exactly start position
            Vector2 end = new Vector2(x, y);
            int numCols = mapMgr.cols;
            int numRows = mapMgr.rows;
            Gdx.app.debug(TAG, "From:" + start + " to " + end + "|numCols:" + numCols + "|numRows:" + numRows);
            int s = (int) start.x + ((int) start.y) * numCols;
            int t = (int) end.x + ((int) (end.y)) * numCols;
            List<Sprite> temp = new ArrayList<Sprite>();
            temp.addAll(mapMgr.npcs);
            temp.addAll(mapMgr.enemies);
//            temp.addAll(mapMgr.events);
            final MyGraph graph = GraphGenerator.generateGraph(mapMgr.getBlockLayer(), temp, numCols, numRows, 32, 32, start);
            final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<MyNode>(graph);
            final GraphPath<MyNode> outPath = new DefaultGraphPath<MyNode>();
            final boolean searchResult = pathfinder.searchNodePath(graph.getNodes().get(s), graph.getNodes().get(t), new ManhattanDistance(), outPath);
            MyPathSmoother pathSmoother = new MyPathSmoother(new MyRaycastCollisionDetector(graph));
            pathSmoother.smoothPath(outPath);
            StringBuilder sb = new StringBuilder();
            for (int i = outPath.getCount() - 1; i >= 0; i--) {
                sb.append("(" + outPath.get(i).getX() + "," + outPath.get(i).getY() + ")|");
                path.add(outPath.get(i));
            }
            if (searchResult) {
                Gdx.app.debug(TAG, "Start Follow Path:" + sb.toString());
                player.followPath(path);
                aim = new Aim(x, y);
            }
        } else {
            aim = null;
        }
    }
项目:Inspiration    文件:IndexedAStarPathFinderTest.java   
@Test
public void searchNodePath_WhenSearchingAdjacentTile_ExpectedOuputPathLengthEquals2 () {
    // @off - disable libgdx formatter
    final String graphDrawing =
            "..........\n" +
            "..........\n" +
            "..........";
    // @on - enable libgdx formatter

    final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

    final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<>(graph);

    final GraphPath<MyNode> outPath = new DefaultGraphPath<>();

    // @off - disable libgdx formatter
    // 0123456789
    // .......... 0
    // .....S.... 10
    // .....E.... 20
    // @on - enable libgdx formatter
    final boolean searchResult1 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(25), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult1);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());

    // @off - disable libgdx formatter
    // 0123456789
    // .......... 0
    // .....SE... 10
    // .......... 20
    // @on - enable libgdx formatter
    outPath.clear();
    final boolean searchResult2 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(16), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult2);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());

    // @off - disable libgdx formatter
    // 0123456789
    // .......... 0
    // ....ES.... 10
    // .......... 20
    // @on - enable libgdx formatter
    outPath.clear();
    final boolean searchResult3 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(14), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult3);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());

    // @off - disable libgdx formatter
    // 0123456789
    // .....E.... 0
    // .....S.... 10
    // .......... 20
    // @on - enable libgdx formatter
    outPath.clear();
    final boolean searchResult4 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(5), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult4);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());
}
项目:Inspiration    文件:IndexedAStarPathFinderTest.java   
@Test
public void searchNodePath_WhenSearchCanHitDeadEnds_ExpectedOuputPathFound () {
    // @off - disable libgdx formatter
    final String graphDrawing =
        ".#.#.......#..#...............\n" +
        ".#............#.....#..#####..\n" +
        "...#.#######..#.....#.........\n" +
        ".#.#.#........#.....########..\n" +
        ".###.#....#####.....#......##.\n" +
        ".#...#....#.........#...##....\n" +
        ".#####....#.........#....#....\n" +
        ".#........#.........#....#####\n" +
        ".####....##.........#......#..\n" +
        "....#...............#......#..";
    // @on - enable libgdx formatter

    final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

    final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<>(graph);

    final GraphPath<MyNode> outPath = new DefaultGraphPath<>();

    // @off - disable libgdx formatter
    // 012345678901234567890123456789
    // S#.#.......#..#............... 0
    // .#............#.....#..#####.. 30
    // ...#.#######..#.....#......... 60
    // .#.#.#........#.....########.. 90
    // .###.#....#####.....#......##. 120
    // .#...#....#.........#...##.... 150
    // .#####....#.........#....#.... 180
    // .#E.......#.........#....##### 210
    // .####....##.........#......#.. 240
    // ....#...............#......#.. 270
    // @on - enable libgdx formatter
    final boolean searchResult = pathfinder.searchNodePath(graph.nodes.get(0), graph.nodes.get(212), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult);
    Assert.assertEquals("Unexpected number of nodes in path", 32, outPath.getCount());
}
项目:gdx-ai    文件:IndexedAStarPathFinderTest.java   
@Test
public void searchNodePath_WhenSearchingAdjacentTile_ExpectedOuputPathLengthEquals2 () {
    // @off - disable libgdx formatter
    final String graphDrawing =
            "..........\n" +
            "..........\n" +
            "..........";
    // @on - enable libgdx formatter

    final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

    final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<>(graph);

    final GraphPath<MyNode> outPath = new DefaultGraphPath<>();

    // @off - disable libgdx formatter
    // 0123456789
    // .......... 0
    // .....S.... 10
    // .....E.... 20
    // @on - enable libgdx formatter
    final boolean searchResult1 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(25), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult1);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());

    // @off - disable libgdx formatter
    // 0123456789
    // .......... 0
    // .....SE... 10
    // .......... 20
    // @on - enable libgdx formatter
    outPath.clear();
    final boolean searchResult2 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(16), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult2);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());

    // @off - disable libgdx formatter
    // 0123456789
    // .......... 0
    // ....ES.... 10
    // .......... 20
    // @on - enable libgdx formatter
    outPath.clear();
    final boolean searchResult3 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(14), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult3);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());

    // @off - disable libgdx formatter
    // 0123456789
    // .....E.... 0
    // .....S.... 10
    // .......... 20
    // @on - enable libgdx formatter
    outPath.clear();
    final boolean searchResult4 = pathfinder.searchNodePath(graph.nodes.get(15), graph.nodes.get(5), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult4);
    Assert.assertEquals("Unexpected number of nodes in path", 2, outPath.getCount());
}
项目:gdx-ai    文件:IndexedAStarPathFinderTest.java   
@Test
public void searchNodePath_WhenSearchCanHitDeadEnds_ExpectedOuputPathFound () {
    // @off - disable libgdx formatter
    final String graphDrawing =
        ".#.#.......#..#...............\n" +
        ".#............#.....#..#####..\n" +
        "...#.#######..#.....#.........\n" +
        ".#.#.#........#.....########..\n" +
        ".###.#....#####.....#......##.\n" +
        ".#...#....#.........#...##....\n" +
        ".#####....#.........#....#....\n" +
        ".#........#.........#....#####\n" +
        ".####....##.........#......#..\n" +
        "....#...............#......#..";
    // @on - enable libgdx formatter

    final MyGraph graph = createGraphFromTextRepresentation(graphDrawing);

    final IndexedAStarPathFinder<MyNode> pathfinder = new IndexedAStarPathFinder<>(graph);

    final GraphPath<MyNode> outPath = new DefaultGraphPath<>();

    // @off - disable libgdx formatter
    // 012345678901234567890123456789
    // S#.#.......#..#............... 0
    // .#............#.....#..#####.. 30
    // ...#.#######..#.....#......... 60
    // .#.#.#........#.....########.. 90
    // .###.#....#####.....#......##. 120
    // .#...#....#.........#...##.... 150
    // .#####....#.........#....#.... 180
    // .#E.......#.........#....##### 210
    // .####....##.........#......#.. 240
    // ....#...............#......#.. 270
    // @on - enable libgdx formatter
    final boolean searchResult = pathfinder.searchNodePath(graph.nodes.get(0), graph.nodes.get(212), new ManhattanDistance(),
        outPath);

    Assert.assertTrue("Unexpected search result", searchResult);
    Assert.assertEquals("Unexpected number of nodes in path", 32, outPath.getCount());
}