d362d7d026e066a9aca80324fd1378e030099971.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, MathUtil, _crd;
  4. _export("MathUtil", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. }],
  9. execute: function () {
  10. _crd = true;
  11. _cclegacy._RF.push({}, "8c615ZS4PRMPKPA9ZqKjiJC", "MathUtil", undefined);
  12. /** 数学工具 */
  13. _export("MathUtil", MathUtil = class MathUtil {
  14. /**
  15. * 获得随机方向
  16. * @param x -1为左,1为右
  17. * @returns
  18. */
  19. static sign(x) {
  20. if (x > 0) {
  21. return 1;
  22. }
  23. if (x < 0) {
  24. return -1;
  25. }
  26. return 0;
  27. }
  28. /**
  29. * 随时间变化进度值
  30. * @param start 初始值
  31. * @param end 结束值
  32. * @param t 时间
  33. */
  34. static progress(start, end, t) {
  35. return start + (end - start) * t;
  36. }
  37. /**
  38. * 插值
  39. * @param numStart 开始数值
  40. * @param numEnd 结束数值
  41. * @param t 时间
  42. */
  43. static lerp(numStart, numEnd, t) {
  44. if (t > 1) {
  45. t = 1;
  46. } else if (t < 0) {
  47. t = 0;
  48. }
  49. return numStart * (1 - t) + numEnd * t;
  50. }
  51. /**
  52. * 角度插值
  53. * @param current 当前角度
  54. * @param target 目标角度
  55. * @param t 时间
  56. */
  57. static lerpAngle(current, target, t) {
  58. current %= 360;
  59. target %= 360;
  60. const dAngle = target - current;
  61. if (dAngle > 180) {
  62. target = current - (360 - dAngle);
  63. } else if (dAngle < -180) {
  64. target = current + (360 + dAngle);
  65. }
  66. return (MathUtil.lerp(current, target, t) % 360 + 360) % 360;
  67. }
  68. /**
  69. * 按一定的速度从一个角度转向令一个角度
  70. * @param current 当前角度
  71. * @param target 目标角度
  72. * @param speed 速度
  73. */
  74. static angleTowards(current, target, speed) {
  75. current %= 360;
  76. target %= 360;
  77. const dAngle = target - current;
  78. if (dAngle > 180) {
  79. target = current - (360 - dAngle);
  80. } else if (dAngle < -180) {
  81. target = current + (360 + dAngle);
  82. }
  83. const dir = target - current;
  84. if (speed > Math.abs(dir)) {
  85. return target;
  86. }
  87. return ((current + speed * Math.sign(dir)) % 360 + 360) % 360;
  88. }
  89. /**
  90. * 获取方位内值,超过时获取对应边界值
  91. * @param value 值
  92. * @param minLimit 最小值
  93. * @param maxLimit 最大值
  94. */
  95. static clamp(value, minLimit, maxLimit) {
  96. if (value < minLimit) {
  97. return minLimit;
  98. }
  99. if (value > maxLimit) {
  100. return maxLimit;
  101. }
  102. return value;
  103. }
  104. /**
  105. * 获得一个值的概率
  106. * @param value 值
  107. */
  108. static probability(value) {
  109. return Math.random() < value;
  110. }
  111. });
  112. /**
  113. * 角度转弧度
  114. */
  115. MathUtil.deg2Rad = Math.PI / 180;
  116. /**
  117. * 弧度转角度
  118. */
  119. MathUtil.rad2Deg = 180 / Math.PI;
  120. _cclegacy._RF.pop();
  121. _crd = false;
  122. }
  123. };
  124. });
  125. //# sourceMappingURL=d362d7d026e066a9aca80324fd1378e030099971.js.map