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'; import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher'; import { GameEvent } from './Enum/GameEvent'; import { LineDrawer } from './LineDrawer'; import { EnemyComponent } from './Components/EnemyComponent'; const { ccclass, property } = _decorator; enum ERaycastType { ALL, CLOSEST } @ccclass('LevelAction') export class LevelAction extends Component { @property(Camera) public camera: Camera = null!; private _renderTex: RenderTexture | null = null; private _cameraOriginalPos: Vec3 = v3(); private _touchStartPos: Vec3 = v3(); private _isDragging = false; public targetNode: Node = null!; // 添加旋转限制属性 @property({type: Number}) public minXRotation: number = -30; // X轴最小旋转角度(上下) @property({type: Number}) public maxXRotation: number = 30; // X轴最大旋转角度(上下) @property({type: Number}) public minYRotation: number = -70; // Y轴最小旋转角度(左右) @property({type: Number}) public maxYRotation: number = 70; // Y轴最大旋转角度(左右) private _originalRotation: Vec3 = v3(); //镜头拉近属性 private _zoomDuration: number = 0.5; // 拉近持续时间(秒) private _raycastType: ERaycastType = ERaycastType.ALL; onLoad(): void { this._cameraOriginalPos = this.camera.node.position.clone(); this._originalRotation = this.camera.node.eulerAngles.clone(); this.registerEvent(); } start() { } private registerEvent(){ // 触摸事件监听 input.on(Input.EventType.TOUCH_START, this._onTouchStart, this); input.on(Input.EventType.TOUCH_MOVE, this._onTouchMove, this); input.on(Input.EventType.TOUCH_END, this._onTouchEnd, this); input.on(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this); //事件监听 EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this); EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this); EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this); } private unRegisterEvent(){ // 触摸事件监听 input.off(Input.EventType.TOUCH_START, this._onTouchStart, this); input.off(Input.EventType.TOUCH_MOVE, this._onTouchMove, this); input.off(Input.EventType.TOUCH_END, this._onTouchEnd, this); input.off(Input.EventType.TOUCH_CANCEL, this._onTouchEnd, this); //事件监听 EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_AIM,this.onAimTarget,this); EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_RESET_AIM,this.onResetAimTarget,this); EventDispatcher.instance.off(GameEvent.EVENT_CAMERA_SHOOT,this.onShoot,this); } private onAimTarget(){ this.moveCameraAlongForward(-30); // 负值表示拉近 } private onResetAimTarget(){ this.moveCameraAlongForward(30); // 正值表示拉远 } private onShoot(){ // 获取正确的屏幕中心坐标 const screenCenter = view.getVisibleSize(); const screenX = screenCenter.width * 0.5 * view.getScaleX(); const screenY = screenCenter.height * 0.5 * view.getScaleY(); // 从屏幕中心发射射线 const ray = new geometry.Ray(); this.camera.screenPointToRay(screenX, screenY, ray); // 射线检测参数 const mask = 0xffffffff; const maxDistance = 1000; const queryTrigger = true; // 执行射线检测 const hasHit = PhysicsSystem.instance.raycast(ray, mask, maxDistance, queryTrigger); if (hasHit) { const results = PhysicsSystem.instance.raycastResults; for (let i = 0; i < results.length; i++) { const item = results[i]; const hitNode = item.collider.node; console.log(`碰撞物体${i}: ${hitNode.name} 距离: ${item.distance.toFixed(2)}`); if(hitNode.getComponent(EnemyComponent)){ EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_TEXT); EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,hitNode); } } } } private moveCameraAlongForward(distance: number) { const currentPos = this.camera.node.position.clone(); const forward = this.camera.node.forward.negative(); const targetPos = currentPos.add(forward.multiplyScalar(distance)); tween(this.camera.node.position) .to(this._zoomDuration, targetPos, { easing: 'smooth', onUpdate: (target: Vec3) => { this.camera.node.position = target; // 根据距离动态调整旋转限制范围 const zoomFactor = Math.abs(distance) / 30; this.minYRotation = -70 * zoomFactor; this.maxYRotation = 70 * zoomFactor; } }) .start(); } /***************************触摸事件**********************************/ private _onTouchStart(event: EventTouch) { const touchPos = event.getLocation(); this._touchStartPos = v3(touchPos.x, touchPos.y, 0); this._isDragging = true; // 记录初始旋转角度 this._originalRotation = this.camera.node.eulerAngles.clone(); } private _onTouchMove(event: EventTouch) { if (!this._isDragging) return; const currentPos = event.getLocation(); const deltaX = currentPos.x - this._touchStartPos.x; const deltaY = currentPos.y - this._touchStartPos.y; // 根据当前相机距离调整旋转灵敏度 const distanceFactor = Math.max(0.5, this.camera.node.position.length() / 30); const sensitivity = 0.1 * distanceFactor; const rotationDelta = v3( deltaY * sensitivity, -deltaX * sensitivity, 0 ); // 计算新旋转角度 const newRotation = this._originalRotation.clone().add(rotationDelta); // 添加旋转限制 newRotation.x = Math.max(this.minXRotation, Math.min(this.maxXRotation, newRotation.x)); newRotation.y = Math.max(this.minYRotation, Math.min(this.maxYRotation, newRotation.y)); this.camera.node.setRotationFromEuler(newRotation); } private _onTouchEnd() { this._isDragging = false; // 不再需要重置_cameraOriginalPos,保持当前相机位置 } /***************************触摸事件end**********************************/ onDestroy () { Tween.stopAllByTarget(this.node); this.unRegisterEvent(); if (this._renderTex) { this._renderTex.destroy(); this._renderTex = null; } } }