b5483cbdecd8cdd9f831177fbc88ee798602c6c3.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Component, EPSILON, RigidBody, Vec3, _decorator, _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _crd, ccclass, property, v3_0, v3_1, MoveRigidBody;
  4. function _initializerDefineProperty(target, property, descriptor, context) { if (!descriptor) return; Object.defineProperty(target, property, { enumerable: descriptor.enumerable, configurable: descriptor.configurable, writable: descriptor.writable, value: descriptor.initializer ? descriptor.initializer.call(context) : void 0 }); }
  5. function _applyDecoratedDescriptor(target, property, decorators, descriptor, context) { var desc = {}; Object.keys(descriptor).forEach(function (key) { desc[key] = descriptor[key]; }); desc.enumerable = !!desc.enumerable; desc.configurable = !!desc.configurable; if ('value' in desc || desc.initializer) { desc.writable = true; } desc = decorators.slice().reverse().reduce(function (desc, decorator) { return decorator(target, property, desc) || desc; }, desc); if (context && desc.initializer !== void 0) { desc.value = desc.initializer ? desc.initializer.call(context) : void 0; desc.initializer = undefined; } if (desc.initializer === void 0) { Object.defineProperty(target, property, desc); desc = null; } return desc; }
  6. function _initializerWarningHelper(descriptor, context) { throw new Error('Decorating class property failed. Please ensure that ' + 'transform-class-properties is enabled and runs after the decorators transform.'); }
  7. return {
  8. setters: [function (_cc) {
  9. _cclegacy = _cc.cclegacy;
  10. __checkObsolete__ = _cc.__checkObsolete__;
  11. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  12. Component = _cc.Component;
  13. EPSILON = _cc.EPSILON;
  14. RigidBody = _cc.RigidBody;
  15. Vec3 = _cc.Vec3;
  16. _decorator = _cc._decorator;
  17. }],
  18. execute: function () {
  19. _crd = true;
  20. _cclegacy._RF.push({}, "4e8cedkWeJDEZoUMtauac/M", "MoveRigidBody", undefined);
  21. __checkObsolete__(['Component', 'EPSILON', 'RigidBody', 'Vec3', '_decorator']);
  22. ({
  23. ccclass,
  24. property
  25. } = _decorator);
  26. v3_0 = new Vec3();
  27. v3_1 = new Vec3();
  28. /**
  29. * 物理方式移动
  30. * 1. 施加线性数度
  31. * 2. 施加阻尼
  32. * 3. 施加重力
  33. * 4. 控制移动速度或速度比率
  34. */
  35. _export("MoveRigidBody", MoveRigidBody = (_dec = ccclass('MoveRigidBody'), _dec2 = property({
  36. tooltip: '阻尼'
  37. }), _dec3 = property({
  38. tooltip: '重力'
  39. }), _dec4 = property({
  40. tooltip: '移动速度'
  41. }), _dec5 = property({
  42. tooltip: '速度比率'
  43. }), _dec(_class = (_class2 = class MoveRigidBody extends Component {
  44. constructor(...args) {
  45. super(...args);
  46. _initializerDefineProperty(this, "damping", _descriptor, this);
  47. _initializerDefineProperty(this, "gravity", _descriptor2, this);
  48. _initializerDefineProperty(this, "_speed", _descriptor3, this);
  49. _initializerDefineProperty(this, "_ratio", _descriptor4, this);
  50. this._rigidBody = null;
  51. this._grounded = true;
  52. // 是否着地
  53. this._curMaxSpeed = 0;
  54. // 当前最大速度
  55. this._prevAngleY = 0;
  56. // 之前的Y角度值
  57. this._stateX = 0;
  58. this._stateZ = 0;
  59. this._velocity = new Vec3();
  60. }
  61. get speed() {
  62. return this._speed;
  63. }
  64. set speed(value) {
  65. this._speed = value;
  66. this._curMaxSpeed = value * this.ratio;
  67. }
  68. get ratio() {
  69. return this._ratio;
  70. }
  71. set ratio(value) {
  72. this._ratio = value;
  73. this._curMaxSpeed = this.speed * value;
  74. }
  75. /** 是否着地 */
  76. get grounded() {
  77. return this._grounded;
  78. }
  79. /** 移动方向 */
  80. get velocity() {
  81. return this._velocity;
  82. }
  83. set velocity(value) {
  84. this._velocity = value;
  85. var x = value.x;
  86. var z = value.z;
  87. if (x > 0 && this._stateX < 0 || x < 0 && this._stateX > 0 || z > 0 && this._stateZ < 0 || z < 0 && this._stateZ > 0) {
  88. this._rigidBody.clearVelocity(); // 当前跟之前方向不一致则清除速度,避免惯性太大
  89. }
  90. this._stateX = x;
  91. this._stateZ = z;
  92. }
  93. start() {
  94. this._rigidBody = this.getComponent(RigidBody);
  95. this._prevAngleY = this.node.eulerAngles.y;
  96. }
  97. /** 刚体停止移动 */
  98. stop() {
  99. this._stateX = 0;
  100. this._stateZ = 0;
  101. this._rigidBody.clearVelocity(); // 清除移动速度
  102. }
  103. update(dt) {
  104. // 施加重力
  105. this.applyGravity(); // 施加阻尼
  106. this.applyDamping(dt); // 未落地无法移动
  107. if (!this.grounded) return; // 施加移动
  108. this.applyLinearVelocity(v3_0, 1);
  109. }
  110. /** 施加重力 */
  111. applyGravity() {
  112. const g = this.gravity;
  113. const m = this._rigidBody.mass;
  114. v3_1.set(0, m * g, 0);
  115. this._rigidBody.applyForce(v3_1);
  116. }
  117. /** 施加阻尼 */
  118. applyDamping(dt) {
  119. // 获取线性速度
  120. this._rigidBody.getLinearVelocity(v3_1);
  121. if (v3_1.lengthSqr() > EPSILON) {
  122. v3_1.multiplyScalar(Math.pow(1.0 - this.damping, dt));
  123. this._rigidBody.setLinearVelocity(v3_1);
  124. }
  125. }
  126. /**
  127. * 施加移动
  128. * @param {Vec3} dir 方向
  129. * @param {number} speed 移动数度
  130. */
  131. applyLinearVelocity(dir, speed) {
  132. if (this._stateX || this._stateZ) {
  133. v3_0.set(this._stateX, 0, this._stateZ);
  134. v3_0.normalize(); // 获取线性速度
  135. this._rigidBody.getLinearVelocity(v3_1);
  136. Vec3.scaleAndAdd(v3_1, v3_1, dir, speed);
  137. const ms = this._curMaxSpeed;
  138. const len = v3_1.lengthSqr();
  139. let ratio = 1;
  140. if (len > ms) {
  141. if (Math.abs(this.node.eulerAngles.y - this._prevAngleY) >= 10) {
  142. ratio = 2;
  143. }
  144. this._prevAngleY = this.node.eulerAngles.y;
  145. v3_1.normalize();
  146. v3_1.multiplyScalar(ms / ratio);
  147. }
  148. this._rigidBody.setLinearVelocity(v3_1);
  149. }
  150. }
  151. }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "damping", [_dec2], {
  152. configurable: true,
  153. enumerable: true,
  154. writable: true,
  155. initializer: function () {
  156. return 0.5;
  157. }
  158. }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "gravity", [_dec3], {
  159. configurable: true,
  160. enumerable: true,
  161. writable: true,
  162. initializer: function () {
  163. return -10;
  164. }
  165. }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "_speed", [property], {
  166. configurable: true,
  167. enumerable: true,
  168. writable: true,
  169. initializer: function () {
  170. return 5;
  171. }
  172. }), _applyDecoratedDescriptor(_class2.prototype, "speed", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "speed"), _class2.prototype), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "_ratio", [property], {
  173. configurable: true,
  174. enumerable: true,
  175. writable: true,
  176. initializer: function () {
  177. return 1;
  178. }
  179. }), _applyDecoratedDescriptor(_class2.prototype, "ratio", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "ratio"), _class2.prototype)), _class2)) || _class));
  180. _cclegacy._RF.pop();
  181. _crd = false;
  182. }
  183. };
  184. });
  185. //# sourceMappingURL=b5483cbdecd8cdd9f831177fbc88ee798602c6c3.js.map