
/*
* 公共接口
*/
export module GameUtils {
    /**
     * 随机函数(返回整型)
     */
    export function randRangeint(min: number, max: number): number  {
        var value:number=Math.floor(Math.random() * (max - min + 1)) + min;
        if(value>max){
            value = max;
        }
        return value;
    }

    /**
     * 随机函数( 不取整 )
     */
    export function randRangefloat(min: number, max: number): number  {
        var value:number = Math.random() * (max - min + 1) + min;
        if(value>max){
            value = max;
        }
        return value;
    }

    export function getTexbyPkgAndName( pkgname:string,texname:string ):Laya.Texture
    {
        var uipackage:fairygui.UIPackage = fairygui.UIPackage.getByName(pkgname);
        if( uipackage )
        {
            return uipackage.getItemAssetByName(texname) as Laya.Texture;
        }
        return null;
    }

    /**
     * 切割分号变成Point对象
     * @param str 
     */
    export function cutfenhaoPoint( str:string ):Laya.Point
    {
        var arr:Array<string> = str.split(";");
        return new Laya.Point( Number( arr[0] ),Number( arr[1] ) );
    }

    /**
     * 切割分号变成数据(number)
     */
    export function cutfenhaotoArrnum( str:string ):Array<number>
    {
        var numArr:Array<number>=[];
        if (str == "")
        {
            return numArr;
        }
        var arr:Array<string> = str.split(";");
        var i:number = 0;
        for( i;i<arr.length;i++ )
        {
            numArr.push( Number( arr[i] ) );
        }
        return numArr;
    }

    /**
     * 切割分号变成数据(string)
     */
    export function cutfenhaotoArrstr( str:string ):Array<string>
    {
        return str.split(";");
    }

    /**
     * 切割分号变成数据( { key:1,value:1 } )
     */
    export function cutfenhaotoObj( str:string ):any
    {
        var obj:any = {};
        var arr:Array<string> = str.split( ";" );
        obj.key = arr[0];
        obj.value = arr[1];
        return obj;
    }

    /**切割str*/
    export function cutstrtoArr( str:string ):Array<string>
    {
        var arr:Array<string> = str.split( "|" );
        return arr;
    }

    /**切割str( 1:1|1:1 ) to Array<{ key:1,value:1 }> */
    export function cutstrtoArr1( str:string ):Array<any>
    {
        var reslutArr:Array<any>=[];
        var arr:Array<string> = str.split( "|" );
        var i:number = 0;
        for( i=0;i<arr.length;i++ )
        {
            var itemarr:Array<string> = arr[i].split( ";" );
            reslutArr.push( { key:itemarr[0],value:itemarr[1] } );
        }
        return reslutArr;
    }

    export function cutstrtoObj( str:string ):any
    {
        var resultobj:any = {};
        var arr:Array<string> = str.split( "|" );
        var i:number = 0;
        for( i=0;i<arr.length;i++ )
        {
            var itemarr:Array<string> = arr[i].split( ";" );
            resultobj[itemarr[0]] = itemarr[1];
        }
        return resultobj;
    }

    export function compareVersion(v1, v2):number
    {
        v1 = v1.split('.');
        v2 = v2.split('.');
        const len = Math.max(v1.length, v2.length);
        while (v1.length < len) {
            v1.push('0');
        }
        while (v2.length < len) {
            v2.push('0');
        }
        for (var i = 0; i < len; i++) {
            const num1 = parseInt(v1[i]);
            const num2 = parseInt(v2[i]);
            if (num1 > num2) {
                return 1;
            } else if (num1 < num2) {
                return -1;
            }
        }
        return 0;
    }
}