local assets=
{ 
    Asset("ANIM", "anim/yuyi_weapon.zip"),
    Asset("ANIM", "anim/swap_haihun.zip"), 
    Asset("ANIM", "anim/swap_longya.zip"), 
    Asset("ANIM", "anim/swap_xueyin.zip"), 

    Asset("ATLAS", "images/inventoryimages/haihun.xml"),
    Asset("IMAGE", "images/inventoryimages/haihun.tex"),
    Asset("ATLAS", "images/inventoryimages/longya.xml"),
    Asset("IMAGE", "images/inventoryimages/longya.tex"),
    Asset("ATLAS", "images/inventoryimages/xueyin.xml"),
    Asset("IMAGE", "images/inventoryimages/xueyin.tex"),
}

local prefabs = 
{
    "haihun",
    "longya",
    "xueyin",
}

local function Upgradedamage(inst)
	if not inst:HasTag("broken") then
    inst.components.weapon:SetDamage(55+inst.weapon_adddmg)
	end
	if inst.components.fueled:IsEmpty() then
		inst:AddTag("broken")
		inst.components.weapon:SetDamage(1)
	end
end

local function OnEquipHaiHun(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_object", "swap_haihun", "swap_haihun")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

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

local function OnEquipLongYa(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_object", "swap_longya", "swap_longya")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

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

local function OnEquipXueYin(inst, owner) 
    owner.AnimState:OverrideSymbol("swap_object", "swap_xueyin", "swap_xueyin")
    owner.AnimState:Show("ARM_carry") 
    owner.AnimState:Hide("ARM_normal") 
end

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

------------------------------------------------------------

local function OnDurability(inst, data)
	inst:AddTag("broken")
	inst.components.weapon:SetDamage(1)
	inst.components.talker:Say(STRINGS.YUYI_WEAPON1)
end

local function TakeItem(inst, item, data)
	inst.components.fueled:DoDelta(3999820)
	if inst:HasTag("haihun") then
		inst.components.weapon:SetDamage(35)
	elseif inst:HasTag("longya") then
		inst.components.weapon:SetDamage(45)
	elseif inst:HasTag("xueyin") then
		inst.components.weapon:SetDamage(55+inst.weapon_adddmg)
	end
	inst:RemoveTag("broken")
end

local function attackfuel(inst, attacker, target)
local player = GetPlayer()

	if target and not inst:HasTag("broken") and not inst.components.fueled:IsEmpty() then
			inst.components.fueled:DoDelta(-50000)
	elseif target and inst.components.fueled:IsEmpty() then
		inst:AddTag("broken")
		inst.components.weapon:SetDamage(1)
		inst.components.talker:Say(STRINGS.YUYI_WEAPON1)
	end

	if target and not target:HasTag("wall") and not target.components.health:IsDead() and inst.weapon_defense and inst.weapon_defense >= 0 and target.components.health.fa_resistances and target.components.health.fa_resistances[FA_DAMAGETYPE.PHYSICAL] then
		if not target:HasTag("armorbreakdark") then
			target:AddTag("armorbreakdark")
			local oldabsorb2 = target.components.health.fa_resistances[FA_DAMAGETYPE.PHYSICAL]
			target.components.health.fa_resistances[FA_DAMAGETYPE.PHYSICAL] = oldabsorb2-inst.weapon_defense/100
			if target.components.health.fa_resistances[FA_DAMAGETYPE.PHYSICAL] <= 0 then target.components.health.fa_resistances[FA_DAMAGETYPE.PHYSICAL] = 0 end
			target:DoTaskInTime(5, function() target.components.health.fa_resistances[FA_DAMAGETYPE.PHYSICAL] = oldabsorb2 target:RemoveTag("armorbreakdark") end)
		end
	end

    if attacker.components.health and attacker.components.health:GetPercent() < 1 and not target:HasTag("wall") and inst.weapon_bloodsuck > 0 and not target:HasTag("myfriend") then
		local bloodsuck = inst.components.weapon.damage*inst.weapon_bloodsuck/100
        attacker.components.health:DoDelta(bloodsuck)
    end

	if target and target.components.locomotor and inst.weapon_slowly > 0 and math.random() < inst.weapon_slowly/100 and not target:HasTag("myfriend") then
		local walkspeedbackup = target.components.locomotor.walkspeed
		local runspeedbackup = target.components.locomotor.runspeed
		if  target.components.locomotor and not target:HasTag("tragetslow") then
			target:AddTag("tragetslow")
			target.components.locomotor.walkspeed = walkspeedbackup/3
			target.components.locomotor.runspeed = runspeedbackup/3
		end
		target:DoTaskInTime(5, function()
		if target and target.components.health and not target.components.health:IsDead() then
				target:RemoveTag("tragetslow")
				target.components.locomotor.walkspeed = walkspeedbackup
				target.components.locomotor.runspeed = runspeedbackup
		end
		end)
	end

	if target and not target:HasTag("wall") and not target:HasTag("structure") and math.random() < inst.weapon_hitcritical/100 and not inst:HasTag("broken") then
		SpawnPrefab("explode_small").Transform:SetPosition(target.Transform:GetWorldPosition())
		if target.components.health then target.components.combat:GetAttacked(player, inst.components.weapon.damage) end
		player.SoundEmitter:PlaySound("yuyi_attack/yuyi_attack/he")
		player.components.talker:Say(STRINGS.YUYI_WEAPON2)
    end

	if target and target.components.burnable and inst.weapon_darkdness > 0 and math.random() < inst.weapon_darkdness/100 and not target:HasTag("myfriend") then
		local fire = SpawnPrefab("blackfire_charge")
		fire.entity:SetParent(target.entity)
	end

	if target and not target:HasTag("wall") and target.components.locomotor and not target.taskp and math.random() < inst.weapon_panic/100 and inst.weapon_panic > 0 and not target:HasTag("myfriend") then
		local hunluan = SpawnPrefab("hunluan")
		hunluan.entity:SetParent(target.entity)
		if target.brain then target.brain:Stop() end
		if target.components.combat then target.components.combat:SetTarget(nil) end
	target.taskp = target:DoPeriodicTask(0.75, function() target.components.locomotor:RunInDirection(math.random()*360) end)
	target:DoTaskInTime(3, function()
		if target and target.taskp and not target.components.health:IsDead() then
			target.taskp:Cancel()
			target.taskp = nil
			target.components.combat:SetTarget(attacker)
			if target.brain then target.brain:Start() end
		end
	end)
	end
end

local function onpreload(inst, data)
	if data then
	inst.weapon_upgrade = data.weapon_upgrade
	inst.weapon_hitcritical = data.weapon_hitcritical
	inst.weapon_bloodsuck = data.weapon_bloodsuck
	inst.weapon_skillscd = data.weapon_skillscd
	inst.weapon_adddmg = data.weapon_adddmg
	inst.weapon_defense = data.weapon_defense
	inst.weapon_hunger = data.weapon_hunger
	inst.weapon_darkdness = data.weapon_darkdness
	inst.weapon_panic = data.weapon_panic
	inst.weapon_slowly = data.weapon_slowly
	inst.weapon_combo_multiplier = data.weapon_combo_multiplier
	Upgradedamage(inst)
	end
end

local function onsave(inst, data)
	data.weapon_upgrade = inst.weapon_upgrade
	data.weapon_hitcritical = inst.weapon_hitcritical
	data.weapon_bloodsuck = inst.weapon_bloodsuck
	data.weapon_skillscd = inst.weapon_skillscd
	data.weapon_adddmg = inst.weapon_adddmg
	data.weapon_darkdness = inst.weapon_darkdness
	data.weapon_panic = inst.weapon_panic
	data.weapon_slowly = inst.weapon_slowly
	data.weapon_hunger = inst.weapon_hunger
	data.weapon_defense = inst.weapon_defense
	data.weapon_combo_multiplier = inst.weapon_combo_multiplier
end

local function OnLoad(inst, data)
	if data then
	inst.weapon_upgrade = data.weapon_upgrade
	inst.weapon_hitcritical = data.weapon_hitcritical
	inst.weapon_bloodsuck = data.weapon_bloodsuck
	inst.weapon_skillscd = data.weapon_skillscd
	inst.weapon_adddmg = data.weapon_adddmg
	inst.weapon_darkdness = data.weapon_darkdness
	inst.weapon_panic = data.weapon_panic
	inst.weapon_slowly = data.weapon_slowly
	inst.weapon_defense = data.weapon_defense
	inst.weapon_hunger = data.weapon_hunger
	inst.weapon_combo_multiplier = data.weapon_combo_multiplier
	Upgradedamage(inst)
	end
end

------------------------------------------------------------

local function haihunfn(colour)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddSoundEmitter()

	inst.weapon_upgrade = TUNING.YUYI_QUICKPICK
	inst.weapon_hitcritical = 0--暴击
	inst.weapon_skillscd = 0--冷却
	inst.weapon_bloodsuck = 0--吸血
	inst.weapon_adddmg = 0--附加伤害
	inst.weapon_panic = 0--混乱
	inst.weapon_darkdness = 0--暗影
	inst.weapon_slowly = 0--减速
	inst.weapon_health = 0--健康上限
	inst.weapon_defense = 0--无视防御
	inst.weapon_hunger = 0--饥饿速率
	inst.weapon_combo_multiplier = 0.01--连击伤害系数

	anim:SetBank("haihun")
	anim:SetBuild("yuyi_weapon")
    anim:PlayAnimation("idle")

	inst:AddTag("haihun")
    inst:AddComponent("talker")
    inst.components.talker.fontsize = 24
    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("weapon")
    inst.components.weapon:SetDamage(35)
    inst.components.weapon:SetOnAttack(attackfuel)

    inst:AddComponent("fueled")
    inst.components.fueled.fueltype = "NIGHTMARE"
    inst.components.fueled:InitializeFuelLevel(10000000)
    inst.components.fueled:SetDepletedFn(OnDurability)
    inst.components.fueled.ontakefuelfn = TakeItem
    inst.components.fueled.accepting = true
    inst.components.fueled:StopConsuming()
	
	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "haihun.tex" )
	inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "haihun"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/haihun.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquipHaiHun )
    inst.components.equippable:SetOnUnequip( OnUnequipHaiHun )
	if not IsDLCEnabled(REIGN_OF_GIANTS) then
	MakeInventoryFloatable(inst, "idle_water", "idle")
	end
    return inst
end

local function longyafn(colour)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddSoundEmitter()
	inst.weapon_upgrade = TUNING.YUYI_QUICKPICK
	inst.weapon_hitcritical = 0--暴击
	inst.weapon_skillscd = 0--冷却
	inst.weapon_bloodsuck = 0--吸血
	inst.weapon_adddmg = 0--附加伤害
	inst.weapon_panic = 0--混乱
	inst.weapon_darkdness = 0--暗影
	inst.weapon_slowly = 0--减速
	inst.weapon_health = 0--健康上限
	inst.weapon_defense = 0--无视防御
	inst.weapon_hunger = 0--饥饿速率
	inst.weapon_combo_multiplier = 0.01--连击伤害系数
	
	anim:SetBank("longya")
	anim:SetBuild("yuyi_weapon")
    anim:PlayAnimation("idle")
	
	inst:AddTag("longya")
    inst:AddComponent("talker")
    inst.components.talker.fontsize = 24
    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("weapon")
	inst.components.weapon:SetRange(0.33)
	inst.components.weapon:SetDamage(45)
    inst.components.weapon:SetOnAttack(attackfuel)

    inst:AddComponent("fueled")
    inst.components.fueled.fueltype = "NIGHTMARE"
    inst.components.fueled:InitializeFuelLevel(17500000)
    inst.components.fueled:SetDepletedFn(OnDurability)
    inst.components.fueled.ontakefuelfn = TakeItem
    inst.components.fueled.accepting = true
    inst.components.fueled:StopConsuming()

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "longya.tex" )
	inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "longya"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/longya.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquipLongYa )
    inst.components.equippable:SetOnUnequip( OnUnequipLongYa )
	if not IsDLCEnabled(REIGN_OF_GIANTS) then
		MakeInventoryFloatable(inst, "idle_water", "idle")
	end
    return inst
end

local function xueyinfn(colour)
    local inst = CreateEntity()
    local trans = inst.entity:AddTransform()
    local anim = inst.entity:AddAnimState()
    MakeInventoryPhysics(inst)
    inst.entity:AddSoundEmitter()
	
	anim:SetBank("xueyin")
	anim:SetBuild("yuyi_weapon")
    anim:PlayAnimation("idle")

	inst.weapon_upgrade = 0--强化
	inst.weapon_hitcritical = 0--暴击
	inst.weapon_skillscd = 0--冷却
	inst.weapon_bloodsuck = 0--吸血
	inst.weapon_adddmg = 0--附加伤害
	inst.weapon_panic = 0--混乱
	inst.weapon_darkdness = 0--暗影
	inst.weapon_slowly = 0--减速
	inst.weapon_health = 0--健康上限
	inst.weapon_defense = 0--无视防御
	inst.weapon_hunger = 0--饥饿速率
	inst.weapon_combo_multiplier = 0.01--连击伤害系数

	inst:AddTag("xueyin")
    inst:AddComponent("talker")
    inst.components.talker.fontsize = 24
    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("weapon")
	inst.components.weapon:SetRange(0.66)
    inst.components.weapon:SetDamage(55+inst.weapon_adddmg)
    inst.components.weapon:SetOnAttack(attackfuel)

    inst:AddComponent("fueled")
    inst.components.fueled.fueltype = "NIGHTMARE"
    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:AddComponent("blinkstaff")
	inst.components.blinkstaff.onblinkfn = function() inst.components.fueled:DoDelta(-800000) end
	local oldblinkstaff = inst.components.blinkstaff.CanBlinkToPoint
	function inst.components.blinkstaff:CanBlinkToPoint(pt)
    local ground = GetWorld()
	local player = GetPlayer()
	local fuelpercent = inst.components.fueled:GetPercent()
	local whatYouHold = player.components.inventory:GetActiveItem()
	if fuelpercent >= 0.6 and not whatYouHold and ground then
		local tile = ground.Map:GetTileAtPoint(pt.x, pt.y, pt.z)
		return tile ~= GROUND.IMPASSIBLE and tile < GROUND.UNDERGROUND
	end
	return false
	end

	inst.entity:AddMiniMapEntity()
	inst.MiniMapEntity:SetIcon( "xueyin.tex" )
	inst:AddComponent("inspectable")
    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.imagename = "xueyin"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/xueyin.xml"
    
    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip( OnEquipXueYin )
    inst.components.equippable:SetOnUnequip( OnUnequipXueYin )

	if not IsDLCEnabled(REIGN_OF_GIANTS) then
	MakeInventoryFloatable(inst, "idle_water", "idle")
	end
    inst.OnLoad = OnLoad
    inst.OnSave = onsave
    inst.OnPreLoad = onpreload
    return inst
end

return  Prefab("common/inventory/haihun", haihunfn, assets, prefabs),
		Prefab("common/inventory/longya", longyafn, assets, prefabs),
		Prefab("common/inventory/xueyin", xueyinfn, assets, prefabs)
