| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 | import { _decorator, Component, Node, EventTouch, math, Rect, UITransform, Sprite, SpriteFrame, Vec2, resources, instantiate, log, Vec3 } from 'cc';import { resMgr } from '../../Frames/ResourcesMgr';import { Role } from './Role';import { dataMgr } from '../../Frames/DataManager';import { RoleData } from '../../DataItem/ItemData';import { ModulerBase } from '../GameFrameWork/ModulerBase';import { MyRole } from './GameScene/MyRole';import { Card } from './Card';import { Bottom } from './GameScene/Bottom';const { ccclass, property } = _decorator;@ccclass('TouchGame')export class TouchGame extends ModulerBase {    private _characterSlot: Node = null;    private _load: Node = null;    private _dragNode: Node = null; // 拖拽节点      private _isDragging: boolean = false; // 拖拽状态        private _imgName: string = null;    private _roleData: RoleData[] = null;    //高亮索引    private _highLightIdx: number = 0;    //高亮高度    private _hight: number = 82;    private _clickedNode: Node = null;    protected onLoad(): void {        this._characterSlot = this.node.parent.getChildByName("CharacterSlot");        this._load = this.node.parent.parent.getChildByName("Road");        this._hight = this._load.getChildByName("Node").getComponent(UITransform).height;        this._roleData = dataMgr.getAllDataByName("RoleCardData");        // 创建拖拽节点          this._dragNode = instantiate(resMgr.getPrefab("DragNode"));        this._dragNode.active = false; // 初始状态隐藏          this._dragNode.parent = this._load;    }    protected start(): void {        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.onTouchEnd, this); // 处理触摸取消事件      }    private onTouchStart(event: EventTouch) {        if (this._isDragging) return;        const pos = event.getUILocation();        for (const element of this._characterSlot.children) {            const box: Rect = element.getComponent(UITransform).getBoundingBoxToWorld();            if (box.contains(pos)) {                this._clickedNode = element;                const clickedNodeCom = this._clickedNode.getComponent(Card);                if(!clickedNodeCom.disabled && clickedNodeCom.clickable){                    const img: SpriteFrame = element.getChildByName("Sprite").getComponent(Sprite).spriteFrame;                    if (!img) {                        this._isDragging = false;                        return;                    }                    if (img) {                        this._isDragging = true;                        this._dragNode.getComponent(Sprite).spriteFrame = img;                        this._dragNode.active = true;                        this._imgName = img.name;                        this.setDragNodePosition(pos);                    }                    break; // 找到一个就停止遍历                 }             }        }    }    private onTouchMove(event: EventTouch) {        if (!this._isDragging) return;        if (this._isDragging) {            this.setDragNodePosition(event.getUILocation());        } else {            return;        }        for (const element of this._load.children) {            if (element.name !== "Roles") {                const box = element.getComponent(UITransform).getBoundingBoxToWorld();                if (box.contains(event.getUILocation())) {                    element.active = true;                } else {                    element.active = false;                }            }        }    }    private onTouchEnd(event: EventTouch) {        if (!this._isDragging) return;        const pos: Vec3 = new Vec3(event.getUILocation().x, event.getUILocation().y, 0)        this.stopDragging(pos);    }    //设置拖拽物体的坐标      private setDragNodePosition(pos: Vec2) {        this._dragNode.setWorldPosition(pos.x, pos.y, 0);    }    private stopDragging(pos: Vec3) {        this._isDragging = false;        for (const element of this._load.children) {            if (element.name !== "Roles") {                const box = element.getComponent(UITransform).getBoundingBoxToWorld();                if (box.contains(new Vec2(pos.x,pos.y))) {                    this._highLightIdx = element.getSiblingIndex();                    break;                }            }        }        const role: Node = instantiate(resMgr.getPrefab("Role"));        role.parent = this._load.getChildByName("Roles");        const roleTS: Role = role.getComponent(MyRole);        const y = 180 + (this._highLightIdx + 1) * this._hight - this._hight / 2;        const rolePos: Vec3 = new Vec3(50, y, pos.z)        roleTS.init(this._imgName, rolePos, this._roleData, 1);        //消耗矿石        this.node.parent.getComponent(Bottom).onReduce();        //冷却当中        this._clickedNode.getComponent(Card).aniPlay();        this._dragNode.active = false;        this._dragNode.getComponent(Sprite).spriteFrame = null;        for (const element of this._load.children) {            if (element.name !== "Roles") {                element.active = false;            }        }    }    protected onDestroy(): void {        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.onTouchEnd, this);    }}
 |