/src/cn/macro/slg/view/MapView.as

https://github.com/macrohard/SLG
ActionScript | 194 lines | 163 code | 28 blank | 3 comment | 27 complexity | d0d08a872e8e6e07fdd4114366a9ae2c MD5 | raw file
  1. package cn.macro.slg.view
  2. {
  3. import flash.geom.Point;
  4. import flash.display.Graphics;
  5. import flash.display.Sprite;
  6. import flash.events.Event;
  7. /**
  8. * @author yyi
  9. */
  10. public class MapView extends Sprite
  11. {
  12. private var gridSize : uint = 60;
  13. private var gridView : GridView;
  14. private var roleList : Array = new Array();
  15. private var roleSelected : Role;
  16. private var cacheGrid : Array;
  17. private var temp:Sprite;
  18. public function MapView(mapData : Array)
  19. {
  20. gridView = new GridView();
  21. gridView.w = mapData[0].length;
  22. gridView.h = mapData.length;
  23. for (var i : uint = 0;i < mapData.length; i++)
  24. {
  25. var tempArr : Array = new Array();
  26. var mapArr : Array = mapData[i];
  27. for (var j : uint = 0;j < mapArr.length; j++)
  28. {
  29. var grid : GridNode = new GridNode(gridSize);
  30. grid.x = j * gridSize;
  31. grid.y = i * gridSize;
  32. grid.xIndex = j;
  33. grid.yIndex = i;
  34. grid.friction = mapArr[j];
  35. gridView.addChild(grid);
  36. tempArr.push(grid);
  37. }
  38. }
  39. addChild(gridView);
  40. this.addEventListener(CellEvent.CELL_CLICK, onSelect);
  41. var role1 : Role = new Role(0);
  42. setRolePos(role1, 5, 4);
  43. roleList.push(role1);
  44. addChild(role1);
  45. var role2 : Role = new Role(1, 30);
  46. setRolePos(role2, 8, 5);
  47. roleList.push(role2);
  48. addChild(role2);
  49. temp = new Sprite();
  50. addChild(temp);
  51. }
  52. public function drawPoint(p:Point) : void
  53. {
  54. var g:Graphics = temp.graphics;
  55. g.beginFill(0xFFFFFF);
  56. g.lineStyle(1, 0x000000);
  57. g.drawCircle(p.x - 1, p.y - 1, 3);
  58. g.endFill();
  59. }
  60. private function setRolePos(role : Role, x : int, y : int) : void
  61. {
  62. var t : GridNode = gridView.getChild(x, y);
  63. role.x = t.x + gridSize * 0.5;
  64. role.y = t.y + gridSize * 0.5;
  65. role.place = t;
  66. }
  67. private function onSelect(e : Event) : void
  68. {
  69. var clickGridNode : GridNode = e.target as GridNode;
  70. var role : Role = checkRole(clickGridNode);
  71. if (role != null)
  72. {
  73. if (role == roleSelected && clickGridNode.selected)
  74. {
  75. clearSelect();
  76. roleSelected = null;
  77. }
  78. else
  79. {
  80. clearSelect();
  81. cacheGrid = MArrayUtil.createMArray(gridView.w, gridView.h);
  82. selectRange(clickGridNode, role.mobility);
  83. roleSelected = role;
  84. }
  85. }
  86. else
  87. {
  88. if (clickGridNode.selected)
  89. {
  90. roleSelected.move(getPath(clickGridNode));
  91. }
  92. clearSelect();
  93. }
  94. }
  95. private function getPath(gridNode : GridNode) : Array
  96. {
  97. var pathArr : Array = new Array();
  98. while(gridNode != roleSelected.place)
  99. {
  100. pathArr.push(gridNode);
  101. var top : int = (gridNode.yIndex > 0) ? cacheGrid[gridNode.xIndex][gridNode.yIndex - 1] : 0;
  102. var bottom : int = (gridNode.yIndex + 1 < gridView.h) ? cacheGrid[gridNode.xIndex][gridNode.yIndex + 1] : 0;
  103. var left : int = (gridNode.xIndex > 0) ? cacheGrid[gridNode.xIndex - 1][gridNode.yIndex] : 0;
  104. var right : int = (gridNode.xIndex + 1 < gridView.w) ? cacheGrid[gridNode.xIndex + 1][gridNode.yIndex] : 0;
  105. var max : int = Math.max(top, bottom, left, right);
  106. switch (max) {
  107. case top:
  108. gridNode = gridView.getChild(gridNode.xIndex, gridNode.yIndex - 1);
  109. break;
  110. case right:
  111. gridNode = gridView.getChild(gridNode.xIndex + 1, gridNode.yIndex);
  112. break;
  113. case bottom:
  114. gridNode = gridView.getChild(gridNode.xIndex, gridNode.yIndex + 1);
  115. break;
  116. case left:
  117. gridNode = gridView.getChild(gridNode.xIndex - 1, gridNode.yIndex);
  118. break;
  119. }
  120. }
  121. return pathArr.reverse();
  122. }
  123. protected function checkRole(grid : GridNode) : Role
  124. {
  125. for (var i : int = 0;i < roleList.length; i++)
  126. {
  127. var role : Role = roleList[i];
  128. if (role.place == grid)
  129. return role;
  130. }
  131. return null;
  132. }
  133. private function selectRange(gridNode : GridNode, mobility : int) : void
  134. {
  135. if (cacheGrid[gridNode.xIndex][gridNode.yIndex] != null && cacheGrid[gridNode.xIndex][gridNode.yIndex] >= mobility)
  136. return;
  137. cacheGrid[gridNode.xIndex][gridNode.yIndex] = mobility;
  138. if (mobility < 0)
  139. return;
  140. gridNode.selected = true;
  141. var topGrid : GridNode = gridView.getChild(gridNode.xIndex, gridNode.yIndex - 1);
  142. if (topGrid != null)
  143. selectRange(topGrid, mobility - topGrid.friction);
  144. var bottomGrid : GridNode = gridView.getChild(gridNode.xIndex, gridNode.yIndex + 1);
  145. if (bottomGrid != null)
  146. selectRange(bottomGrid, mobility - bottomGrid.friction);
  147. var leftGrid : GridNode = gridView.getChild(gridNode.xIndex - 1, gridNode.yIndex);
  148. if (leftGrid != null)
  149. selectRange(leftGrid, mobility - leftGrid.friction);
  150. var rightGrid : GridNode = gridView.getChild(gridNode.xIndex + 1, gridNode.yIndex);
  151. if (rightGrid != null)
  152. selectRange(rightGrid, mobility - rightGrid.friction);
  153. }
  154. protected function clearSelect() : void
  155. {
  156. cacheGrid = null;
  157. for (var i : uint = 0;i < gridView.h; i++)
  158. {
  159. for (var j : uint = 0;j < gridView.w; j++)
  160. {
  161. gridView.getChild(j, i).selected = false;
  162. }
  163. }
  164. }
  165. }
  166. }