GameProject-xianxia-neidan

--[[
--brief: 内丹系统
--date:  2017-06-20
--author: syl
--modified:
--]]

--[[
-- Conf结构
-- neidanConf =
-- {
--  item =
--  {
--      {
--          add = 100,
--          id = 52003,
--          type = 1
--      }
--      ... 7
--  }
--  level =
--  {
--      {
--          {
--              type = 1,
--              limit = 200
--          }
--          ... 3 - 7
--      }
--      ... 50
--  }
--  totalAttrType = 7 属性种类总数
-- }
--]]

--[[
-- ast结构
-- neidanSys =
-- {
--      attr =
--      {
--         
--          {
--              value = 0
--          },
--          ... 7
--      },
--      level = 1
--      upLevel = false
-- }
--]]

module("systems.neidan.neidan", package.seeall)
require("neidan.neidanconf")

local logger        = Logger.GetLogger("NeidanSystem")
local sysId         = siNeidan
local neidanConf    = neidanConf

local function AddAttr(actor, type, value) -- 添加属性
    LActor.luaAttrInit(actor, LuaAttrDef.NeidanAttr)
    LActor.luaAttrClear(actor, LuaAttrDef.NeidanAttr)
    LActor.luaAttrAdd(actor, LuaAttrDef.NeidanAttr, type, value)
    LActor.refreshActor(actor)
end

local function InitActor(actor)
    local ast = LActor.getStaticVar(actor)

    if not ast.neidanSys then
        ast.neidanSys =
        {
            level = 1,
            upLevel = false
        }
    end
    local neidan = ast.neidanSys
    local levelTotalCount = #neidanConf.level[neidan.level] -- 当前等级的属性种类总数
    if neidan.attr then 
        --print("当前等级[" .. neidan.level .. "]角色属性种类总数" .. #neidan.attr)
        --print("当前等级[" .. neidan.level .. "]配置属性种类总数" .. levelTotalCount)
    end

    if not neidan.attr then
        neidan.attr = {}

        for i = 1, levelTotalCount do
            neidan.attr[i] = { value = 0 }
        end
        return neidan

    elseif #neidan.attr < levelTotalCount then -- 配置文件属性增减处理

        for i = 1, levelTotalCount do

            if not neidan.attr[i] then -- 缺哪个补哪个,配置减少不处理配置的时候赋空值即可
                neidan.attr[i] = { value = 0 }
            end
        end
    end

    return neidan
end

local function OnOneKeyHandler(actor, index)
    --print("----------------OnOneKeyHandler----------------")
    local neidan = InitActor(actor)
    local attrAst = neidan.attr
    local level = neidan.level

    local totalConf = neidanConf.totalAttrType
    local levelConf = neidanConf.level
    local attrConf = levelConf[level]
    --print("index:" .. index)
    if index == 0 then

        for i = 1, #attrConf do
            local limit = attrConf[i].limit
            local type = attrConf[i].type
            local item = neidanConf.item[type]
            local add = item.add
            local id = item.id

            local value = attrAst[i].value
            local surplus = limit - value
            --print("value" .. i .. "数量:" .. value)
            --print("level" .. i .. "数量:" .. level)
            --print("limit" .. i .. "数量:" .. limit)
            --print("surplus" .. i .. "数量:" .. surplus)
            --print("add" .. i .. "数量:" .. add)
            local needNeiDanCount = surplus / add
            local haveNeiDanCount = LActor.getItemCount(actor, id, -1, -1, -1)
            --print("拥有的内丹" .. i .. "数量:" .. haveNeiDanCount)
            --print("需要的内丹" .. i .. "数量:" .. needNeiDanCount)
            if haveNeiDanCount ~= 0 then
                local del = 0

                if haveNeiDanCount > needNeiDanCount then
                    del = needNeiDanCount
                    neidan.upLevel = true
                else
                    del = haveNeiDanCount
                    neidan.upLevel = false
                end
                attrAst[i].value = attrAst[i].value + del * add
                --print("添      加" .. i .. "属性:" .. del * add)
                AddAttr(actor, attrConf[i].type, del * add)
                --print("移除的内丹" .. i .. "数量:" .. del)
                LActor.removeItem(actor, id, del, -1, -1, -1, "neidan_remove")
            else
                neidan.upLevel = false
                SendTipMsg(actor, TipMsgId.tpLua10)
            end
        end
        NetSendInfo(actor)
    end

    if index == -1 then
        --print("突破")
        if neidan.upLevel and neidan.level<= 50 then
            neidan.upLevel = false
            neidan.level = neidan.level + 1
            --print("LevelUp:" .. neidan.level)
            NetSendInfo(actor)
        end
    end
end

----------------Net----------------
function NetSendInfo(actor)
    if not LActor.isActorSystemOpen(actor, sysId) then return end
    --print("NetSendInfo")
    local neidan = InitActor(actor)
    local pack = DataPack.allocPacket(actor, 179, 25)

    DataPack.writeChar(pack, #neidan.attr)
    DataPack.writeInt(pack, neidan.level)

    for i = 1, #neidan.attr do
        DataPack.writeInt(pack, neidan.attr[i].value)
    end
    DataPack.flush(pack)
end

function NetRequInfo(actor, pack) -- 全部 和 一个
    if not LActor.isActorSystemOpen(actor, sysId) then return end
    local index = DataPack.readChar(pack)

    OnOneKeyHandler(actor, index)
end

----------------Gm----------------
local function GmClear(actor, args)
    local neidan = InitActor(actor)
    local level = neidan.level

    local levelConf = neidanConf.level
    local attrConf = levelConf[level]


    for i = 1, #attrConf do
        neidan.attr[i].value = 0
    end
end

local function GmPrint(actor, args)
    local neidan = InitActor(actor)
    local level = neidan.level

    local levelConf = neidanConf.level
    local attrConf = levelConf[level]

    for i = 1, #attrConf do
        print("value[" .. i .. "]:" .. neidan.attr[i].value)
    end
end

local function GmOpen(actor)
    logger:Info("NeiDanSystemOpen")
    OnOpenSystem(actor, {sysId})
end

local function GmAdd(actor, args)
    AddAttr(actor, args[1], args[2])
end
----------------Sys----------------
function OnOpenSystem(actor, args)
    InitActor(actor)
    NetSendInfo(actor)
end

function OnLogin(actor)
    -- RefreshAttr(actor)
end

local function OnInit()
    ActorRegisterNetHandler(179, 25, NetRequInfo)

    GmEventFuncList.register("neidan.clear", GmClear, 1)
    GmEventFuncList.register("neidan.print", GmPrint, 1)
    GmEventFuncList.register("neidan.open", GmOpen, 1)
    GmEventFuncList.register("neidan.add", GmAdd, 1)

    RegisterSystem(siNeidan, _M)
end

table.insert(InitFnTable, OnInit) -- @proto 179 25 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值