第10页:第六步代码制作(2)
展开同样在exchange.lua中写入
exchange.lua
require "prefabutil"
local assets =
{
Asset("ANIM", "anim/exchange.zip"),
Asset("IMAGE", "images/inventoryimages/exchange.tex"),
Asset("ATLAS", "images/inventoryimages/exchange.xml"),
}
local prefabs =
{
"exchange",
}
local function getstatus(inst)
end
local function onhammered(inst, worker)
inst.components.lootdropper:DropLoot()
SpawnPrefab("collapse_small").Transform:SetPosition(inst.Transform:GetWorldPosition())
inst.SoundEmitter:PlaySound("dontstarve/common/destroy_wood")
inst:Remove()
end
local function onhit(inst, worker)
inst.AnimState:PlayAnimation("hit")
inst.AnimState:PushAnimation("idle", true)
end
local function onbuilt(inst)
inst.AnimState:PlayAnimation("place")
inst.AnimState:PushAnimation("idle")
end
local function fn(Sim)
local inst = CreateEntity()
local trans = inst.entity:AddTransform()
local anim = inst.entity:AddAnimState()
inst.entity:AddSoundEmitter()
MakeObstaclePhysics(inst, 1)
anim:SetBank("exchange")
anim:SetBuild("exchange")
anim:PlayAnimation("idle", true)
inst:AddTag("structure")
inst:AddTag("structure")
inst:AddComponent("lootdropper")
inst:AddComponent("workable")
inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
inst.components.workable:SetWorkLeft(4)
inst.components.workable:SetOnFinishCallback(onhammered)
inst.components.workable:SetOnWorkCallback(onhit)
inst:AddComponent("inspectable")
local function ShouldAcceptItem(inst, item)
if item:HasTag("hat") then
return false
end
if item.components.edible.foodtype == "SEEDS" then
return false
end
return true
end
local function OnGetItemFromPlayer(inst, giver, item)
local names = {"cave_banana","carrot","corn","pumpkin","eggplant","durian","pomegranate","dragonfruit","berries","meat","smallmeat","fish","eel","drumstick","bird_egg","froglegs"}
inst.name = names[math.random(#names)]
local veggie = SpawnPrefab(inst.name)
local goldcoin = SpawnPrefab("goldcoin")
if item.components.edible.foodtype == "VEGGIE" then
giver.components.inventory:GiveItem(goldcoin)
end
if item.components.edible.foodtype == "MEAT" then
giver.components.inventory:GiveItem(goldcoin)
end
if item.prefab == "goldcoin" then
giver.components.inventory:GiveItem(veggie or meat)
end
end
inst:AddComponent("trader")
inst.components.trader.onaccept = OnGetItemFromPlayer
inst.components.trader:SetAcceptTest(ShouldAcceptItem)
inst.components.inspectable.getstatus = getstatus
return inst
end
return Prefab( "common/exchange", fn, assets, prefabs ),
MakePlacer("common/exchange_placer", "exchange", "exchange", "idle")
更多相关内容请关注:饥荒专区
责任编辑:修凡客