
local trace = function() end

local assets=
{
	Asset("ANIM", "anim/ghosthound.zip"),
	Asset("SOUND", "sound/hound.fsb"),
}

local prefabs =
{
}
SetSharedLootTable( 'ghosth',
{
   -- {'houndstooth',  0.125},
})

local WAKE_TO_FOLLOW_DISTANCE = 5
--local SLEEP_NEAR_HOME_DISTANCE = 10
local SHARE_TARGET_DIST = 30
local HOME_TELEPORT_DIST = 30

local function onkilll(inst, data)
local tynychance = 0.05
local smallllchance = 0.1
local smallchance = 0.2
local normalchance = 0.3
local largechance = 0.7
local player = GetPlayer()
	if data.cause == inst.prefab and not data.inst:HasTag("companion") then
                if math.random() < normalchance and data.inst:HasTag("prey") then
        			player.level = player.level + 1
	player.components.talker:Say("额外 [经验](+1) \n".. (player.level))
	--levelexp(player)	
	elseif math.random() < largechance and not data.inst:HasTag("prey") and not data.inst:HasTag("eyeplant") and not data.inst:HasTag("insect") then
    				player.level = player.level + 1
	player.components.talker:Say("额外 [经验](+1) \n".. (player.level))
	elseif math.random() < tynychance and data.inst:HasTag("insect") then
    				player.level = player.level + 1
	player.components.talker:Say("额外 [经验](+1) \n".. (player.level))
	elseif math.random() < smallllchance and data.inst:HasTag("eyeplant") then
    				player.level = player.level + 1
	player.components.talker:Say("额外 [经验](+1) \n".. (player.level))
	elseif math.random() < smallllchance and data.inst:HasTag("arm") then
    				player.level = player.level + 1
	player.components.talker:Say("额外 [经验](+1) \n".. (player.level))

    			 end
		          end
			end


local function die(inst)
inst:AddTag("ndead")
	inst.components.health:Kill()
end

local function close(inst)
local player = GetPlayer()
inst:AddTag("ghounds") 
inst:AddTag("closea") 
inst:RemoveTag("fara") 
player.components.leader:AddFollower(inst)
inst.components.locomotor.runspeed = 12
if inst:HasTag("remove") then
            die(inst)
player:RemoveTag("ghounds") 
end
 end

local function far(inst)
local player = GetPlayer()
inst:RemoveTag("closea") 
inst:AddTag("fara") 
inst.components.locomotor.runspeed = 18
inst.components.combat:GiveUp()
if inst:HasTag("remove") then
            die(inst)
player:RemoveTag("ghounds") 
end
end

local function ondeath(inst)
	--inst.components.sanityaura.penalty = 0
	local player = GetPlayer()
if player then
--player.components.sanity:RecalculatePenalty()
player:RemoveTag("ghounds") 
local fx = SpawnPrefab("sparklefx")
fx.entity:SetParent(player.entity)
fx.Transform:SetScale(1, 2, 1)
fx.Transform:SetPosition(0.3, 0, 0)
player.components.health:DoDelta(5)
player.components.sanity:DoDelta(3)
--[[ if inst:HasTag("b1") then
player.components.health:DoDelta(5)
player.components.sanity:DoDelta(3)
 elseif inst:HasTag("b2") then
player.components.health:DoDelta(10)
player.components.sanity:DoDelta(6)
 elseif inst:HasTag("b3") then
player.components.health:DoDelta(15)
player.components.sanity:DoDelta(9)
 elseif inst:HasTag("b4") then
player.components.health:DoDelta(20)
player.components.sanity:DoDelta(12)
 elseif inst:HasTag("b5") then
player.components.health:DoDelta(25)
player.components.sanity:DoDelta(15)
 elseif inst:HasTag("b6") then
player.components.health:DoDelta(30)
player.components.sanity:DoDelta(18)
 elseif inst:HasTag("b7") then
player.components.health:DoDelta(35)
player.components.sanity:DoDelta(21)
	end ]]--
end
end

local function growhound(inst, data)
local player = GetPlayer()

     if player:HasTag("level1") then
    inst.components.combat:SetDefaultDamage(10)
     elseif player:HasTag("level2") then
    inst.components.combat:SetDefaultDamage(11)
     elseif player:HasTag("level3") then
    inst.components.combat:SetDefaultDamage(12)
     elseif player:HasTag("level4") then
    inst.components.combat:SetDefaultDamage(13)
     elseif player:HasTag("level5") then
    inst.components.combat:SetDefaultDamage(14)
     elseif player:HasTag("level6") then
    inst.components.combat:SetDefaultDamage(15)
     elseif player:HasTag("level7") then
    inst.components.combat:SetDefaultDamage(16)
     elseif player:HasTag("level8") then
    inst.components.combat:SetDefaultDamage(17)
     elseif player:HasTag("level9") then
    inst.components.combat:SetDefaultDamage(18)
     elseif player:HasTag("level10") then
    inst.components.combat:SetDefaultDamage(19)
     elseif player:HasTag("level11") then
    inst.components.combat:SetDefaultDamage(20)
     elseif player:HasTag("level12") then
    inst.components.combat:SetDefaultDamage(21)
     elseif player:HasTag("level13") then
    inst.components.combat:SetDefaultDamage(22)
     elseif player:HasTag("level14") then
    inst.components.combat:SetDefaultDamage(23)
     elseif player:HasTag("level15") then
    inst.components.combat:SetDefaultDamage(24)
     elseif player:HasTag("level16") then
    inst.components.combat:SetDefaultDamage(25)
     elseif player:HasTag("level17") then
    inst.components.combat:SetDefaultDamage(26)
     elseif player:HasTag("level18") then
    inst.components.combat:SetDefaultDamage(27)
     elseif player:HasTag("level19") then
    inst.components.combat:SetDefaultDamage(28)
     elseif player:HasTag("level20") then
    inst.components.combat:SetDefaultDamage(29)
     elseif player:HasTag("level21") then
    inst.components.combat:SetDefaultDamage(30)
     elseif player:HasTag("level22") then
    inst.components.combat:SetDefaultDamage(31)
     elseif player:HasTag("level23") then
    inst.components.combat:SetDefaultDamage(32)
     elseif player:HasTag("level24") then
    inst.components.combat:SetDefaultDamage(33)
     elseif player:HasTag("level25") then
    inst.components.combat:SetDefaultDamage(34)
     elseif player:HasTag("level26") then
    inst.components.combat:SetDefaultDamage(35)
     elseif player:HasTag("level27") then
    inst.components.combat:SetDefaultDamage(36)
     elseif player:HasTag("level28") then
    inst.components.combat:SetDefaultDamage(37)
     elseif player:HasTag("level29") then
    inst.components.combat:SetDefaultDamage(38)
     elseif player:HasTag("level30") then
    inst.components.combat:SetDefaultDamage(40)
end
end

local function dead(inst, data)
if inst.components.hunger:GetPercent() <= 1 and inst.components.hunger:GetPercent() >= 0.9 then
    inst.Transform:SetScale(0.3, 0.3, 0.3)
    inst:AddTag("b1")

elseif inst.components.hunger:GetPercent() < 0.9 and inst.components.hunger:GetPercent() >= 0.8 then
    inst.Transform:SetScale(0.35, 0.35, 0.35)
    inst:AddTag("b2")
    inst:RemoveTag("b1")

elseif inst.components.hunger:GetPercent() < 0.8 and inst.components.hunger:GetPercent() >= 0.7 then
    inst.Transform:SetScale(0.4, 0.4, 0.4)
    inst:AddTag("b3")
    inst:RemoveTag("b1")
    inst:RemoveTag("b2")

elseif inst.components.hunger:GetPercent() < 0.7 and inst.components.hunger:GetPercent() >= 0.6 then
    inst.Transform:SetScale(0.45, 0.45, 0.45)
    inst:AddTag("b4")
    inst:RemoveTag("b1")
    inst:RemoveTag("b2")
    inst:RemoveTag("b3")

elseif inst.components.hunger:GetPercent() < 0.6 and inst.components.hunger:GetPercent() >= 0.5 then
    inst.Transform:SetScale(0.5, 0.5, 0.5)
    inst:AddTag("b5")
    inst:RemoveTag("b1")
    inst:RemoveTag("b2")
    inst:RemoveTag("b3")
    inst:RemoveTag("b4")

elseif inst.components.hunger:GetPercent() < 0.5 and inst.components.hunger:GetPercent() >= 0.1 then
    inst.Transform:SetScale(0.55, 0.55, 0.55)
    inst:AddTag("b6")
    inst:RemoveTag("b1")
    inst:RemoveTag("b2")
    inst:RemoveTag("b3")
    inst:RemoveTag("b4")
    inst:RemoveTag("b5")

elseif inst.components.hunger:GetPercent() < 0.1 and inst.components.hunger.current > 0  then
    inst.Transform:SetScale(0.6, 0.6, 0.6)
    inst:AddTag("b7")
    inst:RemoveTag("b1")
    inst:RemoveTag("b2")
    inst:RemoveTag("b3")
    inst:RemoveTag("b4")
    inst:RemoveTag("b5")
    inst:RemoveTag("b6")

elseif inst.components.hunger.current == 0 then
            die(inst)

end
end

local function OnHitvampire(inst, data)
--local vampirechance = 0.2
local other = data.target
   --local pos = other:GetPosition()
if other and other.components.health then
            --SpawnPrefab("statue_transition").Transform:SetPosition(other:GetPosition():Get())
        inst.components.health:DoDelta(10)
elseif other and other:HasTag("musha") then
	inst.components.combat:SetTarget(nil)
inst.components.combat:GiveUp()
    end
end

local function OnNewTarget(inst, data)
local other = data.target
    if data.target and data.target:HasTag("player") then
	return
	elseif inst:HasTag("fara") then
	inst.components.combat:SetTarget(nil)
elseif other and other:HasTag("musha") then
	inst.components.combat:SetTarget(nil)
inst.components.combat:GiveUp()

end
end


local function retargetfn(inst)
    local dist = TUNING.HOUND_TARGET_DIST
   -- if inst:HasTag("pet_hound") then
       -- dist = TUNING.HOUND_FOLLOWER_TARGET_DIST
        if inst.components.combat.target == inst.components.follower.leader then
            inst.components.combat:SetTarget(nil)
        end
        inst.components.follower:SetLeader(nil)
end

local function KeepTarget(isnt, target)
    return target and target:IsValid()
end


local function OnAttacked(inst, data)

    inst.components.combat:SuggestTarget(data.attacker)
    inst.components.combat:ShareTarget(data.attacker, 10, function(dude) return dude:HasTag("companion") and not dude.components.health:IsDead() end, 5)

end

local function fncommon()
	local inst = CreateEntity()
	local trans = inst.entity:AddTransform()
	local anim = inst.entity:AddAnimState()
    local physics = inst.entity:AddPhysics()
	local sound = inst.entity:AddSoundEmitter()
	local shadow = inst.entity:AddDynamicShadow()
	shadow:SetSize( 1.5, 0.6 )
    inst.Transform:SetFourFaced()
	
    inst:AddTag("scarytoprey")
    --inst:AddTag("hound")
    inst:AddTag("houndmound")
    inst:AddTag("companion")
    inst:AddTag("notraptrigger")
    inst:AddTag("character")
    inst:AddTag("yamcheb")
    inst:AddTag("noauradamage")

    MakeCharacterPhysics(inst, 10, .5)
     
    anim:SetBank("hound")
    anim:SetBuild("ghosthound")
    anim:PlayAnimation("idle")
    inst:AddComponent("locomotor") 
    inst.components.locomotor.pathcaps = { ignorecreep = true }
    inst.components.locomotor.runspeed = TUNING.HOUND_SPEED
    inst:SetStateGraph("SGdogs")

    local brain = require "brains/ghosthoundbrain"
    inst:SetBrain(brain)
    inst.entity:AddLight()
	inst.Light:SetRadius(.3)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.3)
    inst.Light:SetColour(50/255,50/255,50/255)
	inst.Light:Enable(true)

            inst.Transform:SetScale(0.3, 0.3, 0.3)

    inst:AddComponent("health")
    inst.components.health:SetMaxHealth(150)
        inst:AddComponent("hunger")
	inst.components.hunger:SetMax(300)

   local player = GetPlayer()
   --inst:AddComponent("sanityaura")
    --inst.components.sanityaura.penalty = 20
        --player.components.sanity:RecalculatePenalty()

    inst:AddComponent("combat")
    inst.components.combat:SetDefaultDamage(TUNING.HOUND_DAMAGE)
    inst.components.combat:SetAttackPeriod(TUNING.ICEHOUND_ATTACK_PERIOD)
    inst.components.combat.playerdamagepercent = 0

    inst.components.combat:SetAttackPeriod(2)
  --  inst.components.combat:SetKeepTargetFunction(KeepTarget)
    inst:ListenForEvent("newcombattarget", OnNewTarget)

	inst:ListenForEvent("attacked", OnAttacked)
 inst:ListenForEvent("death", ondeath)
--inst:ListenForEvent("hungerdelta", dead)
--inst:ListenForEvent("hungerdelta", growhound)

    inst:AddComponent("lootdropper")
    inst.components.lootdropper:SetChanceLootTable('ghosth')
    
    inst:AddComponent("inspectable")


    return inst
end

----------add
local function Onfreeze(attacked, data)
        if data and data.attacker and data.attacker.components.freezable then
            data.attacker.components.freezable:AddColdness(0.6)
            data.attacker.components.freezable:SpawnShatterFX()
        end 
    end

local function fnghost(Sim)
	local inst = fncommon(Sim)
	local player = GetPlayer()
	inst.AnimState:SetBuild("ghosthound")
    inst.AnimState:SetBloomEffectHandle( "shaders/anim.ksh" )

    MakeMediumBurnableCharacter(inst, "hound_body")
    inst.entity:AddLight()
	inst.Light:SetRadius(.3)
    inst.Light:SetFalloff(.5)
    inst.Light:SetIntensity(.3)
    inst.Light:SetColour(50/255,50/255,50/255)
	inst.Light:Enable(true)

    --inst.Transform:SetScale(0.6, 0.6, 0.6)
--player:AddTag("ghounds") 

--------------------
        inst:AddComponent("playerprox")
    inst.components.playerprox:SetDist(7, 7)
    inst.components.playerprox:SetOnPlayerNear(close)
    inst.components.playerprox:SetOnPlayerFar(far)
    inst:AddComponent("follower")

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


    inst:ListenForEvent("attacked", Onfreeze)
    inst:ListenForEvent("onhitother", OnHitvampire)
    inst:ListenForEvent("entity_death", function(wrld, data) onkilll(inst, data) end, GetWorld())

player.components.leader:AddFollower(inst)
-------
 if ACTIONS.STAYORDER~= nil then
    inst:AddComponent("staycommand")
    	end
-------

	return inst
end


return Prefab ( "ghosthound2", fnghost, assets, prefabs) 

