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

}

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

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

if inst.level <= 500 then
inst.components.talker:Say("-[Phoenix Blade II]- \n[Grow Points]".. (inst.level))
inst:RemoveTag("levelup")
end
if inst.level >=0 and inst.level <100 then
inst.components.talker:Say("-[Phoenix Blade II] \nNext:[100]\n[Grow Points]".. (inst.level))
inst:AddTag("level1")
elseif inst.level >=100 and inst.level <200 then
inst.components.talker:Say("-[Phoenix Blade II] \nNext:[200]\n[Grow Points]".. (inst.level))
inst:AddTag("level2")
inst:RemoveTag("level1")
elseif inst.level >=200 and inst.level <300 then
inst.components.talker:Say("-[Phoenix Blade II] \nNext:[300]\n[Grow Points]".. (inst.level))
inst:AddTag("level3")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
elseif inst.level >=300 and inst.level <500 then
inst.components.talker:Say("-[Phoenix Blade II] \nNext:[500]\n[Grow Points]".. (inst.level))
inst:AddTag("level4")
inst:RemoveTag("level1")
inst:RemoveTag("level2")
inst:RemoveTag("level3")
elseif inst.level >=500 then
inst.components.talker:Say("-[Phoenix Blade II] \n[MAX]")
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("-Broken PhoenixBlade \nDamage (1)")
end


local function Upgradedamage(inst)
if inst.components.fueled:IsEmpty() then
inst:AddTag("broken") 
   inst.components.weapon:SetDamage(1)
	inst.components.talker:Say("-Broken PhoenixBlade \nDamage (1)")
end
if inst:HasTag("broken") then
   inst.components.weapon:SetDamage(1)
  inst.components.equippable.walkspeedmult = 1
	inst.components.talker:Say("-Broken PhoenixBlade \nDamage (1)")
   end
if not inst:HasTag("broken") then
        if inst:HasTag("level1") then
    inst.components.weapon:SetDamage(52)
  inst.components.equippable.walkspeedmult = 1.03
	inst.components.talker:Say("-[Phoenix Blade II (LV1)]\nDamage (52)\nCritical(5per)\nSpeed up(3)")
        elseif inst:HasTag("level2") then
    inst.components.weapon:SetDamage(53)
  inst.components.equippable.walkspeedmult = 1.06
	inst.components.talker:Say("-[Phoenix Blade II (LV2)]\nDamage (53)\nCritical(8per)nSpeed up(6)")
        elseif inst:HasTag("level3") then
    inst.components.weapon:SetDamage(54)
  inst.components.equippable.walkspeedmult = 1.09
	inst.components.talker:Say("-[(Phoenix Blade II (LV3)]\nDamage (54)\nCritical(12per)nSpeed up(9)")
        elseif inst:HasTag("level4") then
    inst.components.weapon:SetDamage(55)
  inst.components.equippable.walkspeedmult = 1.12
	inst.components.talker:Say("-[Phoenix Blade II (LV4)]\nDamage (55)\nCritical(16per)nSpeed up(12)")
        elseif inst:HasTag("level5") then
    inst.components.weapon:SetDamage(56)
    inst.components.equippable.walkspeedmult = 1.15
	inst.components.talker:Say("-[Phoenix Blade II (LV5)]\nDamage (56)\nCritical(20per)nSpeed up(15)")
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("Lucky Points ! +(2)\n[Grow Points]".. (inst.level))
    elseif  math.random() < expchance2 and inst.level <= 4005 then
	inst.level = inst.level + 5
	levelexp(inst)
	inst.components.talker:Say("Lucky Points ! +(5)\n[Grow Points]".. (inst.level))
	elseif  math.random() < expchance3 and inst.level <= 4005 then
	inst.level = inst.level + 8
	levelexp(inst)
	inst.components.talker:Say("Lucky Points ! +(8)\n[Grow Points]".. (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.08
local framechance3 = 0.12
local framechance4 = 0.16
local framechance5 = 0.2
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("-Broken Weapon \nDamage (1)")
    end

  	  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(-48)
  	  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(-49)
  	  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(-50)
  	  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(-51)
  	  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(-53)

    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") and not player:HasTag("DLC") then
            target.sg:GoToState("hit")
        target.components.burnable:Ignite()
local fx = SpawnPrefab("statue_transition")
local pos = Vector3(target.Transform:GetWorldPosition())
	fx.Transform:SetPosition(pos:Get())
        attacker.components.sanity:DoDelta(-15)
		target.components.health:DoDelta(-112)
		player:RemoveTag("framea") 

    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") and player:HasTag("DLC") then
            target.sg:GoToState("hit")
        target.components.burnable:Ignite()
local fx = SpawnPrefab("groundpoundring_fx")
local pos = Vector3(target.Transform:GetWorldPosition())
	fx.Transform:SetPosition(pos:Get())
        attacker.components.sanity:DoDelta(-15)
		target.components.health:DoDelta(-112)
		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_mushasword2", "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("mushasword2")
    inst.AnimState:PlayAnimation("idle")
    inst.AnimState:SetMultColour(1, 1, 1, 0.6)

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

      inst.components.weapon:SetOnAttack(onattack_FRAME)


    -------
    

    inst:AddComponent("inspectable")
    
    inst:AddComponent("inventoryitem")
    	inst.components.inventoryitem.atlasname = "images/inventoryimages/mushasword2.xml"
     inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "blade_2.tex" )

        inst.entity:AddLight()
	inst.Light:SetRadius(.5)
    inst.Light:SetFalloff(.9)
    inst.Light:SetIntensity(.9)
    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/mushasword2", fn, assets) 
