| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- 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;
- 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 }); }
- 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; }
- 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.'); }
- return {
- setters: [function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- Component = _cc.Component;
- EPSILON = _cc.EPSILON;
- RigidBody = _cc.RigidBody;
- Vec3 = _cc.Vec3;
- _decorator = _cc._decorator;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "4e8cedkWeJDEZoUMtauac/M", "MoveRigidBody", undefined);
- __checkObsolete__(['Component', 'EPSILON', 'RigidBody', 'Vec3', '_decorator']);
- ({
- ccclass,
- property
- } = _decorator);
- v3_0 = new Vec3();
- v3_1 = new Vec3();
- /**
- * 物理方式移动
- * 1. 施加线性数度
- * 2. 施加阻尼
- * 3. 施加重力
- * 4. 控制移动速度或速度比率
- */
- _export("MoveRigidBody", MoveRigidBody = (_dec = ccclass('MoveRigidBody'), _dec2 = property({
- tooltip: '阻尼'
- }), _dec3 = property({
- tooltip: '重力'
- }), _dec4 = property({
- tooltip: '移动速度'
- }), _dec5 = property({
- tooltip: '速度比率'
- }), _dec(_class = (_class2 = class MoveRigidBody extends Component {
- constructor(...args) {
- super(...args);
- _initializerDefineProperty(this, "damping", _descriptor, this);
- _initializerDefineProperty(this, "gravity", _descriptor2, this);
- _initializerDefineProperty(this, "_speed", _descriptor3, this);
- _initializerDefineProperty(this, "_ratio", _descriptor4, this);
- this._rigidBody = null;
- this._grounded = true;
- // 是否着地
- this._curMaxSpeed = 0;
- // 当前最大速度
- this._prevAngleY = 0;
- // 之前的Y角度值
- this._stateX = 0;
- this._stateZ = 0;
- this._velocity = new Vec3();
- }
- get speed() {
- return this._speed;
- }
- set speed(value) {
- this._speed = value;
- this._curMaxSpeed = value * this.ratio;
- }
- get ratio() {
- return this._ratio;
- }
- set ratio(value) {
- this._ratio = value;
- this._curMaxSpeed = this.speed * value;
- }
- /** 是否着地 */
- get grounded() {
- return this._grounded;
- }
- /** 移动方向 */
- get velocity() {
- return this._velocity;
- }
- set velocity(value) {
- this._velocity = value;
- var x = value.x;
- var z = value.z;
- if (x > 0 && this._stateX < 0 || x < 0 && this._stateX > 0 || z > 0 && this._stateZ < 0 || z < 0 && this._stateZ > 0) {
- this._rigidBody.clearVelocity(); // 当前跟之前方向不一致则清除速度,避免惯性太大
- }
- this._stateX = x;
- this._stateZ = z;
- }
- start() {
- this._rigidBody = this.getComponent(RigidBody);
- this._prevAngleY = this.node.eulerAngles.y;
- }
- /** 刚体停止移动 */
- stop() {
- this._stateX = 0;
- this._stateZ = 0;
- this._rigidBody.clearVelocity(); // 清除移动速度
- }
- update(dt) {
- // 施加重力
- this.applyGravity(); // 施加阻尼
- this.applyDamping(dt); // 未落地无法移动
- if (!this.grounded) return; // 施加移动
- this.applyLinearVelocity(v3_0, 1);
- }
- /** 施加重力 */
- applyGravity() {
- const g = this.gravity;
- const m = this._rigidBody.mass;
- v3_1.set(0, m * g, 0);
- this._rigidBody.applyForce(v3_1);
- }
- /** 施加阻尼 */
- applyDamping(dt) {
- // 获取线性速度
- this._rigidBody.getLinearVelocity(v3_1);
- if (v3_1.lengthSqr() > EPSILON) {
- v3_1.multiplyScalar(Math.pow(1.0 - this.damping, dt));
- this._rigidBody.setLinearVelocity(v3_1);
- }
- }
- /**
- * 施加移动
- * @param {Vec3} dir 方向
- * @param {number} speed 移动数度
- */
- applyLinearVelocity(dir, speed) {
- if (this._stateX || this._stateZ) {
- v3_0.set(this._stateX, 0, this._stateZ);
- v3_0.normalize(); // 获取线性速度
- this._rigidBody.getLinearVelocity(v3_1);
- Vec3.scaleAndAdd(v3_1, v3_1, dir, speed);
- const ms = this._curMaxSpeed;
- const len = v3_1.lengthSqr();
- let ratio = 1;
- if (len > ms) {
- if (Math.abs(this.node.eulerAngles.y - this._prevAngleY) >= 10) {
- ratio = 2;
- }
- this._prevAngleY = this.node.eulerAngles.y;
- v3_1.normalize();
- v3_1.multiplyScalar(ms / ratio);
- }
- this._rigidBody.setLinearVelocity(v3_1);
- }
- }
- }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "damping", [_dec2], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function () {
- return 0.5;
- }
- }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "gravity", [_dec3], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function () {
- return -10;
- }
- }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "_speed", [property], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function () {
- return 5;
- }
- }), _applyDecoratedDescriptor(_class2.prototype, "speed", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "speed"), _class2.prototype), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "_ratio", [property], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function () {
- return 1;
- }
- }), _applyDecoratedDescriptor(_class2.prototype, "ratio", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "ratio"), _class2.prototype)), _class2)) || _class));
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=b5483cbdecd8cdd9f831177fbc88ee798602c6c3.js.map
|