LevelAction.ts 8.6 KB

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