void PlayAnimationSet(MyAnimationSetData animationSetData)
        {
            if (MyRandom.Instance.NextFloat(0, 1) < animationSetData.AnimationSet.Probability)
            {
                float total = animationSetData.AnimationSet.AnimationItems.Sum(x => x.Ratio);
                if (total > 0)
                {
                    float r   = MyRandom.Instance.NextFloat(0, 1);
                    float rel = 0;
                    foreach (var animationItem in animationSetData.AnimationSet.AnimationItems)
                    {
                        rel += animationItem.Ratio / total;

                        if (r < rel)
                        {
                            var command = new MyAnimationCommand()
                            {
                                AnimationSubtypeName = animationItem.Animation,
                                PlaybackCommand      = MyPlaybackCommand.Play,
                                Area      = animationSetData.Area,
                                BlendTime = animationSetData.BlendTime,
                                TimeScale = 1,
                                KeepContinuingAnimations = true
                            };

                            ProcessCommand(ref command);
                            break;
                        }
                    }
                }
            }
        }
        public virtual void AddCommand(MyAnimationCommand command, bool sync = false)
        {
            if (command.PlaybackCommand == MyPlaybackCommand.Play && command.BlendOption == MyBlendOption.Immediate)
            {
                m_commandQueue.Clear();
            }

            m_commandQueue.Enqueue(command);
        }
        protected void ProcessCommands()
        {
            if (m_commandQueue.Count > 0)
            {
                MyAnimationCommand command = m_commandQueue.Peek();

                if (command.PlaybackCommand == MyPlaybackCommand.Play)
                {
                    m_commandQueue.Dequeue();

                    MyAnimationDefinition animDefinition;
                    if (!TryGetAnimationDefinition(command.AnimationSubtypeName, out animDefinition))
                    {
                        return;
                    }


                    string bonesArea             = animDefinition.InfluenceArea;
                    var    frameOption           = command.FrameOption;
                    bool   useFirstPersonVersion = false;

                    OnAnimationPlay(animDefinition, command, ref bonesArea, ref frameOption, ref useFirstPersonVersion);

                    //override bones area if required
                    if (!string.IsNullOrEmpty(command.Area))
                    {
                        bonesArea = command.Area;
                    }

                    if (bonesArea == null)
                    {
                        bonesArea = "";
                    }

                    string[] boneAreas = bonesArea.Split(' ');
                    foreach (var boneArea in boneAreas)
                    {
                        PlayerPlay(boneArea, animDefinition, useFirstPersonVersion, frameOption, command.BlendTime, command.TimeScale);
                    }
                }

                else
                {
                    System.Diagnostics.Debug.Assert(command.PlaybackCommand == MyPlaybackCommand.Stop, "Unknown playback command");

                    m_commandQueue.Dequeue();

                    string   bonesArea = command.Area == null ? "" : command.Area;
                    string[] boneAreas = bonesArea.Split(' ');

                    foreach (var boneArea in boneAreas)
                    {
                        PlayerStop(boneArea, command.BlendTime);
                    }
                }
            }
        }
        protected void ProcessCommands()
        {
            if (m_commandQueue.Count > 0)
            {
                MyAnimationCommand command = m_commandQueue.Dequeue();

                ProcessCommand(ref command);
            }
        }
        void ProcessCommand(ref MyAnimationCommand command)
        {
            if (command.PlaybackCommand == MyPlaybackCommand.Play)
            {
                MyAnimationDefinition animDefinition;
                if (!TryGetAnimationDefinition(command.AnimationSubtypeName, out animDefinition))
                {
                    return;
                }

                string bonesArea   = animDefinition.InfluenceArea;
                var    frameOption = command.FrameOption;

                if (frameOption == MyFrameOption.Default)
                {
                    frameOption = animDefinition.Loop ? MyFrameOption.Loop : MyFrameOption.PlayOnce;
                }

                bool useFirstPersonVersion = false;

                OnAnimationPlay(animDefinition, command, ref bonesArea, ref frameOption, ref useFirstPersonVersion);

                //override bones area if required
                if (!string.IsNullOrEmpty(command.Area))
                {
                    bonesArea = command.Area;
                }

                if (bonesArea == null)
                {
                    bonesArea = "";
                }

                if (!command.KeepContinuingAnimations)
                {
                    m_continuingAnimSets.Clear();
                }

                PlayersPlay(bonesArea, animDefinition, useFirstPersonVersion, frameOption, command.BlendTime, command.TimeScale);
            }

            else
            {
                System.Diagnostics.Debug.Assert(command.PlaybackCommand == MyPlaybackCommand.Stop, "Unknown playback command");

                string   bonesArea = command.Area == null ? "" : command.Area;
                string[] boneAreas = bonesArea.Split(' ');

                foreach (var boneArea in boneAreas)
                {
                    PlayerStop(boneArea, command.BlendTime);
                }
            }
        }
 /// <summary>
 /// Process all commands in the animation queue at once.
 /// If any command is generated during flushing, it will be processed later.
 /// </summary>
 protected bool ProcessCommands()
 {
     if (m_commandQueue.Count > 0)
     {
         MyAnimationCommand command = m_commandQueue.Dequeue();
         ProcessCommand(ref command);
         return(true);
     }
     else
     {
         return(false);
     }
 }
 protected virtual void OnAnimationPlay(MyAnimationDefinition animDefinition, MyAnimationCommand command, ref string bonesArea, ref MyFrameOption frameOption, ref bool useFirstPersonVersion)
 {
 }
 /// <summary>
 /// Virtual method called when animation is started, used in MyCharacter.
 /// </summary>
 protected virtual void OnAnimationPlay(MyAnimationDefinition animDefinition, MyAnimationCommand command, ref string bonesArea, ref MyFrameOption frameOption, ref bool useFirstPersonVersion)
 {
 }
        /// <summary>
        /// Enqueue animation command. Parameter sync is used in child classes.
        /// </summary>
        public virtual void AddCommand(MyAnimationCommand command, bool sync = false)
        {
            //if (command.PlaybackCommand == MyPlaybackCommand.Play && command.BlendOption == MyBlendOption.Immediate)
            //{
            //    m_commandQueue.Clear();
            //}

            m_commandQueue.Enqueue(command);
        }
        /// <summary>
        /// Process single animation command.
        /// </summary>
        void ProcessCommand(ref MyAnimationCommand command)
        {
            if (command.PlaybackCommand == MyPlaybackCommand.Play)
            {
                MyAnimationDefinition animDefinition;
                if (!TryGetAnimationDefinition(command.AnimationSubtypeName, out animDefinition))
                    return;

                string bonesArea = animDefinition.InfluenceArea;
                var frameOption = command.FrameOption;

                if (frameOption == MyFrameOption.Default)
                {
                    frameOption = animDefinition.Loop ? MyFrameOption.Loop : MyFrameOption.PlayOnce;
                }

                bool useFirstPersonVersion = false;

                OnAnimationPlay(animDefinition, command, ref bonesArea, ref frameOption, ref useFirstPersonVersion);

                //override bones area if required
                if (!string.IsNullOrEmpty(command.Area))
                    bonesArea = command.Area;

                if (bonesArea == null)
                    bonesArea = "";

                if (!command.KeepContinuingAnimations)
                    m_continuingAnimSets.Clear();

                if (UseNewAnimationSystem)
                {
                    // these commands are now completely ignored.
                    //var animationLayer = AnimationController.Controller.GetLayerByName(bonesArea);
                    //animationLayer.SetState(command.AnimationSubtypeName);
                }
                else
                {
                    PlayersPlay(bonesArea, animDefinition, useFirstPersonVersion, frameOption, command.BlendTime, command.TimeScale);
                }
            }
            else if (command.PlaybackCommand == MyPlaybackCommand.Stop)
            {
                string bonesArea = command.Area == null ? "" : command.Area;
                string[] boneAreas = bonesArea.Split(' ');

                if (UseNewAnimationSystem)
                {

                }
                else
                {
                    foreach (var boneArea in boneAreas)
                    {
                        PlayerStop(boneArea, command.BlendTime);
                    }
                }
            }
            else
            {
                System.Diagnostics.Debug.Fail("Unknown playback command");
            }
        }
        void PlayAnimationSet(MyAnimationSetData animationSetData)
        {
            if (MyRandom.Instance.NextFloat(0, 1) < animationSetData.AnimationSet.Probability)
            {
                float total = animationSetData.AnimationSet.AnimationItems.Sum(x => x.Ratio);
                if (total > 0)
                {
                    float r = MyRandom.Instance.NextFloat(0, 1);
                    float rel = 0;
                    foreach (var animationItem in animationSetData.AnimationSet.AnimationItems)
                    {
                        rel += animationItem.Ratio / total;

                        if (r < rel)
                        {
                            var command = new MyAnimationCommand()
                            {
                                AnimationSubtypeName = animationItem.Animation,
                                PlaybackCommand = MyPlaybackCommand.Play,
                                Area = animationSetData.Area,
                                BlendTime = animationSetData.BlendTime,
                                TimeScale = 1,
                                KeepContinuingAnimations = true
                            };

                            ProcessCommand(ref command);
                            break;
                        }
                    }
                }
            }
        }