LevelAction.ts 6.6 KB

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