require "behaviours/follow"
require "behaviours/faceentity"
require "behaviours/chaseandattack"

local MIN_FOLLOW_DIST = 0
local MAX_FOLLOW_DIST = 10
local TARGET_FOLLOW_DIST = 8

local function GetFaceTargetFn(inst)
    return inst.components.follower.leader
end

local function KeepFaceTargetFn(inst, target)
    return inst.components.follower.leader == target
end

local Qiuqiu_Brain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)


local function GetTargetFn(inst)
	local target = FindEntity(inst, 30, function(guy) return inst:HasTag("mode3") and guy.components.combat and inst.components.combat:CanTarget(guy) and
	not guy.prefab == "slurper" and not guy.components.yiyucast and (guy.components.combat.target == GetPlayer() or guy:HasTag("monster"))end)
	if target ~= nil then return target end
end

function Qiuqiu_Brain:OnStart()
    local root = 
    PriorityNode({
		WhileNode( function() return not GetTargetFn(self.inst) end, "Dodge", ChaseAndAttack(self.inst, 100, 30)),
		ChaseAndAttack(self.inst, 8),
        Follow(self.inst, function() return self.inst.components.follower.leader end, MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST),
        FaceEntity(self.inst, GetFaceTargetFn, KeepFaceTargetFn),
    }, .5)
    self.bt = BT(self.inst, root)
end

return Qiuqiu_Brain