
---------------------------
local assets=
{
	Asset("ANIM", "anim/bangbangtang.zip"),
   	Asset("ATLAS", "images/inventoryimages/bangbangtang.xml"),
	Asset("IMAGE", "images/inventoryimages/bangbangtang.tex"),
	Asset("ANIM", "anim/swap_bangbangtang.zip"),
	Asset("SOUNDPACKAGE", "sound/qiuqiu.fev"),
	Asset("SOUND", "sound/qiuqiu.fsb"),
}

local function levelexp(inst,data)

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


if inst.level >=0 and inst.level <10 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/10")
elseif inst.level >=10 and inst.level <30 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/30")
elseif inst.level >=30 and inst.level <50 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/50")
elseif inst.level >=50 and inst.level <70 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/70")
elseif inst.level >=70 and inst.level <90 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/90")
elseif inst.level >=90 and inst.level <120 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/120")
elseif inst.level >=120 and inst.level <150 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/150")
elseif inst.level >=150 and inst.level <180 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/180")
elseif inst.level >=180 and inst.level <210 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/210")
elseif inst.level >=210 and inst.level <250 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/250")
elseif inst.level >=250 and inst.level <350 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/350")
elseif inst.level >=350 and inst.level <450 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/450")
elseif inst.level >=450 and inst.level <550 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/550")
elseif inst.level >=550 and inst.level <650 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/650")
elseif inst.level >=650 and inst.level <750 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/750")
elseif inst.level >=750 and inst.level <850 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/850")
elseif inst.level >=850 and inst.level <950 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/950")
elseif inst.level >=950 and inst.level <1050 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/1050")
elseif inst.level >=1050 and inst.level <1200 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/1200")
elseif inst.level >=1200 and inst.level <1400 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/1400")
elseif inst.level >=1400 and inst.level <1600 then
inst.components.talker:Say("[经验值]\n".. (inst.level).."/1600")
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
end

local function Upgradedamage(inst, data)
if inst.components.fueled:IsEmpty() then
inst.broken = true
elseif not inst.components.fueled:IsEmpty() then
inst.broken = false
end
if inst.broken then
	inst.boost = false 	
	inst:RemoveComponent("spellcaster")
   inst.components.weapon:SetDamage(1)
	inst.components.talker:Say("-已经破损 \n伤害 (1)")
  
elseif not inst.broken then

 if inst.level >=0 and inst.level <10 then
    inst.components.weapon:SetDamage(40)
	inst.components.talker:Say("(LV1)\n伤害 (40)")
 elseif inst.level >=10 and inst.level <30 then
    inst.components.weapon:SetDamage(41)
	inst.components.talker:Say("(LV2)\n伤害 (41)")
 elseif inst.level >=30 and inst.level <50 then
    inst.components.weapon:SetDamage(42)
	inst.components.talker:Say("(LV3)\n伤害 (42)")
 elseif inst.level >=50 and inst.level <70 then
    inst.components.weapon:SetDamage(43)
	inst.components.talker:Say("(LV4)]\n伤害 (43)")
  elseif inst.level >=70 and inst.level <90 then
    inst.components.weapon:SetDamage(44)
	inst.components.talker:Say("(LV5)\n伤害 (44)")
 elseif inst.level >=90 and inst.level <120 then
    inst.components.weapon:SetDamage(45)
	inst.components.talker:Say("(LV6)\n伤害 (45)")
 elseif inst.level >=120 and inst.level <150 then
    inst.components.weapon:SetDamage(46)
	inst.components.talker:Say("(LV7)\n伤害 (46)")
elseif inst.level >=150 and inst.level <180 then
    inst.components.weapon:SetDamage(47)
	inst.components.talker:Say("(LV8)\n伤害 (47)")
elseif inst.level >=180 and inst.level <210 then
    inst.components.weapon:SetDamage(48)
	inst.components.talker:Say("(LV9)\n伤害 (48)")
elseif inst.level >=210 and inst.level <250 then
    inst.components.weapon:SetDamage(49)
	inst.components.talker:Say("(LV10)\n伤害 (49)")
elseif inst.level >=250 and inst.level <350 then
    inst.components.weapon:SetDamage(50)
	inst.components.talker:Say("(LV11)\n伤害 (50)")
elseif inst.level >=350 and inst.level <450 then
    inst.components.weapon:SetDamage(51)
	inst.components.talker:Say("(LV12)\n伤害 (51)")
elseif inst.level >=450 and inst.level <550 then
    inst.components.weapon:SetDamage(52)
	inst.components.talker:Say("(LV13)\n伤害 (52)")
elseif inst.level >=550 and inst.level <650 then
    inst.components.weapon:SetDamage(53)
	inst.components.talker:Say("(LV14)\n伤害 (53)")
elseif inst.level >=650 and inst.level <750 then
    inst.components.weapon:SetDamage(54)
	inst.components.talker:Say("(LV15)\n伤害 (54)")
elseif inst.level >=750 and inst.level <850 then
    inst.components.weapon:SetDamage(55)
	inst.components.talker:Say("(LV16)\n伤害 (55)")
elseif inst.level >=850 and inst.level <950 then
    inst.components.weapon:SetDamage(56)
	inst.components.talker:Say("(LV17)\n伤害 (56)")
elseif inst.level >=950 and inst.level <1050 then
    inst.components.weapon:SetDamage(57)
	inst.components.talker:Say("(LV18)\n伤害 (57)")
elseif inst.level >=1050 and inst.level <1200 then
    inst.components.weapon:SetDamage(58)
	inst.components.talker:Say("(LV19)\n伤害 (58)")
elseif inst.level >=1200 and inst.level <1400 then
    inst.components.weapon:SetDamage(59)
	inst.components.talker:Say("(LV20)\n伤害 (59)")
elseif inst.level >=1400  then
    inst.components.weapon:SetDamage(60)
	inst.components.talker:Say("(LV21)\n伤害 (60)\n已满级")
end
end
end



local function OnDurability(inst, data)
inst.broken = true
 	Upgradedamage(inst)
	if inst.task then inst.task:Cancel() inst.task = nil 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.broken = false      
Upgradedamage(inst)
	if   inst.level <= 4600 then
	inst.level = inst.level + 5
	levelexp(inst)
     end


	if inst.broken then
	inst.broken = false 
	end
end
-------- --------
local function OnLoad(inst, data)
    Upgradedamage(inst)
end




local function onattack_Frost(inst, attacker, target)
local player = inst.components.inventoryitem.owner
local freezechance1 = 0.2
local freezechance2 = 0.3
local freezechance3 = 0.4
local freezechance1b = 0.4
local freezechance2b = 0.6
local freezechance3b = 0.8
local expchance = 0.1
local damagedur1 = 0.2
local damagedur2 = 0.5
local damagedur3 = 0.7
local damagedur4 = 1


    if target and not inst.broken and math.random() < damagedur1 then
inst.components.fueled:DoDelta(-150000)
    elseif target and not inst.broken and math.random() < damagedur2 then
inst.components.fueled:DoDelta(-75000)
    elseif target and not inst.broken and math.random() < damagedur3 then
inst.components.fueled:DoDelta(-35000)
    elseif target and not inst.broken and math.random() < damagedur4 then
inst.components.fueled:DoDelta(-20000)
    elseif target and inst.broken then
SpawnPrefab("splash").Transform:SetPosition(inst:GetPosition():Get())
	inst.components.talker:Say("破损的棒棒糖 \n伤害 (1)")
    end

	if  math.random() < freezechance1 and target.components.freezable and not inst.boost and inst.level <250 then
        target.components.freezable:AddColdness(0.12)
        target.components.freezable:SpawnShatterFX()
		target.components.health:DoDelta(-2)
		local prefab = "icespike_fx_"..math.random(1,4)
		local fx = SpawnPrefab(prefab)
		fx.Transform:SetScale(1, 0.6, 1)
		fx.Transform:SetPosition(target:GetPosition():Get())
	elseif math.random() < freezechance2 and target.components.freezable and not inst.boost and inst.level >=250 and inst.level <1400 then
        target.components.freezable:AddColdness(0.24)
        target.components.freezable:SpawnShatterFX()
		target.components.health:DoDelta(-4)
		local prefab = "icespike_fx_"..math.random(1,4)
		local fx = SpawnPrefab(prefab)
		fx.Transform:SetScale(1, 0.8, 1)
		fx.Transform:SetPosition(target:GetPosition():Get())
	elseif math.random() < freezechance3 and target.components.freezable and not inst.boost and inst.level >=1400 then
        target.components.freezable:AddColdness(0.36)
        target.components.freezable:SpawnShatterFX()
		target.components.health:DoDelta(-8)		
		local prefab = "icespike_fx_"..math.random(1,4)
		local fx = SpawnPrefab(prefab)
		fx.Transform:SetScale(1, 0.9, 1)
		fx.Transform:SetPosition(target:GetPosition():Get())
	elseif math.random() < freezechance1b and target.components.freezable and inst.boost and inst.level <250 then
        target.components.freezable:AddColdness(0.24)
        target.components.freezable:SpawnShatterFX()
		target.components.health:DoDelta(-4)
		local prefab = "icespike_fx_"..math.random(1,4)
		local fx = SpawnPrefab(prefab)
		fx.Transform:SetScale(1, 0.8, 1)
		fx.Transform:SetPosition(target:GetPosition():Get())
	elseif math.random() < freezechance2b and target.components.freezable and inst.boost and inst.level >=250 and inst.level <1400 then
        target.components.freezable:AddColdness(0.48)
        target.components.freezable:SpawnShatterFX()
		target.components.health:DoDelta(-8)
		local prefab = "icespike_fx_"..math.random(1,4)
		local fx = SpawnPrefab(prefab)
		fx.Transform:SetScale(1, 1, 1)
		fx.Transform:SetPosition(target:GetPosition():Get())
	elseif math.random() < freezechance3b and target.components.freezable and inst.boost and inst.level >=1400 then
        target.components.freezable:AddColdness(0.72)
        target.components.freezable:SpawnShatterFX()
		target.components.health:DoDelta(-16)		
		local prefab = "icespike_fx_"..math.random(1,4)
		local fx = SpawnPrefab(prefab)
		fx.Transform:SetScale(1, 1.2, 1)
		fx.Transform:SetPosition(target:GetPosition():Get())
	end
    if target.components.sleeper and target.components.sleeper:IsAsleep() then
        target.components.sleeper:WakeUp()
    end
    if target.components.burnable and target.components.burnable:IsBurning() then
        target.components.burnable:Extinguish()
    end
    if target.components.combat then
        target.components.combat:SuggestTarget(attacker)
    end
end
local function getspawnlocation(inst, target)
    if inst and  inst.components.combat.target then
    local tarPos = inst.components.combat.target:GetPosition()
    local pos = inst:GetPosition()
    local vec = tarPos - pos
    vec = vec:Normalize()
    local dist = pos:Dist(tarPos)
    return pos + (vec * (dist * .15))
	end
end

local function createlight(staff, target, pos)
    local caster = staff.components.inventoryitem.owner
if caster.components.sanity:GetPercent() >= 0.35 then--脑残大于等于35%才能召唤飓风
    caster.components.sanity:DoDelta(-35)
    local light = SpawnPrefab("lightning")
	light.Transform:SetPosition(pos.x, pos.y, pos.z)
    local target = caster.components.combat.target
    if caster and target then
    local tornado = SpawnPrefab("tornado_moose")
	caster.SoundEmitter:PlaySound("qiuqiu/shouji/shouji")
    local spawnPos = caster:GetPosition() + TheCamera:GetDownVec()
    local totalRadius = caster.components.combat.target.Physics and caster.components.combat.target.Physics:GetRadius() or 0.5 + tornado.Physics:GetRadius() + 0.5
    local target = caster.components.combat.target 
    local targetPos = caster.components.combat.target:GetPosition() + (TheCamera:GetDownVec() * totalRadius)
    tornado.Transform:SetPosition(getspawnlocation(caster, target):Get())
    tornado.components.knownlocations:RememberLocation("target", targetPos)
	else
	local target = caster.components.combat.target
	if caster and not target then
    local tornado = SpawnPrefab("tornado_moose")
	caster.SoundEmitter:PlaySound("qiuqiu/shouji/shouji")
    local targetPos = caster:GetPosition()
	 tornado.Transform:SetPosition(pos.x, pos.y, pos.z)
    --tornado.Transform:SetPosition(getspawnlocation(caster, target):Get())
    tornado.components.knownlocations:RememberLocation("target", targetPos)
    	end
    end
elseif caster.components.sanity:GetPercent() < 0.35 then--
    caster.components.talker:Say("脑残过低不能召唤飓风")
	local fail1 = SpawnPrefab("statue_transition")
    local fail2 = SpawnPrefab("statue_transition_2")

    fail1.Transform:SetPosition(pos.x, pos.y, pos.z)
    fail2.Transform:SetPosition(pos.x, pos.y, pos.z)
    end
end

local function yellow_reticuletargetfn()
    return Vector3(ThePlayer.entity:LocalToWorldSpace(5, 0, 0))
end


local function onequip(inst, owner)

owner.fire = true
    Upgradedamage(inst)
 
	if not owner:HasTag("qiuhaizi") then
	inst.Transform:SetScale(1.5,1.5,1.5)
	end
     owner.AnimState:OverrideSymbol("swap_object", "swap_bangbangtang", "swap_bangbangtang")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 

if  inst.level >=1400 then  --1400

    inst.entity:AddLight()
	inst.Light:SetRadius(.8)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(225/255,120/255,80/255)
end
end

local function onunequip(inst, owner) 
owner.fire = false
    Upgradedamage(inst)
	inst.Light:Enable(false)
    inst:RemoveComponent("spellcaster")
	inst.boost = false 
    if inst.task then inst.task:Cancel() inst.task = nil end
    if inst.sanity then inst.sanity:Cancel() inst.sanity = nil end
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 

end

local function sanity(inst, owner)
if owner.components.health and owner.components.health:IsHurt() then
        owner.components.sanity:DoDelta(1,false)
end
end

local function OnDropped(inst,data)
    end
local function OnPutInInventory(inst)
    end
	
local function consume(inst, owner)
if not inst.broken and inst.boost then
inst.components.fueled:DoDelta(-50000)
end
if inst.broken and inst.boost then
inst.components.fueled:DoDelta(0)
end
end

local function on_boost(inst, data)
local player = inst.components.inventoryitem.owner
        local owner = inst.components.inventoryitem.owner
		    inst.AnimState:SetBank("bangbangtang")
    inst.AnimState:SetBuild("bangbangtang")
    inst.AnimState:PlayAnimation("idle")
local change_fx = SpawnPrefab("small_puff")
        local follower = change_fx.entity:AddFollower()
				follower:FollowSymbol( owner.GUID, "swap_object", 1, -150, 1 )

    inst.AnimState:SetBank("bangbangtang")
    inst.AnimState:SetBuild("bangbangtang")
    inst.AnimState:PlayAnimation("idle")
if owner and inst.broken then
	inst.boost = false 	
	inst:RemoveComponent("spellcaster")
	player.components.talker:Say("棒棒糖已经破损 !")
	inst.components.talker:Say("-[棒棒糖]\n耐久 (0)")
    --inst.components.equippable.walkspeedmult = 1
	inst.Light:Enable(false)
    owner.AnimState:OverrideSymbol("swap_object", "swap_bangbangtang", "swap_bangbangtang")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
        inst.components.useableitem:StopUsingItem()
end
if owner and not inst.boost and not inst.broken then
	inst.boost = true
    inst.task = inst:DoPeriodicTask(1, function() consume(inst, owner) end)
inst.SoundEmitter:PlaySound("dontstarve/common/fireAddFuel") 
         inst.entity:AddLight()
--SpawnPrefab("small_puff").Transform:SetPosition(inst:GetPosition():Get())

        inst.components.useableitem:StopUsingItem()
 
if inst.level <500 then  --250
	inst.components.talker:Say("[冰冻(弱)]\n光(小)")
    --inst.components.equippable.walkspeedmult = 1.1
	inst.Light:SetRadius(.4)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(225/255,120/255,80/255)
	inst.Light:Enable(true)
	
elseif inst.level >=500 and inst.level <1400 then  --250  1400
inst.components.talker:Say("[冰冻(中)]\n光(中)\n召唤飓风")
     --inst.components.equippable.walkspeedmult = 1.2
	inst.Light:SetRadius(.7)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(225/255,120/255,80/255)
	inst.Light:Enable(true)
	
	   inst:AddComponent("reticule")
    inst.components.reticule.targetfn = yellow_reticuletargetfn
    inst.components.reticule.ease = true

    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(createlight)
    inst.components.spellcaster.canuseonpoint = true
    MakeHauntableLaunch(inst)
    AddHauntableCustomReaction(inst, function(inst, haunter)
        if math.random() <= TUNING.HAUNT_CHANCE_RARE then
            local pos = Vector3(inst.Transform:GetWorldPosition())
            local start_angle = math.random()*2*PI
            local offset = FindWalkableOffset(pos, start_angle, math.random(3,12), 60, false, true)
            local pt = pos + offset
            createlight(inst, nil, pt)
            inst.components.hauntable.hauntvalue = TUNING.HAUNT_LARGE
            return true
        end
        return false
    end, true, false, true)
	
elseif inst.level >=1400 then  --1400
	inst.components.talker:Say("[冰冻(强)]\n光(强)\n召唤飓风")
     
    inst.sanity = inst:DoPeriodicTask(10, function() sanity(inst, owner) end)
  --inst.components.equippable.walkspeedmult = 1.3
	inst.Light:SetRadius(1)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(225/255,120/255,80/255)
	inst.Light:Enable(true)
	inst.AnimState:SetBank("bangbangtang")
    inst.AnimState:SetBuild("bangbangtang")
    inst.AnimState:PlayAnimation("idle")
	
     inst:AddComponent("reticule")
    inst.components.reticule.targetfn = yellow_reticuletargetfn
    inst.components.reticule.ease = true

    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(createlight)
    inst.components.spellcaster.canuseonpoint = true
    MakeHauntableLaunch(inst)
    AddHauntableCustomReaction(inst, function(inst, haunter)
        if math.random() <= TUNING.HAUNT_CHANCE_RARE then
            local pos = Vector3(inst.Transform:GetWorldPosition())
            local start_angle = math.random()*2*PI
            local offset = FindWalkableOffset(pos, start_angle, math.random(3,12), 60, false, true)
            local pt = pos + offset
            createlight(inst, nil, pt)
            inst.components.hauntable.hauntvalue = TUNING.HAUNT_LARGE
            return true
        end
        return false
    end, true, false, true)
   
end
-----------
elseif owner and inst.boost and not inst.broken then
	inst.boost = false 
inst.components.talker:Say("需要材料恢复耐久 !")
      inst:RemoveComponent("spellcaster")
    if inst.task then inst.task:Cancel() inst.task = nil end
    if inst.sanity then inst.sanity:Cancel() inst.sanity = nil end

	inst.Light:Enable(false)

  inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
onequip(inst, owner)

        inst.components.useableitem:StopUsingItem()
end
end


local function fn()
	local inst = CreateEntity()
	
	inst.entity:AddTransform()
    inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
	
    inst.entity:AddLight()
	inst.Light:SetRadius(.4)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(80/255,120/255,250/255)
	inst.Light:Enable(true)
	
	MakeInventoryPhysics(inst)  
  
    inst.AnimState:SetBank("bangbangtang")
    inst.AnimState:SetBuild("bangbangtang")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetMultColour(1, 1, 1, 0.7)

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "bangbangtang.tex" )
	inst:AddTag("sharp")
	 inst:AddTag("waterproofer")
	 inst:AddComponent("talker")
    inst.components.talker.fontsize = 30
     inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(1, 0.8, 0.65, 1)
    inst.components.talker.offset = Vector3(200,-250,0)
    inst.components.talker.symbol = "swap_object"

	
	
     inst.entity:SetPristine()
 
    if not TheWorld.ismastersim then
        return inst
    end
	
   inst:AddComponent("weapon")
    inst.components.weapon:SetOnAttack(Upgradedamage)
	inst.components.weapon:SetOnAttack(onattack_Frost)
	inst.components.weapon:SetRange(1.2)
	
    inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem:SetOnDroppedFn(OnDropped)
    inst.components.inventoryitem:SetOnPutInInventoryFn(OnPutInInventory)
    	inst.components.inventoryitem.atlasname = "images/inventoryimages/bangbangtang.xml"
  
    inst.OnLoad = OnLoad

    inst:AddComponent("waterproofer")
    inst.components.waterproofer.effectiveness = 0

    inst:AddComponent("useableitem")
    inst.components.useableitem:SetOnUseFn(on_boost)
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )

       inst:AddComponent("fueled")
       inst.components.fueled.fueltype = "CHEMICAL"
       inst.components.fueled:InitializeFuelLevel(30000000)
		
       inst.components.fueled:SetDepletedFn(OnDurability)
       inst.components.fueled.ontakefuelfn = TakeItem
       inst.components.fueled.accepting = true
	inst.components.fueled:StopConsuming()        
	inst.boost = false 

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

    MakeHauntableLaunch(inst)
    return inst
end

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