饥荒Mod 开发(二二):显示物品信息

饥荒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)) .. "\176C"end-- 检查目标实体是否有库存组件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 tm=0 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()*mx+0.5)-- 如果当前的忠诚百分比大于0if cur>0 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)

添加完上面代码之后就可以进入游戏测试,将鼠标放在物品上就会显示详细信息了
在这里插入图片描述`

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/403039.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解决 Windows Update 更新错误/无法创建还原点 代码 0x80246008

这个问题在我的电脑上由来已久&#xff0c;但是大部分的更新工作可以由其他第三方软件来完成&#xff0c;所有有时候得过且过。但同时&#xff0c;有一些棘手的问题&#xff0c;会提示系统进行 Windows Update&#xff0c;只有硬着头皮解决了。如果你遇到了“系统无法创建还原点…

安卓App报错:android.os.FileUriExposedException

安卓7.0开始&#xff0c;不再允许在App中把 file://Uri 暴露给其他App&#xff0c;因此在代码中需要做下版本判断&#xff0c;在7.0版本及以上需要使用 FileProvider 生成 content://Uri 来代替 file://Uri。同时安卓工程需要做以下调整&#xff1a; 1、在 AndroidManifest.xml…

Android/Linux 系统添加对多点触摸屏的支持

含有 HID 多点触摸控制器的触摸屏、触摸板在 Android 和 Linux 内核中都是由 "hid-multitouch" 驱动进行支持的。因此如果你的系统连接触摸屏没有反应&#xff0c;问题基本都出于驱动未加载或者与触摸屏的 VID 与 PID 不适配。以下分情形讨论&#xff1a; 1、系统中已…

安卓获取屏幕最大(绝对)分辨率

安卓开发时&#xff0c;在很多应用场景需要获取手机屏幕的真实分辨率&#xff0c;然而查阅了大部分博客提供的获取方法发现获取方法并不对。下面几种常用的方法&#xff08;错误&#xff09;和最终正确获取的方法均会展示在下面。 实验场景&#xff1a;Activity&#xff08;隐…

安卓BLE开发教程(一) BLE基础

我试图以一种简单的方式去把重要的事情讲清楚。目的是希望BLE协议栈和基础概念简单化&#xff0c;让自己及类似的安卓开发者可以在较短的时间内把握住BLE的核心及使用方法。BLE本身很复杂&#xff0c;但对于安卓开发而言只要抓住一些核心点&#xff0c;便已足够。如果你想全面了…

安卓BLE开发教程(二) BLE开发流程

在安卓上进行BLE开发时&#xff0c;就不必像理解BLE协议栈那样复杂了。因为安卓的BLE包为我们提供了十分丰富的API、各类常量、各类连接通信情况下的回调API等。 具体流程 一、声明权限 二、获取Adapter适配器 三、开启蓝牙 四、BLE扫描与停止 五、连接设备 六、枚举特征…

Linux驱动如何在不同版本上快速迭代升级

As well known&#xff0c;Linux内核版本更新很快&#xff0c;有些内核版本的迭代升级可能会导致在使用的驱动版本存在编译失败或使用的兼容性问题&#xff0c;如何快速定位到内核版本间变更的地方&#xff0c;并处理掉该问题&#xff0c;列一下我常用的解决方法。&#xff08;…

Ipad平板作为MAC苹果电脑的扩展屏幕的技术研究

直入主题&#xff0c;这方面的研究直接参考当前使用基数最大&#xff0c;反馈最好的两个产品。最新产品讯息&#xff0c;请分别进入各自官网。BTW&#xff0c;Duet Display也跟随Luna Display开始发布硬件了。 Duet Display 颠覆了基于 Wi-Fi 的传统运作原理&#xff0c;改经…

使用std::thread线程相关函数,-static静态编译的程序运行时的一些常见错误

使用std::thread的应用程序&#xff0c;编译时如果是动态链接pthread线程库运行正常&#xff0c;-static静态链接时在某些平台下可能会遇到一些意外错误。如常见编译命令&#xff1a;g -stdC11 test.c -o test -pthread 1、Segmentation fault&#xff08;段错误&#xff09; …

C++ Tips

1、析构函数调用时机 <1> 栈中定义的对象程序会自动调用析构函数 例如CLassName object;这样声明的对象&#xff0c;当程序运行到了对象作用域之外或者程序退出&#xff0c;对象都会被销毁&#xff0c;当然析构函数也会被调用 <2> 堆中new的对象 使用new声明的…

The futex facility returned an unexpected error code

在 linux 程序执行中若遇到该错误&#xff0c;考虑下是否是如下变量使用了强制内存对齐导致。 比如&#xff1a;在将如上变量包含到结构体中&#xff0c;强制1字节或2字节内存对齐。 如&#xff1a;信号量相关 struct semaphore&#xff0c;线程相关的 pthread_mutex_t&#…

Windows系统USB转CDC串口驱动限制说明

USB转串口芯片目前主流的几种USB类别有&#xff1a; 1、USB 厂商类&#xff1b; 2、USB CDC类&#xff1b; 3、USB HID类&#xff1b; 其中若使用USB CDC系统内置驱动时&#xff0c;使用时会有诸多特殊性&#xff0c;如下为使用说明&#xff1a; 1、测试中出现若打开串口状…

苹果MacOS系统上安装第三方驱动失败/无效

近期不少用户在苹果系统上安装一些第三方驱动时反馈没有作用&#xff0c;但是驱动安装提示是完成的&#xff0c;并拷贝到了系统的驱动路径下&#xff1b;造成该问题的原因可参见如下苹果官方说明&#xff1a; User-Approved Kernel Extension Loading 引用下第一段官网说明 …

gcc工具链查看默认编译选项

命令&#xff1a; echo "" | gcc -v -x c -E - 如在Ubuntu系统下输出结果为&#xff1a; ramboubuntu:/tmp$ echo "" | gcc -v -x c -E - Using built-in specs. COLLECT_GCCgcc OFFLOAD_TARGET_NAMESnvptx-none OFFLOAD_TARGET_DEFAULT1 Target: x86_6…

OpenWrt 之 MT7628 移植第三方SPI驱动

1、在OpenWrt系统上移植SPI驱动前&#xff0c;首先要确保SPI相关引脚未被复用为其他功能&#xff0c;比如GPIO&#xff1b;以下操作已假定该条件成立&#xff0c;否则请修改相关dts和c文件中复用配置&#xff1b; 2、打开dts配置文件进行修改&#xff0c;这里我是用的硬件为WR…

OpenWrt 之 MT7628 使用GPIO中断

在支持设备树的系统中使用中断一般有2种方式。 一、DTS配置interrupt节点 这里有个挺好的博客&#xff0c;链接地址&#xff1a;https://biscuitos.github.io/blog/DTS-interrupt/ 也即&#xff0c;找到dts文件中的GPIO中断控制器节点&#xff0c;然后在你的dts驱动节点中根…

CH9102 USB转串口应用体验

近期使用CH9102 USB转串口芯片成功用在原有使用CP2102的产品板上&#xff0c;整个替换和验证过程还是很顺利的&#xff0c;顺带写个blog做个记录。 原项目上使用CP2102搭载ESP32实现Arduino物联网应用&#xff0c;采用USB转串口芯片实现串口下载&#xff0c;代码的Debug调试&am…

CH9101 USB转串口替换FT232R和FT230XQ

学生党一枚&#xff0c;前段时间跟着导师做的项目因为上面用到USB转串口芯片FT232R迟迟买不到&#xff0c;所以打算更换成国产USB转串口芯片CH340&#xff0c;对CH340的认识也很早了&#xff0c;很多年前开始直到现在各种开发板上基本都会标配一颗CH340&#xff0c;像某宝上的S…

FT230X芯片的国产化替代

之前有些项目用到FT230XQ芯片&#xff0c;无奈不好买&#xff08;价格高&#xff09;&#xff0c;想找些替代的型号。原先使用国产CH340芯片比较多&#xff0c;顺带去官网找下有没有其他小封装的芯片型号。导航比较方便&#xff0c;从官网的产品中心&#xff0c;选择“USB”分类…

Java Code之多态

Java代码 package com.iteye.badpie.javacode.duotai; /** * 人民警察 */public interface IPolice { /** * 抓小偷 */public void catchThief(); }package com.iteye.badpie.javacode.duotai;/*** 人民警察*/ public interface IPolice {/*** 抓小偷*/public void catch…