import { ryw_Event } from "../../FrameWork/Event/EventEnum";
import EventMgr from "../../FrameWork/Event/EventMgr";
import Common5 from "../../Platform/th/Common5";





const { ccclass, property } = cc._decorator;

@ccclass
export default class GoodMoveTouchScript extends cc.Component {
    @property(cc.Node)
    checkNode: cc.Node = null;

    @property({
        displayName: '是否隐藏物品'
    })
    isHideNode: boolean = false

    copyNode:cc.Node = null

    onLoad() {
        this.initTouchEvent(this.node, this.checkNode)

    }

    initTouchEvent(node, checkNode) {
        var attrs = {
            checkNode: checkNode,
            recoveposi: node.getPosition()//初始位置
        };
        node.attr(attrs);
        this.openTouchEvent(node)
    }


    openTouchEvent(node) {
        node.on(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.on(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.on(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.on(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }
    closeTouchEvent(node: cc.Node) {
        node.off(cc.Node.EventType.TOUCH_START, this.touchStartNode, this)
        node.off(cc.Node.EventType.TOUCH_MOVE, this.touchMoveNode, this)
        node.off(cc.Node.EventType.TOUCH_CANCEL, this.touchEndNode, this)
        node.off(cc.Node.EventType.TOUCH_END, this.touchEndNode, this)
    }



    touchStartNode(event) {
        let target = event.target
        this.copyNode = cc.instantiate(target)
        this.copyNode.parent = target.parent
        // Common5.playEffect('点击音效')
    }

    touchMoveNode(event) {

        let posi = event.getLocation()//世界坐标
        let target = event.target
        posi = target.parent.convertToNodeSpaceAR(posi)
        this.copyNode.setPosition(posi)

    }

    touchEndNode(event) {

        let target = event.target

        if (Common5.checkIntersectsBox(this.copyNode, target.checkNode)) {
            if(this.isHideNode){
                this.node.active = false
            }
            EventMgr.emitEvent_custom(ryw_Event.goodTouchMoveCheck, this.node)
        }
        this.copyNode.removeFromParent()
    }


    // update (dt) {}
}