weixin_36832008 2025-04-26 22:09 采纳率: 50%
浏览 12

饥荒武器mod制作 键盘监听等问题

当武器装备上后 按Z键或者按其他键 收取附近所有可以装入背包的东西 但是用了好多查到的方法键盘监听事件都没反应 其他功能都好用了就差这个想法没实现了 或者不是按Z键 在页面画出一个按钮也可以 我该怎么办有大佬吗local assets =
{
Asset("ANIM", "anim/xyz_y.zip"),
Asset("ANIM", "anim/swap_xyz_y.zip"),
Asset("ATLAS", "images/xyz_y.xml"), --加载物品栏贴图
Asset("IMAGE", "images/xyz_y.tex"),
}
-- 新增:收取物品和生物的功能
local function CollectItemsAndCreatures(inst, owner)
if not owner or not owner:IsValid() then return end
local x, y, z = owner.Transform:GetWorldPosition()
local ents = TheSim:FindEntities(x, y, z, 30) -- 查找30范围内的所有实体
local collectedItems = 0
local collectedCreatures = 0
local droppedItems = 0
local droppedCreatures = 0
for _, ent in ipairs(ents) do
if ent and ent:IsValid() then
-- 收取物品
if ent.components.inventoryitem and ent.components.inventoryitem.cangoincontainer then
if owner.components.inventory:GiveItem(ent, nil, Vector3(x, y, z)) then
collectedItems = collectedItems + 1
else
-- 如果物品栏已满,将物品扔在玩家脚下周围
local dropX, dropY, dropZ = owner:GetPosition():Get()
ent.Transform:SetPosition(dropX + math.random() * 2 - 1, dropY, dropZ + math.random() * 2 - 1)
droppedItems = droppedItems + 1
end
-- 收取可捕捉的生物
elseif ent.components.captureable and ent.components.captureable:IsCapturable() then
local captured = ent.components.captureable:Capture()
if captured then
if owner.components.inventory:GiveItem(captured, nil, Vector3(x, y, z)) then
collectedCreatures = collectedCreatures + 1
else
-- 如果物品栏已满,将生物扔在玩家脚下周围
local dropX, dropY, dropZ = owner:GetPosition():Get()
captured.Transform:SetPosition(dropX + math.random() * 2 - 1, dropY, dropZ + math.random() * 2 - 1)
droppedCreatures = droppedCreatures + 1
end
end
end
end
end
-- 提示玩家
if collectedItems > 0 or collectedCreatures > 0 then
owner.components.talker:Say(string.format("已收集 %d 个物品和 %d 个生物!", collectedItems, collectedCreatures))
end
if droppedItems > 0 or droppedCreatures > 0 then
owner.components.talker:Say(string.format("物品栏已满,剩余的 %d 个物品和 %d 个生物已扔在玩家脚下周围。", droppedItems, droppedCreatures))
end
if collectedItems == 0 and collectedCreatures == 0 and droppedItems == 0 and droppedCreatures == 0 then
owner.components.talker:Say("范围内没有可收集的物品或生物。")
end
end
local function onequip(inst, owner)
local skin_build = inst:GetSkinBuild()
if skin_build ~= nil then
owner:PushEvent("equipskinneditem", inst:GetSkinName())
owner.AnimState:OverrideItemSkinSymbol("swap_object", skin_build, "swap_xyz_y", inst.GUID, "swap_xyz_y")
else
owner.AnimState:OverrideSymbol("swap_object", "swap_xyz_y", "swap_xyz_y")
end
owner.AnimState:Show("ARM_carry")
owner.AnimState:Hide("ARM_normal")
end
local function onunequip(inst, owner)
owner.AnimState:Hide("ARM_carry")
owner.AnimState:Show("ARM_normal")
local skin_build = inst:GetSkinBuild()
if skin_build ~= nil then
owner:PushEvent("unequipskinneditem", inst:GetSkinName())
end
end
local function OnAttack(weapon, attacker, target)
--带电
if target ~= nil and target:IsValid() and attacker ~= nil and attacker:IsValid() then
SpawnPrefab("electrichitsparks"):AlignToTarget(target, attacker, true)
end
if target and target.components.health and not target.components.health:IsDead() then
local x,y,z = target:GetPosition():Get()
SpawnPrefab("wanda_attack_shadowweapon_old_fx").Transform:SetPosition(x,y+1,z)--警钟攻击特效
end
end
local function fn()
local inst = CreateEntity()
inst.entity:AddTransform()
inst.entity:AddAnimState()
inst.entity:AddNetwork()
MakeInventoryPhysics(inst)
inst.AnimState:SetBank("xyz_y")
inst.AnimState:SetBuild("xyz_y")
inst.AnimState:PlayAnimation("idle")
inst:AddTag("sharp")
inst:AddTag("pointy")
--weapon (from weapon component) added to pristine state for optimization
inst:AddTag("weapon")
MakeInventoryFloatable(inst, "med", 0.05, {1.1, 0.5, 1.1}, true, -9)
inst.entity:SetPristine()
-- 武器组件
inst:AddComponent("weapon")
inst.components.weapon:SetDamage(213)
inst.components.weapon:SetOnAttack(OnAttack)
inst.components.weapon:SetElectric()
inst.components.weapon:SetRange(5) -- 设置攻击距离为5
-- 添加工具组件
inst:AddComponent("tool")
-- 设置工具动作为砍砍
inst.components.tool:SetAction(ACTIONS.CHOP)
-- 设置工具动作为锤子
inst.components.tool:SetAction(ACTIONS.HAMMER)
-- 设置工具动作为挖掘
inst.components.tool:SetAction(ACTIONS.DIG)
-- 设置工具动作为网
inst.components.tool:SetAction(ACTIONS.NET)
-- 设置工具动作为挖矿
inst.components.tool:SetAction(ACTIONS.MINE)
-- 可检查组件
inst:AddComponent("inspectable")
-- 添加一个定时更新事件,用于检测键盘输入
-- inst:DoPeriodicTask(0.1, OnUpdate) -- 每0.1秒检查一次键盘输入
-- 库存物品组件
inst:AddComponent("inventoryitem")
inst.components.inventoryitem.atlasname = "images/xyz_y.xml"
-- 位面伤害组件
local planardamage = inst:AddComponent("planardamage")
planardamage:SetBaseDamage(30)
-- 可装备组件
inst:AddComponent("equippable")
inst.components.equippable:SetOnEquip(onequip)
inst.components.equippable:SetOnUnequip(onunequip)
-- 鬼魂可投掷组件
MakeHauntableLaunch(inst)
inst:AddComponent("speed") -- 确保加载了自定义的 speed 组件
inst.components.speed:SetBaseMultiplier(2.2) -- 设置加速效果为 10%
-- 添加防水组件
inst:AddComponent("waterproofer")
inst.components.waterproofer:SetEffectiveness(TUNING.WATERPROOFNESS_HUGE)
inst:AddComponent("insulator") --绝缘
inst.components.insulator:SetInsulation(TUNING.INSULATION_MED)
-- 添加护甲组件
inst:AddComponent("armor")
inst.components.armor:InitCondition(TUNING.ARMOR_BEEHAT, TUNING.ARMOR_BEEHAT_ABSORPTION)
inst.components.armor:SetTags({"bee"})
-- 新增功能:保暖保冷效果
inst:AddComponent("insulator")
inst.components.insulator.insulation = 80 -- 设置保暖值
inst:AddComponent("blinkstaff") --增加组件(闪现魔杖)
local light = inst.entity:AddLight() --增加照明(以下为参数)
light:SetFalloff(0.4) --衰减,值越大, 光线渐变越大
light:SetIntensity(.7) --变度,值越大越亮
light:SetRadius(2.5) --半径,值越大照的范围越大
light:SetColour(180/255, 195/255, 150/255) --颜色(180/红,195/绿,150/蓝),255不能变, 前面的数越大,对应色彩越浓
light:Enable(true) --这里设置正常为false, true的意思 把手杖扔地上, 它也会发光, 当灯笼用,但是前面的代码 onunequip未装备时,光照还是会被关闭
return inst
end
return Prefab("xyz_y", fn, assets)

  • 写回答

3条回答 默认 最新

  • 阿里嘎多学长 2025-04-26 22:23
    关注

    阿里嘎多学长整理AIGC生成,因移动端显示问题导致当前答案未能完全显示,请使用PC端查看更加详细的解答过程

    饥荒武器mod制作键盘监听问题

    你想在饥荒武器mod中实现键盘监听事件,以便在武器装备上后按下Z键或其他键来收取附近的物品。然而,你已经尝试了多种方法,但都没有成功。

    在 Lua 中,键盘监听事件可以使用 input 函数来实现。下面是一个简单的示例代码:

    local function on_key_press(key)
        if key == "z" then
            -- 收取附近的物品
            local player = game.player
            local inventory = player.main_inventory
            local items = {}
            for _, item in pairs(inventory.items) do
                if item.stack_size > 0 then
                    table.insert(items, item)
                end
            end
            -- todo: 处理items数组
        end
    end
    
    local function on_init()
        -- 注册键盘监听事件
        game.input:set_key_handler(on_key_press, "z")
    end
    
    on_init()
    

    在上面的代码中,我们首先定义了一个 on_key_press 函数,该函数将在键盘按下时被调用。然后,我们使用 game.input:set_key_handler 函数来注册键盘监听事件,并指定了要监听的键为 "z"。

    请注意,这只是一个简单的示例代码,你需要根据你的需求来修改和扩展它。例如,你可能需要处理多个键盘事件,或者在收取物品时进行其他操作。

    如果你想要在页面上画出一个按钮,而不是使用键盘监听事件,可以使用 draw 函数来绘制按钮,然后使用 input 函数来检测按钮是否被点击。

    评论

报告相同问题?

问题事件

  • 创建了问题 4月26日