Beispiel #1
0
        /// <summary>
        /// Updates navigation.  (Expected to be called every frame.)
        /// </summary>
        public void Update()
        {
            // Detect Goal in range.

            bool isInRange = agent.IsInRange(agent.data.goal.point, agent.data.position.point);

            if (isInRange && !mAtGoal)
            {
                mAtGoal = true;

                if ((agent.data.flags & NavFlag.GoalIsDynamic) == 0)
                {
                    PostEvent(NavEventType.ReachedGoal);
                }
            }
            else if (!isInRange && mAtGoal)
            {
                mAtGoal = false;
            }

            // Detect navigation failure.

            if ((agent.data.flags & (NavFlag.PlannerFailed | NavFlag.MoverFailed)) == 0)
            {
                if (mNavFailed)
                {
                    mNavFailed      = false;
                    mNavFailedCount = 0;

                    if (!mNavSuspended)
                    {
                        PostEvent(NavEventType.NavActivated);
                    }
                }
            }
            else if (!mNavFailed)
            {
                mNavFailedCount++;

                if (mNavFailedCount > NavFailureThreshold)
                {
                    mNavFailed = true;

                    PostEvent(NavEventType.NavFailed);

                    if (!mNavSuspended)
                    {
                        PostEvent(NavEventType.NavSuspended);
                    }
                }
            }
            if ((agent.data.flags | NavFlag.PlannerFailed) != 0)
            {
                mNavFailedCount++;

                if (mNavFailedCount > 10)
                {
                    mNavFailed = true;
                }
            }
            else
            {
                mNavFailed      = false;
                mNavFailedCount = 0;
            }

            // Detect suspension

            if (mNavSuspended && agent.data.targetMode != NavMode.Suspended)
            {
                mNavSuspended = false;

                if (!mNavFailed)
                {
                    PostEvent(NavEventType.NavActivated);
                }
            }
            else if (!mNavSuspended && agent.data.targetMode == NavMode.Suspended)
            {
                mNavSuspended = true;

                if (!mNavFailed)
                {
                    PostEvent(NavEventType.NavSuspended);
                }
            }
        }