Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to add a new transition to the current list which is able to be transitioned to from any other state
        /// </summary>
        /// <param name="a_To">The state to transition to from any other state</param>
        /// <param name="a_IsValidateTransition">An optional delegate with no parameters that returns true when the state change is valid and false when it is not</param>
        /// <returns>Returns true if the transition was able to be added and false otherwise</returns>
        public bool AddTransitionFromAny(T a_To, IsValidateTransition a_IsValidateTransition = null)
        {
            if (!m_States.Contains(a_To))
            {
                Debug.Warning("'" + a_To + "'does not exist in '" + typeof(T) + "'");
                return(false);
            }

            if (!m_TransitionsFromAny.ContainsKey(a_To))
            {
                if (a_IsValidateTransition == null)
                {
                    m_TransitionsFromAny[a_To] = () => true;
                }
                else
                {
                    m_TransitionsFromAny[a_To] = a_IsValidateTransition;
                }
                return(true);
            }
            else
            {
                Debug.Warning("'" + a_To + "' already exists as a transition key");
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Attempts to add a new transition to the current list of transitions
        /// </summary>
        /// <param name="a_From">The state to come from</param>
        /// <param name="a_To">The state to go to</param>
        /// <param name="a_IsValidTransition">An optional delegate with no parameters that returns true when the state change is valid and false when it is not</param>
        /// <returns>Returns true if the transition was able to be added and false otherwise</returns>
        public bool AddTransition(T a_From, T a_To, IsValidateTransition a_IsValidTransition = null)
        {
            // if 'a_From' and 'a_To' are the same state
            if (a_From.Equals(a_To))
            {
                Debug.Warning("'" + a_From + "'" + " is the same state as " + "'" + a_To + "'");
                return(false);
            }

            // if 'a_From' or 'a_To' is not in the list of states
            if (!m_States.Contains(a_From) || !m_States.Contains(a_To))
            {
                T invalidKey;   // Will decipher which state is invalid
                if (!m_States.Contains(a_From))
                {
                    invalidKey = a_From;
                }
                else
                {
                    invalidKey = a_To;
                }

                Debug.Warning("'" + invalidKey + "' does not exist in '" + typeof(T) + "'");
                return(false);
            }

            // Properly serializes 'a_From' and 'a_To' into the expected key format
            string key = a_From.ToString() + "->" + a_To.ToString();

            // if the key 'key' does not currently exist in 'm_Transitions'
            if (!m_Transitions.ContainsKey(key))
            {
                // if the user did not pass in a delegate to check the transition
                if (a_IsValidTransition == null)
                {
                    m_Transitions[key] = () => true;            // Set a default one that always allows the transition
                }
                else
                {
                    m_Transitions[key] = a_IsValidTransition;   // Otherwise use the one they passed in
                }
                return(true);
            }
            else
            {
                Debug.Warning("'" + key + "' already exists as a transition key");
                return(false);
            }
        }