public int PlayAnimation(string pAnimationName, FramedSprite pSprite)
        {
            if (animations.ContainsKey(pAnimationName))
            {
                AnimationJob job = new AnimationJob {
                    Sprite          = pSprite,
                    Tokens          = animations[pAnimationName],
                    State           = AnimationState.Running,
                    CurrentStep     = 0,
                    ElapsedMsInStep = 0
                };

                animationId++;

                if (animationId == int.MaxValue)
                {
                    animationId = 0;
                }

                jobs.Add(animationId, job);
                return(animationId);
            }

            throw new Exception("Animation '" + pAnimationName + "' does not exist.");
        }
        public void Update(GameTime pGameTime)
        {
            List <int> animationsToRemove = new List <int>();

            foreach (KeyValuePair <int, AnimationJob> job in jobs)
            {
                AnimationJob animation = job.Value;

                if (animation.State == AnimationState.Running)
                {
                    int elapsedTimeMs = (int)pGameTime.ElapsedGameTime.TotalMilliseconds;
                    int currentStep   = animation.CurrentStep;
                    int elapsedInStep = animation.ElapsedMsInStep;

                    do
                    {
                        var token = animation.Tokens[currentStep];
                        if (token.Type == AnimationTokenType.SetFrame)
                        {
                            animation.Sprite.SetCurrentFrame(token.Value);
                            elapsedTimeMs--;
                            currentStep++;
                        }
                        else if (token.Type == AnimationTokenType.Wait)
                        {
                            if (token.Value > elapsedInStep + elapsedTimeMs)
                            {
                                elapsedInStep += elapsedTimeMs;
                                elapsedTimeMs  = 0;
                            }
                            else if (token.Value <= elapsedInStep + elapsedTimeMs)
                            {
                                currentStep++;
                                elapsedTimeMs -= token.Value - elapsedInStep;
                                elapsedInStep  = 0;
                            }
                        }

                        if (currentStep >= animation.Tokens.Length)
                        {
                            currentStep = 0;
                        }
                    } while (elapsedTimeMs > 0);

                    animation.CurrentStep     = currentStep;
                    animation.ElapsedMsInStep = elapsedInStep;
                }

                if (animation.State == AnimationState.Stopped)
                {
                    animationsToRemove.Add(job.Key);
                }
            }

            foreach (int id in animationsToRemove)
            {
                jobs.Remove(id);
            }
        }