--[[
--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
GameProject-xianxia-neidan
最新推荐文章于 2025-12-11 15:33:51 发布
本文介绍了一个游戏中的内丹系统,详细解析了系统的核心结构和功能实现,包括属性配置、角色属性初始化、升级处理等关键流程。
1122

被折叠的 条评论
为什么被折叠?



