Lua 游戏 实现合并同类物品
发布日期:2021-11-13 20:28:11
浏览次数:3
分类:技术文章
本文共 2743 字,大约阅读时间需要 9 分钟。
``
--根据type和id生成一个唯一的索引 local function generateIndex( ty,id ) assert(type(ty) == "number") assert(type(id) == "number") return ty * 1000000 + id end local function ungenerateIndex( index ) local ty = math.modf( index / 1000000 ) -- 取整数 local id = math.fmod( index, 1000000 ) -- 取余数 return ty,id end --合并奖励 function mergeDropAward(awards) assert(type(awards) == "table") if #awards <= 1 then return awards end -- 合并 local temp = { } local tempx = { } local pos = 0 for _, award in ipairs(awards) do assert(type(award) == "table") local index = generateIndex( award.type,award.id ) local p = temp[index] if p then local re = tempx[p] re.count = re.count + award.count else table.insert(tempx,{ type = award.type,id = award.id,count = award.count}) pos = pos + 1 temp[index] = pos end end return tempx end local awards = { { type = 1,id = 1,count = 10}, { type = 2,id = 1,count = 10}, { type = 1,id = 1,count = 10}, { type = 4,id = 1,count = 10}, { type = 4,id = 1,count = 10}, { type = 1,id = 1,count = 10}, } local rewards = mergeDropAward(awards) for _,v in ipairs(rewards) do print(v.type,v.id,v.count) end
当然还有一种更巧妙的方式来实现同类物品合并,那就是元表,下面就用元表来实现以下
local function _copyTable(s, t) for k, v in pairs(s) do if type(v) == "table" then t[k] = { } _copyTable(v, t[k]) else t[k] = v end endendfunction copyTable(s) if type(s) ~= "table" then return s end local t = { } _copyTable(s, t) return tendfunction getArrayPlus(addKey) local pluskey = addKey local recordkey = { } local function _GetTick(tb) local tick = { } for k, v in pairs(tb) do if k ~= pluskey then table.insert(tick, v) end end return table.concat(tick, ".") end local function _Merger(sourceTb, newTb) for __,v in pairs(newTb) do if type(v) == "table" then _Merger(sourceTb, v) else local tick = _GetTick(newTb) local index = recordkey[tick] if index then sourceTb[index][pluskey] = sourceTb[index][pluskey] + newTb[pluskey] else table.insert(sourceTb, copyTable(newTb)) recordkey[tick] = #sourceTb end break end end end local operateTb = setmetatable({ }, { __add = function(sourceTb, addTb) addTb = addTb or { } _Merger(sourceTb, addTb) return sourceTb end}) return operateTbendlocal addtab = getArrayPlus("count") for _,v in ipairs(rewardslist) do addtab = addtab + vendfor _,v in ipairs(addtab) do print(v.type,v.id,v.count)end
如有任何疑问,请加群641792143交流与学习
转载地址:https://blog.csdn.net/lpl312905509/article/details/100195498 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年04月14日 11时17分39秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Druid连接池实现自定义场景的多数据库的连接
2019-04-27
CentOs7命令行(静默)的方式安装oracle数据库
2019-04-27
基于VMware安装CentOs7的镜像
2019-04-27
PL/SQL数据库管理工具的使用
2019-04-27
史上最简单的spring-boot集成websocket的实现方式
2019-04-27
带你玩转属于自己的spring-boot-starter系列(一)
2019-04-27
带你玩转属于自己自己的spring-boot-starter系列(二)
2019-04-27
带你玩转属于自己的spring-boot-starter系列(三)
2019-04-27
基于SnowFlake算法如何让分库分表中不同的ID落在同一个库的算法的实现
2019-04-27
Linux文件管理参考
2019-04-27
FTP文件管理项目(本地云)项目日报(一)
2019-04-27
FTP文件管理项目(本地云)项目日报(二)
2019-04-27
FTP文件管理项目(本地云)项目日报(三)
2019-04-27
FTP文件管理项目(本地云)项目日报(四)
2019-04-27
【C++】勉强能看的线程池详解
2019-04-27