a0fc016fe00e1c38cec395e2881b0823b389bb22.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Component, Node, Vec3, _decorator, Timer, Vec3Util, _dec, _class, _crd, ccclass, property, MoveTo;
  4. function _reportPossibleCrUseOfTimer(extras) {
  5. _reporterNs.report("Timer", "../../core/common/timer/Timer", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfVec3Util(extras) {
  8. _reporterNs.report("Vec3Util", "../../core/utils/Vec3Util", _context.meta, extras);
  9. }
  10. return {
  11. setters: [function (_unresolved_) {
  12. _reporterNs = _unresolved_;
  13. }, function (_cc) {
  14. _cclegacy = _cc.cclegacy;
  15. __checkObsolete__ = _cc.__checkObsolete__;
  16. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  17. Component = _cc.Component;
  18. Node = _cc.Node;
  19. Vec3 = _cc.Vec3;
  20. _decorator = _cc._decorator;
  21. }, function (_unresolved_2) {
  22. Timer = _unresolved_2.Timer;
  23. }, function (_unresolved_3) {
  24. Vec3Util = _unresolved_3.Vec3Util;
  25. }],
  26. execute: function () {
  27. _crd = true;
  28. _cclegacy._RF.push({}, "5e22a+qWUpI6ZHSVRRj2DYT", "MoveTo", undefined);
  29. /*
  30. * @Author: dgflash
  31. * @Date: 2022-03-25 18:12:10
  32. * @LastEditors: dgflash
  33. * @LastEditTime: 2023-01-19 14:59:50
  34. */
  35. __checkObsolete__(['Component', 'Node', 'Vec3', '_decorator']);
  36. ({
  37. ccclass,
  38. property
  39. } = _decorator);
  40. /** 移动到指定目标位置 */
  41. _export("MoveTo", MoveTo = (_dec = ccclass('MoveTo'), _dec(_class = class MoveTo extends Component {
  42. constructor(...args) {
  43. super(...args);
  44. /** 目标位置 */
  45. this.target = null;
  46. /** 移动方向 */
  47. this.velocity = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  48. error: Error()
  49. }), Vec3Util) : Vec3Util).zero;
  50. /** 移动速度(每秒移动的像素距离) */
  51. this.speed = 0;
  52. /** 是否计算将 Y 轴带入计算 */
  53. this.hasYAxis = true;
  54. /** 坐标标(默认本地坐标) */
  55. this.ns = Node.NodeSpace.LOCAL;
  56. /** 偏移距离 */
  57. this.offset = 0;
  58. /** 偏移向量 */
  59. this.offsetVector = null;
  60. /** 移动开始 */
  61. this.onStart = null;
  62. /** 移动完成回调 */
  63. this.onComplete = null;
  64. /** 距离变化时 */
  65. this.onChange = null;
  66. /** 延时触发器 */
  67. this.timer = new (_crd && Timer === void 0 ? (_reportPossibleCrUseOfTimer({
  68. error: Error()
  69. }), Timer) : Timer)();
  70. /** 终点备份 */
  71. this.end = null;
  72. }
  73. onLoad() {
  74. this.enabled = false;
  75. }
  76. move() {
  77. this.enabled = true;
  78. }
  79. update(dt) {
  80. let end;
  81. console.assert(this.speed > 0, "移动速度必须要大于零");
  82. if (this.target instanceof Node) {
  83. end = this.ns == Node.NodeSpace.WORLD ? this.target.worldPosition : this.target.position;
  84. } else {
  85. end = this.target;
  86. } // 移动目标节点被释放时
  87. if (end == null) {
  88. this.exit();
  89. return;
  90. } // 目标移动后,重计算移动方向与移动到目标点的速度
  91. if (this.end == null || !this.end.strictEquals(end)) {
  92. var _this$onChange;
  93. let target = end.clone();
  94. if (this.offsetVector) {
  95. target = target.add(this.offsetVector);
  96. }
  97. if (this.hasYAxis == false) target.y = 0; // 移动方向与移动数度
  98. let start = this.ns == Node.NodeSpace.WORLD ? this.node.worldPosition : this.node.position;
  99. this.velocity = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  100. error: Error()
  101. }), Vec3Util) : Vec3Util).sub(target, start).normalize(); // 移动时间与目标偏位置计算
  102. let distance = Vec3.distance(start, target) - this.offset; // 目标位置修改事件
  103. (_this$onChange = this.onChange) == null || _this$onChange.call(this);
  104. if (distance <= 0) {
  105. this.exit();
  106. return;
  107. } else {
  108. var _this$onStart;
  109. (_this$onStart = this.onStart) == null || _this$onStart.call(this);
  110. this.timer.step = distance / this.speed;
  111. this.end = end.clone();
  112. }
  113. }
  114. if (this.speed > 0) {
  115. let trans = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  116. error: Error()
  117. }), Vec3Util) : Vec3Util).mul(this.velocity, this.speed * dt);
  118. if (this.ns == Node.NodeSpace.WORLD) this.node.worldPosition = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  119. error: Error()
  120. }), Vec3Util) : Vec3Util).add(this.node.worldPosition, trans);else this.node.position = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  121. error: Error()
  122. }), Vec3Util) : Vec3Util).add(this.node.position, trans);
  123. } // 移动完成事件
  124. if (this.timer.update(dt)) {
  125. if (this.offset == 0) {
  126. if (this.ns == Node.NodeSpace.WORLD) this.node.worldPosition = this.end;else this.node.position = this.end;
  127. }
  128. this.exit();
  129. }
  130. }
  131. exit() {
  132. var _this$onComplete;
  133. (_this$onComplete = this.onComplete) == null || _this$onComplete.call(this);
  134. this.enabled = false;
  135. this.target = null;
  136. this.velocity = (_crd && Vec3Util === void 0 ? (_reportPossibleCrUseOfVec3Util({
  137. error: Error()
  138. }), Vec3Util) : Vec3Util).zero;
  139. this.speed = 0;
  140. this.hasYAxis = true;
  141. this.ns = Node.NodeSpace.LOCAL;
  142. this.offset = 0;
  143. this.offsetVector = null;
  144. this.onStart = null;
  145. this.onComplete = null;
  146. this.onChange = null;
  147. this.timer.reset();
  148. this.end = null;
  149. }
  150. }) || _class));
  151. _cclegacy._RF.pop();
  152. _crd = false;
  153. }
  154. };
  155. });
  156. //# sourceMappingURL=a0fc016fe00e1c38cec395e2881b0823b389bb22.js.map