核心组件及其流程如下:
材料 -> 烹饪锅(stewer) -> 食物(吃edilbe)(腐烂perishable) -> 玩家(eater)
modicon.tex和modicon.xml来显示mod的图标 128x128像素
modinfo来说明服务器/客户端mod,mod名称、作者等
modmain作为mod代码的入口
exported放动画
图片大小是140x140像素,这样烹饪出来的食物大小看着就差不多。
images/inventoryimages放物品栏贴图,烹饪书的贴图也用这个 64x64像素
scripts放代码
这些第三章都有详细介绍到。
在scripts文件夹下,新建个myfooods.lua,编写菜谱
--参考代码:scripts/preparedfoods.lua、scripts/preparedfoods_warly.lua
local foods = {
myfood = {
oneatenfn = function(inst, eater)
--吃东西时,生成个树枝
if not eater then return end
local x, y, z = eater.Transform:GetWorldPosition()
SpawnPrefab("twigs").Transform:SetPosition(x, y + 1, z)
end,
--烹饪锅判断是否能生成食物,并根据priority选择高的
test = function(cooker, names, tags)
--return names.twigs and tags.veggie and tags.veggie >= 1 and tags.meat and tags.meat>= 0.5)
return true
end,
foodtype = FOODTYPE.MEAT,
health = 0,
hunger = 60,
sanity = 5,
perishtime = TUNING.PERISH_MED, --腐烂时间
cooktime = 0.5,
}
}
for k,v in pairs(foods) do
v.name = k
v.weight = v.weight or 1
v.priority = v.priority or 50
--烹饪书贴图
v.cookbook_atlas = "images/inventoryimages/"..v.name..".xml"
end
return foods
在scripts文件夹下,新建prefabs文件夹,在里面新建myfoods.lua
local foods = require("myfoods")
local prefabs = {}
for k, v in pairs(foods) do
local assets = {
Asset("ANIM", "anim/"..v.name..".zip"),
Asset("ATLAS", "images/inventoryimages/"..v.name..".xml")
}
local fn = function()
local inst = CreateEntity()
inst.entity:AddTransform()
inst.entity:AddAnimState()
inst.entity:AddNetwork()
inst.AnimState:SetBuild(v.name)
inst.AnimState:SetBank(v.name)
inst.AnimState:PlayAnimation("idle")
MakeInventoryPhysics(inst)
MakeInventoryFloatable(inst)
inst:AddTag("preparedfood")
inst.entity:SetPristine()
if not TheWorld.ismastersim then
return inst
end
--核心组件edible,其他参数去看components/edible.lua
inst:AddComponent("edible")
inst.components.edible.healthvalue = v.health or 0
inst.components.edible.hungervalue = v.hunger or 0
inst.components.edible.sanityvalue = v.sanity or 0
inst.components.edible.foodtype = v.foodtype or FOODTYPE.GENERIC
if v.oneatenfn then
inst.components.edible:SetOnEatenFn(v.oneatenfn)
end
--可检查,检查的语句写在modmain
inst:AddComponent("inspectable")
--物品栏
inst:AddComponent("inventoryitem")
inst.components.inventoryitem.atlasname = "images/inventoryimages/"..v.name..".xml"
--可堆叠
inst:AddComponent("stackable")
inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM
--可腐烂
inst:AddComponent("perishable")
inst.components.perishable:SetPerishTime(v.perishtime or TUNING.PERISH_MED)
inst.components.perishable:StartPerishing()
inst.components.perishable.onperishreplacement = "spoiled_food"
--可作祟
MakeHauntableLaunchAndPerish(inst)
--诱饵
inst:AddComponent("bait")
--可交易
inst:AddComponent("tradable")
return inst
end
table.insert(prefabs, Prefab(v.name, fn, assets))
end
--分别返回prefabs表中的元素,相当于 return prefabs[1], prefabs[2], ...
return unpack(prefabs)
在modmain.lua中
--预设物文件
PrefabFiles = {
"myfoods"
}
--一键GLOBAL
GLOBAL.setmetatable(env,{__index=function(t,k) return GLOBAL.rawget(GLOBAL,k) end})
-- food
--require返回scripts文件夹下我们或官方的代码的return值
local foods = require("myfoods")
for k, v in pairs(foods) do
AddCookerRecipe("cookpot", v) --添加到烹饪锅
AddCookerRecipe("portablecookpot", v) --便携式烹饪锅
end
--检查
STRINGS.NAMES.MYFOOD = "食物"
STRINGS.CHARACTERS.GENERIC.DESCRIBE.MYFOOD = "光明料理!"
→饥荒联机版Mod开发——制作可入锅,烹饪,凉干的食物(七)
←饥荒联机版Mod开发——modmain(五)