Guest User

Untitled

a guest
Mar 19th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.25 KB | None | 0 0
  1. import com.badlogic.gdx.ApplicationAdapter;
  2. import com.badlogic.gdx.Gdx;
  3. import com.badlogic.gdx.ai.pfa.*;
  4. import com.badlogic.gdx.ai.pfa.indexed.IndexedAStarPathFinder;
  5. import com.badlogic.gdx.ai.pfa.indexed.IndexedGraph;
  6. import com.badlogic.gdx.graphics.Color;
  7. import com.badlogic.gdx.graphics.GL20;
  8. import com.badlogic.gdx.graphics.OrthographicCamera;
  9. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  10. import com.badlogic.gdx.math.Vector3;
  11. import com.badlogic.gdx.utils.Array;
  12. import com.badlogic.gdx.utils.Pools;
  13.  
  14. public class AStarExample extends ApplicationAdapter {
  15. OrthographicCamera camera;
  16. ShapeRenderer renderer;
  17. GameMap map;
  18. Node startNode;
  19. Node goalNode;
  20. IndexedAStarPathFinder<Node> pathFinder;
  21. Heuristic<Node> heuristic;
  22. GraphPath<Connection<Node>> path;
  23.  
  24. private static final float NODE_SIZE = 32f;
  25.  
  26. @Override
  27. public void create() {
  28. renderer = new ShapeRenderer();
  29. camera = new OrthographicCamera();
  30. camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  31. camera.position.x -= 100;
  32. camera.position.y -= 50;
  33. camera.update();
  34.  
  35. map = new GameMap(10, 10);
  36. startNode = map.nodes[2][5];
  37. goalNode = map.nodes[6][5];
  38.  
  39. map.nodes[4][3].blocked
  40. = map.nodes[4][4].blocked
  41. = map.nodes[4][5].blocked
  42. = map.nodes[4][6].blocked
  43. = map.nodes[4][7].blocked
  44. = map.nodes[5][7].blocked
  45. = map.nodes[6][7].blocked
  46. = map.nodes[7][7].blocked
  47. = true;
  48.  
  49. map.updateConnections();
  50.  
  51. pathFinder = new IndexedAStarPathFinder<Node>(map);
  52. heuristic = new Heuristic<Node>() {
  53. @Override
  54. public float estimate(Node node, Node endNode) {
  55. return (float) (Math.abs(node.x - endNode.x) + Math.abs(node.y - endNode.y));
  56. }
  57. };
  58. path = new DefaultGraphPath<Connection<Node>>();
  59. pathFinder.searchConnectionPath(startNode, goalNode, heuristic, path);
  60. }
  61.  
  62. @Override
  63. public void render() {
  64. Gdx.gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
  65. Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  66.  
  67. handleMouseEvent();
  68.  
  69. renderer.setProjectionMatrix(camera.combined);
  70. renderer.begin(ShapeRenderer.ShapeType.Line);
  71. drawMap();
  72. drawStartNode();
  73. drawGoalNode();
  74. drawPath();
  75. renderer.end();
  76. }
  77.  
  78. @Override
  79. public void dispose() {
  80. renderer.dispose();
  81. }
  82.  
  83. private void handleMouseEvent() {
  84. Vector3 touchPos = Pools.obtain(Vector3.class);
  85. touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
  86. camera.unproject(touchPos);
  87.  
  88. int nodeX = (int) (touchPos.x / NODE_SIZE);
  89. int nodeY = (int) (touchPos.y / NODE_SIZE);
  90. if (nodeX >= 0 && nodeX < map.width && nodeY >= 0 && nodeY < map.height) {
  91. Node mouseNode = map.nodes[nodeX][nodeY];
  92. if (!mouseNode.blocked) {
  93. goalNode = mouseNode;
  94. path.clear();
  95. pathFinder.searchConnectionPath(startNode, goalNode, heuristic, path);
  96. }
  97. }
  98.  
  99. Pools.free(touchPos);
  100. }
  101.  
  102. private void drawMap() {
  103. renderer.setColor(Color.GRAY);
  104. for (int i = 0; i < map.width; i++) {
  105. for (int j = 0; j < map.height; j++) {
  106. Node node = map.nodes[i][j];
  107. float x = node.x * NODE_SIZE;
  108. float y = node.y * NODE_SIZE;
  109. renderer.rect(x, y, NODE_SIZE, NODE_SIZE);
  110. if (node.blocked) {
  111. renderer.setColor(Color.RED);
  112. renderer.line(x, y, x + NODE_SIZE, y + NODE_SIZE);
  113. renderer.line(x, y + NODE_SIZE, x + NODE_SIZE, y);
  114. renderer.setColor(Color.GRAY);
  115. }
  116. }
  117. }
  118. }
  119.  
  120. private void drawStartNode() {
  121. float x = startNode.x * NODE_SIZE;
  122. float y = startNode.y * NODE_SIZE;
  123. renderer.setColor(Color.GREEN);
  124. renderer.circle(x + NODE_SIZE / 2, y + NODE_SIZE / 2, NODE_SIZE / 2 - 6);
  125. }
  126.  
  127. private void drawGoalNode() {
  128. float x = goalNode.x * NODE_SIZE;
  129. float y = goalNode.y * NODE_SIZE;
  130. renderer.setColor(Color.CYAN);
  131. renderer.circle(x + NODE_SIZE / 2, y + NODE_SIZE / 2, NODE_SIZE / 2 - 6);
  132. }
  133.  
  134. private void drawPath() {
  135. if (path.getCount() > 0) {
  136. renderer.setColor(Color.YELLOW);
  137. for (int i = 0; i < path.getCount(); i++) {
  138. Connection<Node> connection = path.get(i);
  139. Node fromNode = connection.getFromNode();
  140. Node toNode = connection.getToNode();
  141. renderer.line(
  142. fromNode.x * NODE_SIZE + NODE_SIZE / 2, fromNode.y * NODE_SIZE + NODE_SIZE / 2,
  143. toNode.x * NODE_SIZE + NODE_SIZE / 2, toNode.y * NODE_SIZE + NODE_SIZE / 2);
  144. }
  145. }
  146. }
  147.  
  148. class GameMap implements IndexedGraph<Node> {
  149. Node[][] nodes;
  150. int width;
  151. int height;
  152.  
  153. GameMap(int width, int height) {
  154. this.width = width;
  155. this.height = height;
  156.  
  157. nodes = new Node[width][height];
  158.  
  159. for (int i = 0; i < width; i++) {
  160. for (int j = 0; j < height; j++) {
  161. nodes[i][j] = new Node(GameMap.this, i, j);
  162. }
  163. }
  164. }
  165.  
  166. @Override
  167. public int getIndex(Node node) {
  168. return node.index;
  169. }
  170.  
  171. @Override
  172. public int getNodeCount() {
  173. return width * height;
  174. }
  175.  
  176. @Override
  177. public Array<Connection<Node>> getConnections(Node fromNode) {
  178. return fromNode.connections;
  179. }
  180.  
  181. void updateConnections() {
  182. for (int i = 0; i < width; i++) {
  183. for (int j = 0; j < height; j++) {
  184. nodes[i][j].updateConnections(this);
  185. }
  186. }
  187. }
  188. }
  189.  
  190. class Node {
  191. int x;
  192. int y;
  193. int index;
  194. boolean blocked;
  195. Array<Connection<Node>> connections;
  196.  
  197. Node(GameMap map, int x, int y) {
  198. this.x = x;
  199. this.y = y;
  200. index = x * map.height + y;
  201.  
  202. connections = new Array<Connection<Node>>();
  203. }
  204.  
  205. void updateConnections(GameMap map) {
  206. connections.clear();
  207.  
  208. if (x > 0) {
  209. Node leftNode = map.nodes[x - 1][y];
  210. if (!leftNode.blocked)
  211. connections.add(new DefaultConnection<Node>(this, leftNode));
  212. }
  213. if (x < map.width - 1) {
  214. Node rightNode = map.nodes[x + 1][y];
  215. if (!rightNode.blocked)
  216. connections.add(new DefaultConnection<Node>(this, rightNode));
  217. }
  218. if (y > 0) {
  219. Node bottomNode = map.nodes[x][y - 1];
  220. if (!bottomNode.blocked)
  221. connections.add(new DefaultConnection<Node>(this, bottomNode));
  222. }
  223. if (y < map.height - 1) {
  224. Node topNode = map.nodes[x][y + 1];
  225. if (!topNode.blocked)
  226. connections.add(new DefaultConnection<Node>(this, topNode));
  227. }
  228. }
  229. }
  230. }
Add Comment
Please, Sign In to add comment