--[NEW] Here we list any assets required by our prefab.
local assets=
{
	--[NEW] this is the name of the Spriter file.
	Asset("ANIM", "anim/worm_assassin.zip"),
}

SetSharedLootTable( 'worm_assassin',
{
    {'cookedmonstermeat',            1.00},
	{'cookedmonstermeat',            1.00},
	{'cookedmonstermeat',            1.00},
	{'purplegem',            0.5},
})

local function OnNewTarget(inst, data, target)
    if data.target and data.target:HasTag("player") then
		inst.components.combat.damagemultiplier = 0.5
end
end

local function OnAttacked(inst, data)
    local attacker = data and data.attacker
    if attacker and attacker:HasTag("player") then
		inst.components.combat.damagemultiplier = 0.5
	else
    inst.components.combat:SetTarget(attacker)
    inst.components.combat:ShareTarget(attacker, 10, function(dude) return dude:HasTag("companion") end, 5)
end
end

local function KeepTarget(inst, target)
    return inst.components.combat:CanTarget(target) and (not inst:HasTag("companion") or inst:IsNear(target, 20))
end

local function WormspiderSanityAura(inst, observer)
	
	if inst.components.follower and inst.components.follower.leader == observer then
		return -TUNING.SANITYAURA_SMALL
	end
	
	return 0
end

local function FollowPlayer(inst)
	local player = GetPlayer()
	if player and player.components.leader then
		player.components.leader:AddFollower(inst)
	end
end

local function updatedamage(inst)
    if GetClock():IsDay() then
        inst.components.combat.defaultdamage = 100
    elseif GetClock():IsNight() then
        inst.components.combat.defaultdamage = 150
    elseif GetClock():IsDusk() then
        inst.components.combat.defaultdamage = 125
    end

end

local function OnSpawnNightmareFuel(inst, fuel)
	inst.sg:GoToState("taunt", fuel)
end

local function create_assassin()

	local inst = CreateEntity()
	
	inst:AddTag("character")
	inst:AddTag("wormspider")
	inst:AddTag("companion")
	inst:AddComponent("inspectable")
	inst:AddTag("scarytoprey")
	inst:AddTag("noauradamage")
	
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    anim:SetBank("worm_assassin")
    anim:SetBuild("worm_assassin")
	inst.Transform:SetFourFaced()
	
    inst:AddComponent("locomotor")
	inst.components.locomotor.walkspeed = 8
    inst.components.locomotor.runspeed = 12
	
	inst.entity:AddDynamicShadow()
    inst.DynamicShadow:SetSize(4, 1.5 )
	
	inst:AddComponent("health")
    inst.components.health:SetMaxHealth(150) 
	inst.components.health:StartRegen(2, 1)
	
	inst:AddComponent("combat")
    inst.components.combat:SetRange(5, 7.5)
    inst.components.combat:SetAttackPeriod(4)
    inst.components.combat.defaultdamage = 100
	inst.components.combat:SetKeepTargetFunction(KeepTarget)
	
    inst:AddComponent("periodicspawner")
    inst.components.periodicspawner:SetOnSpawnFn(OnSpawnNightmareFuel)
    inst.components.periodicspawner.prefab = "nightmarefuel"
    inst.components.periodicspawner.basetime = TUNING.TOTAL_DAY_TIME * 2
    inst.components.periodicspawner.randtime = TUNING.TOTAL_DAY_TIME * 2
    inst.components.periodicspawner:Start()
	
	inst:AddComponent("norecipelootdrop")
    inst:AddComponent("lootdropper")
	inst.components.lootdropper:SetChanceLootTable('worm_assassin')
	
    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aurafn = WormspiderSanityAura
	
	MakeCharacterPhysics(inst, 10, .5)
	
	inst:AddComponent("follower")
	
	inst:ListenForEvent("attacked", OnAttacked)
    inst:ListenForEvent("newcombattarget", OnNewTarget)
	
	local player = GetPlayer()
	if player and player.components.leader then
		player.components.leader:AddFollower(inst)
	end
	
    inst:AddComponent("named")
    inst.components.named.possiblenames = STRINGS.WORMNAMES
    inst.components.named:PickNewName()
	
    inst:SetStateGraph("SGwormassassin")
    local brain = require "brains/assassinwormbrain"
	inst:SetBrain(brain)

    inst:ListenForEvent( "dusktime", function() updatedamage(inst) end , GetWorld())
    inst:ListenForEvent( "daytime", function() updatedamage(inst) end , GetWorld())
    inst:ListenForEvent( "nighttime", function() updatedamage(inst) end , GetWorld())
    updatedamage(inst)
	
    return inst
end

--[NEW] Here we register our new prefab so that it can be used in game.
return Prefab( "monsters/worm_assassin", create_assassin, assets, nil)
