Exemple #1
0
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     if (enabled)
     {
         Debug.Log(args.State + " (" + args.Priority + ")");
     }
 }
Exemple #2
0
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     if (mappingLookup.ContainsKey(args.State))
     {
         myAnimator.SwitchAnimation(mappingLookup[args.State]);
     }
 }
Exemple #3
0
        /// <summary>
        /// Gets a string representation of the animation to play.
        /// </summary>
        /// <returns>The new state string.</returns>
        /// <param name="args">Animation event arguments.</param>
        virtual protected string GetNewStateString(AnimationEventArgs args)
        {
            //TODO Is it worth using a buffer?
            string result = args.State.AsString();

            if (aimer != null)
            {
                int aimDir = GetYAimAsInt(aimer.GetAimDirection(myCharacter));
                if (aimDir == 1 && statesWithAimUpModifer.Contains(args.State))
                {
                    result += "_UP";
                }
                if (aimDir == -1 && statesWithAimDownModifer.Contains(args.State))
                {
                    result += "_DOWN";
                }
            }
            if (statesWithLeftRightModifer.Contains(args.State))
            {
                if (myMob.LastFacedDirection == 1)
                {
                    result += "_RIGHT";
                }
                else
                {
                    result += "_LEFT";
                }
            }
            return(result);
        }
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     // Don't queue states that are already queued
     if (queuedStates.Count < 1 || queuedStates.Peek() != args.State)
     {
         queuedStates.Enqueue(args.State);
         queuedPriorities.Enqueue(args.Priority);
     }
 }
Exemple #5
0
 /// <summary>
 /// Gets a string representation of the animation to play.
 /// </summary>
 /// <returns>The new state string.</returns>
 /// <param name="args">Animation event arguments.</param>
 virtual protected string GetNewStateString(AnimationEventArgs args)
 {
     foreach (AnimationTransitionMapping m in transitions)
     {
         if (m.previousState == args.PreviousState && m.newState == args.State)
         {
             return(args.PreviousState.AsString() + "_TO_" + args.State.AsString());
         }
     }
     return(args.State.AsString());
 }
Exemple #6
0
        /// <summary>
        /// Handles animation state changed.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        protected void AnimationStateChanged(object sender, AnimationEventArgs args)
        {
            string newStateString = GetNewStateString(args);

#if UNITY_EDITOR
        #if UNITY_5
            // In editor mode check that the state is present
            if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
            {
                if (!editor_stateNames.Contains(newStateString))
                {
                    Debug.LogError("The state " + newStateString + " is not in the animator, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                    return;
                }
            }
        #else
            // In editor mode check that the state is present
            if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
            {
                if (!editor_stateNames.Contains(newStateString))
                {
                    Debug.LogError("The state " + newStateString + " is not in the animator, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                    return;
                }
            }
        #endif
#endif
            // Don't queue states that are already queued
            if (queuedStates.Count < 1 || queuedStates.Peek() != args.State.AsString())
            {
                queuedStates.Enqueue(newStateString);
                queuedPriorities.Enqueue(args.Priority);
            }
            if (args.OverrideState != null && animationStateOverrideLookup.ContainsKey(args.OverrideState))
            {
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                myAnimator.runtimeAnimatorController = animationStateOverrideLookup[args.OverrideState];
                if (state != AnimationState.NONE.AsString())
                {
                    myAnimator.Play(state, 0, info.normalizedTime);
                    stateChangedThisFrame = true;
                }
            }
            else if (args.OverrideState == null)
            {
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                myAnimator.runtimeAnimatorController = defaultController;
                if (state != AnimationState.NONE.AsString())
                {
                    myAnimator.Play(state, 0, info.normalizedTime);
                    stateChangedThisFrame = true;
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Handles animation state changed.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        protected void AnimationStateChanged(object sender, AnimationEventArgs args)
        {
            string newStateString = GetNewStateString(args);

            // In this mode we cancel animation in the queue that have a lower priority
            if (!playEveryAnimation)
            {
                int nextPriority = queuedPriorities.Count > 0 ? queuedPriorities.Peek() :  -1;
                while (args.Priority > nextPriority && queuedPriorities.Count > 0)
                {
                    nextPriority = queuedPriorities.Peek();
                    queuedPriorities.Dequeue();
                    queuedStates.Dequeue();
                }
            }

            // This is a transition, queue the next state
            if (newStateString != args.State.AsString())
            {
                // Don't queue states that are already queued
                if (queuedStates.Count < 1 || queuedStates.Peek() != newStateString)
                {
                    queuedStates.Enqueue(newStateString);
                    queuedPriorities.Enqueue(args.Priority);

                    // Also queue the subsequent state
                    queuedStates.Enqueue(args.State.AsString());
                    queuedPriorities.Enqueue(args.Priority - 1);
                }
            }
            else
            {
                // Don't queue states that are already queued
                if (queuedStates.Count < 1 || queuedStates.Peek() != newStateString)
                {
                    queuedStates.Enqueue(newStateString);
                    queuedPriorities.Enqueue(args.Priority);
                }
            }


#if UNITY_EDITOR
            if (myAnimator.GetClipByName(newStateString) == null)
            {
                Debug.LogWarning("Clip with the name " + newStateString + " not found in animator. Clip names must be an exact uppercase match for state or transition names.");
                return;
            }
#endif
        }
Exemple #8
0
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     StopAllCoroutines();
     foreach (SoundEffectMapping se in mappings)
     {
         // Check state and layer matches
         if (se.state == args.State && (se.previousState == AnimationState.NONE || se.previousState == args.PreviousState) && (se.groundLayer == 0 || ((1 << myCharacter.GroundLayer) & se.groundLayer) == (1 << myCharacter.GroundLayer)))
         {
             if (se.repeatInterval > 0)
             {
                 StartCoroutine(RepeatSound(se));
             }
             else
             {
                 se.soundEffect.Play();
             }
         }
     }
 }
Exemple #9
0
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     StopAllCoroutines();
     foreach (ParticleEffectMapping p in mappings)
     {
         // Check state and layer matches
         if (p.state == args.State && (p.previousState == AnimationState.NONE || p.previousState == args.PreviousState) && (p.groundLayer == 0 || ((1 << myCharacter.GroundLayer) & p.groundLayer) == (1 << myCharacter.GroundLayer)))
         {
             if (p.repeatInterval > 0)
             {
                 StartCoroutine(RepeatEffect(p));
             }
             else
             {
                 DoPlay(p);
             }
         }
     }
 }
Exemple #10
0
        /// <summary>
        /// Unity Update hook.
        /// </summary>
        void Update()
        {
            stateChangedThisFrame = false;
            // If we have a new animation to play
            if (queuedStates.Count > 0)
            {
                string            nextState    = queuedStates.Peek();
                int               nextPriority = queuedPriorities.Peek();
                AnimatorStateInfo info         = myAnimator.GetCurrentAnimatorStateInfo(0);
                // Ensure we played the current state for at least one frame, this is to work around for Mecanim issue where calling Play isn't always playing the animation
                if (state == AnimationState.NONE.AsString() || info.IsName(state))
                {
                    // Next animation has higher priority, play it now
                    if (nextPriority >= priority || info.normalizedTime >= 1.0f)                      // || info.loop)
                    {
                        myAnimator.Play(nextState);
                        state    = nextState;
                        priority = nextPriority;
                        queuedStates.Dequeue();
                        queuedPriorities.Dequeue();
                        stateChangedThisFrame = true;
                    }
                }
            }

            // If we have an aimer set the gun variables
            if (aimer != null)
            {
                Vector2 aimDirection    = aimer.GetAimDirection((Character)myCharacter);
                int     aimDirectionInt = GetYAimAsInt(aimDirection);
                if (aimDirectionInt != previousAimDirection)
                {
                    animationEventArgs.UpdateAnimationEventArgs(myCharacter.AnimationState, myCharacter.AnimationState, myCharacter.OverrideState);
                    if (!stateChangedThisFrame)
                    {
                        AnimationStateChanged(this, animationEventArgs);
                    }
                }
                previousAimDirection = aimDirectionInt;
            }
            animationEventArgs = new AnimationEventArgs(AnimationState.NONE, AnimationState.NONE, null);
        }
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     // Stop warnings in Unity 2017
     if (!myAnimator.isActiveAndEnabled)
     {
         return;
     }
     if (currentOverride != args.OverrideState)
     {
         currentOverride = args.OverrideState;
         if (currentOverride == null || currentOverride == "")
         {
             myAnimator.Play(AnimationState.IDLE.AsString());
         }
         else
         {
             myAnimator.Play(currentOverride);
         }
     }
 }
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged( object sender, AnimationEventArgs args)
 {
     // Don't queue states that are already queued
     if (queuedStates.Count < 1 || queuedStates.Peek() != args.State)
     {
         queuedStates.Enqueue (args.State);
         queuedPriorities.Enqueue (args.Priority);
     }
 }
        /// <summary>
        /// Handles animation state changed.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        protected void AnimationStateChanged( object sender, AnimationEventArgs args)
        {
            string newStateString = GetNewStateString (args);

            #if UNITY_EDITOR
            #if UNITY_5
            // In editor mode check that the state is present
            if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
            {
                if (!editor_stateNames.Contains(args.State.AsString()))
                {
                    Debug.LogError("The state " + args.State.AsString() + " is not in the animator, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                    return;
                }
            }
            #else
            // In editor mode check that the state is present
            if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
            {
                if (!editor_stateNames.Contains(newStateString))
                {
                    Debug.LogError("The state " + args.State.AsString() + " is not in the animator, state names must be an exact UPPERCASE match for the Animation State or Transition State. This is being skipped in editor mode but this will lead to errors in release builds.");
                    return;
                }
            }
            #endif
            #endif
            // Don't queue states that are already queued
            if (queuedStates.Count < 1 || queuedStates.Peek() != newStateString)
            {
                queuedStates.Enqueue (newStateString);
                queuedPriorities.Enqueue (args.Priority);
            }
            if (args.OverrideState != null && animationStateOverrideLookup.ContainsKey(args.OverrideState))
            {
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                myAnimator.runtimeAnimatorController = animationStateOverrideLookup[args.OverrideState];
                if (state != AnimationState.NONE.AsString())
                {
                    myAnimator.Play(state, 0, info.normalizedTime);
                }
                // info = myAnimator.GetCurrentAnimatorStateInfo(0);
            }
            else if (args.OverrideState == null)
            {
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                myAnimator.runtimeAnimatorController = defaultController;
                if (state != AnimationState.NONE.AsString())
                {
                    myAnimator.Play(state, 0, info.normalizedTime);
                }
            }
        }
        /// <summary>
        /// Handles animation state changed.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        virtual protected void AnimationStateChanged(object sender, AnimationEventArgs args)
        {
            float normalizedTime = 0.0f;

            if (!ensureStatePlaysForOneFrame || hasPlayed)
            {
                myAnimator.SetInteger("PreviousState", (int)args.PreviousState);
                myAnimator.SetInteger("State", (int)args.State);
                state       = args.State;
                queuedState = AnimationState.NONE;
                hasPlayed   = false;
                myAnimator.SetBool("HasPlayed", false);
            }
            else
            {
                queuedState = args.State;
            }

            if (args.OverrideState != null && animationStateOverrideLookup.ContainsKey(args.OverrideState) && myAnimator.runtimeAnimatorController != animationStateOverrideLookup[args.OverrideState])
            {
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                myAnimator.runtimeAnimatorController = animationStateOverrideLookup[args.OverrideState];
                myAnimator.SetInteger("PreviousState", (int)state);
                myAnimator.SetInteger("State", (int)state);
                myAnimator.SetBool("HasPlayed", true);

                normalizedTime = (info.length < OneShotLength) ? 0 :  info.normalizedTime;

                if (aimer != null)
                {
                    Vector2 aimDirection = aimer.GetAimDirection((Character)myCharacter);
                    myAnimator.SetFloat("GunPositionX", aimDirection.x);
                    myAnimator.SetFloat("GunPositionY", aimDirection.y);

#if UNITY_EDITOR
        #if UNITY_5
                    // In editor mode check that the state is present
                    if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
                    {
        #else
                    // In editor mode check that the state is present
                    if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
                    {
        #endif

                        if (nonAimingStates.Contains(state.AsString()) && editor_stateNames.Contains(state.AsString()))
                        {
                            Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                            return;
                        }
                        else if (aimDirection.y > 0 && !editor_stateNames.Contains(state.AsString() + "_UP"))
                        {
                            Debug.LogError("The state " + args.State.AsString() + "_UP is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
                            return;
                        }
                        else if (aimDirection.y < 0 && !editor_stateNames.Contains(state.AsString() + "_DOWN"))
                        {
                            Debug.LogError("The state " + args.State.AsString() + "_DOWN is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
                            return;
                        }
                        else if (aimDirection.y == 0 && !editor_stateNames.Contains(state.AsString()))
                        {
                            Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                            return;
                        }
                    }
#endif
                    if (nonAimingStates.Contains(state.AsString()))
                    {
                        myAnimator.Play(state.AsString(), 0, info.normalizedTime);
                    }
                    else if (aimDirection.y > 0)
                    {
                        myAnimator.Play(state.AsString() + "_UP", 0, normalizedTime);
                    }
                    else if (aimDirection.y < 0)
                    {
                        myAnimator.Play(state.AsString() + "_DOWN", 0, normalizedTime);
                    }
                    else
                    {
                        myAnimator.Play(state.AsString(), 0, info.normalizedTime);
                    }
                }
                else
                {
#if UNITY_EDITOR
        #if UNITY_5
                    // In editor mode check that the state is present
                    if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
                    {
        #else
                    // In editor mode check that the state is present
                    if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
                    {
        #endif
                        if (!editor_stateNames.Contains(state.AsString()))
                        {
                            Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                            return;
                        }
                    }
#endif
                    myAnimator.Play(state.AsString(), 0, normalizedTime);
                }
                info = myAnimator.GetCurrentAnimatorStateInfo(0);
            }
            else if (args.OverrideState == null && myAnimator.runtimeAnimatorController != defaultController)
            {
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                myAnimator.runtimeAnimatorController = defaultController;
                normalizedTime = (info.length < OneShotLength) ? 0 :  info.normalizedTime;
                if (state != AnimationState.NONE)
                {
                    myAnimator.SetInteger("PreviousState", (int)state);
                    myAnimator.SetInteger("State", (int)state);
                    myAnimator.SetBool("HasPlayed", true);
                    if (aimer != null)
                    {
                        Vector2 aimDirection = aimer.GetAimDirection((Character)myCharacter);
                        myAnimator.SetFloat("GunPositionX", aimDirection.x);
                        myAnimator.SetFloat("GunPositionY", aimDirection.y);
#if UNITY_EDITOR
        #if UNITY_5
                        // In editor mode check that the state is present
                        if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
                        {
        #else
                        // In editor mode check that the state is present
                        if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
                        {
        #endif
                            if (nonAimingStates.Contains(state.AsString()) && editor_stateNames.Contains(state.AsString()))
                            {
                                Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                                return;
                            }
                            else if (aimDirection.y > 0 && !editor_stateNames.Contains(state.AsString() + "_UP"))
                            {
                                Debug.LogError("The state " + args.State.AsString() + "_UP is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
                                return;
                            }
                            else if (aimDirection.y < 0 && !editor_stateNames.Contains(state.AsString() + "_DOWN"))
                            {
                                Debug.LogError("The state " + args.State.AsString() + "_DOWN is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
                                return;
                            }
                            else if (aimDirection.y == 0 && !editor_stateNames.Contains(state.AsString()))
                            {
                                Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                                return;
                            }
                        }
#endif
                        if (nonAimingStates.Contains(state.AsString()))
                        {
                            myAnimator.Play(state.AsString(), 0, info.normalizedTime);
                        }
                        else if (aimDirection.y > 0)
                        {
                            myAnimator.Play(state.AsString() + "_UP", 0, normalizedTime);
                        }
                        else if (aimDirection.y < 0)
                        {
                            myAnimator.Play(state.AsString() + "_DOWN", 0, normalizedTime);
                        }
                        else
                        {
                            myAnimator.Play(state.AsString(), 0, normalizedTime);
                        }
                    }
                    else
                    {
#if UNITY_EDITOR
        #if UNITY_5
                        // In editor mode check that the state is present
                        if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
                        {
        #else
                        // In editor mode check that the state is present
                        if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
                        {
        #endif
                            if (!editor_stateNames.Contains(state.AsString()))
                            {
                                Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
                                return;
                            }
                        }
#endif
                        myAnimator.Play(state.AsString(), 0, normalizedTime);
                    }
                }
            }
        }
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged(object sender, AnimationEventArgs args)
 {
     queuedStates.Enqueue(args.State);
     queuedPriorities.Enqueue(args.Priority);
 }
 /// <summary>
 /// Handles animation state changed.
 /// </summary>
 /// <param name="sender">Sender.</param>
 /// <param name="args">Arguments.</param>
 protected void AnimationStateChanged( object sender, AnimationEventArgs args)
 {
     queuedStates.Enqueue (args.State);
     queuedPriorities.Enqueue (args.Priority);
 }
        /// <summary>
        /// Unity Update hook.
        /// </summary>
        void Update()
        {
            stateChangedThisFrame = false;
            // If we have a new animation to play
            if (queuedStates.Count > 0)
            {
                string nextState = queuedStates.Peek ();
                int nextPriority = queuedPriorities.Peek ();
                AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
                // Ensure we played the current state for at least one frame, this is to work around for Mecanim issue where calling Play isn't always playing the animation
                if (state == AnimationState.NONE.AsString() || info.IsName(state))
                {
                    // Next animation has higher priority, play it now
                    if (nextPriority >= priority || info.normalizedTime >= 1.0f ) // || info.loop)
                    {
                        myAnimator.Play(nextState);
                        state = nextState;
                        priority = nextPriority;
                        queuedStates.Dequeue ();
                        queuedPriorities.Dequeue();
                        stateChangedThisFrame = true;
                    }
                }
            }

            // If we have an aimer set the gun variables
            if (aimer != null)
            {
                Vector2 aimDirection = aimer.GetAimDirection((Character)myCharacter);
                int aimDirectionInt = GetYAimAsInt(aimDirection);
                if (aimDirectionInt != previousAimDirection)
                {
                    animationEventArgs.UpdateAnimationEventArgs(myCharacter.AnimationState, myCharacter.AnimationState, myCharacter.OverrideState);
                    if (!stateChangedThisFrame) AnimationStateChanged(this, animationEventArgs);
                }
                previousAimDirection = aimDirectionInt;
            }
            animationEventArgs = new AnimationEventArgs(AnimationState.NONE, AnimationState.NONE, null);
        }
 /// <summary>
 /// Gets a string representation of the animation to play.
 /// </summary>
 /// <returns>The new state string.</returns>
 /// <param name="args">Animation event arguments.</param>
 protected virtual string GetNewStateString(AnimationEventArgs args)
 {
     foreach(AnimationTransitionMapping m in transitions)
     {
         if (m.previousState == args.PreviousState && m.newState == args.State)
         {
             return args.PreviousState.AsString() + "_TO_" + args.State.AsString();
         }
     }
     return args.State.AsString ();
 }
		/// <summary>
		/// Handles animation state changed.
		/// </summary>
		/// <param name="sender">Sender.</param>
		/// <param name="args">Arguments.</param>
		virtual protected void AnimationStateChanged( object sender, AnimationEventArgs args)
		{
			float normalizedTime = 0.0f;

			if (!ensureStatePlaysForOneFrame || hasPlayed)
			{
				myAnimator.SetInteger("PreviousState", (int)args.PreviousState);
				myAnimator.SetInteger("State", (int)args.State);
				state = args.State;
				queuedState = AnimationState.NONE;
				hasPlayed = false;
				myAnimator.SetBool("HasPlayed", false);
			}
			else
			{
				queuedState = args.State;
			}

			if (args.OverrideState != null && animationStateOverrideLookup.ContainsKey(args.OverrideState) && myAnimator.runtimeAnimatorController != animationStateOverrideLookup[args.OverrideState])
			{
				AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
				myAnimator.runtimeAnimatorController = animationStateOverrideLookup[args.OverrideState];
				myAnimator.SetInteger("PreviousState", (int)state);
				myAnimator.SetInteger("State", (int)state);
				myAnimator.SetBool("HasPlayed", true);

				normalizedTime = (info.length < OneShotLength) ? 0 :  info.normalizedTime;

				if (aimer != null)
				{
					Vector2 aimDirection = aimer.GetAimDirection((Character)myCharacter);
					myAnimator.SetFloat("GunPositionX",aimDirection.x);
					myAnimator.SetFloat("GunPositionY",aimDirection.y);

#if UNITY_EDITOR
	#if UNITY_5
					// In editor mode check that the state is present
					if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
					{
	#else
					// In editor mode check that the state is present
					if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
					{
	#endif

						if (nonAimingStates.Contains(state.AsString()) && editor_stateNames.Contains(state.AsString()))
					    {
							Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
							return;
						}
						else if (aimDirection.y > 0 && !editor_stateNames.Contains(state.AsString() +  "_UP"))
						{
							Debug.LogError("The state " + args.State.AsString() + "_UP is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
							return;
						}
						else if (aimDirection.y < 0 && !editor_stateNames.Contains(state.AsString() +  "_DOWN"))
						{
							Debug.LogError("The state " + args.State.AsString() + "_DOWN is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
							return;
						}
						else if (aimDirection.y == 0 && !editor_stateNames.Contains(state.AsString()))
						{

							Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
							return;
						}
					}
#endif
					if (nonAimingStates.Contains(state.AsString())) myAnimator.Play(state.AsString(), 0, info.normalizedTime);
					else if (aimDirection.y > 0) myAnimator.Play(state.AsString() + "_UP", 0, normalizedTime);
					else if (aimDirection.y < 0) myAnimator.Play(state.AsString() + "_DOWN", 0, normalizedTime);
					else myAnimator.Play(state.AsString(), 0, info.normalizedTime);
				}
				else
				{
#if UNITY_EDITOR
	#if UNITY_5
					// In editor mode check that the state is present
					if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
					{
	#else
						// In editor mode check that the state is present
						if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
						{
	#endif
						if (!editor_stateNames.Contains(state.AsString()))
						{
							Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
							return;
						}
					}
#endif
					myAnimator.Play(state.AsString(), 0, normalizedTime);
				}
				info = myAnimator.GetCurrentAnimatorStateInfo(0);
			}
			else if (args.OverrideState == null && myAnimator.runtimeAnimatorController != defaultController)
			{
				AnimatorStateInfo info = myAnimator.GetCurrentAnimatorStateInfo(0);
				myAnimator.runtimeAnimatorController = defaultController;
				normalizedTime = (info.length < OneShotLength) ? 0 :  info.normalizedTime;
				if (state != AnimationState.NONE)
				{
					myAnimator.SetInteger("PreviousState", (int)state);
					myAnimator.SetInteger("State", (int)state);
					myAnimator.SetBool("HasPlayed", true);
					if (aimer != null)
					{
						Vector2 aimDirection = aimer.GetAimDirection((Character)myCharacter);
						myAnimator.SetFloat("GunPositionX",aimDirection.x);
						myAnimator.SetFloat("GunPositionY",aimDirection.y);
#if UNITY_EDITOR
	#if UNITY_5
						// In editor mode check that the state is present
						if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
						{
	#else
						// In editor mode check that the state is present
						if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
						{
	#endif
							if (nonAimingStates.Contains(state.AsString()) && editor_stateNames.Contains(state.AsString()))
							{
								Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
								return;
							}
							else if (aimDirection.y > 0 && !editor_stateNames.Contains(state.AsString() +  "_UP"))
							{
								Debug.LogError("The state " + args.State.AsString() + "_UP is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
								return;
							}
							else if (aimDirection.y < 0 && !editor_stateNames.Contains(state.AsString() +  "_DOWN"))
							{
								Debug.LogError("The state " + args.State.AsString() + "_DOWN is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State with suffixes of _UP and _DOWN. This is being skipped in editor mode but this will lead to errors in release builds.");
								return;
							}
							else if (aimDirection.y == 0 && !editor_stateNames.Contains(state.AsString()))
							{
								
								Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides and aimers, state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
								return;
							}
						}
#endif
						if (nonAimingStates.Contains(state.AsString())) myAnimator.Play(state.AsString(), 0, info.normalizedTime);
						else if (aimDirection.y > 0) myAnimator.Play(state.AsString() + "_UP", 0, normalizedTime);
						else if (aimDirection.y < 0) myAnimator.Play(state.AsString() + "_DOWN", 0, normalizedTime);
						else myAnimator.Play(state.AsString(), 0, normalizedTime);
					}
					else
					{
#if UNITY_EDITOR
	#if UNITY_5
						// In editor mode check that the state is present
						if (myAnimator.runtimeAnimatorController is UnityEditor.Animations.AnimatorController)
						{
	#else
						// In editor mode check that the state is present
						if (myAnimator.runtimeAnimatorController is UnityEditorInternal.AnimatorController)
						{
	#endif
							if (!editor_stateNames.Contains(state.AsString()))
							{
								Debug.LogError("The state " + args.State.AsString() + " is not in the animator, when you use overrides state names must be an exact UPPERCASE match for the Animation State. This is being skipped in editor mode but this will lead to errors in release builds.");
								return;
							}
						}
#endif
						myAnimator.Play(state.AsString(), 0, normalizedTime);						
					}
				}
			}
		}
        /// <summary>
        /// Handles animation state changed.
        /// </summary>
        /// <param name="sender">Sender.</param>
        /// <param name="args">Arguments.</param>
        protected void AnimationStateChanged( object sender, AnimationEventArgs args)
        {
            string newStateString = GetNewStateString (args);

            // In this mode we cancel animation in the queue that have a lower priority
            if (!playEveryAnimation)
            {
                int nextPriority = queuedPriorities.Count > 0 ? queuedPriorities.Peek () :  -1;
                while (args.Priority > nextPriority && queuedPriorities.Count > 0)
                {
                    nextPriority = queuedPriorities.Peek ();
                    queuedPriorities.Dequeue();
                    queuedStates.Dequeue();
                }
            }

            // This is a transition, queue the next state
            if (newStateString != args.State.AsString())
            {
                // Don't queue states that are already queued
                if (queuedStates.Count < 1 || queuedStates.Peek() != newStateString)
                {
                    queuedStates.Enqueue (newStateString);
                    queuedPriorities.Enqueue (args.Priority);

                    // Also queue the subsequent state
                    queuedStates.Enqueue (args.State.AsString());
                    queuedPriorities.Enqueue (args.Priority - 1);
                }
            }
            else
            {
                // Don't queue states that are already queued
                if (queuedStates.Count < 1 || queuedStates.Peek() != newStateString)
                {
                    queuedStates.Enqueue (newStateString);
                    queuedPriorities.Enqueue (args.Priority);
                }
            }

            #if UNITY_EDITOR
            if (myAnimator.GetClipByName(newStateString) == null)
            {
                Debug.LogWarning("Clip with the name " + newStateString + " not found in animator. Clip names must be an exact uppercase match for state or transition names.");
                return;
            }
            #endif
        }
 /// <summary>
 /// Gets a string representation of the animation to play.
 /// </summary>
 /// <returns>The new state string.</returns>
 /// <param name="args">Animation event arguments.</param>
 protected virtual string GetNewStateString(AnimationEventArgs args)
 {
     //TODO Is it worth using a buffer?
     string result = args.State.AsString ();
     if (aimer != null)
     {
         int aimDir = GetYAimAsInt(aimer.GetAimDirection(myCharacter));
         if (aimDir == 1 && statesWithAimUpModifer.Contains (args.State)) result += "_UP";
         if (aimDir == -1 && statesWithAimDownModifer.Contains (args.State)) result += "_DOWN";
     }
     if (statesWithLeftRightModifer.Contains(args.State))
     {
         if (myMob.LastFacedDirection == 1)
         {
             result += "_RIGHT";
         }
         else
         {
             result += "_LEFT";
         }
     }
     return result;
 }