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

2023-12-25 00:36

本文主要是介绍饥荒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)

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

在这里插入图片描述

这篇关于饥荒Mod 开发(二二):显示物品信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/533680

相关文章

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

Win32下C++实现快速获取硬盘分区信息

《Win32下C++实现快速获取硬盘分区信息》这篇文章主要为大家详细介绍了Win32下C++如何实现快速获取硬盘分区信息,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 实现代码CDiskDriveUtils.h#pragma once #include <wtypesbase

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

基于Python开发PDF转Doc格式小程序

《基于Python开发PDF转Doc格式小程序》这篇文章主要为大家详细介绍了如何基于Python开发PDF转Doc格式小程序,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 用python实现PDF转Doc格式小程序以下是一个使用Python实现PDF转DOC格式的GUI程序,采用T

使用Python开发一个图像标注与OCR识别工具

《使用Python开发一个图像标注与OCR识别工具》:本文主要介绍一个使用Python开发的工具,允许用户在图像上进行矩形标注,使用OCR对标注区域进行文本识别,并将结果保存为Excel文件,感兴... 目录项目简介1. 图像加载与显示2. 矩形标注3. OCR识别4. 标注的保存与加载5. 裁剪与重置图像

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

使用Go语言开发一个命令行文件管理工具

《使用Go语言开发一个命令行文件管理工具》这篇文章主要为大家详细介绍了如何使用Go语言开发一款命令行文件管理工具,支持批量重命名,删除,创建,移动文件,需要的小伙伴可以了解下... 目录一、工具功能一览二、核心代码解析1. 主程序结构2. 批量重命名3. 批量删除4. 创建文件/目录5. 批量移动三、如何安

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Python如何实现PDF隐私信息检测

《Python如何实现PDF隐私信息检测》随着越来越多的个人信息以电子形式存储和传输,确保这些信息的安全至关重要,本文将介绍如何使用Python检测PDF文件中的隐私信息,需要的可以参考下... 目录项目背景技术栈代码解析功能说明运行结php果在当今,数据隐私保护变得尤为重要。随着越来越多的个人信息以电子形