local assets=
{
	Asset("ANIM", "anim/swap_frostpocket.zip"),
	Asset("ATLAS", "images/inventoryimages/broken_frosthammer.xml"),
	Asset("IMAGE", "images/inventoryimages/broken_frosthammer.tex"),

}

local slotpos_2 = {}

for y = 0, 1 do
	table.insert(slotpos_2, Vector3(-162, -y*75 + 114 ,0))
	table.insert(slotpos_2, Vector3(-162 +75, -y*75 + 114 ,0))
end

local function itemtest(inst, item, slot)
	if not item:HasTag("tstone") then
			return true
end
end

---------------------------
local function levelexp(inst,data)

	local max_exp = 410
	local exp = math.min(inst.level, max_exp)

if inst.level <= 400 then
inst.components.talker:Say("-[冰霜战甲 I]-\n[成长点数]".. (inst.level))
inst:RemoveTag("levelup")
end
if inst.level >=0 and inst.level <10 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[10]\n[成长点数]".. (inst.level))
inst:AddTag("level1")
elseif inst.level >=10 and inst.level <30 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[30]\n[成长点数]".. (inst.level))
inst:AddTag("level2")
inst:RemoveTag("level1")
elseif inst.level >=30 and inst.level <60 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[60]\n[成长点数]".. (inst.level))
inst:AddTag("level3")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
elseif inst.level >=60 and inst.level <100 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[100]\n[成长点数]".. (inst.level))
inst:AddTag("level4")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
elseif inst.level >=100 and inst.level <150 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[150]\n[成长点数]".. (inst.level))
inst:AddTag("level5")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
elseif inst.level >=150 and inst.level <200 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[200]\n[成长点数]".. (inst.level))
inst:AddTag("level6")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
elseif inst.level >=200 and inst.level <250 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[250]\n[成长点数]".. (inst.level))
inst:AddTag("level7")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
elseif inst.level >=250 and inst.level <300 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[300]\n[成长点数]".. (inst.level))
inst:AddTag("level8")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
elseif inst.level >=300 and inst.level <400 then
inst.components.talker:Say("-[冰霜战甲 I] \n下一级:[400]\n[成长点数]".. (inst.level))
inst:AddTag("level9")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
elseif inst.level >=400 then
inst.components.talker:Say("-[冰霜战甲 I] \n[满级]")
inst:AddTag("level10")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
inst:RemoveTag("level5")
inst:RemoveTag("level6")
inst:RemoveTag("level7")
inst:RemoveTag("level8")
inst:RemoveTag("level9")

end
end

local function onpreload(inst, data)
	if data then
		if data.level then
		inst.level = data.level
			levelexp(inst)
end
	end
		end
local function onsave(inst, data)
	data.level = inst.level
	data.charge_time = inst.charge_time
end
local function ShouldAcceptItem(inst, item)
    if item.components.edible and inst.components.hunger and inst.components.eater then
        return inst.components.eater:CanEat(item) and inst.components.hunger:GetPercent() <= 1
    end
end
local function OnGetItemFromPlayer(inst, giver, item)
    if item.components.edible then
        end
        if inst.components.eater:Eat(item) then
	inst.components.hunger:DoDelta(10)
		local fx = SpawnPrefab("splash")
	local pos = Vector3(inst.Transform:GetWorldPosition())
fx.Transform:SetPosition(pos:Get())

        end
    end

local function OnDurability(inst, data)
inst:AddTag("broken")
inst.components.fueled:StopConsuming()        
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0)
inst.components.talker:Say("-[残破的 冰霜战甲I]\n护甲值 (0)\n耐久度 (0)\n速度上升 (0)")
end

local function UpgradeArmor(inst)
if inst:HasTag("broken") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.01)
   end
if not inst:HasTag("broken") then
        if inst:HasTag("level1") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.05)
	inst.components.talker:Say("-[冰霜战甲 I (1级)]\n护甲值 (5)\n冻结(5)")
        elseif inst:HasTag("level2") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.1)
	inst.components.talker:Say("-[冰霜战甲 I (2级)]\n护甲值 (10)\n冻结(10)")
        elseif inst:HasTag("level3") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.15)
	inst.components.talker:Say("-[冰霜战甲 I (3级)]\n护甲值 (15)\n冻结(15)")
        elseif inst:HasTag("level4") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.2)
	inst.components.talker:Say("-[冰霜战甲 I (4级)]\n护甲值 (20)\n冻结(20)")
        elseif inst:HasTag("level5") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.25)
	inst.components.talker:Say("-[冰霜战甲 I (5级)]\n护甲值 (25)\n冻结(25)")
        elseif inst:HasTag("level6") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.24)
	inst.components.talker:Say("-[冰霜战甲 I (6级)]\n护甲值 (24)\n冻结(24)")
        elseif inst:HasTag("level7") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.28)
	inst.components.talker:Say("-[冰霜战甲 I (7级)]\n护甲值 (28)\n冻结(28)")
        elseif inst:HasTag("level8") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.32)
	inst.components.talker:Say("-[冰霜战甲 I (8级)]\n护甲值 (32)\n冻结(32)")
        elseif inst:HasTag("level9") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.36)
	inst.components.talker:Say("-[冰霜战甲 I (9级)]\n护甲值 (36)\n冻结(36)")
        elseif inst:HasTag("level10") then
    inst.components.armor:InitCondition(9999999999999999999999999999999999999999999999999999999999999, 0.4)
	inst.components.talker:Say("-[冰霜战甲 I (满级10级)]\n护甲值 (40)\n冻结(40)")

end
end
end

 -------- --------
local function TakeItem(inst, item, data)
local expchance0 = 1
local expchance1 = 0.3
local expchance2 = 0.2
local expchance3 = 0.12
	inst.components.fueled:DoDelta(5000000)
	SpawnPrefab("splash").Transform:SetPosition(inst:GetPosition():Get())
inst:RemoveTag ("broken")     
UpgradeArmor(inst)
   if math.random() < expchance1 and inst.level <= 4005 then
	inst.level = inst.level + 2
	levelexp(inst)
	inst.components.talker:Say("幸运点数 ! +(2)\n[成长点数]".. (inst.level))
    elseif  math.random() < expchance2 and inst.level <= 4005 then
	inst.level = inst.level + 5
	levelexp(inst)
	inst.components.talker:Say("幸运点数 ! +(5)\n[成长点数]".. (inst.level))
	elseif  math.random() < expchance3 and inst.level <= 4005 then
	inst.level = inst.level + 8
	levelexp(inst)
	inst.components.talker:Say("幸运点数 ! +(8)\n[成长点数]".. (inst.level))
	elseif  math.random() < expchance0 and inst.level <= 4005 then
	inst.level = inst.level + 1
	levelexp(inst)
    end
end

local function OnLoad(inst, data)
    UpgradeArmor(inst)
end

--------

local function onequip(inst, owner) 
   inst:AddTag("talkf")
    UpgradeArmor(inst)
inst.components.fueled:StartConsuming()        

    owner.AnimState:OverrideSymbol("swap_body", "swap_frostpocket", "backpack")
    owner.AnimState:OverrideSymbol("swap_body", "swap_frostpocket", "swap_body")
    owner.components.inventory:SetOverflow(inst)
    inst.components.container:Open(owner)

    inst.expfn = function(attacked, data)
local expchance = 0.5
local damagedur1 = 0.2
local damagedur2 = 0.5
local damagedur3 = 0.7
local damagedur4 = 1

if data and data.attacker and math.random() < damagedur1 then
inst.components.fueled:DoDelta(-150000)
elseif data and data.attacker and math.random() < damagedur2 then
inst.components.fueled:DoDelta(-100000)
elseif data and data.attacker and math.random() < damagedur3 then
inst.components.fueled:DoDelta(-50000)
elseif data and data.attacker and math.random() < damagedur4 then
inst.components.fueled:DoDelta(-50000)

end
if data and data.attacker and math.random() < expchance and inst.level < 4010 then
inst.components.fueled:DoDelta(-50000)
	inst.level = inst.level + 1
			levelexp(inst)

		end 
	    end

    inst.freezefn = function(attacked, data)
        if data and data.attacker and data.attacker.components.freezable and inst:HasTag("level1") then
            data.attacker.components.freezable:AddColdness(0.05)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level2") then
            data.attacker.components.freezable:AddColdness(0.1)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level3") then
            data.attacker.components.freezable:AddColdness(0.15)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level4") then
            data.attacker.components.freezable:AddColdness(0.2)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level5") then
            data.attacker.components.freezable:AddColdness(0.25)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level6") then
            data.attacker.components.freezable:AddColdness(0.24)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level7") then
            data.attacker.components.freezable:AddColdness(0.28)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level8") then
            data.attacker.components.freezable:AddColdness(0.32)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level9") then
            data.attacker.components.freezable:AddColdness(0.36)
            data.attacker.components.freezable:SpawnShatterFX()
        elseif data and data.attacker and data.attacker.components.freezable and inst:HasTag("level10") then
            data.attacker.components.freezable:AddColdness(0.4)
            data.attacker.components.freezable:SpawnShatterFX()
        end 
    end

    inst:ListenForEvent("attacked", inst.freezefn, owner)
    inst:ListenForEvent("attacked", inst.expfn, owner)
end

local function onunequip(inst, owner) 
inst.components.fueled:StopConsuming()        
   inst:RemoveTag("talkf")
    UpgradeArmor(inst)
    owner.AnimState:ClearOverrideSymbol("swap_body")
    owner.AnimState:ClearOverrideSymbol("backpack")
    owner.components.inventory:SetOverflow(nil)
    inst.components.container:Close(owner)

    inst:RemoveEventCallback("attacked", inst.freezefn, owner)
    inst:RemoveEventCallback("attacked", inst.expfn, owner)

end

local function onopen(inst)
  inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
end

local function onclose(inst)
  inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
end

local function fn(Sim)
	local inst = CreateEntity()
    
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
  	inst.entity:AddSoundEmitter()
    MakeInventoryPhysics(inst)
    
    local minimap = inst.entity:AddMiniMapEntity()
    minimap:SetIcon("broken_frosthammer.tex")

    inst.AnimState:SetBank("backpack1")
    inst.AnimState:SetBuild("swap_frostpocket")
    inst.AnimState:PlayAnimation("anim")
    
    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.atlasname = "images/inventoryimages/broken_frosthammer.xml"

   -- inst.components.inventoryitem.cangoincontainer = false
    inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/krampuspack"

    inst:AddComponent("armor")

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

        inst:AddTag("fridge")

    inst:AddComponent("container")
    inst.components.container.itemtestfn = itemtest

    inst.components.container:SetNumSlots(#slotpos_2)
    inst.components.container.widgetslotpos = slotpos_2
    inst.components.container.widgetanimbank = "ui_backpack_2x4"
    inst.components.container.widgetanimbuild = "ui_chest_frosthammer"
    inst.components.container.widgetpos = Vector3(120,-180,0)
    inst.components.container.widgetpos = Vector3(-5,-50,0)
    inst.components.container.side_widget = true
    inst.components.container.type = "pack"

    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose

inst:AddTag("level1")
    inst.OnLoad = OnLoad

        inst:AddComponent("fueled")
       inst.components.fueled.fueltype = "CHEMICAL"
        inst.components.fueled:InitializeFuelLevel(20000000)
       inst.components.fueled:SetDepletedFn(OnDurability)
        inst.components.fueled.ontakefuelfn = TakeItem
        inst.components.fueled.accepting = true
inst.components.fueled:StartConsuming()        

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 19
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(0.7, 0.85, 1, 1)
    inst.components.talker.offset = Vector3(0,100,0)
    inst.components.talker.symbol = "swap_object"

	inst.level = 0
inst:ListenForEvent("levelup", levelexp)
	inst.OnSave = onsave
	inst.OnPreLoad = onpreload

    return inst
end

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

