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

}

---------------------------
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 <50 then
inst.components.talker:Say("-[凤凰剑 I] \n下一级:[50]\n[成长点数]".. (inst.level))
inst:AddTag("level1")
elseif inst.level >=50 and inst.level <100 then
inst.components.talker:Say("-[凤凰剑 I] \n下一级:[100]\n[成长点数]".. (inst.level))
inst:AddTag("level2")
inst:RemoveTag("level1")
elseif inst.level >=100 and inst.level <200 then
inst.components.talker:Say("-[凤凰剑 I] \n下一级:[200]\n[成长点数]".. (inst.level))
inst:AddTag("level3")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
elseif inst.level >=200 and inst.level <400 then
inst.components.talker:Say("-[凤凰剑 I] \n下一级:[400]\n[成长点数]".. (inst.level))
inst:AddTag("level4")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
elseif inst.level >=400 then
inst.components.talker:Say("-[凤凰剑 I] \n[满级]")
inst:AddTag("level5")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
inst:RemoveTag("level4")
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 OnDurability(inst, data)
inst:AddTag("broken")
    inst.components.weapon:SetDamage(1)
	inst.components.talker:Say("-残破的 凤凰剑 \n伤害 (1)")
end

local function Upgradedamage(inst)
if inst.components.fueled:IsEmpty() then
inst:AddTag("broken") 
   inst.components.weapon:SetDamage(1)
	inst.components.talker:Say("-残破的 凤凰剑 \n伤害 (1)")
end
if inst:HasTag("broken") then
   inst.components.weapon:SetDamage(1)
  inst.components.equippable.walkspeedmult = 1
	inst.components.talker:Say("-残破的 凤凰剑 \n伤害 (1)")
   end
if not inst:HasTag("broken") then
        if inst:HasTag("level1") then
    inst.components.weapon:SetDamage(43)
  inst.components.equippable.walkspeedmult = 1.02
	inst.components.talker:Say("-[凤凰剑 I (1级)]\n伤害 (43)\n属性攻击(5%)\n速度上升(2)")
        elseif inst:HasTag("level2") then
    inst.components.weapon:SetDamage(44)
  inst.components.equippable.walkspeedmult = 1.04
	inst.components.talker:Say("-[凤凰剑 I (2级)]\n伤害 (44)\n属性攻击(7%)\n速度上升(4)")
        elseif inst:HasTag("level3") then
    inst.components.weapon:SetDamage(45)
  inst.components.equippable.walkspeedmult = 1.06
	inst.components.talker:Say("-[凤凰剑 I (3级)]\n伤害 (45)\n属性攻击(9%)\n速度上升(6)")
        elseif inst:HasTag("level4") then
    inst.components.weapon:SetDamage(46)
  inst.components.equippable.walkspeedmult = 1.08
	inst.components.talker:Say("-[凤凰剑 I (4级)]\n伤害 (46)\n属性攻击(11%)\n速度上升(8)")
        elseif inst:HasTag("level5") then
    inst.components.weapon:SetDamage(48)
  inst.components.equippable.walkspeedmult = 1.1
	inst.components.talker:Say("-[凤凰剑 I (满级5级)]\n伤害 (48)\n属性攻击(13%)\n速度上升(10)")
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")     
Upgradedamage(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)
    Upgradedamage(inst)
end

local function onattack_FRAME(inst, attacker, target)
local player = GetPlayer()
local framechance1 = 0.05
local framechance2 = 0.07
local framechance3 = 0.09
local framechance4 = 0.11
local framechance5 = 0.13
local expchance = 0.05
local damagedur1 = 0.2
local damagedur2 = 0.5
local damagedur3 = 0.7
local damagedur4 = 1

if math.random() < expchance and not inst:HasTag("broken") and inst.level < 4010 then
	inst.level = inst.level + 1
	levelexp(inst)
end
    if target and not inst:HasTag("broken") and math.random() < damagedur1 then
inst.components.fueled:DoDelta(-150000)
    elseif target and not inst:HasTag("broken") and math.random() < damagedur2 then
inst.components.fueled:DoDelta(-75000)
    elseif target and not inst:HasTag("broken") and math.random() < damagedur3 then
inst.components.fueled:DoDelta(-35000)
    elseif target and not inst:HasTag("broken") and math.random() < damagedur4 then
inst.components.fueled:DoDelta(-20000)
    elseif target and inst:HasTag("broken") then
		local fx = SpawnPrefab("splash")
	local pos = Vector3(target.Transform:GetWorldPosition())
fx.Transform:SetPosition(pos:Get())
	inst.components.talker:Say("-残破的 武器 \n伤害 (1)")
    end

--[[    if target.components.burnable and target.sg and not target.sg:HasStateTag("frozen") and not target:HasTag("wall") and not target:HasTag("structure") and target.sg.sg.states.hit and math.random() < framechance1 and not player:HasTag("framea") then
            target.sg:GoToState("hit")
        target.components.burnable:Ignite()
        target.components.health:DoDelta(-46)]]

  	  if target and target.components.burnable and inst:HasTag("level1") and not target:HasTag("wall") and not target:HasTag("structure") and math.random() < framechance1 and not player:HasTag("framea") then
        target.components.burnable:Ignite()
        target.components.health:DoDelta(-43)
  	  elseif target and target.components.burnable and inst:HasTag("level2") and not target:HasTag("wall") and not target:HasTag("structure") and math.random() < framechance1 and not player:HasTag("framea") then
        target.components.burnable:Ignite()
        target.components.health:DoDelta(-44)
  	  elseif target and target.components.burnable and inst:HasTag("level3") and not target:HasTag("wall") and not target:HasTag("structure") and math.random() < framechance1 and not player:HasTag("framea") then
        target.components.burnable:Ignite()
        target.components.health:DoDelta(-45)
  	  elseif target and target.components.burnable and inst:HasTag("level4") and not target:HasTag("wall") and not target:HasTag("structure") and math.random() < framechance1 and not player:HasTag("framea") then
        target.components.burnable:Ignite()
        target.components.health:DoDelta(-46)
  	  elseif target and target.components.burnable and inst:HasTag("level5") and not target:HasTag("wall") and not target:HasTag("structure") and math.random() < framechance1 and not player:HasTag("framea") then
        target.components.burnable:Ignite()
        target.components.health:DoDelta(-48)

    elseif target.components.burnable and target.sg and not target.sg:HasStateTag("frozen") and not target:HasTag("wall") and not target:HasTag("structure") and player:HasTag("framea") then
            target.sg:GoToState("hit")
        target.components.burnable:Ignite()
SpawnPrefab("sparks").Transform:SetPosition(target:GetPosition():Get())
        attacker.components.sanity:DoDelta(-15)
		target.components.health:DoDelta(-92)
		player:RemoveTag("framea") 
	    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
		player:RemoveTag("framea") 
end

local function OnDropped(inst,data)
   inst:AddTag("talkf")
    end
local function OnPutInInventory(inst)
   inst:RemoveTag("talkf")
    end

local function onequip(inst, owner)
    Upgradedamage(inst)
   inst:AddTag("talkf")
local player = GetPlayer()
    owner.AnimState:OverrideSymbol("swap_object", "swap_mushasword", "swap_nightmaresword")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
		player:AddTag("phoenixblade") 

end
local function onunequip(inst, owner) 
    Upgradedamage(inst)
   inst:RemoveTag("talkf")
local player = GetPlayer()
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
		player:RemoveTag("phoenixblade") 
		player:RemoveTag("framea") 

end


local function fn(Sim)
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    
    inst.AnimState:SetBank("nightmaresword")
    inst.AnimState:SetBuild("mushasword")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetMultColour(1, 1, 1, 0.6)

    inst:AddTag("sharp")
    
    inst:AddComponent("weapon")
    inst.components.weapon:SetOnAttack(Upgradedamage)

      inst.components.weapon:SetOnAttack(onattack_FRAME)

    -------
    
    inst:AddComponent("inventoryitem")
    	inst.components.inventoryitem.atlasname = "images/inventoryimages/mushasword.xml"
     inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "blade_1.tex" )

        inst.entity:AddLight()
	inst.Light:SetRadius(.3)
    inst.Light:SetFalloff(.7)
    inst.Light:SetIntensity(.5)
    inst.Light:SetColour(250/255,65/255,65/255)
	inst.Light:Enable(true)

    inst.OnLoad = OnLoad

    inst:AddComponent("talker")
    inst.components.talker.fontsize = 19
    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:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( onequip )
    inst.components.equippable:SetOnUnequip( onunequip )

        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:StopConsuming()

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

    return inst
end

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