饥荒Mod 开发(十二):一键制作

饥荒Mod 开发(十一):修改物品堆叠
饥荒Mod 开发(十三):木牌传送

饥荒中制作物品的痛点

饥荒中可以使用工作台制作物品,对于较复杂的物品我们可能需要制作多次。比如制作木牌,我们需要2个步骤
木头–》木板–》 木牌。 虽然我们有很多木头,但是也只能先一步一步制作木板,才能制作木牌,从下面的图可以看出虽然我们有很多的木头,想要制作一个木牌,不能一步到位,得先制作木板。
在这里插入图片描述

比如制作 木甲,我们需要木头和绳子, 而绳子又需要干草制作。 所以我们需要先制作2个绳子,然后才能制作木甲,这样真的太繁琐了。而且饥荒的制作台想要找个东西还真的很难。
在这里插入图片描述

一键制作

我们可以在制作台上增加一个小按钮,如果材料充足,并且可以制作的话就显示个小按钮,点击就可以制作 子材料。modmian.lua 中添加下面代码即可

-- 引入 ImageButton 控件
local ImageButton = GLOBAL.require "widgets/imagebutton"

-- 修改 RecipePopup 控件的行为
AddClassPostConstruct("widgets/recipepopup", function(self)
    -- 保存原始的 Refresh 方法
    local old = self.Refresh

    -- 重写 Refresh 方法
    self.Refresh = function(...)
        -- 调用原始的 Refresh 方法
        old(...)
        -- 如果弹窗没有显示,则直接返回
        if not self.shown then
            return
        end
        -- 获取当前的配方和拥有者
        local recipe = self.recipe
        local owner = self.owner

        -- 如果 doAction 控件存在,则先隐藏它
        if self.doAction then
            self.doAction:Hide()
        end

        -- 遍历配方的所有材料
        for k,v in pairs(recipe.ingredients) do
            -- 判断材料是否是需要合成的物品
            local slotrecipe = GLOBAL.Recipes[v.type]
            if slotrecipe then
                -- 判断拥有者是否知道这个配方,是否可以制作这个物品,以及是否拥有足够的材料
                local knows = owner.components.builder:KnowsRecipe(v.type)
                local can_build = owner.components.builder:CanBuild(v.type)
                local has, num_found = owner.components.inventory:Has(v.type, GLOBAL.RoundUp(v.amount * owner.components.builder.ingredientmod), true)

                -- 如果知道配方,可以制作物品,但是没有足够的材料,并且 doAction 控件不存在,则创建一个新的 doAction 控件
                if knows and can_build and not has and not self.doAction then                
                    self.doAction = self.contents:AddChild(ImageButton("images/ui.xml", "button_small.tex", "button_small_over.tex", "button_small_disabled.tex"))
                    self.doAction:SetPosition(220, 140)
                    self.doAction:SetText("材料")
                    self.doAction:MoveToFront()
                    self.doAction:SetOnClick(function()
                        GLOBAL.DoRecipeClick(self.owner, self.doAction.slotrecipe)
                    end)
                end

                if knows and can_build and not has then
                    self.doAction.slotrecipe = slotrecipe
                    self.doAction:Show()
                end
                 -- 如果不知道配方,但是可以制作物品,则让拥有者说出需要制作原型的提示
                if not knows and can_build then
                    --说话
                    owner.components.talker:Say(GLOBAL.STRINGS.NAMES[string.upper(slotrecipe.name)] .. "(需要制作一个原型)")
                end
            end
        end
    end
end)

下面是一个木牌的制作台
在这里插入图片描述
下面是木甲的制作台
在这里插入图片描述

点击上面 “材料” 按钮,将会制动制作 子材料, 然后就可以直接建造了,再也不用到处去找了。
一键制作源码

  • 31
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值