
local assets=
{
	Asset("ANIM", "anim/drop_lightsword_sk.zip"),
    Asset("ANIM", "anim/lightsword_sk.zip"), 
	Asset("ANIM", "anim/lightsword_sk_op.zip"),
    Asset("ATLAS", "images/inventoryimages/lightsword_sk.xml"),
}

local prefabs = 
{
}
local function onequip(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_object", "lightsword_sk", "lightsword_sk")
	owner.SoundEmitter:PlaySound("dontstarve/wilson/equip_item_gold")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 

	end
local function onunequip(inst, owner) 
    owner.AnimState:Hide("ARM_carry") 
    owner.AnimState:Show("ARM_normal") 
end

    local function onfinished(inst)
        if inst.components.equippable then
		if GetPlayer() ~= nil then
            local target = GetPlayer()
            local item = target.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
            if inst == item then
                target.components.inventory:GiveItem(item)
            end
            inst:RemoveComponent("equippable")
        end end
    end
local function fn()
local inst = CreateEntity()
local sk = 0
local n = 19
	inst.entity:AddTransform()
	inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
	
	inst.AnimState:SetBank("drop_lightsword_sk")
    inst.AnimState:SetBuild("drop_lightsword_sk")
    inst.AnimState:PlayAnimation("idle")
	
	if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "lightsword_sk"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/lightsword_sk.xml"
     
    inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(onequip)
	inst.components.equippable:SetOnUnequip(onunequip)
    
	inst:AddComponent("inspectable")
local function onusebush(inst)
local owner = inst.components.inventoryitem:GetGrandOwner()
local ndo = inst.components.finiteuses.current
sk = 1
if  inst.components.finiteuses.current < n then ndo = ndo - 1 
inst.components.finiteuses:Use(ndo) 
else inst.components.finiteuses:Use(n)   end
inst.components.weapon:SetRange(15, 20) 
owner.AnimState:OverrideSymbol("swap_object", "lightsword_sk_op", "lightsword_sk_op")
 end
local function onattack(inst, attacker, target)
    local pt1 = attacker:GetPosition()
    local pt2 = target:GetPosition()
	if sk == 1 then 
    attacker:DoTaskInTime(0.01, function()
        attacker.Transform:SetPosition(pt2.x, pt2.y, pt2.z)
        SpawnPrefab("explode_small").Transform:SetPosition(target.Transform:GetWorldPosition())
        SpawnPrefab("groundpoundring_fx").Transform:SetPosition(target.Transform:GetWorldPosition())
        if target.components.locomotor then target.components.locomotor:Stop() end
        attacker:StartThread(function()
            for k = 1, 3 do
                local blood = SpawnPrefab("splash")
				local bloods = SpawnPrefab("swordtx")
                blood.Transform:SetPosition(pt2.x, 1.5, pt2.z)
                blood.Transform:SetScale(2, 2, 2)
				bloods.Transform:SetPosition(pt2.x, 1.5, pt2.z)
                bloods.Transform:SetScale(2, 2, 2)
                Sleep(.1)
				for k = 1, 3 do
				target.components.combat:GetAttacked(attacker, 10) end
            end

				 
        end )
        if target.components.health and target.components.health:GetPercent() < 0.3 then
        end
    end )
    attacker:DoTaskInTime(0.3, function()
        attacker.Transform:SetPosition(pt1.x, pt1.y, pt1.z)
    end ) sk = 0 
	inst.components.useableitem:StopUsingItem()
	inst.components.weapon:SetRange(0, 2)
	attacker.AnimState:OverrideSymbol("swap_object", "lightsword_sk", "lightsword_sk")
	end
end
--
--
inst:AddComponent("useableitem")
    inst.components.useableitem:SetOnUseFn(onusebush)
    inst:AddComponent("weapon")
    inst.components.weapon:SetDamage(30)
  inst.components.weapon:SetRange(0, 2)
    inst.components.weapon:SetOnAttack(onattack)
inst:AddComponent("finiteuses")
	inst.components.finiteuses:SetMaxUses(100)
	inst.components.finiteuses:SetUses(100)
	inst.components.finiteuses:SetOnFinished(onfinished)
-----------交易组件------------
  local function ShouldAcceptItem(inst, item)
       if item.prefab == "magicpower" then
        return true
    end
    return false
end
local function OnGetItemFromPlayer(inst, giver, item)
if item.prefab == "magicpower" then
local nb = -10
local ndo = inst.components.finiteuses.current
if ndo >= 90 then nb = ndo - 100 else nb = -10
inst.components.finiteuses:Use(nb) 
if not inst.components.equippable then
            inst:AddComponent("equippable")
            inst.components.equippable:SetOnEquip(onequip)
            inst.components.equippable:SetOnUnequip(onunequip)
        end
 end end
end
inst:AddComponent("trader")
    inst.components.trader.onaccept = OnGetItemFromPlayer
    inst.components.trader:SetAcceptTest(ShouldAcceptItem)
    return inst
end
return Prefab("lightsword_sk", fn, assets)