Beispiel #1
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);
    }