export namespace Utils {
    export function str2arr(str: string, char: string){
        return str.split(char);
    }

    export function createNode(parent: cc.Node, name: string = ''){
        let node = new cc.Node(name);
        parent.addChild(node);
        return node;
    }

    export function intDefault(data, def: number = 0){
        if(data == '') return 0;
        return Number(data);
    }

    // 角度转弧度
    export function radian(degree: number){
        return degree * (Math.PI/180);
    }

    export function degree(radian: number){
        return radian * (180/Math.PI);
    }


    export function getCpt<T extends cc.Component>(cpt: {new(): T}, node: cc.Node): T{
        let moveCpt = node.getComponent(cpt);
        if(!moveCpt) {
            moveCpt = node.addComponent(cpt);
        }
        return moveCpt;
    }

    export function addFullWidget(node: cc.Node){
        let widget = node.addComponent(cc.Widget);
        widget.isAlignBottom = true;
        widget.isAlignTop = true;
        widget.isAlignLeft = true;
        widget.isAlignRight = true;

        widget.left = 0;
        widget.right = 0;
        widget.top = 0;
        widget.bottom = 0;
    }

    export function strReplaceSeparator(str:string){
        return str.replace(/\;|\；/g, ',');
    }

    export function strToJson(str: string): any{
        let jsonStr = strReplaceSeparator(str)
        jsonStr = jsonStr.replace(/\]\[/g, '],[');
        return JSON.parse(jsonStr);
    }

    export function pos2vec2(pos: string): cc.Vec2 {
        let posJson = Utils.strToJson(pos);
        return cc.v2(Number(posJson[0]), -Number(posJson[1]));
    }
}