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

news2024/9/30 5:33:52

饥荒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.SetString
	self.text.SetString = function(text,str)
		  -- 获取鼠标下的世界实体
        local target = GLOBAL.TheInput:GetWorldEntityUnderMouse() 
        -- 如果存在目标实体
		if target   then
			  -- 如果目标实体有预制体
            if target.prefab then
                -- 在字符串后添加预制体的代码
                str = str .. "\n代码: " .. target.prefab
            end
            -- 如果目标实体有可旅行组件
            if target.components.travelable then
                -- 在字符串后添加可旅行组件的名称
                str = str .."\n".. tostring(target.components.travelable.name)
            end
			if 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)/10
                end
                -- 如果目标实体有战斗组件,并且默认伤害大于0
                if target.components.combat and target.components.combat.defaultdamage > 0 then
                    -- 在字符串后添加生物的攻击力
                    str = str.."\n攻击力: "..target.components.combat.defaultdamage
                end
			    -- 如果目标实体是温度计
                if target.prefab == "winterometer" then
                    -- 获取当前温度
                    local temp = GLOBAL.GetSeasonManager() and GLOBAL.GetSeasonManager():GetCurrentTemperature() or 30
                    local high_temp = TUNING.OVERHEAT_TEMP
                    local 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).."%"
                        end
                    end
                    -- 获取目标实体身体装备的物品
                    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).."%"
                        end
                    end
                end

				-- 检查目标实体是否可以被驯养
                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()
                        -- 如果驯养值不为0
                        if 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 then
                                ten = "战牛"
                            -- 如果倾向为RIDER,则设置为"行牛"
                            elseif k == GLOBAL.TENDENCY.RIDER then
                                ten = "行牛"
                            -- 如果倾向为PUDGY,则设置为"肥牛"
                            elseif k == GLOBAL.TENDENCY.PUDGY then
                                ten = "肥牛"
                            end
                            -- 在字符串后添加倾向的信息
                            str = str .. string.format("\n %s:%.2f", ten, v)
                        end
                    end
                end
				-- 检查目标实体是否可以被采摘,并且有目标时间
                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).." 天"
                    end
                end
				-- 检查目标实体是否有烹饪组件,并且烹饪时间大于0
                if 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,则设置为0
                    if tm <0 then tm=0 end
                    -- 在字符串后添加正在烹饪的食物和剩余时间
                    str = str .."\n正在烹饪: "..tostring(cookname).."\n剩余时间(秒): "..tm
                end
                -- 检查目标实体是否有农作物组件,并且有生长百分比
                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 
                    -- 如果生长百分比小于1
                    if 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)
                    -- 如果当前的忠诚百分比大于0
                    if cur>0 then
                        -- 在字符串后添加忠诚的百分比
                        str = str.."\n忠诚: "..cur
                    end
                end
                -- 检查目标实体是否有船耐久组件
                if target.components.boathealth  then
                    -- 在字符串后添加船的当前耐久和最大耐久
                    str = str.."\n船: "..math.ceil(target.components.boathealth.currenthealth).."/"..target.components.boathealth.maxhealth
                end
				-- 检查目标实体是否有有限使用组件
                if target.components.finiteuses then
                    -- 如果有消耗属性
                    if target.components.finiteuses.consumption then
                        local use = 1
                        -- 遍历消耗属性
                        for k,v in pairs(target.components.finiteuses.consumption) do
                            use = v
                        end
                        -- 在字符串后添加耐久的当前值和总值
                        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() .. ")"
                end
			end
		end
		return old_SetString(text,str)
	end
end)

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

在这里插入图片描述

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

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

相关文章

DataProcess-VOC数据图像和标签一起进行Resize

VOC数据图像和标签一起进行Resize 参加检测比赛的时候&#xff0c;很多时候工业原始数据尺度都比较大&#xff0c;如果对数据不提前进行处理&#xff0c;会导致数据在加载进内存时花费大量的时间&#xff0c;所以在执行训练程序之前需要将图像提前进行预处理。对于目标检测的数…

Log4net 教程

一、Log4net 教程 在CodeProject上找到一篇关于Log4net的教程&#xff1a;log4net Tutorial&#xff0c;这篇博客的作者是&#xff1a;Tim Corey &#xff0c;对应源代码地址为&#xff1a; https://github.com/TimCorey/Log4netTutorial&#xff0c;视频地址为&#xff1a;Ap…

案例144:基于微信小程序的自修室预约系统

文末获取源码 开发语言&#xff1a;Java 框架&#xff1a;SSM JDK版本&#xff1a;JDK1.8 数据库&#xff1a;mysql 5.7 开发软件&#xff1a;eclipse/myeclipse/idea Maven包&#xff1a;Maven3.5.4 小程序框架&#xff1a;uniapp 小程序开发软件&#xff1a;HBuilder X 小程序…

C/C++图形化编程(2)

归纳编程学习的感悟&#xff0c; 记录奋斗路上的点滴&#xff0c; 希望能帮到一样刻苦的你&#xff01; 如有不足欢迎指正&#xff01; 共同学习交流&#xff01; &#x1f30e;欢迎各位→点赞 &#x1f44d; 收藏⭐ 留言​&#x1f4dd; 站在巨人的肩上是为了超过巨人&#x…

esp32使用lvgl,给图片取模显示图片

使用LVGL官方工具。 https://lvgl.io/tools/imageconverter 上传图片&#xff0c;如果想要透明效果&#xff0c;那么选择 输出格式C array&#xff0c;点击Convert进行转换。 下载.c文件放置到工程下使用即可。

Py之tensorflow-addons:tensorflow-addons的简介、安装、使用方法之详细攻略

Py之tensorflow-addons&#xff1a;tensorflow-addons的简介、安装、使用方法之详细攻略 目录 tensorflow-addons的简介 tensorflow-addons的安装 tensorflow-addons的使用方法 1、使用 TensorFlow Addons 中的功能&#xff1a; tensorflow-addons的简介 TensorFlow Addon…

本地搜索文件太慢怎么办?用Everything搜索秒出结果(附安装包)

每次用电脑本地的搜索都慢的一批&#xff0c;后来发现了一个搜索利器 基本上搜索任何文件都不用等待。 并且页面非常简洁&#xff0c;也没有任何广告&#xff0c;用起来非常舒服。 软件官网如下&#xff1a; voidtools 官网提供三个版本&#xff0c;用起来差别不大。 网盘链…

Javacv-利用Netty实现推流直播复用(flv)

前言 上一篇文章《JavaCV之rtmp推流&#xff08;FLV和M3U8&#xff09;》介绍了javacv的基本使用&#xff0c;今天来讲讲如何实现推流复用。 以监控摄像头的直播为例&#xff0c;通常分为三步&#xff1a; 从设备获取音视频流利用javacv进行解码&#xff08;例如flv或m3u8&am…

(2021|CoRR,AugCLIP,优化)FuseDream:通过改进的 CLIP+GAN 空间优化实现免训练文本到图像生成

FuseDream: Training-Free Text-to-Image Generation with Improved CLIPGAN Space Optimization 公众&#xff1a;EDPJ&#xff08;添加 VX&#xff1a;CV_EDPJ 或直接进 Q 交流群&#xff1a;922230617 获取资料&#xff09; 目录 0. 摘要 1. 简介 2. CLIPGAN 文本到图…

如何使用kali来进行一次ddos攻击

本文章用于记录自己的学习路线&#xff0c;不用于其他任何途径! ! ! 哈喽啊&#xff01;又是好久不见&#xff0c;本博主在之前发过一个ddos攻击的介绍。 emm…虽然那篇文章也提到了ddos攻击的方式&#xff0c;但太过于简陋&#xff0c;好像也没有什么用&#xff0c;so&#…

金蝶云星空权限项表结构

文章目录 金蝶云星空权限项表结构BOS平台【权限项】MSSQL脚本使用场景优点减少手工一个个创建的人工成本&#xff0c;还容易出错保留内码&#xff0c;可以在代码层级使用&#xff0c;方便 金蝶云星空权限项表结构 BOS平台【权限项】 MSSQL脚本 --权限项主表 SELECT * FROM db…

快速学习 webpack

目录 1. webpack基本概念 webpack能做什么&#xff1f; 2. webpack的使用步骤 2.1_webpack 更新打包 3. webpack的配置 3.1_打包流程图 3.2_案例-webpack隔行变色 3.3_插件-自动生成html文件 3.4_加载器 - 处理css文件问题 3.5_加载器 - 处理css文件 3.6_加载器 - 处…

大数据----基于sogou.500w.utf8数据的MapReduce编程

目录 一、前言二、准备数据三、编程实现3.1、统计出搜索过包含有“仙剑奇侠传”内容的UID及搜索关键字记录3.2、统计rank<3并且order>2的所有UID及数量3.3、上午7-9点之间&#xff0c;搜索过“赶集网”的用户UID3.4、通过Rank&#xff1a;点击排名 对数据进行排序 四、参…

jQuery: 整理4---创建元素和添加元素

1.创建元素&#xff1a;$("内容") const p "<p>这是一个p标签</p>" console.log(p)console.log($(p)) 2. 添加元素 2.1 前追加子元素 1. 指定元素.prepend(内容) -> 在指定元素的内部的最前面追加内容&#xff0c;内容可以是字符串、…

代码随想录算法训练营 | day60 单调栈 84.柱状图中最大的矩形

刷题 84.柱状图中最大的矩形 题目链接 | 文章讲解 | 视频讲解 题目&#xff1a;给定 n 个非负整数&#xff0c;用来表示柱状图中各个柱子的高度。每个柱子彼此相邻&#xff0c;且宽度为 1 。 求在该柱状图中&#xff0c;能够勾勒出来的矩形的最大面积。 1 < heights.len…

动态规划系列 | 最长上升子序列模型(上)

文章目录 最长上升子序列回顾题目描述问题分析程序代码复杂度分析 怪盗基德的滑翔翼题目描述输入格式输出格式 问题分析程序代码复杂度分析 登山题目描述输入格式输出格式 问题分析程序代码复杂度分析 合唱队形题目描述输入格式输出格式 问题分析程序代码复杂度分析 友好城市题…

基于Java SSM框架实现医院挂号上班打卡系统项目【项目源码+论文说明】计算机毕业设计

基于java的SSM框架实现医院挂号上班打卡系统演示 摘要 在网络发展的时代&#xff0c;国家对人们的健康越来越重视&#xff0c;医院的医疗设备更加先进&#xff0c;医生的医术、服务水平也不断在提高&#xff0c;给用户带来了很大的选择余地&#xff0c;而且人们越来越追求更个…

Linux与Bash 编程——Linux文件处理命令-L1

目录&#xff1a; linux系统与shell环境准备 Linux系统简介操作系统简史Linux的发行版&#xff1a;Linux与Windows比较&#xff1a;Linux安装安装包下载Linux的访问方式远程登录方式远程登录软件&#xff1a;mobaxterm的使用&#xff1a;使用电脑命令行连接&#xff1a;sshd的…

系列十四、SpringBoot + JVM参数配置实战调优

一、SpringBoot JVM参数配置实战调优 1.1、概述 前面的系列文章大篇幅的讲述了JVM的内存结构以及各种参数&#xff0c;今天就使用SpringBoot项目实战演示一下&#xff0c;如何进行JVM参数调优&#xff0c;如果没有阅读过前面系列文章的朋友&#xff0c;建议先阅读后再看本篇文…

python库win32gui,windows的API管理及自动化

使用了python实现了打开windows的鼠标属性页面并更改鼠标的主键的功能&#xff0c;今天主要是针对使用的库进行一个讲解&#xff0c;也即是win32gui库的详细讲解。 对于windows的打开的窗口中&#xff0c;可以通过窗口的类型和名字来进行窗口的具体查找&#xff0c;使用的win3…