LevelAction.ts 10 KB

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