AStar pathfinding class (use with AStarMap)


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package com.game.astar
  2. {
  3. import com.app.data.AppFlags;
  4. import com.robot.comm.DispatchManager;
  5. import com.robot.comm.R_Event;
  6.  
  7. import flash.display.*;
  8. import flash.events.*;
  9. import flash.utils.getTimer;
  10.  
  11. //-------------------------------------------------------------------
  12. public class AStar extends Sprite {
  13.  
  14. public var map:AStarMap;
  15. protected const cellSize:int = 6;
  16.  
  17. //----------------------------------------------------------------
  18. protected var _solution_array:Array;
  19. public function set solution_array(v:Array):void { _solution_array = v; };
  20. public function get solution_array():Array { return _solution_array; };
  21. //----------------------------------------------------------------
  22.  
  23. public function AStar() {
  24.  
  25. addEventListener(Event.ADDED_TO_STAGE, initialize);
  26. }
  27. //-------------------------------------------------------------------
  28. public function initialize(evt:Event):void
  29. {
  30. //stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
  31. //addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
  32. //addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
  33.  
  34. map = new AStarMap(15, 15);
  35. /*
  36. map.setCell(2, 4, map.CELL_FILLED);
  37. map.setCell(3, 4, map.CELL_FILLED);
  38. map.setCell(4, 4, map.CELL_FILLED);
  39. map.setCell(5, 4, map.CELL_FILLED);
  40. */
  41. map.setEndPoints(13,0,1,14);
  42. drawMap();
  43. }
  44.  
  45. //-------------------------------------------------------------------
  46. public function keyDownHandler(evt:KeyboardEvent):void
  47. {
  48. if(evt.keyCode == 82) {
  49. map.clearMap();
  50. drawMap();
  51. }
  52.  
  53. //32 = space
  54. if(evt.keyCode == 32) solve();
  55. }
  56.  
  57. //-------------------------------------------------------------------
  58. public function solve():void
  59. {
  60. if(solution_array) solution_array.length = 0;
  61. solution_array = map.solve();
  62.  
  63. drawMap();
  64.  
  65. if(solution_array.length == 0) return;
  66.  
  67. //trace along path array
  68. graphics.lineStyle(2, 0xffffff);
  69. var endCell:Object = solution_array[0];
  70. //graphics.drawCircle(endCell.x * cellSize + cellSize/2, endCell.y * cellSize + cellSize/2, 30);
  71. graphics.moveTo(endCell.x * cellSize + cellSize/2, endCell.y * cellSize + cellSize/2);
  72. graphics.lineTo(endCell.parentCell.x * cellSize + cellSize/2, endCell.parentCell.y * cellSize + cellSize/2);
  73. for each(var cell:Object in solution_array)
  74. {
  75. //trace(cell.x * cellSize, cell.parentCell.x * cellSize);
  76. graphics.moveTo(cell.x * cellSize + cellSize/2, cell.y * cellSize + cellSize/2);
  77. graphics.lineTo(cell.parentCell.x * cellSize + cellSize/2, cell.parentCell.y * cellSize + cellSize/2);
  78. }
  79. }
  80.  
  81. //-------------------------------------------------------------------
  82. public function mouseDownHandler(evt:MouseEvent):void
  83. {
  84. var xx:int = evt.localX / cellSize;
  85. var yy:int = evt.localY / cellSize;
  86.  
  87. if(evt.shiftKey) { //erase cells
  88. map.setCell(xx, yy, map.CELL_FREE);
  89. } else { //fill cells
  90. map.setCell(xx, yy, map.CELL_FILLED);
  91. }
  92.  
  93. drawMap();
  94. }
  95.  
  96. //-------------------------------------------------------------------
  97. public function mouseMoveHandler(evt:MouseEvent):void
  98. {
  99. if(evt.buttonDown) {
  100.  
  101. var xx:int = evt.localX / cellSize;
  102. var yy:int = evt.localY / cellSize;
  103.  
  104. if(evt.shiftKey) { //erase cells
  105. map.setCell(xx, yy, map.CELL_FREE);
  106. } else { //fill cells
  107. map.setCell(xx, yy, map.CELL_FILLED);
  108. }
  109.  
  110. drawMap();
  111. }
  112. }
  113.  
  114. //-------------------------------------------------------------------
  115. public function drawMap():void
  116. {
  117. graphics.clear();
  118.  
  119. //draw background square
  120. graphics.beginFill(0x000000,0.1);
  121. graphics.drawRect(0,0,map.gridWidth*cellSize,map.gridHeight*cellSize);
  122.  
  123. //draw cells
  124. for(var xx:int = 0; xx < map.gridWidth; xx++) {
  125. for(var yy:int = 0; yy < map.gridHeight; yy++) {
  126. if(map.getCell(xx,yy).cellType == map.CELL_FILLED) {
  127. fillRect(graphics, xx * cellSize, yy * cellSize, 0xAA0000);
  128. }
  129. if(map.getCell(xx,yy).cellType == map.CELL_ORIGIN) {
  130. fillRect(graphics, xx * cellSize, yy * cellSize, 0x00AA00);
  131. }
  132. if(map.getCell(xx,yy).cellType == map.CELL_DESTINATION) {
  133. fillRect(graphics, xx * cellSize, yy * cellSize, 0x0000AA);
  134.  
  135. }
  136. }
  137. }
  138.  
  139. //draw grid
  140. graphics.lineStyle(1, 0xDDDDDD);
  141. var ii:int = 0;
  142. for(ii = cellSize; ii < map.gridWidth * cellSize; ii += cellSize) {
  143. graphics.moveTo(ii, 0);
  144. graphics.lineTo(ii, map.gridHeight * cellSize);
  145. }
  146. for(ii = cellSize; ii < map.gridHeight * cellSize; ii += cellSize) {
  147. graphics.moveTo(0, ii);
  148. graphics.lineTo(map.gridWidth * cellSize, ii);
  149. }
  150.  
  151. //draw outline
  152. graphics.lineStyle(1, 0xAAAAAA);
  153. graphics.moveTo(0, 0);
  154. graphics.lineTo(map.gridWidth * cellSize, 0);
  155. graphics.lineTo(map.gridWidth * cellSize, map.gridHeight * cellSize);
  156. graphics.lineTo(0, map.gridHeight * cellSize);
  157. graphics.lineTo(0,0);
  158.  
  159. }
  160.  
  161. //-------------------------------------------------------------------
  162. private function fillRect(target:Graphics, cellX:int, cellY:int, color:int):void
  163. {
  164. target.lineStyle(1, color);
  165. target.moveTo(cellX + 2, cellY + 2);
  166. target.beginFill(color, 0.5);
  167. target.drawRect(cellX + 2, cellY + 2, cellSize - 4, cellSize - 4);
  168. //target.drawCircle(cellX * cellSize, cellY * cellSize, 10);
  169. target.endFill();
  170. }
  171. }
  172. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.