// 寻找离指定施放者最近的单位
    public BattleUnit FindNearestEnemyUnit(BattleUnit caster)
    {
        BattleUnit targetEntity = null;
        // 找出敌对阵营
        BattleCamp enemyCamp = caster.enemyCamp;
        // 获取指定标记的所有单位
        List <BattleUnit> entities = BattleUnitManager.instance.GetEntities(enemyCamp);
        // 比较距离,找出最近单位
        float minDistance = Int32.MaxValue;

        foreach (BattleUnit entity in entities)
        {
            Vector2 distance = caster.Get2DPosition() - entity.Get2DPosition();
            if (distance.magnitude < minDistance)
            {
                targetEntity = entity;
                minDistance  = distance.magnitude;
            }
        }

        if (minDistance > caster.GetViewRange())
        {
            return(null);
        }

        return(targetEntity);
    }
Example #2
0
    public override bool IsTrue(TBTWorkingData wData = null)
    {
        // 判断物体是否在视野范围内
        BattleBehaviorWorkingData data    = wData as BattleBehaviorWorkingData;
        AutoCastAbilityRequest    request = data.request as AutoCastAbilityRequest;
        BattleUnit owner  = data.owner;
        Unit       target = request.target;

        // 目标是自己
        if (target == owner)
        {
            return(true);
        }

        Ability ability      = request.ability;
        float   abilityRange = ability.GetCastRange();

        // 全屏技能
        if (abilityRange == -1)
        {
            return(true);
        }

        Vector2 ownerPos  = owner.Get2DPosition();
        Vector2 targetPos = target.Get2DPosition();
        float   distance  = (targetPos - ownerPos).magnitude;

        //BattleLog.Log(string.Format("【CON_IsInAbilityRange】距离:{0},技能范围:{1}", distance, abilityRange));
        return(distance <= abilityRange);
    }
    public TargetCollection CalculateAOETarget(BattleUnit caster, AbilityData abilityData, RequestTarget requestTarget, ActionTarget actionTarget)
    {
        var targetCollection = new TargetCollection();

        // 根据中心类型获取目标点
        var   centerType = actionTarget.Center;
        float centerX, centerZ;

        if (centerType == ActionMultipleTargetsCenter.CASTER)
        {
            caster.Get2DPosition(out centerX, out centerZ);
        }
        else if (centerType == ActionMultipleTargetsCenter.TARGET)
        {
            if (requestTarget.targetType == AbilityRequestTargetType.POINT)
            {
                BattleLog.LogError("单体技能[{0}]目标配置为TARGET,REQUEST TARGET却是POINT类型", abilityData.configFileName);
                return(null);
            }
            requestTarget.GetTarget2DPosition(out centerX, out centerZ);
        }
        else if (centerType == ActionMultipleTargetsCenter.POINT)
        {
            requestTarget.GetTarget2DPosition(out centerX, out centerZ);
        }
        else
        {
            BattleLog.LogError("区域技能[{0}]中有未实现的技能中心点类型[{1}]", abilityData.configFileName, centerType);
            return(null);
        }

        // 根据目标点和范围获取目标对象
        List <BattleUnit> tempTargets;
        var casterCamp = caster.camp;
        var Teams      = actionTarget.Teams;
        var Types      = actionTarget.Types;
        var aoeType    = actionTarget.aoeType;

        if (aoeType == AOEType.Radius)
        {
        }
        else if (aoeType == AOEType.Line)
        {
        }
        else if (aoeType == AOEType.Sector)
        {
        }
        else
        {
            BattleLog.LogError("区域技能[{0}]中有未实现区域类型{1}", abilityData.configFileName, aoeType);
        }

        return(targetCollection);
    }
    private BattleUnit FilterTargetEntityFromList(BattleUnit caster, Ability ability, List <BattleUnit> targetUnits)
    {
        if (targetUnits.Count <= 0)
        {
            return(null);
        }

        AbilityUnitAITargetCondition aiTargetCondition = ability.GetAiTargetCondition();

        switch (aiTargetCondition)
        {
        case AbilityUnitAITargetCondition.UNIT_TARGET_RANDOM:
            return(GetRandomHeroEntity(targetUnits));

        case AbilityUnitAITargetCondition.UNIT_TARGET_RANGE_MIN:
            return(GetRangeHeroEntity(caster.Get2DPosition(), targetUnits, MinValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_RANGE_MAX:
            return(GetRangeHeroEntity(caster.Get2DPosition(), targetUnits, MaxValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_HP_MIN:
            return(GetHpHeroEntity(targetUnits, MinValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_HP_MAX:
            return(GetHpHeroEntity(targetUnits, MaxValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_HP_PERCENT_MIN:
            return(GetHpPercentHeroEntity(targetUnits, MinValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_HP_PERCENT_MAX:
            return(GetHpPercentHeroEntity(targetUnits, MaxValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_ENERGY_MIN:
            return(GetEnergyHeroEntity(targetUnits, MinValueCompare()));

        case AbilityUnitAITargetCondition.UNIT_TARGET_ENERGY_MAX:
            return(GetEnergyHeroEntity(targetUnits, MaxValueCompare()));
        }
        BattleLog.LogError("[FilterTargetUnitFromList]没有找到指定Entity,返回列表第一个单位!");
        return(null);
    }
 public void GetTarget2DPosition(out float x, out float z)
 {
     if (targetType == AbilityRequestTargetType.UNIT)
     {
         m_TargetUnit.Get2DPosition(out x, out z);
     }
     else
     {
         x = targetPos.x;
         z = targetPos.y;
     }
 }
Example #6
0
    public override bool IsTrue(TBTWorkingData wData = null)
    {
        // 判断物体是否在视野范围内
        BattleBehaviorWorkingData data    = wData as BattleBehaviorWorkingData;
        AIBehaviorRequest         request = data.request;
        BattleUnit owner          = data.owner;
        float      viewRange      = owner.GetViewRange();
        Vector2    targetPos      = request.target.Get2DPosition();
        Vector2    ownerPos       = owner.Get2DPosition();
        float      distanceSquare = (targetPos - ownerPos).magnitude;

        BattleLog.Log(string.Format("【CON_IsInRange】距离:{0},可见范围:{1}", distanceSquare, viewRange * viewRange));
        return(distanceSquare <= viewRange * viewRange);
    }
Example #7
0
    protected override int onExecute(TBTWorkingData wData)
    {
        BattleBehaviorWorkingData behaviorData = wData as BattleBehaviorWorkingData;
        float             deltaTime            = behaviorData.deltaTime;
        BattleUnit        source  = behaviorData.owner;
        AIBehaviorRequest request = behaviorData.request;
        Unit target = request.target;

        Vector2 sourcePos = source.Get2DPosition();
        Vector2 targetPos = target.Get2DPosition();

        Vector2 sourceForward = source.Get2DForward();
        Vector2 targetForward = (targetPos - sourcePos).normalized;
        float   turnSpeed     = source.GetTurnSpeed();
        float   angle         = Vector2.Angle(targetForward, sourceForward);
        float   radianToTurn  = turnSpeed * deltaTime;

        //BattleLog.Log("【NOD_TurnTo】angle:{0},{1}", angle, radianToTurn * Mathf.Rad2Deg);
        if (angle < radianToTurn * Mathf.Rad2Deg)
        {
            source.Set2DForward(targetForward);
            GameMsg.instance.SendMessage(GameMsgDef.Hero_TurnTo2D, new HeorTurnEventArgs()
            {
                id      = source.id,
                forward = targetForward,
            });
            return(TBTRunningStatus.FINISHED);
        }

        // 叉积算出方向 unity是左手坐标系,所以反过来了
        Vector3 cross = Vector3.Cross(targetForward, sourceForward);

        if (cross.z > 0)
        {
            radianToTurn = -radianToTurn;
        }

        Vector2 newForward = BattleMath.Vector2RotateFromRadian(sourceForward.x, sourceForward.y, radianToTurn);

        //BattleLog.Log("【NOD_TurnTo】自己朝向:{0} 目标朝向:{1} 相隔角度:{2} 旋转弧度:{3} 叉乘:{4} 新的朝向:{5}", sourceForward.ToString(),targetForward.ToString(), angle.ToString(),(radianToTurn * Mathf.Rad2Deg).ToString(), cross.ToString(), newForward.ToString());
        source.Set2DForward(newForward);
        GameMsg.instance.SendMessage(GameMsgDef.Hero_TurnTo2D, new HeorTurnEventArgs()
        {
            id      = source.id,
            forward = newForward,
        });
        return(TBTRunningStatus.EXECUTING);
    }
    public override bool IsTrue(TBTWorkingData wData)
    {
        BattleBehaviorWorkingData behaviorData = wData as BattleBehaviorWorkingData;
        BattleUnit        owner   = behaviorData.owner;
        AIBehaviorRequest request = behaviorData.request;
        Unit target = request.target;

        Vector2 ownerPos  = owner.Get2DPosition();
        Vector2 targetPos = target.Get2DPosition();

        Vector2 toForward    = targetPos - ownerPos;
        Vector2 ownerForward = owner.Get2DForward();
        float   angle        = Vector2.Angle(toForward, ownerForward);

        //BattleLog.Log("【CON_IsAngleNeedTurnTo】angle:{0}", angle);
        return(angle > 0.1f);
    }
Example #9
0
        protected override int onExecute(TBTWorkingData wData)
        {
            BattleBehaviorWorkingData behaviorData = wData as BattleBehaviorWorkingData;
            float             deltaTime            = behaviorData.deltaTime;
            BattleUnit        source  = behaviorData.owner;
            AIBehaviorRequest request = behaviorData.request;
            Unit target = request.target;

            Vector2 ownerPos  = source.Get2DPosition();
            Vector2 targetPos = target.Get2DPosition();

            float attackRange = source.GetAttackRange();
            float distance    = (targetPos - ownerPos).magnitude;

            if (distance <= attackRange)
            {
                return(TBTRunningStatus.FINISHED);
            }

            float moveSpeed  = source.GetMoveSpeed();
            float detalTime  = behaviorData.deltaTime;
            float toForwardX = (targetPos.x - ownerPos.x) / distance;
            float toForwardZ = (targetPos.y - ownerPos.y) / distance;
            float newPosX    = ownerPos.x + detalTime * moveSpeed * toForwardX;
            float newPosZ    = ownerPos.y + detalTime * moveSpeed * toForwardZ;

            source.Set2DPosition(newPosX, newPosZ);
            //BattleLog.Log("【NOD_MoveTo】移动速度:{0} 当前位置:{1},{2} 目标位置:{3},{4} newPosX:{5} newPosZ:{6}}",
            //    moveSpeed.ToString(), ownerPos.x.ToString(),ownerPos.y.ToString(), targetPos.x.ToString(),targetPos.y.ToString()
            //    ,newPosX.ToString(),newPosZ.ToString());

            GameMsg.instance.SendMessage(GameMsgDef.Hero_MoveTo, new HeorMoveEventArgs()
            {
                id        = source.id,
                targetPos = new Vector3(newPosX, 0, newPosZ),
                forward   = new Vector3(toForwardX, 0, toForwardZ)
            });
            return(TBTRunningStatus.EXECUTING);
        }