public override void EnterState()
        {
            if (!(this.StateMachine is AvatarAnimationStateMachine))
            {
                throw new Exception("AnimationSequence expects to be used by AvatarAnimationStateMachine, not " + this.StateMachine.GetType().Name);
            }

            AvatarAnimationStateMachine stateMachine = (AvatarAnimationStateMachine)this.StateMachine;

            if (mExecutingAnimations != null)
            {
                mExecutingAnimations.Exit();
            }

            IScheduler scheduler = GameFacade.Instance.RetrieveMediator <SchedulerMediator>().Scheduler;

            mExecutingAnimations = scheduler.StartCoroutine(SequenceAnimations(stateMachine));
        }
        private IEnumerator <IYieldInstruction> SequenceAnimations(AvatarAnimationStateMachine stateMachine)
        {
            YieldWhile yieldWhileStateMachineIsAnimating = new YieldWhile(delegate()
            {
                return(stateMachine.IsAnimating);
            });

            // Iterate through each of the animations in the sequence
            foreach (Listing animation in mAnimations)
            {
                Hangout.Shared.Action queueNextAnimation = delegate()
                {
                    mCurrentPlayingClip = animation.Clip;
                    stateMachine.PlayAnimation(animation.Clip);
                    //Debug.Log(Time.time.ToString("f3") + ": Waiting for animation (" + animation.State.name + ") to complete in " + animation.State.length.ToString("f3") + " seconds");
                };

                // If this is an infinitely looping animation, play it forever
                while (animation.Loops.Infinite)
                {
                    queueNextAnimation();
                    yield return(yieldWhileStateMachineIsAnimating);
                }

                // otherwise, play the animation for the specified number of loops
                uint loopCount = (uint)mRandom.Next((int)animation.Loops.Min, (int)animation.Loops.Max);
                for (uint i = 0; i < loopCount; ++i)
                {
                    queueNextAnimation();
                    yield return(new YieldForSeconds(animation.Clip.length));
                }
            }

            while (!mOneShot)
            {
                stateMachine.PlayAnimation(mCurrentPlayingClip);
                yield return(yieldWhileStateMachineIsAnimating);
            }
            mCurrentPlayingClip = null;
        }