85324c286de79dc19c38ea4c5df703e7ef861f55.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, CCFloat, Component, game, Input, input, KeyCode, math, _decorator, _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _class, _class2, _descriptor, _descriptor2, _descriptor3, _descriptor4, _crd, ccclass, property, menu, Vec2, Vec3, Quat, v2_1, v2_2, v3_1, qt_1, KEYCODE, FreeFlightCamera;
  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. CCFloat = _cc.CCFloat;
  13. Component = _cc.Component;
  14. game = _cc.game;
  15. Input = _cc.Input;
  16. input = _cc.input;
  17. KeyCode = _cc.KeyCode;
  18. math = _cc.math;
  19. _decorator = _cc._decorator;
  20. }],
  21. execute: function () {
  22. _crd = true;
  23. _cclegacy._RF.push({}, "6c841mU+4JNvqwHy5tJsJh0", "FreeFlightCamera", undefined);
  24. /**
  25. * 自由飞行摄像机
  26. * 使用方式:
  27. * 1、组件绑定到任意一设想机上
  28. * 2、通过W(上)、S(下)、A(左)、D(右)、Q(Y轴向下)、E(Y轴向上)来操作摄像机移动
  29. * 3、按住SHIFT键会加速飞行
  30. * 4、鼠标左或右键按下滑动控制摄像机视角原地旋转
  31. * 5、鼠标滚轮键滑动摄像机拉近或拉远
  32. * 6、只支持PC上使用
  33. */
  34. __checkObsolete__(['CCFloat', 'Component', 'EventKeyboard', 'EventMouse', 'EventTouch', 'game', 'Input', 'input', 'KeyCode', 'math', '_decorator']);
  35. ({
  36. ccclass,
  37. property,
  38. menu
  39. } = _decorator);
  40. ({
  41. Vec2,
  42. Vec3,
  43. Quat
  44. } = math);
  45. v2_1 = new Vec2();
  46. v2_2 = new Vec2();
  47. v3_1 = new Vec3();
  48. qt_1 = new Quat();
  49. KEYCODE = {
  50. W: 'W'.charCodeAt(0),
  51. S: 'S'.charCodeAt(0),
  52. A: 'A'.charCodeAt(0),
  53. D: 'D'.charCodeAt(0),
  54. Q: 'Q'.charCodeAt(0),
  55. E: 'E'.charCodeAt(0),
  56. SHIFT: KeyCode.SHIFT_LEFT
  57. };
  58. _export("FreeFlightCamera", FreeFlightCamera = (_dec = ccclass("FreeFlightCamera"), _dec2 = menu('OopsFramework/Camera/FreeFlightCamera (自由飞行摄像机)'), _dec3 = property({
  59. type: CCFloat,
  60. tooltip: "移动速度"
  61. }), _dec4 = property({
  62. type: CCFloat,
  63. tooltip: "按Shift键后的速度"
  64. }), _dec5 = property({
  65. type: CCFloat,
  66. slide: true,
  67. range: [0.05, 0.5, 0.01],
  68. tooltip: "移动后惯性效果"
  69. }), _dec6 = property({
  70. type: CCFloat,
  71. tooltip: "旋转速度"
  72. }), _dec(_class = _dec2(_class = (_class2 = class FreeFlightCamera extends Component {
  73. constructor() {
  74. super(...arguments);
  75. _initializerDefineProperty(this, "moveSpeed", _descriptor, this);
  76. _initializerDefineProperty(this, "moveSpeedShiftScale", _descriptor2, this);
  77. _initializerDefineProperty(this, "damp", _descriptor3, this);
  78. _initializerDefineProperty(this, "rotateSpeed", _descriptor4, this);
  79. this._euler = new Vec3();
  80. this._velocity = new Vec3();
  81. this._position = new Vec3();
  82. this._speedScale = 1;
  83. }
  84. onLoad() {
  85. input.on(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
  86. input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  87. input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
  88. input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
  89. input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
  90. input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  91. Vec3.copy(this._euler, this.node.eulerAngles);
  92. Vec3.copy(this._position, this.node.position);
  93. }
  94. onDestroy() {
  95. input.off(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
  96. input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
  97. input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
  98. input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
  99. input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
  100. input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  101. }
  102. update(dt) {
  103. // position
  104. Vec3.transformQuat(v3_1, this._velocity, this.node.rotation);
  105. Vec3.scaleAndAdd(this._position, this._position, v3_1, this.moveSpeed * this._speedScale);
  106. Vec3.lerp(v3_1, this.node.position, this._position, dt / this.damp); // 向量线性插值产生位移惯性效果
  107. this.node.setPosition(v3_1); // rotation
  108. Quat.fromEuler(qt_1, this._euler.x, this._euler.y, this._euler.z);
  109. Quat.slerp(qt_1, this.node.rotation, qt_1, dt / this.damp); // 四元素线性插值产生旋转惯性效果
  110. this.node.setRotation(qt_1);
  111. }
  112. onMouseWheel(event) {
  113. var delta = -event.getScrollY() * this.moveSpeed * 0.1; // 向下滚动时增量为正
  114. Vec3.transformQuat(v3_1, Vec3.UNIT_Z, this.node.rotation);
  115. Vec3.scaleAndAdd(this._position, this.node.position, v3_1, delta);
  116. }
  117. onKeyDown(event) {
  118. var v = this._velocity;
  119. if (event.keyCode === KEYCODE.SHIFT) {
  120. this._speedScale = this.moveSpeedShiftScale;
  121. } else if (event.keyCode === KEYCODE.W) {
  122. if (v.z === 0) {
  123. v.z = -1;
  124. }
  125. } else if (event.keyCode === KEYCODE.S) {
  126. if (v.z === 0) {
  127. v.z = 1;
  128. }
  129. } else if (event.keyCode === KEYCODE.A) {
  130. if (v.x === 0) {
  131. v.x = -1;
  132. }
  133. } else if (event.keyCode === KEYCODE.D) {
  134. if (v.x === 0) {
  135. v.x = 1;
  136. }
  137. } else if (event.keyCode === KEYCODE.Q) {
  138. if (v.y === 0) {
  139. v.y = -1;
  140. }
  141. } else if (event.keyCode === KEYCODE.E) {
  142. if (v.y === 0) {
  143. v.y = 1;
  144. }
  145. }
  146. }
  147. onKeyUp(event) {
  148. var v = this._velocity;
  149. if (event.keyCode === KEYCODE.SHIFT) {
  150. this._speedScale = 1;
  151. } else if (event.keyCode === KEYCODE.W) {
  152. if (v.z < 0) {
  153. v.z = 0;
  154. }
  155. } else if (event.keyCode === KEYCODE.S) {
  156. if (v.z > 0) {
  157. v.z = 0;
  158. }
  159. } else if (event.keyCode === KEYCODE.A) {
  160. if (v.x < 0) {
  161. v.x = 0;
  162. }
  163. } else if (event.keyCode === KEYCODE.D) {
  164. if (v.x > 0) {
  165. v.x = 0;
  166. }
  167. } else if (event.keyCode === KEYCODE.Q) {
  168. if (v.y < 0) {
  169. v.y = 0;
  170. }
  171. } else if (event.keyCode === KEYCODE.E) {
  172. if (v.y > 0) {
  173. v.y = 0;
  174. }
  175. }
  176. }
  177. onTouchStart(e) {
  178. game.canvas.requestPointerLock();
  179. }
  180. onTouchMove(e) {
  181. e.getStartLocation(v2_1);
  182. if (v2_1.x > game.canvas.width * 0.4) {
  183. // rotation
  184. e.getDelta(v2_2);
  185. this._euler.y -= v2_2.x * this.rotateSpeed * 0.1; // 上下旋转
  186. this._euler.x += v2_2.y * this.rotateSpeed * 0.1; // 左右旋转
  187. } else {
  188. // position
  189. e.getLocation(v2_2);
  190. Vec2.subtract(v2_2, v2_2, v2_1);
  191. this._velocity.x = v2_2.x * 0.01;
  192. this._velocity.z = -v2_2.y * 0.01;
  193. }
  194. }
  195. onTouchEnd(e) {
  196. if (document.exitPointerLock) {
  197. document.exitPointerLock();
  198. }
  199. e.getStartLocation(v2_1);
  200. if (v2_1.x < game.canvas.width * 0.4) {
  201. // position
  202. this._velocity.x = 0;
  203. this._velocity.z = 0;
  204. }
  205. }
  206. }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "moveSpeed", [_dec3], {
  207. configurable: true,
  208. enumerable: true,
  209. writable: true,
  210. initializer: function initializer() {
  211. return 1;
  212. }
  213. }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "moveSpeedShiftScale", [_dec4], {
  214. configurable: true,
  215. enumerable: true,
  216. writable: true,
  217. initializer: function initializer() {
  218. return 5;
  219. }
  220. }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "damp", [_dec5], {
  221. configurable: true,
  222. enumerable: true,
  223. writable: true,
  224. initializer: function initializer() {
  225. return 0.2;
  226. }
  227. }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "rotateSpeed", [_dec6], {
  228. configurable: true,
  229. enumerable: true,
  230. writable: true,
  231. initializer: function initializer() {
  232. return 1;
  233. }
  234. })), _class2)) || _class) || _class));
  235. _cclegacy._RF.pop();
  236. _crd = false;
  237. }
  238. };
  239. });
  240. //# sourceMappingURL=85324c286de79dc19c38ea4c5df703e7ef861f55.js.map