LevelAction.ts 8.5 KB

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