TestAStar.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Learn cc.Class:
  2. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
  4. // Learn Attribute:
  5. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
  10. cc.Class({
  11. extends: cc.Component,
  12. properties: {
  13. allowDiagonals: {
  14. default: false,
  15. serializable: false,
  16. },
  17. diagonalCost: {
  18. default: false,
  19. serializable: false,
  20. },
  21. dotTiebreaker: {
  22. default: false,
  23. serializable: false,
  24. },
  25. badSorting: {
  26. default: true,
  27. serializable: false,
  28. },
  29. },
  30. setAllowDiagonals(event) {
  31. // cc.log('event',event);
  32. this.allowDiagonals = event.isChecked;
  33. this.draw();
  34. },
  35. setDiagonalCost(event) {
  36. // cc.log('event',event);
  37. this.diagonalCost = event.isChecked;
  38. this.draw();
  39. },
  40. setDotTiebreaker(event) {
  41. // cc.log('event',event);
  42. this.dotTiebreaker = event.isChecked;
  43. this.draw();
  44. },
  45. setBadSorting(event) {
  46. // cc.log('event',event);
  47. this.badSorting = event.isChecked;
  48. this.draw();
  49. },
  50. start() {
  51. // AStar.Init();
  52. this.draw();
  53. },
  54. clearMap() {
  55. let map = AStar.getMap();
  56. for (var i = 0; i < 32; i++) {
  57. for (var j = 0; j < 32; j++) {
  58. map[i][j].solid = false;
  59. }
  60. }
  61. this.draw();
  62. },
  63. randomMap() {
  64. let map = AStar.getMap();
  65. for (var i = 0; i < 32; i++) {
  66. for (var j = 0; j < 32; j++) {
  67. map[i][j].solid = Math.random() > 0.8;
  68. }
  69. }
  70. this.draw();
  71. },
  72. draw() {
  73. // console.log('this.allowDiagonals=', this.allowDiagonals,
  74. // 'this.diagonalCost=', this.diagonalCost,
  75. // 'this.dotTiebreaker=', this.dotTiebreaker,
  76. // 'this.badSorting=', this.badSorting)
  77. AStar.setInitInfo(this.allowDiagonals, this.diagonalCost, this.dotTiebreaker, this.badSorting);
  78. var ctx = this.getComponent(cc.Graphics);
  79. let map = AStar.getMap();
  80. var start = { x: 2, y: 2 };
  81. AStar.setStart(start);
  82. var end = { x: 31, y: 31 };
  83. ctx.fillColor = new cc.Color().fromHEX('#EEEEEE');
  84. ctx.strokeColor = new cc.Color().fromHEX('#888888');
  85. ctx.rect(0, 0, 500, 500);
  86. ctx.stroke();
  87. ctx.fill();
  88. //设置AStar类里面的graphics
  89. AStar.setGraphics(ctx);
  90. var r = AStar.pathFind(start.x, start.y, end.x, end.y);
  91. // cc.log('得到的路径 = ', r);
  92. ctx.strokeColor = new cc.Color().fromHEX('#888888');
  93. ctx.rect(0.5, 0.5, 500, 500);
  94. ctx.stroke();
  95. ctx.fillColor = new cc.Color().fromHEX('#333333');
  96. ctx.strokeColor = new cc.Color().fromHEX('#333333');
  97. for (let i = 0; i < 32; i++) {
  98. for (let j = 0; j < 32; j++) {
  99. if (map[i][j].solid) {
  100. ctx.rect(i * 10, j * 10, 10, 10);
  101. ctx.stroke();
  102. ctx.fill();
  103. }
  104. else {
  105. ctx.rect(i * 10 - 0.5, j * 10 - 0.5, 10, 10);
  106. ctx.stroke();
  107. }
  108. }
  109. }
  110. // return;
  111. ctx.fillColor = new cc.Color().fromHEX('#FF7777');
  112. ctx.rect(start.x * 10, start.y * 10, 10, 10);
  113. ctx.stroke();
  114. ctx.fill();
  115. ctx.fillColor = new cc.Color().fromHEX('#7777FF');
  116. ctx.rect(end.x * 10, end.y * 10, 10, 10);
  117. ctx.stroke();
  118. ctx.fill();
  119. ctx.strokeColor = new cc.Color().fromHEX('#FF0000');
  120. for (let i = 0; i < r.length; i++) {
  121. if (i == 0) ctx.moveTo(r[i].x * 10 + 5.5, r[i].y * 10 + 5.5);
  122. else ctx.lineTo(r[i].x * 10 + 5.5, r[i].y * 10 + 5.5);
  123. }
  124. ctx.stroke();
  125. }
  126. });