784ba4a54c928038eb170ee09ab498f2db8046f7.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, math, randomRange, Vec3, _dec, _class, _crd, ccclass, property, PlayerCamera;
  4. return {
  5. setters: [function (_cc) {
  6. _cclegacy = _cc.cclegacy;
  7. __checkObsolete__ = _cc.__checkObsolete__;
  8. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  9. _decorator = _cc._decorator;
  10. Component = _cc.Component;
  11. math = _cc.math;
  12. randomRange = _cc.randomRange;
  13. Vec3 = _cc.Vec3;
  14. }],
  15. execute: function () {
  16. _crd = true;
  17. _cclegacy._RF.push({}, "619d01JKG1NDJ7W6p8rn+AL", "PlayerCamera", undefined);
  18. __checkObsolete__(['_decorator', 'Component', 'math', 'Node', 'randomRange', 'Vec3']);
  19. ({
  20. ccclass,
  21. property
  22. } = _decorator);
  23. _export("PlayerCamera", PlayerCamera = (_dec = ccclass('PlayerCamera'), _dec(_class = class PlayerCamera extends Component {
  24. constructor() {
  25. super(...arguments);
  26. /** 是否允许相机抖动 */
  27. this.canShake = true;
  28. /** 抖动持续时间 */
  29. this.duration = 0;
  30. /** 已过去的时间 */
  31. this.elapsed = 0;
  32. /** 计时器 */
  33. this.timer = 0;
  34. /** 相机原始位置 */
  35. this.originalPos = new Vec3();
  36. /** 抖动幅度 */
  37. this.shakeAmplitude = 1;
  38. // 新增Z轴震动参数
  39. this.targetZ = 0;
  40. this.startZ = 0;
  41. }
  42. /**
  43. * 组件开始运行时调用,记录相机的原始位置
  44. */
  45. start() {
  46. this.originalPos = this.node.position.clone();
  47. }
  48. /**
  49. * 触发相机抖动(新增Z轴参数)
  50. * @param duration - 抖动持续时间
  51. * @param amplitude - XY轴抖动幅度
  52. * @param zAmplitude - Z轴后坐力幅度
  53. */
  54. shake(duration, amplitude, zAmplitude) {
  55. if (zAmplitude === void 0) {
  56. zAmplitude = 0;
  57. }
  58. if (this.canShake) return;
  59. this.canShake = true;
  60. this.duration = duration;
  61. this.shakeAmplitude = amplitude; // 记录Z轴初始位置并计算目标位置
  62. this.startZ = this.node.position.z;
  63. this.targetZ = this.startZ + zAmplitude;
  64. this.elapsed = 0;
  65. }
  66. update(deltaTime) {
  67. if (!this.canShake) {
  68. this.node.position = this.originalPos;
  69. return;
  70. }
  71. this.elapsed += deltaTime;
  72. var progress = Math.min(this.elapsed / this.duration, 1); //线性插值计算Z轴位置
  73. var currentZ = math.lerp(this.startZ, this.targetZ, this.easeOutQuad(progress)); //XY轴保持随机抖动
  74. var xOffset = randomRange(-this.shakeAmplitude, this.shakeAmplitude);
  75. var yOffset = randomRange(-this.shakeAmplitude, this.shakeAmplitude);
  76. this.node.setPosition(this.originalPos.x + xOffset, this.originalPos.y + yOffset, currentZ);
  77. if (progress >= 1) {
  78. this.canShake = false; //最终确保回到精确的原始位置
  79. this.node.setPosition(this.originalPos.x, this.originalPos.y, this.startZ);
  80. }
  81. }
  82. /** 二次缓动函数 */
  83. easeOutQuad(t) {
  84. return t * (2 - t);
  85. }
  86. }) || _class));
  87. _cclegacy._RF.pop();
  88. _crd = false;
  89. }
  90. };
  91. });
  92. //# sourceMappingURL=784ba4a54c928038eb170ee09ab498f2db8046f7.js.map