Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.badlogic.gdx.ApplicationAdapter;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.ai.pfa.*;
- import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder;
- import com.badlogic.gdx.ai.pfa.indexed.IndexedGraph;
- import com.badlogic.gdx.graphics.Color;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.graphics.OrthographicCamera;
- import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
- import com.badlogic.gdx.math.Vector3;
- import com.badlogic.gdx.utils.Array;
- import com.badlogic.gdx.utils.Pools;
- public class AStarExample extends ApplicationAdapter {
- OrthographicCamera camera;
- ShapeRenderer renderer;
- GameMap map;
- Node startNode;
- Node goalNode;
- IndexedAStarPathFinder<Node> pathFinder;
- Heuristic<Node> heuristic;
- GraphPath<Connection<Node>> path;
- private static final float NODE_SIZE = 32f;
- @Override
- public void create() {
- renderer = new ShapeRenderer();
- camera = new OrthographicCamera();
- camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
- camera.position.x -= 100;
- camera.position.y -= 50;
- camera.update();
- map = new GameMap(10, 10);
- startNode = map.nodes[2][5];
- goalNode = map.nodes[6][5];
- map.nodes[4][3].blocked
- = map.nodes[4][4].blocked
- = map.nodes[4][5].blocked
- = map.nodes[4][6].blocked
- = map.nodes[4][7].blocked
- = map.nodes[5][7].blocked
- = map.nodes[6][7].blocked
- = map.nodes[7][7].blocked
- = true;
- map.updateConnections();
- pathFinder = new IndexedAStarPathFinder<Node>(map);
- heuristic = new Heuristic<Node>() {
- @Override
- public float estimate(Node node, Node endNode) {
- return (float) (Math.abs(node.x - endNode.x) + Math.abs(node.y - endNode.y));
- }
- };
- path = new DefaultGraphPath<Connection<Node>>();
- pathFinder.searchConnectionPath(startNode, goalNode, heuristic, path);
- }
- @Override
- public void render() {
- Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
- Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
- handleMouseEvent();
- renderer.setProjectionMatrix(camera.combined);
- renderer.begin(ShapeRenderer.ShapeType.Line);
- drawMap();
- drawStartNode();
- drawGoalNode();
- drawPath();
- renderer.end();
- }
- @Override
- public void dispose() {
- renderer.dispose();
- }
- private void handleMouseEvent() {
- Vector3 touchPos = Pools.obtain(Vector3.class);
- touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
- camera.unproject(touchPos);
- int nodeX = (int) (touchPos.x / NODE_SIZE);
- int nodeY = (int) (touchPos.y / NODE_SIZE);
- if (nodeX >= 0 && nodeX < map.width && nodeY >= 0 && nodeY < map.height) {
- Node mouseNode = map.nodes[nodeX][nodeY];
- if (!mouseNode.blocked) {
- goalNode = mouseNode;
- path.clear();
- pathFinder.searchConnectionPath(startNode, goalNode, heuristic, path);
- }
- }
- Pools.free(touchPos);
- }
- private void drawMap() {
- renderer.setColor(Color.GRAY);
- for (int i = 0; i < map.width; i++) {
- for (int j = 0; j < map.height; j++) {
- Node node = map.nodes[i][j];
- float x = node.x * NODE_SIZE;
- float y = node.y * NODE_SIZE;
- renderer.rect(x, y, NODE_SIZE, NODE_SIZE);
- if (node.blocked) {
- renderer.setColor(Color.RED);
- renderer.line(x, y, x + NODE_SIZE, y + NODE_SIZE);
- renderer.line(x, y + NODE_SIZE, x + NODE_SIZE, y);
- renderer.setColor(Color.GRAY);
- }
- }
- }
- }
- private void drawStartNode() {
- float x = startNode.x * NODE_SIZE;
- float y = startNode.y * NODE_SIZE;
- renderer.setColor(Color.GREEN);
- renderer.circle(x + NODE_SIZE / 2, y + NODE_SIZE / 2, NODE_SIZE / 2 - 6);
- }
- private void drawGoalNode() {
- float x = goalNode.x * NODE_SIZE;
- float y = goalNode.y * NODE_SIZE;
- renderer.setColor(Color.CYAN);
- renderer.circle(x + NODE_SIZE / 2, y + NODE_SIZE / 2, NODE_SIZE / 2 - 6);
- }
- private void drawPath() {
- if (path.getCount() > 0) {
- renderer.setColor(Color.YELLOW);
- for (int i = 0; i < path.getCount(); i++) {
- Connection<Node> connection = path.get(i);
- Node fromNode = connection.getFromNode();
- Node toNode = connection.getToNode();
- renderer.line(
- fromNode.x * NODE_SIZE + NODE_SIZE / 2, fromNode.y * NODE_SIZE + NODE_SIZE / 2,
- toNode.x * NODE_SIZE + NODE_SIZE / 2, toNode.y * NODE_SIZE + NODE_SIZE / 2);
- }
- }
- }
- class GameMap implements IndexedGraph<Node> {
- Node[][] nodes;
- int width;
- int height;
- GameMap(int width, int height) {
- this.width = width;
- this.height = height;
- nodes = new Node[width][height];
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- nodes[i][j] = new Node(GameMap.this, i, j);
- }
- }
- }
- @Override
- public int getIndex(Node node) {
- return node.index;
- }
- @Override
- public int getNodeCount() {
- return width * height;
- }
- @Override
- public Array<Connection<Node>> getConnections(Node fromNode) {
- return fromNode.connections;
- }
- void updateConnections() {
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- nodes[i][j].updateConnections(this);
- }
- }
- }
- }
- class Node {
- int x;
- int y;
- int index;
- boolean blocked;
- Array<Connection<Node>> connections;
- Node(GameMap map, int x, int y) {
- this.x = x;
- this.y = y;
- index = x * map.height + y;
- connections = new Array<Connection<Node>>();
- }
- void updateConnections(GameMap map) {
- connections.clear();
- if (x > 0) {
- Node leftNode = map.nodes[x - 1][y];
- if (!leftNode.blocked)
- connections.add(new DefaultConnection<Node>(this, leftNode));
- }
- if (x < map.width - 1) {
- Node rightNode = map.nodes[x + 1][y];
- if (!rightNode.blocked)
- connections.add(new DefaultConnection<Node>(this, rightNode));
- }
- if (y > 0) {
- Node bottomNode = map.nodes[x][y - 1];
- if (!bottomNode.blocked)
- connections.add(new DefaultConnection<Node>(this, bottomNode));
- }
- if (y < map.height - 1) {
- Node topNode = map.nodes[x][y + 1];
- if (!topNode.blocked)
- connections.add(new DefaultConnection<Node>(this, topNode));
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment