LevelAction.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { _decorator, BoxCollider2D, Button, Camera, CCFloat, CircleCollider2D, Color, Component, debug, DebugView, director, EventTouch, find, geometry, Input, input, math, Node, NodeEventType, PhysicsSystem, Quat, 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. import { tgxUIMgr } from '../../core_tgx/tgx';
  11. import { UI_BattleGambit } from '../../scripts/UIDef';
  12. const { ccclass, property } = _decorator;
  13. //动画时长
  14. export const ANIMATION_DURATION = 0.5;
  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 isTweening: boolean = false;
  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. const match = tgxUIMgr.inst.isShowing(UI_BattleGambit);
  54. if (!match) {
  55. tgxUIMgr.inst.showUI(UI_BattleGambit);
  56. }
  57. }
  58. private registerEvent(){
  59. // 触摸事件监听
  60. input.on(Input.EventType.TOUCH_START, this._onTouchStart, this);
  61. input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  62. input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  63. input.on(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  64. //事件监听
  65. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  66. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  67. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this);
  68. EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK,this.onCameraToTarget,this);
  69. }
  70. private unRegisterEvent(){
  71. // 触摸事件监听
  72. input.off(Input.EventType.TOUCH_START, this._onTouchStart, this);
  73. input.off(Input.EventType.TOUCH_MOVE, this._onTouchMove, this);
  74. input.off(Input.EventType.TOUCH_END, this._onTouchEnd, this);
  75. input.off(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this);
  76. //事件监听
  77. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this);
  78. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this);
  79. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this);
  80. EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SCREENSHOT_RADAR_LOCK,this.onCameraToTarget,this);
  81. }
  82. private onAimTarget(){
  83. this.moveCameraAlongForward(-30); // 负值表示拉近
  84. }
  85. private onResetAimTarget(){
  86. this.moveCameraAlongForward(30); // 正值表示拉远
  87. }
  88. private onShoot(){
  89. // 获取正确的屏幕中心坐标
  90. const screenCenter = view.getVisibleSize();
  91. const screenX = screenCenter.width * 0.5 * view.getScaleX();
  92. const screenY = screenCenter.height * 0.5 * view.getScaleY();
  93. // 从屏幕中心发射射线
  94. const ray = new geometry.Ray();
  95. this.camera.screenPointToRay(screenX, screenY, ray);
  96. // 射线检测参数
  97. const mask = 0xffffffff;
  98. const maxDistance = 1000;
  99. const queryTrigger = true;
  100. // 执行射线检测
  101. const hasHit = PhysicsSystem.instance.raycast(ray, mask, maxDistance, queryTrigger);
  102. if (hasHit) {
  103. const results = PhysicsSystem.instance.raycastResults;
  104. for (let i = 0; i < results.length; i++) {
  105. const item = results[i];
  106. const hitNode = item.collider.node;
  107. // console.log(`碰撞物体${i}: ${hitNode.name} 距离: ${item.distance.toFixed(2)}`);
  108. if(hitNode.getComponent(EnemyComponent)){
  109. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_TEXT);
  110. EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,hitNode);
  111. }
  112. }
  113. }
  114. }
  115. private moveCameraAlongForward(distance: number) {
  116. if(this._isZooming) return;
  117. this._isZooming = true;
  118. const currentPos = this.camera.node.position.clone();
  119. const forward = this.camera.node.forward.negative();
  120. const targetPos = currentPos.add(forward.multiplyScalar(distance));
  121. tween(this.camera.node.position)
  122. .to(ANIMATION_DURATION, targetPos, {
  123. easing: 'smooth',
  124. onUpdate: (target: Vec3) => {
  125. this.camera.node.position = target;
  126. // 根据镜头距离动态调整旋转限制
  127. this.adjustRotationLimits();
  128. this._isZooming = false;
  129. }
  130. })
  131. .start();
  132. }
  133. //相机转向目标
  134. private async onCameraToTarget(targetNode: Node){
  135. const camera = this.camera;
  136. if (!targetNode || !camera) return;
  137. const targetPos = new Vec3();
  138. targetNode.getWorldPosition(targetPos);
  139. // 获取相机位置
  140. const cameraPos = new Vec3();
  141. camera.node.getWorldPosition(cameraPos);
  142. // 计算从相机到目标的方向向量
  143. const direction = new Vec3();
  144. Vec3.subtract(direction, targetPos, cameraPos);
  145. direction.normalize();
  146. // 计算目标欧拉角
  147. const targetYaw = math.toDegree(Math.atan2(-direction.x, -direction.z));
  148. const targetPitch = math.toDegree(Math.asin(direction.y));
  149. // 获取当前欧拉角
  150. const currentRotation = camera.node.eulerAngles.clone();
  151. // 创建一个对象用于tween
  152. const tweenObj = {
  153. pitch: currentRotation.x,
  154. yaw: currentRotation.y
  155. };
  156. this.isTweening = true;
  157. tween(tweenObj)
  158. .to(ANIMATION_DURATION, {
  159. pitch: targetPitch,
  160. yaw: targetYaw
  161. }, {
  162. easing: 'smooth',
  163. onUpdate: () => {
  164. // 更新相机旋转
  165. camera.node.setRotationFromEuler(tweenObj.pitch, tweenObj.yaw, 0);
  166. },
  167. onComplete: () => {
  168. this.isTweening = false;
  169. }
  170. })
  171. .start();
  172. }
  173. private adjustRotationLimits() {
  174. // 计算当前镜头距离比例 (0-1范围)
  175. const distanceRatio = Math.min(1, Math.max(0, (this.camera.node.position.z - this._cameraOriginalPos.z) / -30));
  176. // 动态调整旋转限制范围
  177. const dynamicMinY = this.minYRotation * (1 + distanceRatio);
  178. const dynamicMaxY = this.maxYRotation * (1 + distanceRatio);
  179. // 应用调整后的限制
  180. this.minYRotation = dynamicMinY;
  181. this.maxYRotation = dynamicMaxY;
  182. }
  183. /***************************触摸事件**********************************/
  184. private _onTouchStart(event: EventTouch) {
  185. const touchPos = event.getLocation();
  186. this._touchStartPos = v3(touchPos.x, touchPos.y, 0);
  187. this._isDragging = true;
  188. // 记录初始旋转角度
  189. this._originalRotation = this.camera.node.eulerAngles.clone();
  190. const radarComponent = AliensGlobalInstance.instance.renderNode.getComponent(RadarComponent)!;
  191. if(radarComponent){
  192. radarComponent.unlockPositionUpdate();
  193. }
  194. }
  195. private async _onTouchMove(event: EventTouch) {
  196. if (!this._isDragging) return;
  197. const currentPos = event.getLocation();
  198. const deltaX = currentPos.x - this._touchStartPos.x;
  199. const deltaY = currentPos.y - this._touchStartPos.y;
  200. // 仅计算旋转角度变化
  201. const newRotation = this._originalRotation.clone();
  202. newRotation.y = this._originalRotation.y - deltaX * 0.2;
  203. newRotation.x = this._originalRotation.x + deltaY * 0.2;
  204. // 添加旋转限制
  205. newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x));
  206. newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y));
  207. this.camera.node.setRotationFromEuler(newRotation);
  208. await this.saveCameraState();
  209. }
  210. //保存相机的位置和旋转角度
  211. private async saveCameraState() {
  212. const cameraOriginalPos = this.camera.node.worldPosition.clone();
  213. const originalRotation = this.camera.node.eulerAngles.clone();
  214. const screenShot = AliensGlobalInstance.instance.renderNode.getComponent(ScreenShotComponent)!;
  215. screenShot.saveCameraState(cameraOriginalPos,originalRotation);
  216. }
  217. private _onTouchEnd() {
  218. this._isDragging = false;
  219. const radarComponent = AliensGlobalInstance.instance.renderNode.getComponent(RadarComponent)!;
  220. if(radarComponent){
  221. radarComponent.unlockPositionUpdate();
  222. }
  223. }
  224. /***************************触摸事件end**********************************/
  225. onDestroy () {
  226. Tween.stopAllByTarget(this.node);
  227. this.unRegisterEvent();
  228. if (this._renderTex) {
  229. this._renderTex.destroy();
  230. this._renderTex = null;
  231. }
  232. }
  233. }