Exemple #1
0
    /// <summary>
    /// 选择一个攻击目标
    /// </summary>
    /// <param name="self">自己</param>
    /// <param name="canAttack">是否考虑必须可以攻击到</param>
    /// <returns></returns>
    public FightUnit SelectFightTarget(FightUnit self, bool canAttack)
    {
        if (self == null || self.IsDead)
        {
            EDebug.LogError("FightLogic.SelectFightTarget failed, fightunit is null or dead");
            return(null);
        }
        int   selIdx = -1;
        float dis    = float.MaxValue;

        //先循环一遍看看有没有可以攻击的对象,如果没有,则不考虑是否无敌
        int targetsCanBeAttackCnt = 0;

        for (int idx = 0; idx < AllFighters.Count; ++idx)
        {
            FightUnit unit = AllFighters[idx];
            if (!unit.CanBeAttack(self, false, true, true))
            {
                continue;
            }
            targetsCanBeAttackCnt++;
        }
        for (int idx = 0; idx < AllFighters.Count; ++idx)
        {
            FightUnit unit = AllFighters[idx];
            if (!unit.CanBeAttack(self, 0 == targetsCanBeAttackCnt, !canAttack, false))
            {
                continue;
            }
            //计算距离
            float gridDis = PathFinder.GridDis(self.GridPos, unit.GridPos);
            if (gridDis < dis)
            {
                selIdx = idx;
                dis    = gridDis;
            }
        }
        return((selIdx >= 0) ? AllFighters[selIdx] : null);
    }