import { _decorator, clamp, macro, sys, Component, Enum, SpriteAtlas, view, EventTouch, Sprite, Vec2, Node, UITransform, SpriteFrame, Vec3, instantiate, input, Input, EventKeyboard, KeyCode, Prefab, RichText, Event, Button, resources, EventMouse, log, math } from 'cc'; const { ccclass, property } = _decorator; // import { _decorator, clamp, macro, sys, Component, Enum, SpriteAtlas, view, EventTouch, Sprite, Vec2, Node, UITransform, SpriteFrame, Vec3, instantiate, input, Input, EventKeyboard, KeyCode, Prefab, RichText, Event, Button, resources, EventMouse, log, math } from 'cc'; // const { ccclass, property } = _decorator; enum MomovingDirectionve { No, X, Y, XY, } @ccclass('ActionController') // 动作控制器 export class ActionController extends Component { @property({ type: Enum(MomovingDirectionve), displayName: "移动方向" }) public direction: MomovingDirectionve = MomovingDirectionve.No; @property({ visible() { return this.direction === MomovingDirectionve.X || this.direction === MomovingDirectionve.XY || this.direction === MomovingDirectionve.Y } }) @property({ displayName: "边缘位置" }) public offset: number = 40; public touchState: boolean = false; public touchStartPos: Vec2 = new Vec2(); private xMin: number = -550; private xMax: number = 600; private yMin: number = 1000; private yMax: number = -1000; targetNode: Node = null; speed: number = 4; private keyDown = {} onLoad() { this.listeningTouch(); this.listeningKey(); this.getAllComponents(); } start() { this.init(); this.checkResolution(); } init() { /* TODO 根据发布的平台自动判断运行逻辑 */ } getAllComponents() { this.targetNode = this.node; } listeningKey() { input.on(Input.EventType.KEY_DOWN, (event) => { this.keyDown[event.keyCode] = true; }, this); input.on(Input.EventType.KEY_UP, (event) => { this.keyDown[event.keyCode] = false; }, this); } listeningTouch() { this.node.on(Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.on(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); } checkResolution() { const viewportRect = view.getViewportRect(); log(`Viewport Rect: ${viewportRect.width} x ${viewportRect.height}`); const visibleSize = view.getVisibleSize(); log(`Visible Size: ${visibleSize.width} x ${visibleSize.height}`); this.xMax = visibleSize.width / 2 - this.offset; this.xMin = -visibleSize.width / 2 + this.offset; this.yMax = visibleSize.height / 2 - this.offset; this.yMin = -visibleSize.height / 2 + this.offset; } update(deltaTime: number) { if (this.keyDown[KeyCode.KEY_A]) { this.moveLeft(); } if (this.keyDown[KeyCode.KEY_D]) { this.moveRight(); } if (this.keyDown[KeyCode.KEY_W]) { this.moveUp(); } if (this.keyDown[KeyCode.KEY_S]) { this.moveDown(); } } onTouchStart(event: EventTouch) { const touchLocation = event.getUILocation(); this.touchStartPos.set(touchLocation.x, touchLocation.y); // 记录初始触摸位置 this.touchState = true; } onTouchMove(event: EventTouch) { const touchLocation = event.getUILocation(); const deltaX = touchLocation.x - this.touchStartPos.x; // 计算手指滑动的X轴距离 const deltaY = touchLocation.y - this.touchStartPos.y; // 计算手指滑动的Y轴距离 this.updatePosition(deltaX, deltaY); // 根据滑动距离更新节点位置 this.touchStartPos.set(touchLocation.x, touchLocation.y); // 更新触摸起点 } onTouchEnd(event: EventTouch) { this.touchState = false; } onTouchCancel(event: EventTouch) { this.touchState = false; } updatePosition(deltaX: number, deltaY: number) { let newPosX = this.targetNode.position.x; let newPosY = this.targetNode.position.y; if (this.direction == MomovingDirectionve.X || this.direction == MomovingDirectionve.XY) { newPosX = clamp(newPosX + deltaX, this.xMin, this.xMax); } if (this.direction == MomovingDirectionve.Y || this.direction == MomovingDirectionve.XY) { newPosY = clamp(newPosY + deltaY, this.yMin, this.yMax); } this.targetNode.setPosition(new Vec3(newPosX, newPosY, 0)); } moveUp() { this.move(0, this.speed); } moveDown() { this.move(0, -this.speed); } moveLeft() { this.move(-this.speed, 0); } moveRight() { this.move(this.speed, 0); } private move(offsetX: number, offsetY: number) { const currentPosition = this.node.position; const newPosition = new Vec3(currentPosition.x + offsetX, currentPosition.y + offsetY, currentPosition.z); this.node.setPosition(newPosition); } onDestroy(): void { //input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this); this.node.off(Node.EventType.TOUCH_START, this.onTouchStart, this); this.node.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this); this.node.off(Node.EventType.TOUCH_END, this.onTouchEnd, this); this.node.off(Node.EventType.TOUCH_CANCEL, this.onTouchCancel, this); } }