GetNearstTargetHelper() public static method

public static GetNearstTargetHelper ( EntityInfo srcObj, CharacterRelation relation ) : EntityInfo
srcObj EntityInfo
relation CharacterRelation
return EntityInfo
        private void IdleHandler(EntityInfo npc, long deltaTime)
        {
            AiData_Leader aiData = GetAiData(npc);
            SkillInfo     skInfo = AiLogicUtility.NpcFindCanUseSkill(npc, aiData, aiData.IsAutoOperate);

            if (skInfo == null)
            {
                return;
            }
            CharacterRelation relation =
                (skInfo.TargetType == SkillTargetType.Friend ||
                 skInfo.TargetType == SkillTargetType.RandFriend) ?
                CharacterRelation.RELATION_FRIEND :
                CharacterRelation.RELATION_ENEMY;

            EntityInfo attackTarget = AiLogicUtility.GetNearstTargetHelper(npc, aiData.IsAutoOperate ? npc.ViewRange : skInfo.Distance, relation);

            if (attackTarget != null)
            {
                ChangeToState(npc, (int)AiStateId.Combat);
            }
        }
        private void CombatHandler(EntityInfo npc, long deltaTime)
        {
            if (npc.GetSkillStateInfo().IsSkillActivated())
            {
                return;
            }

            AiStateInfo info          = npc.GetAiStateInfo();
            EntityInfo  leader        = AiLogicUtility.GetLivingCharacterInfoHelper(npc, info.LeaderID);
            bool        isAutoOperate = IsAutoOperate(leader);

            ScriptRuntime.Vector3 srcPos = npc.GetMovementStateInfo().GetPosition3D();
            Vector3 homePos = Vector3.Zero;

            if (null != leader)
            {
                GetHomePos(npc.GetMovementStateInfo().FormationIndex, leader);
            }
            float distSqrToHome = Geometry.DistanceSquare(srcPos, homePos);

            if (distSqrToHome > npc.GohomeRange * npc.GohomeRange)
            {
                NotifyAiStopPursue(npc);
                ChangeToState(npc, (int)AiStateId.GoHome);
                return;
            }

            ///
            EntityInfo     attackTarget = null;
            SkillStateInfo currSkInfo   = npc.GetSkillStateInfo();
            ///找到可以使用的技能
            SkillInfo skInfo = AiLogicUtility.NpcFindCanUseSkill(npc, this.GetAiData(npc), isAutoOperate);

            NotifyAiSelectSkill(npc, skInfo);
            if (skInfo == null)
            {
                //没有可以使用的技能就切换到Idle状态
                ChangeToState(npc, (int)AiStateId.Idle);
                return;
            }

            CharacterRelation relation =
                (skInfo.TargetType == SkillTargetType.Friend ||
                 skInfo.TargetType == SkillTargetType.RandFriend) ?
                CharacterRelation.RELATION_FRIEND :
                CharacterRelation.RELATION_ENEMY;

            attackTarget = AiLogicUtility.GetNearstTargetHelper(
                npc, skInfo.Distance, relation);

            if (attackTarget != null && null != skInfo) //攻击范围内找到可攻击目标
            {
                info.Target = attackTarget.GetId();
                NotifyAiStopPursue(npc);
                NotifyAiSkill(npc, skInfo.SkillId); //攻击目标
                return;
            }
            attackTarget = AiLogicUtility.GetNearstTargetHelper(
                npc, npc.ViewRange, relation);
            if (attackTarget != null && isAutoOperate)                                    //视野范围内找到可攻击目标
            {
                NotifyAiPursue(npc, attackTarget.GetMovementStateInfo().GetPosition3D()); // 追赶目标
                return;
            }

            currSkInfo.SetCurSkillInfo(0);
            NotifyAiStopPursue(npc);
            ChangeToState(npc, (int)AiStateId.GoHome);
        }
        internal static void DoPatrolCommandState(EntityInfo entity, long deltaTime, AbstractAiStateLogic logic)
        {
            AiStateInfo info = entity.GetAiStateInfo();

            info.Time += deltaTime;
            if (info.Time > 100)
            {
                info.Time = 0;
                EntityInfo target = null;
                if (info.IsExternalTarget)
                {
                    target = AiLogicUtility.GetSeeingLivingCharacterInfoHelper(entity, info.Target);
                    if (null == target)
                    {
                        target = AiLogicUtility.GetNearstTargetHelper(entity, CharacterRelation.RELATION_ENEMY);
                        if (null != target)
                        {
                            info.Target = target.GetId();
                        }
                    }
                }
                else
                {
                    target = AiLogicUtility.GetNearstTargetHelper(entity, CharacterRelation.RELATION_ENEMY);
                    if (null != target)
                    {
                        info.Target = target.GetId();
                    }
                }
                if (null != target)
                {
                    logic.AiSendStoryMessage(entity, "obj_patrol_exit", entity.GetId());
                    logic.AiSendStoryMessage(entity, string.Format("npc_patrol_exit:{0}", entity.GetUnitId()), entity.GetId());
                    logic.ChangeToState(entity, (int)AiStateId.Idle);
                }
                else
                {
                    AiData_ForPatrolCommand data = GetAiDataForPatrolCommand(entity);
                    if (null != data)
                    {
                        ScriptRuntime.Vector3 srcPos = entity.GetMovementStateInfo().GetPosition3D();
                        if (data.PatrolPath.HavePathPoint && !data.PatrolPath.IsReached(srcPos))
                        {
                            logic.NotifyAiPursue(entity, data.PatrolPath.CurPathPoint);
                        }
                        else
                        {
                            data.PatrolPath.UseNextPathPoint();
                            if (data.PatrolPath.HavePathPoint)
                            {
                                logic.NotifyAiPursue(entity, data.PatrolPath.CurPathPoint);
                            }
                            else
                            {
                                if (data.IsLoopPatrol)
                                {
                                    logic.AiSendStoryMessage(entity, "obj_patrol_restart", entity.GetId());
                                    logic.AiSendStoryMessage(entity, string.Format("npc_patrol_restart:{0}", entity.GetUnitId()), entity.GetId());
                                    data.PatrolPath.Restart();
                                }
                                else
                                {
                                    logic.AiSendStoryMessage(entity, "obj_patrol_finish", entity.GetId());
                                    logic.AiSendStoryMessage(entity, string.Format("npc_patrol_finish:{0}", entity.GetUnitId()), entity.GetId());
                                    logic.NotifyAiStopPursue(entity);
                                    logic.ChangeToState(entity, (int)AiStateId.Idle);
                                }
                            }
                        }
                        info.HomePos = entity.GetMovementStateInfo().GetPosition3D();
                    }
                    else
                    {
                        logic.NotifyAiStopPursue(entity);
                        logic.ChangeToState(entity, (int)AiStateId.Idle);
                    }
                }
            }
        }
        private void CombatHandler(EntityInfo npc, long deltaTime)
        {
            AiStateInfo   aiInfo = npc.GetAiStateInfo();
            AiData_Leader aiData = GetAiData(npc);

            if (npc.GetSkillStateInfo().IsSkillActivated())
            {
                return;
            }
            ///
            SkillStateInfo currSkInfo = npc.GetSkillStateInfo();
            ///找到可以使用的技能
            SkillInfo skInfo = AiLogicUtility.NpcFindCanUseSkill(npc, this.GetAiData(npc), aiData.IsAutoOperate);

            NotifyAiSelectSkill(npc, skInfo);
            if (skInfo == null)
            {
                //没有可以使用的技能就切换到Idle状态
                ChangeToState(npc, (int)AiStateId.Idle);
                return;
            }

            CharacterRelation relation =
                (skInfo.TargetType == SkillTargetType.Friend ||
                 skInfo.TargetType == SkillTargetType.RandFriend) ?
                CharacterRelation.RELATION_FRIEND :
                CharacterRelation.RELATION_ENEMY;
            EntityInfo attackTarget = AiLogicUtility.GetNearstAttackerHelper(npc, relation, aiData);

            if (null != attackTarget)
            {
                NotifyAiTarget(npc, attackTarget);
                if (Geometry.DistanceSquare(npc.GetMovementStateInfo().GetPosition3D(), attackTarget.GetMovementStateInfo().GetPosition3D()) < skInfo.Distance * skInfo.Distance)
                {
                    aiInfo.Target = attackTarget.GetId();
                    NotifyAiStopPursue(npc);
                    NotifyAiSkill(npc, skInfo.SkillId);
                    return;
                }
            }
            attackTarget = AiLogicUtility.GetNearstTargetHelper(npc, skInfo.Distance, relation);
            if (attackTarget != null && null != skInfo)   //攻击范围内找到可攻击目标
            {
                NotifyAiTarget(npc, attackTarget);
                aiInfo.Target = attackTarget.GetId();
                NotifyAiStopPursue(npc);
                NotifyAiSkill(npc, skInfo.SkillId); //攻击目标
                return;
            }
            if (aiData.IsAutoOperate)
            {
                attackTarget = AiLogicUtility.GetNearstTargetHelper(npc, npc.ViewRange, relation);
                if (attackTarget != null && null != skInfo)   //视野内找到可攻击目标
                {
                    NotifyAiPursue(npc, attackTarget.GetMovementStateInfo().GetPosition3D());
                    return;
                }
            }

            ///退出战斗模式清理一下手动技能
            currSkInfo.SetCurSkillInfo(0);
            aiData.ManualSkillId = 0;
            NotifyAiStopPursue(npc);
            ChangeToState(npc, (int)AiStateId.Idle);
        }