예제 #1
0
        public bool Update()
        {
            string animName  = "";
            float  totalTime = 0;

            if (Owner.Properties.IsObject)
            {
                //Agk.SetObjectAnimationSpeed((uint)Owner.Properties.ResourceNumber, )
                animName  = Agk.GetObjectAnimationName((uint)Owner.Properties.ResourceNumber, 1);
                totalTime = Agk.GetObjectAnimationDuration((uint)Owner.Properties.ResourceNumber, animName);
            }

            int firstFrame = Animation.Keys[Animation.CurrentKey][0];
            int lastFrame  = Animation.Keys[Animation.CurrentKey][1];
            int totalFrame = (int)Math.Floor(totalTime * Animation.Framerate);

            CurrentFrame = CurrentFrame + (Animation.Framerate * Speed * (App.Timing.Delta * 0.001f)); //convert to seconds

            if (CurrentFrame < firstFrame)
            {
                CurrentFrame = firstFrame;
            }

            if (CurrentFrame >= lastFrame)
            {
                if (IsLoop)
                {
                    //handle frame, looping
                    CurrentFrame = firstFrame;

                    if (Owner.Properties.IsObject)
                    {
                        //float animTime = (CurrentFrame * totalTime) / lastFrame;
                        float animTime = (CurrentFrame * totalTime) / totalFrame;
                        Agk.SetObjectAnimationFrame((uint)Owner.Properties.ResourceNumber, animName, animTime, 0.0f);
                    }
                    else
                    {
                        Agk.SetSpriteFrame((uint)Owner.Properties.ResourceNumber, (int)CurrentFrame);
                    }

                    //handle variant
                    if (Animation.Keys.Count > 1)
                    {
                        if (App.Timing.Timer - _VariantMark >= _VariantDelay)
                        {
                            Animation.CurrentKey = (int)Agk.Random(0, (uint)(Animation.Keys.Count - 1));
                            _VariantDelay        = Animation.VariantDelayMin + (int)Agk.Random(0, (uint)Animation.VariantDelayMax);
                            _VariantMark         = (int)App.Timing.Timer;
                        }
                    }

                    //handle callback
                    bool isLoopDone = false;
                    if (!String.IsNullOrEmpty(Callback))
                    {
                        isLoopDone = (bool)Dispatcher.Invoke(Callback, CallbackArgs);
                        if (isLoopDone)
                        {
                            return(true);
                        }
                    }
                    return(false);
                }
                else
                {
                    //handle frame, played once, end it
                    CurrentFrame = lastFrame;

                    if (Owner.Properties.IsObject)
                    {
                        //float animTime = (CurrentFrame * Agk.GetObjectAnimationDuration((uint)Owner.Properties.ResourceNumber, animName)) / Animation.Keys[Animation.CurrentKey][1];
                        float animTime = (CurrentFrame * totalTime) / totalFrame;
                        Agk.SetObjectAnimationFrame((uint)Owner.Properties.ResourceNumber, animName, animTime, 0.0f);
                    }
                    else
                    {
                        Agk.SetSpriteFrame((uint)Owner.Properties.ResourceNumber, (int)CurrentFrame);
                    }

                    //handle callback
                    if (!String.IsNullOrEmpty(Callback))
                    {
                        Dispatcher.Invoke(Callback, CallbackArgs);
                    }
                    return(true);
                }
            }
            else
            {
                //handle frame, sequence hasnt completed, continue
                if (Owner.Properties.IsObject)
                {
                    //float animTime = (CurrentFrame * Agk.GetObjectAnimationDuration((uint)Owner.Properties.ResourceNumber, animName)) / Animation.Keys[Animation.CurrentKey][1];
                    float animTime = (CurrentFrame * totalTime) / totalFrame;
                    Agk.SetObjectAnimationFrame((uint)Owner.Properties.ResourceNumber, animName, animTime, 0.0f);
                }
                else
                {
                    Agk.SetSpriteFrame((uint)Owner.Properties.ResourceNumber, (int)CurrentFrame);
                }

                //handle callback
                bool isLoopDone = false;
                if (!String.IsNullOrEmpty(Callback))
                {
                    isLoopDone = (bool)Dispatcher.Invoke(Callback, CallbackArgs);
                    if (isLoopDone)
                    {
                        return(true);
                    }
                }
                return(false);
            }
        }