饥荒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

相关文章

基于 Cursor 开发 Spring Boot 项目详细攻略

《基于Cursor开发SpringBoot项目详细攻略》Cursor是集成GPT4、Claude3.5等LLM的VSCode类AI编程工具,支持SpringBoot项目开发全流程,涵盖环境配... 目录cursor是什么?基于 Cursor 开发 Spring Boot 项目完整指南1. 环境准备2. 创建

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

Python开发简易网络服务器的示例详解(新手入门)

《Python开发简易网络服务器的示例详解(新手入门)》网络服务器是互联网基础设施的核心组件,它本质上是一个持续运行的程序,负责监听特定端口,本文将使用Python开发一个简单的网络服务器,感兴趣的小... 目录网络服务器基础概念python内置服务器模块1. HTTP服务器模块2. Socket服务器模块

Java 与 LibreOffice 集成开发指南(环境搭建及代码示例)

《Java与LibreOffice集成开发指南(环境搭建及代码示例)》本文介绍Java与LibreOffice的集成方法,涵盖环境配置、API调用、文档转换、UNO桥接及REST接口等技术,提供... 目录1. 引言2. 环境搭建2.1 安装 LibreOffice2.2 配置 Java 开发环境2.3 配

Python38个游戏开发库整理汇总

《Python38个游戏开发库整理汇总》文章介绍了多种Python游戏开发库,涵盖2D/3D游戏开发、多人游戏框架及视觉小说引擎,适合不同需求的开发者入门,强调跨平台支持与易用性,并鼓励读者交流反馈以... 目录PyGameCocos2dPySoyPyOgrepygletPanda3DBlenderFife

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁