--Here we list any assets required by our prefab.
local assets=
{
    --this is the name of the Spriter file.
    Asset("ANIM", "anim/hooh.zip"),
	
	    --This is the FMOD file which contains all of the sound events.
    Asset("SOUNDPACKAGE", "sound/hooh.fev"),

    --This is the FMOD file which contains all the actual sound data.
    Asset("SOUND", "sound/hooh.fsb"),  
	
	--This is the FMOD file which contains all of the sound events.
    Asset("SOUNDPACKAGE", "sound/hoohmusic.fev"),

    --This is the FMOD file which contains all the actual sound data.
    Asset("SOUND", "sound/hoohmusic.fsb"),  
    
}

local prefabs=
{
    "tornado",
	"houndfire",
	"rainbowfeather",
	"meat",
	"drumstick",
	"hoohegg",
	"sacredash",
}

local loot = {
	"drumstick", "drumstick",
	"meat", "meat", "meat",
	"rainbowfeather", "rainbowfeather", "rainbowfeather", "rainbowfeather", "rainbowfeather",
	"hoohegg",
	"sacredash", "sacredash", "sacredash",
}

local function PushMusic(inst)
    if ThePlayer == nil then
        inst._playingmusic = false
    elseif ThePlayer:IsNear(inst, inst._playingmusic and 30 or 20) then
        inst._playingmusic = true
        ThePlayer:PushEvent("triggeredevent", { name = "hooh" })
    elseif inst._playingmusic and not ThePlayer:IsNear(inst, 40) then
        inst._playingmusic = false
    end
end

local function EnableFlap(inst, enable)
    if enable then
        if not inst.flapping then
            inst.flapping = true
            if not inst:IsAsleep() then
                inst.SoundEmitter:PlaySound("hooh/hooh/wings", "flap")
            end
        end
    elseif inst.flapping then
        inst.flapping = false
        inst.SoundEmitter:KillSound("flap")
    end
end

local function tornadoimmunity(inst, amount, overtime, cause, ignore_invincible, afflicter, ignore_absorb)
    return afflicter ~= nil and (afflicter:HasTag("tornado")) --change the tag to whatever the tornado has.
end

local function RetargetFn(inst)
    return not inst.components.sleeper:IsAsleep()
        and (   FindEntity(
                    inst,
                    10,
                    function(guy)
                        return guy.components.combat.target == inst
                            and inst.components.combat:CanTarget(guy)
                    end,
                    { "_combat" }, --see entityreplica.lua
                    { "prey", "smallcreature", "INLIMBO" }
                )
            )
        or nil
end

local function KeepTargetFn(inst, target)
    return inst.components.combat:CanTarget(target)
end

local function OnAttacked(inst, data)
    inst.components.combat:SetTarget(data.attacker)
end

local function OnHitOther(inst, target)
	if math.random() < 0.5 then
		local tornado = SpawnPrefab("tornado")
		local x,y,z = inst.components.combat.target.Transform:GetWorldPosition()
		tornado.Transform:SetPosition(x,y,z)
		tornado.components.knownlocations:RememberLocation("target", target:GetPosition())
		tornado.WINDSTAFF_CASTER = inst
		tornado.WINDSTAFF_CASTER_ISPLAYER = true
	elseif math.random() == 0.5 then
		local tornado = SpawnPrefab("tornado")
		local x,y,z = inst.components.combat.target.Transform:GetWorldPosition()
		tornado.Transform:SetPosition(x,y,z)
		tornado.components.knownlocations:RememberLocation("target", target:GetPosition())
		tornado.WINDSTAFF_CASTER = inst
		tornado.WINDSTAFF_CASTER_ISPLAYER = true
		local houndfire = SpawnPrefab("houndfire")
		local x,y,z = inst.components.combat.target.Transform:GetWorldPosition()
		houndfire.Transform:SetPosition(x,y,z)
	else
		local houndfire = SpawnPrefab("houndfire")
		local x,y,z = inst.components.combat.target.Transform:GetWorldPosition()
		houndfire.Transform:SetPosition(x,y,z)
	end
end



--This function creates a new entity based on a prefab.
local function init_prefab()
	
    --First we create an entity.
    local inst = CreateEntity()

    --Then we add a transform component se we can place this entity in the world.
    local trans = inst.entity:AddTransform()

    --Then we add an animation component which allows us to animate our entity.
    local anim = inst.entity:AddAnimState()
	
	inst.entity:AddDynamicShadow()
    inst.entity:AddNetwork()
    
    --The bank name is the name of the Spriter file.
    anim:SetBank("hooh")

    --The build name is the name of the animation folder in spriter.
    anim:SetBuild("hooh")

    --We need to add a 'locomotor' component otherwise our creature can't walk around the world.
    inst:AddComponent("locomotor")

    --Here we can tune how fast our creature runs forward.
    inst.components.locomotor.runspeed = 3

    --We need to add a 'physics' component for our character to walk through the world.  Lucky for us, this
    --this is made easy for us by the 'MakeCharacterPhysics' function.  This function sets up physics,
    --collision tags, collision masks, friction etc.  All we need to give it is a mass and a radius which 
    --in this case we've set to 10 and 0.5.
    inst.Transform:SetScale(3, 3, 3)
    MakeFlyingGiantCharacterPhysics(inst, 500, 1.4)
    inst.Transform:SetFourFaced()
		
    inst:AddTag("epic")
    inst:AddTag("noepicmusic")
    inst:AddTag("scarytoprey")
    inst:AddTag("largecreature")
    inst:AddTag("flying")
	inst:AddTag("bird")

    inst.flapping = true
    inst.EnableFlap = EnableFlap
	
	inst.entity:AddLight()
	inst.Light:Enable(true)
    inst.Light:SetRadius(5)
    inst.Light:SetFalloff(0.5)
    inst.Light:SetIntensity(0.75)
    inst.Light:SetColour(255/255, 182/255, 0/255)
    --Here we attach our stategraph to our prefab.
    inst:SetStateGraph("SGhooh")
	
	inst.entity:AddSoundEmitter()
	inst.SoundEmitter:PlaySound("hooh/hooh/wings", "flap")
	--inst.SoundEmitter:PlaySound("hooh/hooh/music", "hooh")
	
	inst.entity:SetPristine()
	
    if not TheNet:IsDedicated() then
        inst._playingmusic = false
        inst:DoPeriodicTask(1, PushMusic, 0)
    end
	
	if not TheWorld.ismastersim then
        return inst
    end
	
    --We need a reference to the brain we want to attach to our creature.
    local brain = require "brains/hoohbrain"

    --And then we attach the brain to our creature.
    inst:SetBrain(brain)
    inst:AddComponent("lootdropper")
    inst:AddComponent("health")
	inst.components.health:SetMaxHealth(6000)
    inst.components.health.fire_damage_scale = 0
	inst:AddComponent("inspectable")
	inst.components.health.redirect = tornadoimmunity
	
    inst:ListenForEvent("attacked", OnAttacked)
	
	inst:AddComponent("combat")
    inst:AddComponent("sleeper")	
	inst.components.sleeper:SetResistance(2)
    inst.components.combat:SetDefaultDamage(10)
    inst.components.combat:SetRange(10, 15)
    inst.components.lootdropper:SetLoot(loot)
	inst.components.combat.onhitotherfn = OnHitOther
	
    MakeHugeFreezableCharacter(inst)
    inst.components.freezable:SetResistance(TUNING.DRAGONFLY_FREEZE_THRESHOLD)
    inst.components.freezable.damagetobreak = TUNING.DRAGONFLY_FREEZE_RESIST
    inst.components.freezable.diminishingreturns = true
	
	
    --return our new entity so that it can be added to the world.
    return inst
end

--Here we register our new prefab so that it can be used in game.
return Prefab( "hooh", init_prefab, assets, nil)

