LevelAction.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { _decorator, BoxCollider2D, Button, Camera, CircleCollider2D, Collider2D, Component, EventTouch, find, Input, input, Node, NodeEventType, RenderTexture, v3, Vec3 } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('LevelAction')
  4. export class LevelAction extends Component {
  5. @property(Camera)
  6. public camera: Camera = null!;
  7. private _renderTex: RenderTexture | null = null;
  8. private _cameraOriginalPos: Vec3 = v3();
  9. private _touchStartPos: Vec3 = v3();
  10. private _isDragging = false;
  11. public targetNode: Node = null!;
  12. // 添加旋转限制属性
  13. @property({type: Number})
  14. public minXRotation: number = -30; // X轴最小旋转角度(上下)
  15. @property({type: Number})
  16. public maxXRotation: number = 30; // X轴最大旋转角度(上下)
  17. @property({type: Number})
  18. public minYRotation: number = -55; // Y轴最小旋转角度(左右)
  19. @property({type: Number})
  20. public maxYRotation: number = 55; // Y轴最大旋转角度(左右)
  21. private _originalRotation: Vec3 = v3();
  22. onLoad(): void {
  23. this._cameraOriginalPos = this.camera.node.position.clone();
  24. this._originalRotation = this.camera.node.eulerAngles.clone();
  25. // 触摸事件监听
  26. input.on(Input.EventType.TOUCH_START, this._onTouchStart, this);
  27. input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  28. input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  29. input.on(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  30. }
  31. start() {
  32. }
  33. /***************************触摸事件**********************************/
  34. private _onTouchStart(event: EventTouch) {
  35. const touchPos = event.getLocation();
  36. this._touchStartPos = v3(touchPos.x, touchPos.y, 0);
  37. this._isDragging = true;
  38. this._originalRotation = this.camera.node.eulerAngles.clone();
  39. }
  40. private _onTouchMove(event: EventTouch) {
  41. if (!this._isDragging) return;
  42. const currentPos = event.getLocation();
  43. const deltaX = currentPos.x - this._touchStartPos.x;
  44. const deltaY = currentPos.y - this._touchStartPos.y;
  45. // 计算新的旋转角度
  46. const newRotation = this._originalRotation.clone();
  47. newRotation.y = this._originalRotation.y - deltaX * 0.2; // 左右滑动控制Y轴旋转
  48. newRotation.x = this._originalRotation.x + deltaY * 0.2; // 上下滑动控制X轴旋转
  49. // 添加旋转限制
  50. newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x));
  51. newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
  52. this.camera.node.setRotationFromEuler(newRotation);
  53. }
  54. private _onTouchEnd() {
  55. this._isDragging = false;
  56. // 不再需要重置_cameraOriginalPos,保持当前相机位置
  57. }
  58. /***************************触摸事件end**********************************/
  59. onDestroy () {
  60. if (this._renderTex) {
  61. this._renderTex.destroy();
  62. this._renderTex = null;
  63. }
  64. }
  65. }