GridLayer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. e =
  5. (this && this.__decorate) ||
  6. function (t, e, i, s) {
  7. var o,
  8. a = arguments.length,
  9. n = a < 3 ? e : null === s ? (s = Object.getOwnPropertyDescriptor(e, i)) : s;
  10. if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) n = Reflect.decorate(t, e, i, s);
  11. else
  12. for (var r = t.length - 1; 0 <= r; r--)
  13. (o = t[r]) && (n = (a < 3 ? o(n) : 3 < a ? o(e, i, n) : o(e, i)) || n);
  14. return 3 < a && n && Object.defineProperty(e, i, n), n;
  15. };
  16. Object.defineProperty(i, "__esModule", {value: !0});
  17. const s = t("MapType"),
  18. {ccclass: o, property: a, menu: n, requireComponent: r, executeInEditMode: l} = cc._decorator;
  19. t = class extends cc.Component {
  20. constructor() {
  21. super(...arguments),
  22. (this.graphics = null),
  23. (this.lineSize1 = 6),
  24. (this.lineSize2 = 3),
  25. (this.lingColor3 = cc.color(0, 0, 0, 187).fromHEX("#bbbbbbbb")),
  26. (this.lingColor1 = cc.color(255, 0, 0, 187).fromHEX("#00550025")),
  27. (this.lingColor2 = "#11115566"),
  28. (this._test = !1);
  29. }
  30. onLoad() {
  31. this.graphics ||
  32. ((this.graphics = this.getComponent(cc.Graphics)),
  33. this.graphics || (this.graphics = this.addComponent(cc.Graphics)));
  34. }
  35. set test(t) {
  36. (this._test = t), this.drawGrid(this.node.parent.width, this.node.parent.height, 100, 100, s.MapType.angle90);
  37. }
  38. get test() {
  39. return this._test;
  40. }
  41. clear() {
  42. this.graphics.clear();
  43. }
  44. drawGrid(t, e, i, s, o) {
  45. (this._mapWidth = t),
  46. (this._mapHeight = e),
  47. (this._ceilWidth = i),
  48. (this._ceilHeight = s),
  49. (this._halfCeilWidth = Math.floor(i / 2)),
  50. (this._halfCeilHeight = Math.floor(s / 2));
  51. (i = 2 * Math.ceil(t / this._ceilWidth) + 1),
  52. (s = 2 * Math.ceil(e / this._ceilHeight) + 1),
  53. (t = i * this._halfCeilWidth),
  54. (e = s * this._halfCeilHeight);
  55. 0 == o
  56. ? this.draw45AngleGrid(s, i, t, e)
  57. : 1 == o
  58. ? this.draw90AngleGrid(s, i, t, e)
  59. : 2 == o && this.drawHoneycombGrid(t, e);
  60. }
  61. draw45AngleGrid(t, e, i, s) {
  62. this.graphics.clear();
  63. for (var o = 0; o < t + 1; o++)
  64. o % 2 == 0
  65. ? this.graphics.strokeColor.fromHEX(this.lingColor2)
  66. : (this.graphics.strokeColor = this.lingColor1),
  67. (this.graphics.lineWidth = 1),
  68. this.graphics.moveTo(0, o * this._halfCeilHeight),
  69. this.graphics.lineTo(i, o * this._halfCeilHeight),
  70. this.graphics.stroke();
  71. for (var a = 0; a < e + 1; a++)
  72. a % 2 == 0
  73. ? this.graphics.strokeColor.fromHEX(this.lingColor2)
  74. : (this.graphics.strokeColor = this.lingColor1),
  75. (this.graphics.lineWidth = 1),
  76. this.graphics.moveTo(a * this._halfCeilWidth, 0),
  77. this.graphics.lineTo(a * this._halfCeilWidth, s),
  78. this.graphics.stroke();
  79. for (var n, r, l, h, c = 1; c < e + t; c++)
  80. (this.graphics.strokeColor = this.lingColor3),
  81. (this.graphics.lineWidth = 2),
  82. (n = c * this._halfCeilWidth),
  83. c % 2 == 1
  84. ? ((l = r = 0),
  85. (h = c * this._halfCeilHeight),
  86. i < n && ((n = i), (r = (c - e) * this._halfCeilHeight)),
  87. s < h && ((l = (c - t) * this._halfCeilWidth), (h = s)),
  88. this.graphics.moveTo(n, r),
  89. this.graphics.lineTo(l, h))
  90. : ((r = s),
  91. (h = (t - c) * this._halfCeilHeight),
  92. i < n && ((n = i), (r = (t - (c - e)) * this._halfCeilHeight)),
  93. h < (l = 0) && ((l = (c - t) * this._halfCeilWidth), (h = 0)),
  94. this.graphics.moveTo(n, r),
  95. this.graphics.lineTo(l, h),
  96. this.graphics.stroke());
  97. }
  98. draw90AngleGrid(t, e, i, s) {
  99. this.graphics.clear();
  100. for (var o = this.lineSize1, a = this.lineSize2, n = 0; n < t + 1; n++)
  101. n % 2 == 0
  102. ? ((this.graphics.strokeColor = this.lingColor3), (this.graphics.lineWidth = o))
  103. : ((this.graphics.strokeColor = this.lingColor1), (this.graphics.lineWidth = a)),
  104. this.graphics.moveTo(0, n * this._halfCeilHeight),
  105. this.graphics.lineTo(i, n * this._halfCeilHeight),
  106. this.graphics.stroke();
  107. for (var r = 0; r < e + 1; r++)
  108. r % 2 == 0
  109. ? ((this.graphics.strokeColor = this.lingColor3), (this.graphics.lineWidth = o))
  110. : ((this.graphics.strokeColor = this.lingColor1), (this.graphics.lineWidth = a)),
  111. this.graphics.moveTo(r * this._halfCeilWidth, 0),
  112. this.graphics.lineTo(r * this._halfCeilWidth, s),
  113. this.graphics.stroke();
  114. }
  115. drawHoneycombGrid(t, e) {
  116. this.graphics.clear(), this._ceilWidth;
  117. for (
  118. var i = this._ceilWidth / 4,
  119. s = 3 * i,
  120. o = this._ceilHeight / 2,
  121. a = 2 * i - o,
  122. n = t / this._ceilWidth,
  123. r = e / this._ceilWidth,
  124. l = 2 * Math.ceil((this._mapWidth - i) / (6 * i)),
  125. h = Math.ceil((this._mapHeight - o) / this._ceilHeight),
  126. c = 0;
  127. c < 4 * r + 1;
  128. c++
  129. )
  130. c % 4 == 0
  131. ? (this.graphics.strokeColor.fromHEX(this.lingColor2), (this.graphics.lineWidth = 1))
  132. : ((this.graphics.strokeColor = this.lingColor1), (this.graphics.lineWidth = 1.2)),
  133. c % 4 == 1
  134. ? (this.graphics.moveTo(0, (c - 1) * i + a), this.graphics.lineTo(t, (c - 1) * i + a))
  135. : c % 4 == 3
  136. ? (this.graphics.moveTo(0, (c - 1) * i + o), this.graphics.lineTo(t, (c - 1) * i + o))
  137. : (this.graphics.moveTo(0, c * i), this.graphics.lineTo(t, c * i)),
  138. this.graphics.stroke();
  139. for (var d = 0; d < 4 * n + 1; d++)
  140. d % 4 == 0
  141. ? this.graphics.strokeColor.fromHEX(this.lingColor2)
  142. : (this.graphics.strokeColor = this.lingColor1),
  143. (this.graphics.lineWidth = 1),
  144. this.graphics.moveTo(d * i, 0),
  145. this.graphics.lineTo(d * i, e),
  146. this.graphics.stroke();
  147. (this.graphics.strokeColor = this.lingColor3), (this.graphics.lineWidth = 1.5);
  148. for (var u = 0; u < l; u++) {
  149. var p = u * s,
  150. f = 0;
  151. u % 2 != 0 && (f = o);
  152. for (var g = 0; g < h; g++) {
  153. var m = g * this._ceilHeight + f;
  154. u % 2 == 0
  155. ? (this.drawLine(p + i, 0 + m, p + s, 0 + m),
  156. this.drawLine(p + s, 0 + m, p + this._ceilWidth, o + m),
  157. this.drawLine(p + this._ceilWidth, o + m, p + s, this._ceilHeight + m),
  158. g == h - 1 && this.drawLine(p + s, this._ceilHeight + m, p + i, this._ceilHeight + m),
  159. this.drawLine(p + i, this._ceilHeight + m, 0 + p, o + m),
  160. this.drawLine(0 + p, o + m, p + i, 0 + m))
  161. : (this.drawLine(p + i, 0 + m, p + s, 0 + m),
  162. u == l - 1 &&
  163. (this.drawLine(p + s, 0 + m, p + this._ceilWidth, o + m),
  164. this.drawLine(p + this._ceilWidth, o + m, p + s, this._ceilHeight + m)),
  165. g == h - 1 &&
  166. (this.drawLine(p + s, this._ceilHeight + m, p + i, this._ceilHeight + m),
  167. this.drawLine(p + i, this._ceilHeight + m, 0 + p, o + m),
  168. u != l - 1 && this.drawLine(p + this._ceilWidth, o + m, p + s, this._ceilHeight + m)));
  169. }
  170. }
  171. }
  172. drawLine(t, e, i, s) {
  173. this.graphics.moveTo(t, e), this.graphics.lineTo(i, s), this.graphics.stroke();
  174. }
  175. };
  176. e([a(cc.Graphics)], t.prototype, "graphics", void 0),
  177. e([a()], t.prototype, "lineSize1", void 0),
  178. e([a()], t.prototype, "lineSize2", void 0),
  179. e([a()], t.prototype, "lingColor3", void 0),
  180. e([a()], t.prototype, "lingColor1", void 0),
  181. e([a], t.prototype, "_test", void 0),
  182. e([a()], t.prototype, "test", null),
  183. (t = e([o, r(cc.Graphics), n("game/map/GridLayer"), l], t)),
  184. (i.default = t);