| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- 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;
- 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__;
- CCFloat = _cc.CCFloat;
- Component = _cc.Component;
- game = _cc.game;
- Input = _cc.Input;
- input = _cc.input;
- KeyCode = _cc.KeyCode;
- math = _cc.math;
- _decorator = _cc._decorator;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "6c841mU+4JNvqwHy5tJsJh0", "FreeFlightCamera", undefined);
- /**
- * 自由飞行摄像机
- * 使用方式:
- * 1、组件绑定到任意一设想机上
- * 2、通过W(上)、S(下)、A(左)、D(右)、Q(Y轴向下)、E(Y轴向上)来操作摄像机移动
- * 3、按住SHIFT键会加速飞行
- * 4、鼠标左或右键按下滑动控制摄像机视角原地旋转
- * 5、鼠标滚轮键滑动摄像机拉近或拉远
- * 6、只支持PC上使用
- */
- __checkObsolete__(['CCFloat', 'Component', 'EventKeyboard', 'EventMouse', 'EventTouch', 'game', 'Input', 'input', 'KeyCode', 'math', '_decorator']);
- ({
- ccclass,
- property,
- menu
- } = _decorator);
- ({
- Vec2,
- Vec3,
- Quat
- } = math);
- v2_1 = new Vec2();
- v2_2 = new Vec2();
- v3_1 = new Vec3();
- qt_1 = new Quat();
- KEYCODE = {
- W: 'W'.charCodeAt(0),
- S: 'S'.charCodeAt(0),
- A: 'A'.charCodeAt(0),
- D: 'D'.charCodeAt(0),
- Q: 'Q'.charCodeAt(0),
- E: 'E'.charCodeAt(0),
- SHIFT: KeyCode.SHIFT_LEFT
- };
- _export("FreeFlightCamera", FreeFlightCamera = (_dec = ccclass("FreeFlightCamera"), _dec2 = menu('OopsFramework/Camera/FreeFlightCamera (自由飞行摄像机)'), _dec3 = property({
- type: CCFloat,
- tooltip: "移动速度"
- }), _dec4 = property({
- type: CCFloat,
- tooltip: "按Shift键后的速度"
- }), _dec5 = property({
- type: CCFloat,
- slide: true,
- range: [0.05, 0.5, 0.01],
- tooltip: "移动后惯性效果"
- }), _dec6 = property({
- type: CCFloat,
- tooltip: "旋转速度"
- }), _dec(_class = _dec2(_class = (_class2 = class FreeFlightCamera extends Component {
- constructor() {
- super(...arguments);
- _initializerDefineProperty(this, "moveSpeed", _descriptor, this);
- _initializerDefineProperty(this, "moveSpeedShiftScale", _descriptor2, this);
- _initializerDefineProperty(this, "damp", _descriptor3, this);
- _initializerDefineProperty(this, "rotateSpeed", _descriptor4, this);
- this._euler = new Vec3();
- this._velocity = new Vec3();
- this._position = new Vec3();
- this._speedScale = 1;
- }
- onLoad() {
- input.on(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
- input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
- input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
- input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
- input.on(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
- input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
- Vec3.copy(this._euler, this.node.eulerAngles);
- Vec3.copy(this._position, this.node.position);
- }
- onDestroy() {
- input.off(Input.EventType.MOUSE_WHEEL, this.onMouseWheel, this);
- input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
- input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
- input.off(Input.EventType.TOUCH_START, this.onTouchStart, this);
- input.off(Input.EventType.TOUCH_MOVE, this.onTouchMove, this);
- input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
- }
- update(dt) {
- // position
- Vec3.transformQuat(v3_1, this._velocity, this.node.rotation);
- Vec3.scaleAndAdd(this._position, this._position, v3_1, this.moveSpeed * this._speedScale);
- Vec3.lerp(v3_1, this.node.position, this._position, dt / this.damp); // 向量线性插值产生位移惯性效果
- this.node.setPosition(v3_1); // rotation
- Quat.fromEuler(qt_1, this._euler.x, this._euler.y, this._euler.z);
- Quat.slerp(qt_1, this.node.rotation, qt_1, dt / this.damp); // 四元素线性插值产生旋转惯性效果
- this.node.setRotation(qt_1);
- }
- onMouseWheel(event) {
- var delta = -event.getScrollY() * this.moveSpeed * 0.1; // 向下滚动时增量为正
- Vec3.transformQuat(v3_1, Vec3.UNIT_Z, this.node.rotation);
- Vec3.scaleAndAdd(this._position, this.node.position, v3_1, delta);
- }
- onKeyDown(event) {
- var v = this._velocity;
- if (event.keyCode === KEYCODE.SHIFT) {
- this._speedScale = this.moveSpeedShiftScale;
- } else if (event.keyCode === KEYCODE.W) {
- if (v.z === 0) {
- v.z = -1;
- }
- } else if (event.keyCode === KEYCODE.S) {
- if (v.z === 0) {
- v.z = 1;
- }
- } else if (event.keyCode === KEYCODE.A) {
- if (v.x === 0) {
- v.x = -1;
- }
- } else if (event.keyCode === KEYCODE.D) {
- if (v.x === 0) {
- v.x = 1;
- }
- } else if (event.keyCode === KEYCODE.Q) {
- if (v.y === 0) {
- v.y = -1;
- }
- } else if (event.keyCode === KEYCODE.E) {
- if (v.y === 0) {
- v.y = 1;
- }
- }
- }
- onKeyUp(event) {
- var v = this._velocity;
- if (event.keyCode === KEYCODE.SHIFT) {
- this._speedScale = 1;
- } else if (event.keyCode === KEYCODE.W) {
- if (v.z < 0) {
- v.z = 0;
- }
- } else if (event.keyCode === KEYCODE.S) {
- if (v.z > 0) {
- v.z = 0;
- }
- } else if (event.keyCode === KEYCODE.A) {
- if (v.x < 0) {
- v.x = 0;
- }
- } else if (event.keyCode === KEYCODE.D) {
- if (v.x > 0) {
- v.x = 0;
- }
- } else if (event.keyCode === KEYCODE.Q) {
- if (v.y < 0) {
- v.y = 0;
- }
- } else if (event.keyCode === KEYCODE.E) {
- if (v.y > 0) {
- v.y = 0;
- }
- }
- }
- onTouchStart(e) {
- game.canvas.requestPointerLock();
- }
- onTouchMove(e) {
- e.getStartLocation(v2_1);
- if (v2_1.x > game.canvas.width * 0.4) {
- // rotation
- e.getDelta(v2_2);
- this._euler.y -= v2_2.x * this.rotateSpeed * 0.1; // 上下旋转
- this._euler.x += v2_2.y * this.rotateSpeed * 0.1; // 左右旋转
- } else {
- // position
- e.getLocation(v2_2);
- Vec2.subtract(v2_2, v2_2, v2_1);
- this._velocity.x = v2_2.x * 0.01;
- this._velocity.z = -v2_2.y * 0.01;
- }
- }
- onTouchEnd(e) {
- if (document.exitPointerLock) {
- document.exitPointerLock();
- }
- e.getStartLocation(v2_1);
- if (v2_1.x < game.canvas.width * 0.4) {
- // position
- this._velocity.x = 0;
- this._velocity.z = 0;
- }
- }
- }, (_descriptor = _applyDecoratedDescriptor(_class2.prototype, "moveSpeed", [_dec3], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function initializer() {
- return 1;
- }
- }), _descriptor2 = _applyDecoratedDescriptor(_class2.prototype, "moveSpeedShiftScale", [_dec4], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function initializer() {
- return 5;
- }
- }), _descriptor3 = _applyDecoratedDescriptor(_class2.prototype, "damp", [_dec5], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function initializer() {
- return 0.2;
- }
- }), _descriptor4 = _applyDecoratedDescriptor(_class2.prototype, "rotateSpeed", [_dec6], {
- configurable: true,
- enumerable: true,
- writable: true,
- initializer: function initializer() {
- return 1;
- }
- })), _class2)) || _class) || _class));
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=85324c286de79dc19c38ea4c5df703e7ef861f55.js.map
|