制作人在那个网站能看,深圳seo优化外包,仿门户网站,最近一周新闻热点大事件饥荒Mod 开发(二一)#xff1a;超大便携背包#xff0c;超大物品栏#xff0c;永久保鲜 饥荒中的物品没有详细信息#xff0c;基本上只有一个名字#xff0c;所以很多物品的功能都不知道#xff0c;比如浆果吃了也不知道恢复什么#xff0c; 采集的胡萝卜也不知道什么功…饥荒Mod 开发(二一)超大便携背包超大物品栏永久保鲜 饥荒中的物品没有详细信息基本上只有一个名字所以很多物品的功能都不知道比如浆果吃了也不知道恢复什么 采集的胡萝卜也不知道什么功效可真是太不方便了所以当我们把鼠标放在物品上面时需要显示物品的详细信息。
原理
widgets/hoverer 类用来显示鼠标悬浮的提示所以我们需要拦截这个 悬浮的创建设置需要显示的内容
显示自定义提示
在modmain.lua 文件中添加下面代码用来拦截 widgets/hoverer 创建然后重写 SetString 方法 local round2 function(num, idp)return GLOBAL.tonumber(string.format(%. .. (idp or 0) .. f, num))
end
--鼠标悬浮在物品上显示信息
AddClassPostConstruct(widgets/hoverer,function(self)local old_SetString self.text.SetStringself.text.SetString function(text,str)-- 获取鼠标下的世界实体local target GLOBAL.TheInput:GetWorldEntityUnderMouse() -- 如果存在目标实体if target then-- 如果目标实体有预制体if target.prefab then-- 在字符串后添加预制体的代码str str .. \n代码: .. target.prefabend-- 如果目标实体有可旅行组件if target.components.travelable then-- 在字符串后添加可旅行组件的名称str str ..\n.. tostring(target.components.travelable.name)endif target.components then-- 如果目标实体有生命组件if target.components.health then-- 在字符串后添加生物的血量str str..\n..math.ceil(target.components.health.currenthealth*10)/10 ../..math.ceil(target.components.health.maxhealth*10)/10end-- 如果目标实体有战斗组件并且默认伤害大于0if target.components.combat and target.components.combat.defaultdamage 0 then-- 在字符串后添加生物的攻击力str str..\n攻击力: ..target.components.combat.defaultdamageend-- 如果目标实体是温度计if target.prefab winterometer then-- 获取当前温度local temp GLOBAL.GetSeasonManager() and GLOBAL.GetSeasonManager():GetCurrentTemperature() or 30local high_temp TUNING.OVERHEAT_TEMPlocal low_temp 0-- 限制温度在最高温度和最低温度之间temp math.min( math.max(low_temp, temp), high_temp)-- 在字符串后添加温度str str..\n温度: .. tostring(math.floor(temp)) .. \176Cend-- 检查目标实体是否有库存组件if target.components.inventory then-- 获取目标实体手部装备的物品local handitem target.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HANDS)if handitem then-- 如果有手部装备的物品可以在这里添加相关的处理代码end-- 获取目标实体头部装备的物品local headitem target.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.HEAD)if headitem then-- 如果头部装备的物品有防具组件if headitem.components.armor then-- 在字符串后添加头部防御的信息str str..\n头部防御: ..headitem.components.armor.absorb_percent*100 ..%-- 在字符串后添加头部装备的耐久信息str str.. 耐久: ..math.floor(headitem.components.armor:GetPercent() *100)..%endend-- 获取目标实体身体装备的物品local bodyitem target.components.inventory:GetEquippedItem(GLOBAL.EQUIPSLOTS.BODY)if bodyitem then-- 如果身体装备的物品有防具组件if bodyitem.components.armor then-- 在字符串后添加身体防御的信息str str..\n身体防御: ..bodyitem.components.armor.absorb_percent*100 ..%-- 在字符串后添加身体装备的耐久信息str str.. 耐久: ..math.floor(bodyitem.components.armor:GetPercent() *100)..%endendend-- 检查目标实体是否可以被驯养if target.components.domesticatable ~ nil then-- 如果目标实体有驯养和顺从的方法if target.components.domesticatable.GetDomestication and target.components.domesticatable.GetObedience ~ nil then-- 获取目标实体的饥饿值local hunger target.components.hunger.current-- 获取目标实体的顺从值local obedience target.components.domesticatable:GetObedience()-- 获取目标实体的驯养值local domestication target.components.domesticatable:GetDomestication()-- 如果驯养值不为0if domestication ~ 0 then-- 在字符串后添加饥饿、顺从和驯养的信息str str..\n饥饿: ..round2(hunger)..\n顺从: ..round2(obedience*100,0)..%..\n驯服: ..round2(domestication*100,0)..%end-- 遍历目标实体的倾向for k,v in pairs(target.components.domesticatable.tendencies) do-- 默认倾向为默认local ten 默认-- 如果倾向为ORNERY则设置为战牛if k GLOBAL.TENDENCY.ORNERY thenten 战牛-- 如果倾向为RIDER则设置为行牛elseif k GLOBAL.TENDENCY.RIDER thenten 行牛-- 如果倾向为PUDGY则设置为肥牛elseif k GLOBAL.TENDENCY.PUDGY thenten 肥牛end-- 在字符串后添加倾向的信息str str .. string.format(\n %s:%.2f, ten, v)endendend-- 检查目标实体是否可以被采摘并且有目标时间if target.components.pickable and target.components.pickable.targettime then-- 在字符串后添加距离成长的时间树枝、草、浆果、咖啡树str str ..\n距离成长: .. tostring(math.ceil((target.components.pickable.targettime - GLOBAL.GetTime())/48)/10) .. 天end-- 检查目标实体是否可以被砍伐并且有目标时间if target.components.hackable and target.components.hackable.targettime then-- 在字符串后添加距离成长的时间藤蔓、竹林str str..\n距离成长: ..tostring(math.ceil((target.components.hackable.targettime - GLOBAL.GetTime())/48)/10).. 天end-- 检查目标实体是否可以被部署并且有生长时间if target.components.deployable and target.growtime then-- 在字符串后添加树苗的生长时间str str..\n树苗: ..tostring(math.ceil((target.growtime - GLOBAL.GetTime())/48)/10).. 天end-- 检查目标实体是否可以成长并且有目标时间if target.components.growable and target.components.growable.targettime then-- 在字符串后添加下一阶段的时间树str str..\n下一阶段: ..tostring( math.ceil((target.components.growable.targettime - GLOBAL.GetTime())/48)/10).. 天end-- 检查目标实体是否有晾肉架组件并且正在晾肉if target.components.dryer and target.components.dryer:IsDrying() then-- 如果正在晾肉并且有获取晾肉时间的方法if target.components.dryer:IsDrying() and target.components.dryer.GetTimeToDry then-- 在字符串后添加剩余的晾肉时间str str..\n剩余: ..round2((target.components.dryer:GetTimeToDry()/TUNING.TOTAL_DAY_TIME)0.1,1).. 天endend-- 检查目标实体是否有烹饪组件并且烹饪时间大于0if target.components.stewer and target.components.stewer:GetTimeToCook() 0 then-- 计算剩余的烹饪时间local tm math.ceil(target.components.stewer.targettime-GLOBAL.GetTime(),0)-- 获取烹饪的食物名称local cookname GLOBAL.STRINGS.NAMES[string.upper(target.components.stewer.product)]-- 如果剩余时间小于0则设置为0if tm 0 then tm0 end-- 在字符串后添加正在烹饪的食物和剩余时间str str ..\n正在烹饪: ..tostring(cookname)..\n剩余时间(秒): ..tmend-- 检查目标实体是否有农作物组件并且有生长百分比if target.components.crop and target.components.crop.growthpercent then-- 如果有产品预制体if target.components.crop.product_prefab then-- 在字符串后添加产品的名称str str..\n..(GLOBAL.STRINGS.NAMES[string.upper(target.components.crop.product_prefab)])end -- 如果生长百分比小于1if target.components.crop.growthpercent 1 then-- 在字符串后添加距离成长的百分比str str..\n距离成长: ..math.ceil(target.components.crop.growthpercent*1000)/10 ..% end end-- 检查目标实体是否有燃料组件并且不是库存目标if target.components.fueled and not target.components.inventorytarget then-- 在字符串后添加燃料的百分比str str..\n燃料: ..math.ceil((target.components.fueled.currentfuel/target.components.fueled.maxfuel)*100) ..% end-- 检查目标实体是否有追随者组件并且有最大追随时间if target.components.follower and target.components.follower.maxfollowtime then-- 获取最大追随时间mx target.components.follower.maxfollowtime-- 计算当前的忠诚百分比cur math.floor(target.components.follower:GetLoyaltyPercent()*mx0.5)-- 如果当前的忠诚百分比大于0if cur0 then-- 在字符串后添加忠诚的百分比str str..\n忠诚: ..curendend-- 检查目标实体是否有船耐久组件if target.components.boathealth then-- 在字符串后添加船的当前耐久和最大耐久str str..\n船: ..math.ceil(target.components.boathealth.currenthealth)../..target.components.boathealth.maxhealthend-- 检查目标实体是否有有限使用组件if target.components.finiteuses then-- 如果有消耗属性if target.components.finiteuses.consumption thenlocal use 1-- 遍历消耗属性for k,v in pairs(target.components.finiteuses.consumption) douse vend-- 在字符串后添加耐久的当前值和总值str str ..\n耐久: ..math.floor(target.components.finiteuses.current/use.5)../..math.floor(target.components.finiteuses.total/use.5)else-- 在字符串后添加耐久的当前值和总值str str ..\n耐久: ..target.components.finiteuses.current../..target.components.finiteuses.total end end-- 检查目标实体是否有可工作组件if target.components.workable then-- 获取工作动作local action target.components.workable:GetWorkAction()-- 在字符串后添加工作动作str str ..\n动作 .. tostring(action.id)end-- 检查目标实体是否有生长组件if target.components.growth then-- 在字符串后添加等级和经验值str str .. \n等级 .. target.components.growth:GetLevel() .. 经验: ..target.components.growth:GetCurrentExp()../..target.components.growth:GetCurrentMaxExp() .. endendendreturn old_SetString(text,str)end
end)添加完上面代码之后就可以进入游戏测试将鼠标放在物品上就会显示详细信息了