LevelAction.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { _decorator, BoxCollider2D, Button, Camera, CircleCollider2D, Collider2D, Component, EventTouch, find, Input, input, Node, NodeEventType, RenderTexture, Tween, tween, v3, Vec3 } from 'cc';
  2. import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
  3. import { GameEvent } from './Enum/GameEvent';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('LevelAction')
  6. export class LevelAction extends Component {
  7. @property(Camera)
  8. public camera: Camera = null!;
  9. private _renderTex: RenderTexture | null = null;
  10. private _cameraOriginalPos: Vec3 = v3();
  11. private _touchStartPos: Vec3 = v3();
  12. private _isDragging = false;
  13. public targetNode: Node = null!;
  14. // 添加旋转限制属性
  15. @property({type: Number})
  16. public minXRotation: number = -30; // X轴最小旋转角度(上下)
  17. @property({type: Number})
  18. public maxXRotation: number = 30; // X轴最大旋转角度(上下)
  19. @property({type: Number})
  20. public minYRotation: number = -70; // Y轴最小旋转角度(左右)
  21. @property({type: Number})
  22. public maxYRotation: number = 70; // Y轴最大旋转角度(左右)
  23. private _originalRotation: Vec3 = v3();
  24. //镜头拉近属性
  25. private _zoomDuration: number = 0.5; // 拉近持续时间(秒)
  26. onLoad(): void {
  27. this._cameraOriginalPos = this.camera.node.position.clone();
  28. this._originalRotation = this.camera.node.eulerAngles.clone();
  29. this.registerEvent();
  30. }
  31. start() {
  32. }
  33. private registerEvent(){
  34. // 触摸事件监听
  35. input.on(Input.EventType.TOUCH_START, this._onTouchStart, this);
  36. input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  37. input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  38. input.on(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  39. //事件监听
  40. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  41. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  42. }
  43. private unRegisterEvent(){
  44. // 触摸事件监听
  45. input.off(Input.EventType.TOUCH_START, this._onTouchStart, this);
  46. input.off(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  47. input.off(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  48. input.off(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  49. //事件监听
  50. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  51. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  52. }
  53. private onAimTarget(){
  54. this.moveCameraAlongForward(-30); // 负值表示拉近
  55. }
  56. private onResetAimTarget(){
  57. this.moveCameraAlongForward(30); // 正值表示拉远
  58. }
  59. private moveCameraAlongForward(distance: number) {
  60. const currentPos = this.camera.node.position.clone();
  61. const forward = this.camera.node.forward.negative();
  62. const targetPos = currentPos.add(forward.multiplyScalar(distance));
  63. tween(this.camera.node.position)
  64. .to(this._zoomDuration, targetPos, {
  65. easing: 'smooth',
  66. onUpdate: (target: Vec3) => {
  67. this.camera.node.position = target;
  68. }
  69. })
  70. .start();
  71. }
  72. /***************************触摸事件**********************************/
  73. private _onTouchStart(event: EventTouch) {
  74. const touchPos = event.getLocation();
  75. this._touchStartPos = v3(touchPos.x, touchPos.y, 0);
  76. this._isDragging = true;
  77. this._originalRotation = this.camera.node.eulerAngles.clone();
  78. }
  79. private _onTouchMove(event: EventTouch) {
  80. if (!this._isDragging) return;
  81. const currentPos = event.getLocation();
  82. const deltaX = currentPos.x - this._touchStartPos.x;
  83. const deltaY = currentPos.y - this._touchStartPos.y;
  84. // 计算新的旋转角度
  85. const newRotation = this._originalRotation.clone();
  86. newRotation.y = this._originalRotation.y - deltaX * 0.2; // 左右滑动控制Y轴旋转
  87. newRotation.x = this._originalRotation.x + deltaY * 0.2; // 上下滑动控制X轴旋转
  88. // 添加旋转限制
  89. newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x));
  90. newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
  91. this.camera.node.setRotationFromEuler(newRotation);
  92. }
  93. private _onTouchEnd() {
  94. this._isDragging = false;
  95. // 不再需要重置_cameraOriginalPos,保持当前相机位置
  96. }
  97. /***************************触摸事件end**********************************/
  98. onDestroy () {
  99. Tween.stopAllByTarget(this.node);
  100. this.unRegisterEvent();
  101. if (this._renderTex) {
  102. this._renderTex.destroy();
  103. this._renderTex = null;
  104. }
  105. }
  106. }