CharmInfo.lua 1.56 KB
cc.exports.CharmInfo = {}

local _map = {};

--魅力值配置表信息
local function init()
    local configStr = cc.FileUtils:getInstance():getStringFromFile("res/config/gd_charm.txt");

    local function readLineFun(lineStr)
        local info;
        local contents = StringUtil.split(lineStr,'\t');
        if #contents > 1 then
            info = {};
            info.level = tonumber(contents[1]);
            info.charm = tonumber(contents[2]);
            info.name = contents[3];
        end
        return info;
    end

    local index = 1;
    local function readLine(str)
        local a,b = string.find(str,"\r\n");
        if a then
            local lineStr = string.sub(str,1,a-1);

            if index > 2 then
                local info = readLineFun(lineStr);
                if info then
                    _map[info.level] = info;
                end
            end

            index = index + 1;
            str = string.sub(str,b+1);
            readLine(str);
        else
            local info = readLineFun(str);
            if info then
                _map[info.level] = info;
            end
        end
    end
    readLine(configStr);
end
init();

--通过级别获取信息
function CharmInfo.getCharmInfo(level)
    return _map[level];
end

--通过魅力值获取信息
function CharmInfo.getInfoByCharm(charm)
    local info = {level=0,charm=0,name=""};
    for i,v in ipairs(_map) do
        if charm >= v.charm then
            info = v;
        end
    end
    if not info then
        info = _map[#_map];
    end
    return info;
end

return CharmInfo