LevelAction.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { _decorator, BoxCollider2D, Button, Camera, CircleCollider2D, Collider2D, Component, EventTouch, find, Input, input, Node, NodeEventType, RenderTexture, 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 = -55; // Y轴最小旋转角度(左右)
  21. @property({type: Number})
  22. public maxYRotation: number = 55; // Y轴最大旋转角度(左右)
  23. private _originalRotation: Vec3 = v3();
  24. onLoad(): void {
  25. this._cameraOriginalPos = this.camera.node.position.clone();
  26. this._originalRotation = this.camera.node.eulerAngles.clone();
  27. this.registerEvent();
  28. }
  29. start() {
  30. }
  31. private registerEvent(){
  32. // 触摸事件监听
  33. input.on(Input.EventType.TOUCH_START, this._onTouchStart, this);
  34. input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  35. input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  36. input.on(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  37. //事件监听
  38. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  39. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  40. }
  41. private unRegisterEvent(){
  42. // 触摸事件监听
  43. input.off(Input.EventType.TOUCH_START, this._onTouchStart, this);
  44. input.off(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  45. input.off(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  46. input.off(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  47. //事件监听
  48. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  49. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  50. }
  51. private onAimTarget(){
  52. console.log(`拉近相机镜头!`);
  53. }
  54. private onResetAimTarget(){
  55. console.log(`重置相机镜头!`);
  56. }
  57. /***************************触摸事件**********************************/
  58. private _onTouchStart(event: EventTouch) {
  59. const touchPos = event.getLocation();
  60. this._touchStartPos = v3(touchPos.x, touchPos.y, 0);
  61. this._isDragging = true;
  62. this._originalRotation = this.camera.node.eulerAngles.clone();
  63. }
  64. private _onTouchMove(event: EventTouch) {
  65. if (!this._isDragging) return;
  66. const currentPos = event.getLocation();
  67. const deltaX = currentPos.x - this._touchStartPos.x;
  68. const deltaY = currentPos.y - this._touchStartPos.y;
  69. // 计算新的旋转角度
  70. const newRotation = this._originalRotation.clone();
  71. newRotation.y = this._originalRotation.y - deltaX * 0.2; // 左右滑动控制Y轴旋转
  72. newRotation.x = this._originalRotation.x + deltaY * 0.2; // 上下滑动控制X轴旋转
  73. // 添加旋转限制
  74. newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x));
  75. newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
  76. this.camera.node.setRotationFromEuler(newRotation);
  77. }
  78. private _onTouchEnd() {
  79. this._isDragging = false;
  80. // 不再需要重置_cameraOriginalPos,保持当前相机位置
  81. }
  82. /***************************触摸事件end**********************************/
  83. onDestroy () {
  84. this.unRegisterEvent();
  85. if (this._renderTex) {
  86. this._renderTex.destroy();
  87. this._renderTex = null;
  88. }
  89. }
  90. }