local assets =
{
	Asset("ANIM", "anim/jianqiao2.zip"),
	Asset("ANIM", "anim/swap_jianqiao2.zip"),
    Asset("ATLAS", "images/inventoryimages/jianqiao2.xml"),
    Asset("IMAGE", "images/inventoryimages/jianqiao2.tex"),
	
	Asset("ANIM", "anim/ui_thatchpack_1x4.zip"),
}
--修复耐久
local function PercentChanged(inst, data)
local percent = inst.components.armor.condition/inst.components.armor.maxcondition
local jqfpercent = 4200*percent
	--inst.components.fueled.currentfuel = jqfpercent
end

-------
local function texiaozu(inst, owner)
    inst.components.armor:SetAbsorption(1)
    
    local fx = SpawnPrefab("forcefieldfx")
    fx.entity:SetParent(owner.entity)
    fx.Transform:SetPosition(0, 0.2, 0)
    fx.Transform:SetScale(1.2, 1.2, 1.2)
    fx.AnimState:PlayAnimation("hit")
    fx.AnimState:PushAnimation("idle_loop")

    inst.components.armor.ontakedamage = function(inst, damage_amount)
        if owner then
            local sanity = owner.components.sanity
            if sanity then
                local unsaneness = damage_amount * 0.05
                sanity:DoDelta(-unsaneness, false)
            end
        end
    end
    inst.active = true
    --冷却时间5s
    owner:DoTaskInTime(5, function()
        fx.kill_fx(fx)
        if inst:IsValid() then
            inst.components.armor.ontakedamage = nil
            --owner.components.health.absorb = 0
            inst.components.armor:SetAbsorption(0.8)
            owner:DoTaskInTime(15, function() inst.active = false end)
        end
    end)
end


local function tryproc(inst, owner)
    if not inst.active and math.random() < 0.2 then
        texiaozu(inst, owner)
    end
end


local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "swap_jianqiao2", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_jianqiao2", "swap_body")
        inst.procfn = function() tryproc(inst, owner) end
        owner:ListenForEvent("attacked", inst.procfn)
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
 owner:RemoveEventCallback("attacked", inst.procfn)
    
end



local function onopen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_open", "open")
end

local function onclose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/wilson/backpack_close", "open")
end



local slotpos = {}

for x = 0, 4 do
	table.insert(slotpos, Vector3(-x*75 - 1060 , -162 - 330,0))
end


local function itemtest(inst, item, slot)
	return item:HasTag("weapon_sword")
end

local function fn()
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()


    MakeInventoryPhysics(inst)
	
    inst.AnimState:SetBank("jianqiao2")
    inst.AnimState:SetBuild("jianqiao2")
    inst.AnimState:PlayAnimation("idle")
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "jianqiao2.tex" )
    --inst.MiniMapEntity:SetIcon("jianqiao2.png")
    
    --inst.foleysound = "dontstarve/movement/foley/backpack"
    --inst:AddTag("backpack")





    
    inst:AddComponent("inspectable")
    
		inst:AddComponent("trader")
    inst.components.trader:SetAcceptTest(function(inst, item) return item and item.prefab=="goldnugget" end)
    inst.components.trader.onaccept=function(inst,giver,item)
local jqarmor = inst.components.armor.condition + 600
if jqarmor <= 4200 then
    inst.components.armor:SetCondition(jqarmor)
	--inst.components.fueled.currentfuel = jqarmor
else
    inst.components.armor:SetCondition(4200)
	--inst.components.fueled.currentfuel = 4200
end
	end
    inst.components.trader.onrefuse=function(inst,giver,item) if giver.components.talker then giver.components.talker:Say("需要噩梦燃料修复") end end
	
    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.keepondeath = true
	inst.components.inventoryitem.imagename = "jianqiao2"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/jianqiao2.xml"
	inst.components.inventoryitem.cangoincontainer = true
    inst.components.inventoryitem.foleysound = "dontstarve_DLC002/common/foley/grass_thatch_pack"
    --MakeInventoryFloatable(inst, "idle_water", "anim")

    inst:AddComponent("equippable")
    inst.components.equippable.equipslot = EQUIPSLOTS.BODY
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)

    inst:AddComponent("armor")
    inst.components.armor:InitCondition(4200, 0.8)
	
    inst:AddComponent("container")
    inst.components.container.itemtestfn = itemtest	
    inst.components.container:SetNumSlots(#slotpos)
    inst.components.container.widgetslotpos = slotpos
    --inst.components.container.widgetanimbank = "ui_thatchpack_1x4"
    --inst.components.container.widgetanimbuild = "ui_thatchpack_1x4"
    --inst.components.container.widgetpos = Vector3(-5,-70,0)
    inst.components.container.side_widget = true
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose
	
	--inst:ListenForEvent("attacked", OnAttacked)

        

 

   --MakeHauntableLaunchAndDropFirstItem(inst)

    return inst
end

return Prefab("common/inventory/jianqiao2", fn, assets)