local assets=
{
    Asset("ANIM", "anim/lunarstaff.zip"),
    Asset("ANIM", "anim/swap_lunarstaff.zip"),

    Asset("ATLAS", "inventoryimages/lunarstaff.xml"),
    Asset("IMAGE", "inventoryimages/lunarstaff.tex"),
}

local prefabs =
{
    "staffcastfx",
	"staffmeteor",

}

local function onfinished(inst)
    inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
    inst:Remove()
end



local function OnEquip(inst, owner)
		owner.SoundEmitter:PlaySound("dontstarve/common/telebase_gemplace")
        owner.AnimState:OverrideSymbol("swap_object", "swap_lunarstaff", "swap_lunarstaff")
        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 createlight(staff, target, pos)

 local caster = staff.components.inventoryitem.owner
    if caster ~= nil and caster.components.sanity ~= nil then
        caster.components.sanity:DoDelta(-10)
    end

    local light = SpawnPrefab("lunarshower")
	light.LUNARCASTER = staff.components.inventoryitem.owner
    light.Transform:SetPosition(pos:Get())








			staff.components.finiteuses:Use(1)

end


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



local function fn(Sim)
    local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()
    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("lunarstaff")
    inst.AnimState:SetBuild("lunarstaff")
    inst.AnimState:PlayAnimation("idle")


      inst.entity:SetPristine()


    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inspectable")

    inst.fxcolour = {223/255, 208/255, 69/255}
    inst.castsound = "dontstarve/common/staffteleport"

    inst:AddComponent("spellcaster")
    inst.components.spellcaster:SetSpellFn(createlight)
    inst.components.spellcaster.canuseonpoint = true
	inst:AddComponent("reticule")
    inst.components.reticule.targetfn = yellow_reticuletargetfn
    inst.components.reticule.ease = true
    inst:AddTag("nopunch")
    inst:AddComponent("finiteuses")
    inst.components.finiteuses:SetOnFinished(onfinished)
	inst.components.finiteuses:SetMaxUses(TUNING.YELLOWSTAFF_USES * 2)
    inst.components.finiteuses:SetUses(TUNING.YELLOWSTAFF_USES * 2)

    MakeHauntableLaunch(inst)


    inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
    inst.components.equippable:SetOnUnequip(OnUnequip)
    inst.components.equippable.walkspeedmult = 1.05

	inst:AddComponent("inventoryitem")
	inst.components.inventoryitem.imagename = "lunarstaff"
	inst.components.inventoryitem.atlasname = "inventoryimages/lunarstaff.xml"






    return inst
end

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