LevelAction.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { _decorator, BoxCollider2D, Button, Camera, CircleCollider2D, Color, Component, debug, DebugView, director, EventTouch, find, geometry, Input, input, Node, NodeEventType, PhysicsSystem, RenderTexture, Tween, tween, v3, Vec3, view } from 'cc';
  2. import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
  3. import { GameEvent } from './Enum/GameEvent';
  4. import { LineDrawer } from './LineDrawer';
  5. import { EnemyComponent } from './Components/EnemyComponent';
  6. const { ccclass, property } = _decorator;
  7. enum ERaycastType {
  8. ALL,
  9. CLOSEST
  10. }
  11. @ccclass('LevelAction')
  12. export class LevelAction extends Component {
  13. @property(Camera)
  14. public camera: Camera = null!;
  15. private _renderTex: RenderTexture | null = null;
  16. private _cameraOriginalPos: Vec3 = v3();
  17. private _touchStartPos: Vec3 = v3();
  18. private _isDragging = false;
  19. private _isZooming = false;
  20. public targetNode: Node = null!;
  21. // 添加旋转限制属性
  22. @property({type: Number,displayName:"相机X轴最小旋转角度(上下)"})
  23. public minXRotation: number = -30; // X轴最小旋转角度(上下)
  24. @property({type: Number,displayName:"相机X轴最大旋转角度(上下)"})
  25. public maxXRotation: number = 30; // X轴最大旋转角度(上下)
  26. @property({type: Number,displayName:"相机Y轴最小旋转角度(左右)"})
  27. public minYRotation: number = -50; // Y轴最小旋转角度(左右)
  28. @property({type: Number,displayName:"相机Y轴最大旋转角度(左右)"})
  29. public maxYRotation: number = 50; // Y轴最大旋转角度(左右)
  30. private _originalRotation: Vec3 = v3();
  31. //镜头拉近属性
  32. private _zoomDuration: number = 0.5; // 拉近持续时间(秒)
  33. private _raycastType: ERaycastType = ERaycastType.ALL;
  34. onLoad(): void {
  35. this._cameraOriginalPos = this.camera.node.position.clone();
  36. this._originalRotation = this.camera.node.eulerAngles.clone();
  37. this.registerEvent();
  38. }
  39. start() {
  40. }
  41. private registerEvent(){
  42. // 触摸事件监听
  43. input.on(Input.EventType.TOUCH_START, this._onTouchStart, this);
  44. input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  45. input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  46. input.on(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  47. //事件监听
  48. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  49. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  50. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this);
  51. }
  52. private unRegisterEvent(){
  53. // 触摸事件监听
  54. input.off(Input.EventType.TOUCH_START, this._onTouchStart, this);
  55. input.off(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  56. input.off(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  57. input.off(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  58. //事件监听
  59. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  60. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  61. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this);
  62. }
  63. private onAimTarget(){
  64. this.moveCameraAlongForward(-30); // 负值表示拉近
  65. }
  66. private onResetAimTarget(){
  67. this.moveCameraAlongForward(30); // 正值表示拉远
  68. }
  69. private onShoot(){
  70. // 获取正确的屏幕中心坐标
  71. const screenCenter = view.getVisibleSize();
  72. const screenX = screenCenter.width * 0.5 * view.getScaleX();
  73. const screenY = screenCenter.height * 0.5 * view.getScaleY();
  74. // 从屏幕中心发射射线
  75. const ray = new geometry.Ray();
  76. this.camera.screenPointToRay(screenX, screenY, ray);
  77. // 射线检测参数
  78. const mask = 0xffffffff;
  79. const maxDistance = 1000;
  80. const queryTrigger = true;
  81. // 执行射线检测
  82. const hasHit = PhysicsSystem.instance.raycast(ray, mask, maxDistance, queryTrigger);
  83. if (hasHit) {
  84. const results = PhysicsSystem.instance.raycastResults;
  85. for (let i = 0; i < results.length; i++) {
  86. const item = results[i];
  87. const hitNode = item.collider.node;
  88. console.log(`碰撞物体${i}: ${hitNode.name} 距离: ${item.distance.toFixed(2)}`);
  89. if(hitNode.getComponent(EnemyComponent)){
  90. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_TEXT);
  91. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,hitNode);
  92. }
  93. }
  94. }
  95. }
  96. private moveCameraAlongForward(distance: number) {
  97. if(this._isZooming) return;
  98. this._isZooming = true;
  99. const currentPos = this.camera.node.position.clone();
  100. const forward = this.camera.node.forward.negative();
  101. const targetPos = currentPos.add(forward.multiplyScalar(distance));
  102. tween(this.camera.node.position)
  103. .to(this._zoomDuration, targetPos, {
  104. easing: 'smooth',
  105. onUpdate: (target: Vec3) => {
  106. this.camera.node.position = target;
  107. // 根据镜头距离动态调整旋转限制
  108. this.adjustRotationLimits();
  109. this._isZooming = false;
  110. }
  111. })
  112. .start();
  113. }
  114. private adjustRotationLimits() {
  115. // 计算当前镜头距离比例 (0-1范围)
  116. const distanceRatio = Math.min(1, Math.max(0, (this.camera.node.position.z - this._cameraOriginalPos.z) / -30));
  117. // 动态调整旋转限制范围
  118. const dynamicMinY = this.minYRotation * (1 + distanceRatio);
  119. const dynamicMaxY = this.maxYRotation * (1 + distanceRatio);
  120. // 应用调整后的限制
  121. this.minYRotation = dynamicMinY;
  122. this.maxYRotation = dynamicMaxY;
  123. }
  124. /***************************触摸事件**********************************/
  125. private _onTouchStart(event: EventTouch) {
  126. const touchPos = event.getLocation();
  127. this._touchStartPos = v3(touchPos.x, touchPos.y, 0);
  128. this._isDragging = true;
  129. // 记录初始旋转角度
  130. this._originalRotation = this.camera.node.eulerAngles.clone();
  131. }
  132. private _onTouchMove(event: EventTouch) {
  133. if (!this._isDragging) return;
  134. const currentPos = event.getLocation();
  135. const deltaX = currentPos.x - this._touchStartPos.x;
  136. const deltaY = currentPos.y - this._touchStartPos.y;
  137. // 仅计算旋转角度变化
  138. const newRotation = this._originalRotation.clone();
  139. newRotation.y = this._originalRotation.y - deltaX * 0.2;
  140. newRotation.x = this._originalRotation.x + deltaY * 0.2;
  141. // 添加旋转限制
  142. newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x));
  143. newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
  144. this.camera.node.setRotationFromEuler(newRotation);
  145. }
  146. private _onTouchEnd() {
  147. this._isDragging = false;
  148. // 不再需要重置_cameraOriginalPos,保持当前相机位置
  149. }
  150. /***************************触摸事件end**********************************/
  151. onDestroy () {
  152. Tween.stopAllByTarget(this.node);
  153. this.unRegisterEvent();
  154. if (this._renderTex) {
  155. this._renderTex.destroy();
  156. this._renderTex = null;
  157. }
  158. }
  159. }