--[[
BY
 _____   _____   __   _  
|  _  \ /  _  \ |  \ | | 
| | | | | | | | |   \| | 
| | | | | | | | | |\   | 
| |_| | | |_| | | | \  | 
|_____/ \_____/ |_|  \_| 

]]

local brain = require "brains/donskeletonmobbrain"


local assets =
{
	Asset( "ANIM", "anim/mob/donskeletonmob.zip" ),
}

local donpetskeleton_maxlife = 300
local donpetskeleton_speed = 6.5
local donpetskeleton_damage = 42
local donpetskeleton_atkperiod = 2

local function OnAttacked(inst, data)
    if data.attacker ~= nil then
        if data.attacker.components.petleash_don ~= nil and
            data.attacker.components.petleash_don:IsPet(inst) then
            if inst.components.lootdropper == nil then
                inst:AddComponent("lootdropper")
            end
            inst.components.lootdropper:SpawnLootPrefab("nightmarefuel", inst:GetPosition())
            data.attacker.components.petleash_don:DespawnPet(inst)
        elseif data.attacker.components.combat ~= nil then
            inst.components.combat:SuggestTarget(data.attacker)
        end
    end
end

local function retargetfn(inst)
    local leader = inst.components.follower:GetLeader()
    return leader ~= nil
        and FindEntity(
            leader,
            TUNING.SHADOWWAXWELL_TARGET_DIST,
            function(guy)
                return guy ~= inst
                    and (guy.components.combat:TargetIs(leader) or
                        guy.components.combat:TargetIs(inst))
                    and inst.components.combat:CanTarget(guy)
            end,
            { "_combat" },
            { "playerghost", "INLIMBO" }
        )
        or nil
end

local function keeptargetfn(inst, target)
    return inst.components.follower:IsNearLeader(14)
        and inst.components.combat:CanTarget(target)
end


local function nodebrisdmg(inst, amount, overtime, cause, ignore_invincible, afflicter, ignore_absorb)
    return afflicter ~= nil and afflicter:HasTag("quakedebris")
end

local function OnDeath(inst)
    
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddLight()
	inst.entity:AddNetwork()

	MakeGhostPhysics(inst, 1, 0.5)

	inst.Transform:SetFourFaced(inst)

	inst.AnimState:SetBank("wilson")
	inst.AnimState:SetBuild("DonSkeletonMob")
	inst.AnimState:PlayAnimation("idle")
	inst.AnimState:SetMultColour(27/255, 198/255, 169/255, .35)
	inst.AnimState:SetBloomEffectHandle("shaders/anim_bloom_ghost.ksh")
	inst.AnimState:SetLightOverride(0.25)
	
	inst.Light:SetIntensity(.3)
    inst.Light:SetRadius(.3)
    inst.Light:SetFalloff(.6)
    inst.Light:Enable(true)
    inst.Light:SetColour(27/255, 198/255, 169/255)

	local tool = "swap_spear"
	local hat = nil

	if tool ~= nil then
		inst.AnimState:OverrideSymbol("swap_object", tool, tool)
		inst.AnimState:Hide("ARM_normal")
	else
		inst.AnimState:Hide("ARM_carry")
	end

	if hat ~= nil then
		inst.AnimState:OverrideSymbol("swap_hat", hat, "swap_hat")
		inst.AnimState:Hide("HAIR_NOHAT")
		inst.AnimState:Hide("HAIR")
	else
		inst.AnimState:Hide("HAT")
		inst.AnimState:Hide("HAIR_HAT")
	end

	inst:AddTag("character")
    inst:AddTag("scarytoprey")
    inst:AddTag("ghost")
    inst:AddTag("noauradamage")
    inst:AddTag("notraptrigger")
	inst:AddTag("shadowminionskeleton")
    inst:AddTag("NOBLOCK")
	
	inst:AddTag("healthinfo")

	inst.entity:SetPristine()

	if not TheWorld.ismastersim then
		return inst
	end
	
	inst._playerlink = nil

	inst:AddComponent("inspectable")
	
	inst:AddComponent("locomotor")
	inst.components.locomotor.runspeed = donpetskeleton_speed
	inst.components.locomotor.pathcaps = { ignorecreep = true }
	inst.components.locomotor:SetSlowMultiplier(.6)

	inst:AddComponent("health")
	inst.components.health:SetMaxHealth(donpetskeleton_maxlife)
	inst.components.health.nofadeout = true
	inst.components.health.redirect = nodebrisdmg

	inst:AddComponent("combat")
	inst.components.combat.hiteffectsymbol = "torso"
	inst.components.combat:SetRange(2)
	inst.components.combat:SetDefaultDamage(donpetskeleton_damage)
    inst.components.combat:SetAttackPeriod(donpetskeleton_atkperiod)
    inst.components.combat:SetRetargetFunction(2, retargetfn)
    inst.components.combat:SetKeepTargetFunction(keeptargetfn)

	inst:AddComponent("follower")
	inst.components.follower:KeepLeaderOnAttacked()
	inst.components.follower.keepdeadleader = true

	inst:SetBrain(brain)
	inst:SetStateGraph("SGshadowwaxwell")

	inst:ListenForEvent("death", OnDeath)
	inst:ListenForEvent("attacked", OnAttacked)
	
	return inst
end

return Prefab("donskeletonmob", fn, assets)
