123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- import { _decorator, BoxCollider2D, Button, Camera, CircleCollider2D, Collider2D, Component, EventTouch, find, Input, input, Node, NodeEventType, RenderTexture, v3, Vec3 } from 'cc';
- import { EventDispatcher } from '../../core_tgx/easy_ui_framework/EventDispatcher';
- import { GameEvent } from './Enum/GameEvent';
- const { ccclass, property } = _decorator;
- @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 = -55; // Y轴最小旋转角度(左右)
- @property({type: Number})
- public maxYRotation: number = 55; // Y轴最大旋转角度(左右)
- private _originalRotation: Vec3 = v3();
- 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);
- }
- 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);
- }
- private onAimTarget(){
- console.log(`拉近相机镜头!`);
- }
- private onResetAimTarget(){
- console.log(`重置相机镜头!`);
- }
- /***************************触摸事件**********************************/
- 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 newRotation = this._originalRotation.clone();
- newRotation.y = this._originalRotation.y - deltaX * 0.2; // 左右滑动控制Y轴旋转
- newRotation.x = this._originalRotation.x + deltaY * 0.2; // 上下滑动控制X轴旋转
-
- // 添加旋转限制
- 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 () {
- this.unRegisterEvent();
- if (this._renderTex) {
- this._renderTex.destroy();
- this._renderTex = null;
- }
- }
- }
|