Ejemplo n.º 1
0
        public override BhvStatus Invoke(Npc a, BhvTree b)
        {
            if (a.MovePos == Vector3.zero)
            {
                b.SetStatus(BhvStatus.FAILURE);
                return(b.Status);
            }

            if (a.TargetObj != null && b.SeeTarget)
            {
                StopMove(a);
                b.SetStatus(BhvStatus.FAILURE);
                return(b.Status);
            }

            if (a.TargetObj == null && a.NavAgent.hasPath)
            {
                // Sometimes the calculated destination on the nav mesh isn't exactly where we wanted to move
                if (a.MovePos != a.NavAgent.destination)
                {
                    a.SetMovePos(a.NavAgent.destination);
                }

                // Something caught our attention midmove
                if (a.TargetPos != Vector3.zero)
                {
                    // Debug.Log( "Something caught our attention midmove" );
                    StopMove(a);
                    b.SetStatus(BhvStatus.FAILURE);
                    return(b.Status);
                }
            }

            float moveDist = a.MoveDistSqr;

            // Need to include NavAgent.radius or else the Npc won't ever reach the MovePos
            if (moveDist <= Util.GetSquare(a.Params.StopDist + a.NavAgent.radius))
            {
                StopMove(a);
                b.SetStatus(BhvStatus.SUCCESS);
                return(b.Status);
            }

            float nSpd = a.Params.MoveSpd * a.Params.MoveWalk;

            // Set Speed accordingly
            if (a.NavAgent.speed != nSpd)
            {
                SetSpeed(a, nSpd);
            }

            if (!b.AnimInfo.IsName(AnimString.WALK))
            {
                SetAnimTrigger(a, AnimString.WALK);
            }

            if (b.TimerCheck >= a.Params.ChkIdle)
            {
                b.TimerCheck = 0.0f;
                CheckEnv(a, b);
            }

            if (a.NavAgent.destination != a.MovePos)
            {
                StartMove(a, a.MovePos);
            }

            return(b.Status);
        }
Ejemplo n.º 2
0
        public override BhvStatus Invoke(Npc a, BhvTree b)
        {
            if (a.TargetObj == null)
            {
                b.SetStatus(BhvStatus.FAILURE);
                return(b.Status);
            }

            if (a.TargetEnt?.Stats.IsDead ?? false)
            {
                StopMove(a);
                a.ResetTargetObj();
                b.SetStatus(BhvStatus.SUCCESS);
                return(b.Status);
            }

            if (b.DistTarget <= Util.GetSquare(a.Params.AtkDist))
            {
                b.SetStatus(BhvStatus.SUCCESS);
                return(b.Status);
            }

            if (a.NavAgent.pathStatus == NavMeshPathStatus.PathPartial || a.NavAgent.pathStatus == NavMeshPathStatus.PathInvalid)
            {
                bool failure = false;

                if (a.NavAgent.pathStatus == NavMeshPathStatus.PathInvalid)
                {
                    failure = true;
                }

                if (a.NavAgent.pathStatus == NavMeshPathStatus.PathPartial)
                {
                    if (Util.CheckDistSqr(a.NavAgent.destination, a.TargetPos) > Util.GetSquare(a.Params.AtkDist))
                    {
                        failure = true;
                    }
                }

                if (failure)
                {
                    StopMove(a);
                    b.SetPathFailed(true);
                    b.SetStatus(BhvStatus.FAILURE);
                    return(b.Status);
                }
            }

            a.SetMovePos(a.TargetPos);

            float nSpd = a.Params.MoveSpd;

            if (b.AnimInfo.IsName(AnimString.ATK))
            {
                nSpd *= a.Params.AtkSlow;
            }
            else
            {
                if (!b.AnimInfo.IsName(AnimString.RUN))
                {
                    SetAnimTrigger(a, AnimString.RUN);
                }
            }

            // Set Speed accordingly
            if (a.NavAgent.speed != nSpd)
            {
                SetSpeed(a, nSpd);
            }

            if (b.TimerCheck >= a.Params.ChkAlert)
            {
                if (a.TargetPos != a.MovePos)
                {
                    a.SetMovePos(a.TargetPos);
                }

                b.TimerCheck = 0.0f;
                CheckEnv(a, b);
            }

            if (a.NavAgent.destination != a.MovePos)
            {
                StartMove(a, a.MovePos);
            }

            return(b.Status);
        }
Ejemplo n.º 3
0
        public override BhvStatus Invoke(Npc a, BhvTree b)
        {
            if (a.TargetObj == null)
            {
                b.SetStatus(BhvStatus.SUCCESS);
                return(b.Status);
            }

            if (a.TargetEnt?.Stats.IsDead ?? false)
            {
                StopMove(a);
                a.ResetTargetObj();
                b.SetStatus(BhvStatus.SUCCESS);
                return(b.Status);
            }

            if (!b.PathFailed && b.TimerCheck >= a.Params.ChkAlert)
            {
                b.TimerCheck = 0.0f;
                CheckEnv(a, b);
            }

            // Set Speed accordingly
            if (a.NavAgent.speed != a.Params.MoveSpd)
            {
                SetSpeed(a, a.Params.MoveSpd);
            }

            if (a.MovePos == Vector3.zero)
            {
                Vector3 nPos = FindFleePoint(a);

                if (nPos == Vector3.zero)
                {
                    // Debug.Log( "Unable to find flee point" );
                    if (!b.AnimInfo.IsName(AnimString.IDLE))
                    {
                        SetAnimTrigger(a, AnimString.IDLE);
                    }
                    return(BhvStatus.RUNNING);
                }

                // Debug.Log( "Found flee point" );
                a.SetMovePos(nPos);
            }

            if (a.NavAgent.pathPending)
            {
                return(BhvStatus.RUNNING);
            }

            if (!a.NavAgent.hasPath)
            {
                StartMove(a, a.MovePos);
            }
            else
            {
                if (!b.AnimInfo.IsName(AnimString.RUN))
                {
                    SetAnimTrigger(a, AnimString.RUN);
                }

                if (a.MovePos != a.NavAgent.destination)
                {
                    a.SetMovePos(a.NavAgent.destination);
                }

                // TurnToward( a, a.NavAgent.steeringTarget );
            }

            // Need to include NavAgent.radius or else the Npc won't ever reach the MovePos
            if (b.DistMove <= Util.GetSquare(a.Params.StopDist + a.NavAgent.radius) && a.NavAgent.hasPath)
            {
                if ((a.TargetDistSqr > Util.GetSquare(a.Params.AwareMax + 10.0f)))
                {
                    StopMove(a);
                    // Debug.Log( "Safe distance from target, flight over" );
                    a.ResetTargetObj();
                    b.SetPathFailed(false);
                    b.SetStatus(BhvStatus.SUCCESS);
                    return(b.Status);
                }
                else
                {
                    StopMove(a);
                    // Debug.Log( "Resetting move for recalculation" );
                }
            }

            return(b.Status);
        }