local assets =
{
    Asset("ANIM", "anim/sword_4.zip"),
    Asset("ANIM", "anim/swap_sword_4.zip"),
	Asset("IMAGE","images/inventoryimages/sword_4.tex"),
	Asset("ATLAS","images/inventoryimages/sword_4.xml"),
	Asset("ANIM", "anim/ui_r503_1x1.zip"),
	Asset("ANIM", "anim/ui_chest_3x3.zip"),
}


-- 事件 - 瞬移完成
local function onBlink(staff, pos, caster)
        caster.components.sanity:DoDelta(-2)

end

--------------
--攻击特效
local function onAttackduan(inst, owner, target)
      if target then
	     if inst:HasTag("power_magma") then 
		    target.components.health:DoDelta(-7)
		 	local x, y, z = target.Transform:GetWorldPosition()
	        local fx = SpawnPrefab("rongyan_1")
	        fx.Transform:SetPosition(x, y, z)
			  --二段伤害
			  if target and target.components.health and not target.components.health:IsDead() then
				 target:DoTaskInTime(5, function() target.components.health:DoDelta(-5) end)
				 local x2, y2, z2 = target.Transform:GetWorldPosition()
				 local fx2 = SpawnPrefab("rongyan_2")
	             fx2.Transform:SetPosition(x2, y2, z2)
			  end

		 end
      end

end

--检测
local function OnDurabilityNil(inst, data)
  if inst:HasTag("power_sea") then
	inst.components.weapon:SetProjectile("langhua")
	inst.components.weapon:SetRange(7, 7)	
  else 
    inst.components.weapon:SetProjectile(nil)
	inst.components.weapon:SetRange(0.8)	
  end
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 function onequip(inst, owner)

    owner.AnimState:OverrideSymbol("swap_object", "swap_sword_4", "swap_sword_4")
    owner.AnimState:Show("ARM_carry")
    owner.AnimState:Hide("ARM_normal")
	--inst.components.container:Open(owner)
	
end

local function onunequip(inst, owner)

    owner.AnimState:Hide("ARM_carry")
    owner.AnimState:Show("ARM_normal")
	inst.components.container:Close(owner)
end

local function itemtest(inst, item, slot)
	return item.prefab == "bluegem" or "redgem" or "purplegem" or "greengem" or "orangegem" or "yellowgem" or "thulecite"
end


local sword_4_slot = {}

--for x = 9, 10 do
	--table.insert(sword_4_slot, Vector3(-x*75 - 1060 , -162 - 330,0))
--end

for y = 2, 0, -1 do
	for x = 0, 2 do
		table.insert(sword_4_slot, Vector3(80*x-80*2+80, 80*y-80*2+80,0))
	end
end

local function fn()
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddSoundEmitter()
	
    inst.AnimState:SetBank("sword_4")
    inst.AnimState:SetBuild("sword_4")
    inst.AnimState:PlayAnimation("idle")
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "sword_4.tex" )
    
	--inst:AddTag("shadow")
	--inst:AddTag("feng")
    inst:AddTag("sharp")
    inst:AddTag("weapon_sword")
    --inst:AddTag("extinguisher")
	


    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(24)

    -------
    inst.components.weapon:SetRange(0.8)
    inst.components.weapon:SetOnAttack(onAttackduan)
	--inst.components.weapon:SetProjectile("langhua")
	--inst.components.weapon:SetRange(10, 10)	

     inst:AddComponent("blinkstaff")
     inst.components.blinkstaff.onblinkfn = onBlink
	
    inst:AddComponent("inspectable")
	


    inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "sword_4"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/sword_4.xml"
	--inst.components.inventoryitem.nobounce = true
	inst.components.inventoryitem.keepondeath = true


	inst:AddComponent("talker")
    inst.components.talker.fontsize = 36
    inst.components.talker.font = TALKINGFONT
    inst.components.talker.colour = Vector3(.9, .4, .4)
    inst.components.talker.offset = Vector3(0, 0, 0)
    inst.components.talker.symbol = "swap_object"
	inst.Transform:SetScale(1.5,1.5,1.5)
	
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    --inst.components.equippable.dapperness = 20/60
	
	inst:AddComponent("stackable")
	inst.components.stackable.maxsize = 1

    inst:AddComponent("container")
    inst.components.container.itemtestfn = itemtest	
    inst.components.container:SetNumSlots(#sword_4_slot)
    inst.components.container.widgetslotpos = sword_4_slot
    inst.components.container.widgetanimbank = "ui_chest_3x3"
    inst.components.container.widgetanimbuild = "ui_chest_3x3"
    inst.components.container.widgetpos = Vector3(0,200,0)
	inst.components.container.side_align_tip = 160
    --inst.components.container.side_widget = true
    --inst.components.container.type = "chest"
    inst.components.container.onopenfn = onopen
    inst.components.container.onclosefn = onclose	
 
 	inst:ListenForEvent("itemget", function(inst, data)
			if data.item.prefab == "bluegem" then 
               inst:AddTag("power_sea")

			end
			if data.item.prefab == "redgem" then 
               inst:AddTag("power_magma")

			end
	end)
	
	inst:ListenForEvent("itemlose", function(inst, data)
			if not inst.components.container:Has("bluegem",1) then
             inst:RemoveTag("power_sea")

			end
			if not inst.components.container:Has("redgem",1) then
             inst:RemoveTag("power_magma")

			end
	end)
	--检测镶嵌的宝石效果
 	--inst:ListenForEvent("ondurabilitynil", ondurabilitynil)
	--inst:DoPeriodicTask(0, function() OnDurabilityNil(inst, data) end)
	inst.powerattack = inst:DoPeriodicTask(0, OnDurabilityNil)
	
    return inst
end

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