private void BeginLoadingState(NavState destination)
 {
     CurrentState = destination;
     CurrentState.StateMachine.SetActiveChild(CurrentState);
     CurrentState.StartLoading();
     UpdateCurrentState();
 }
Esempio n. 2
0
        public void Initialize(NavStateContext context, NavState parent)
        {
            m_context = context;
            m_state   = parent;

            Initialize();
        }
Esempio n. 3
0
        public static string CalculatePath(NavState state)
        {
            s_builder.Clear();
            s_scratchList.Clear();

            while (true)
            {
                s_scratchList.Add(state);

                if (state.IsRoot)
                {
                    break;
                }

                state = state.StateMachine;
            }

            s_scratchList.Reverse();

            s_builder.Append(s_scratchList[0].Name);

            for (var index = 1; index < s_scratchList.Count; index++)
            {
                s_builder.Append("/");
                s_builder.Append(s_scratchList[index].Name);
            }

            return(s_builder.ToString());
        }
        private void EnqueueStatesToLeave(NavState untilParent)
        {
            NavState activeState = CurrentState;

            while (activeState != untilParent)
            {
                m_navigationPlan.AddStateToLeave(activeState);
                activeState = activeState.StateMachine;
            }
        }
        private void StartUnloadingStatesToLeave()
        {
            List <NavState> statesToLeave    = m_navigationPlan.StatesToLeave;
            int             numStatesToLeave = statesToLeave.Count;

            for (int i = 0; i < numStatesToLeave; ++i)
            {
                NavState stateToLeave = statesToLeave[i];
                stateToLeave.StartUnloading();
            }
        }
        public void NavStateModeChanged(NavState state)
        {
            if (m_enableDebugLogging)
            {
                m_logger.Info($"{state.Path} -> {state.Mode}");
            }

#if UNITY_EDITOR
            UnityEditor.EditorApplication.RepaintHierarchyWindow();
#endif
        }
        private void LeaveStates()
        {
            List <NavState> statesToLeave    = m_navigationPlan.StatesToLeave;
            int             numStatesToLeave = statesToLeave.Count;

            for (int i = 0; i < numStatesToLeave; ++i)
            {
                NavState stateToLeave = statesToLeave[i];
                stateToLeave.OnStateLeave();
            }
        }
Esempio n. 8
0
        public void HandleTransition(string transition)
        {
            NavState targetState = null;

            if (Navigator.TryGetStateForTransition(transition, ref targetState))
            {
                TransitionTo(targetState);
            }
            else
            {
                m_logger.Error($"Failed to find transition: {transition} [{Active.Path}]");
            }
        }
        private void InternalNavigateTo(NavState destination)
        {
            // TODO: Handle where navigation is requested while a state is loading.
            UnityEngine.Debug.Assert(m_navigationPlan.IsComplete, "Navigation was requested before navigation finished!");

            m_navigationPlan.Reset();
            m_navigationPlan.OriginState = CurrentState;

            NavStateUtils.GetParents(destination, m_parentsScratch);
            m_parentsScratch.Reverse();

            NavStateMachine parentToEnterFrom = null;

            // Determine the states to be entered
            for (var index = 0; index < m_parentsScratch.Count; index++)
            {
                NavStateMachine parent = m_parentsScratch[index];
                if (parentToEnterFrom == null && NavStateUtils.NeedsToBeEntered(parent))
                {
                    parentToEnterFrom = parent;
                }

                if (parentToEnterFrom != null)
                {
                    m_navigationPlan.AddStateToEnter(parent);
                }
            }

            m_navigationPlan.AddStateToEnter(destination);

            // Check if the destination is a state machine and repeatedly add the default state until we get to a leaf.
            if (!AddDefaultStatesToEnter(destination))
            {
                return;
            }

            parentToEnterFrom = parentToEnterFrom == null ? destination.StateMachine : parentToEnterFrom.StateMachine;

            // Determine the states that will be exited
            EnqueueStatesToLeave(parentToEnterFrom);

            m_navigationPlan.NavigationStarted();

            // Begin the unloading process
            StartUnloadingStatesToLeave();

            UpdateCurrentState();
        }
        private void OnCurrentStateUnloadingFinished()
        {
            m_navigationPlan.StateUnloaded();

            if (m_navigationPlan.CurrentStateToUnload() != null)
            {
                CurrentState = m_navigationPlan.CurrentStateToUnload();
                UpdateCurrentState();                 // This will check whether it is ready to finalize unloading and continue from there.
            }
            else
            {
                // All states that need to be unloaded have been. Now we can exit them.
                LeaveStates();
                BeginLoadingState(m_navigationPlan.CurrentStateToLoad());
            }
        }
        /// <summary>
        /// Checks if the given state is a StateMachine and adds the StateMachine's default state.
        /// </summary>
        private bool AddDefaultStatesToEnter(NavState destination)
        {
            var destinationStateMachine = destination as NavStateMachine;

            while (destinationStateMachine != null)
            {
                var defaultChild = destinationStateMachine.DefaultState;
                if (defaultChild == null)
                {
                    m_logger.Error($"Failed to navigate to next state. {destinationStateMachine.Path} is missing the default state");
                    return(false);
                }

                m_navigationPlan.AddStateToEnter(defaultChild);
                destinationStateMachine = defaultChild as NavStateMachine;
            }
            return(true);
        }
Esempio n. 12
0
        public static void GetParents(NavState state, List <NavStateMachine> parentsOut)
        {
            parentsOut.Clear();
            if (state == null)
            {
                return;
            }

            var parent = state.StateMachine;

            while (true)
            {
                parentsOut.Add(parent);
                parent = parent.StateMachine;

                if (parent.IsRoot)
                {
                    break;
                }
            }
        }
Esempio n. 13
0
 public static bool NeedsToBeEntered(NavState state)
 {
     return(state.Mode == StateMode.Inactive || state.NeedsReenter());
 }
Esempio n. 14
0
 public static NavState[] GetChildren(NavState state)
 {
     return(GetComponentsInChildrenWithoutParent(state.gameObject));
 }
 /// <summary>
 /// The Navigator will set the active child during navigation
 /// </summary>
 public void SetActiveChild(NavState state)
 {
     m_activeChild = state;
 }
 public void Navigate(NavState targetState)
 {
     InternalNavigateTo(targetState);
 }
        public bool CanProcessTransition(string transition)
        {
            NavState targetState = null;

            return(m_navigationPlan.IsComplete && TryGetStateForTransition(transition, ref targetState));
        }
Esempio n. 18
0
 public void TransitionTo(NavState targetState)
 {
     LastActiveState = Active;
     Navigator.Navigate(targetState);
 }
Esempio n. 19
0
 public void AddStateToEnter(NavState navState)
 {
     StatesToEnter.Add(navState);
 }
Esempio n. 20
0
 public void AddStateToLeave(NavState navState)
 {
     StatesToLeave.Add(navState);
 }
 public bool TryGetStateForTransition(string transition, ref NavState targetState)
 {
     return(CurrentState.TryGetStateForTransition(transition, ref targetState));
 }