MoveComponent.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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}), (i.Dir = void 0);
  17. const s = t("MapService"),
  18. o = t("BaseMan");
  19. ((t = i.Dir || (i.Dir = {}))[(t.Bottom = 0)] = "Bottom"),
  20. (t[(t.BottomLeft = 1)] = "BottomLeft"),
  21. (t[(t.TopLeft = 3)] = "TopLeft"),
  22. (t[(t.Left = 2)] = "Left"),
  23. (t[(t.Top = 4)] = "Top"),
  24. (t[(t.TopRight = 5)] = "TopRight"),
  25. (t[(t.Right = 6)] = "Right"),
  26. (t[(t.BottomRight = 7)] = "BottomRight");
  27. var {ccclass: t} = cc._decorator,
  28. t = e(
  29. [t],
  30. (t = class extends cc.Component {
  31. constructor() {
  32. super(...arguments),
  33. (this.moving = !1),
  34. (this._nodeIndex = 0),
  35. (this._roadNodeArr = []),
  36. (this._moveAngle = 0),
  37. (this.man = null),
  38. (this._cb = null),
  39. (this._cbTarget = null);
  40. }
  41. onLoad() {
  42. this.man || (this.man = this.getComponent(o.default));
  43. }
  44. update(t) {
  45. var e, i, s, o;
  46. this.moving &&
  47. ((i = (e = this._roadNodeArr[this._nodeIndex]).px - this.node.x),
  48. (s = e.py - this.node.y),
  49. (t = this.man.moveSpeed * t) * t < i * i + s * s
  50. ? (0 == this._moveAngle &&
  51. ((this._moveAngle = Math.atan2(s, i)),
  52. (o = Math.round((-this._moveAngle + Math.PI) / (Math.PI / 4))),
  53. (this.man.direction = 5 < o ? o - 6 : o + 2)),
  54. (o = Math.cos(this._moveAngle) * t),
  55. (t = Math.sin(this._moveAngle) * t),
  56. (this.node.x += o),
  57. (this.node.y += t))
  58. : ((this._moveAngle = 0),
  59. this._nodeIndex == this._roadNodeArr.length - 1
  60. ? ((this.node.x = e.px), (this.node.y = e.py), this.stop())
  61. : this.walk()));
  62. }
  63. walkByRoad(t) {
  64. (this._roadNodeArr = t), (this._nodeIndex = 0), (this._moveAngle = 0), this.walk(), this.move();
  65. }
  66. moveTo(t, e, i) {
  67. (this._cb = e), (this._cbTarget = i);
  68. t = s.default.ins.getPaths(this.node.position, t);
  69. t.length && this.walkByRoad(t);
  70. }
  71. walk() {
  72. this._nodeIndex < this._roadNodeArr.length - 1 && this._nodeIndex++;
  73. }
  74. move() {
  75. (this.moving = !0), (this.man.state = o.CharactorState.run);
  76. }
  77. stop() {
  78. (this.moving = !1),
  79. (this.man.state = o.CharactorState.stand),
  80. this._cb && this._cb.call(this._cbTarget);
  81. }
  82. })
  83. );
  84. i.default = t;