LevelAction.ts 8.4 KB

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