FlatRedBall_Spriter.SpriterObject.TimedActivity C# (CSharp) Method

TimedActivity() public method

public TimedActivity ( float secondDifference, double secondDifferenceSquaredDividedByTwo, float secondsPassedLastFrame ) : void
secondDifference float
secondDifferenceSquaredDividedByTwo double
secondsPassedLastFrame float
return void
        public override void TimedActivity(float secondDifference, double secondDifferenceSquaredDividedByTwo, float secondsPassedLastFrame)
        {
            base.TimedActivity(secondDifference, secondDifferenceSquaredDividedByTwo, secondsPassedLastFrame);

            if (Animating)
            {
                SecondsIn += secondDifference;

                if (NextKeyFrame != null && SecondsIn >= NextKeyFrame.Time)
                {
                    ++CurrentKeyFrameIndex;
                    UpdateCollisionBoxes();
                }

                // Interpolate between the current keyframe and next keyframe values based on time difference
                if (SecondsIn < AnimationTotalTime && NextKeyFrame != null)
                {
                    float percentage = GetPercentageIntoFrame(SecondsIn, CurrentKeyFrame.Time, NextKeyFrame.Time);
                    foreach (var currentPair in CurrentKeyFrame.Values)
                    {
                        SetInterpolatedValues(currentPair, percentage);
                    }

                    if (Math.Abs(NextKeyFrame.Time - CurrentKeyFrame.Time) < .000001f)
                    {
                        foreach (var keyFrameValues in NextKeyFrame.Values)
                        {
                            SetInterpolatedValues(keyFrameValues, percentage);
                        }
                        ++CurrentKeyFrameIndex;
                        UpdateCollisionBoxes();
                    }
                }
                else
                {
                    if (SecondsIn >= AnimationTotalTime)
                    {
                        if (!Looping)
                        {
                            Animating = false;
                            OnAnimationFinished(CurrentAnimation);
                        }
                        else
                        {
                            RestartAnimationWithWrapping();
                        }
                    }
                    else
                    {
                        SetAllObjectValuesToCurrentFrame();
                    }

                }

                UpdateAllObjectDependencies();
            }
        }

Usage Example

        public void Test2Objects()
        {
            var so = new SpriterObject("Global", false);

            var sprite = new ScaledSprite();
            var pivot = new ScaledPositionedObject();
            var sprite2 = new ScaledSprite();
            var pivot2 = new ScaledPositionedObject();

            pivot.AttachTo(so, true);
            sprite.AttachTo(pivot, true);

            pivot2.AttachTo(so, true);
            sprite2.AttachTo(pivot2, true);
            so.Animations.Add("", new SpriterObjectAnimation("", true, 2.0f, new List<KeyFrame>()));

            var keyFrame = new KeyFrame
            {
                Time = 0
            };
            keyFrame.Values[pivot] = new KeyFrameValues
            {
                RelativePosition = Vector3.Zero
            };
            keyFrame.Values[pivot2] = new KeyFrameValues
            {
                RelativePosition = Vector3.Zero
            };

            so.Animations[""].KeyFrames.Add(keyFrame);

            keyFrame = new KeyFrame
            {
                Time = 1.0f
            };
            keyFrame.Values[pivot] = new KeyFrameValues
            {
                RelativePosition = new Vector3(0f, 10f, 0f)
            };
            keyFrame.Values[pivot2] = new KeyFrameValues
            {
                RelativePosition = new Vector3(10f, 0f, 0f)
            };

            so.Animations[""].KeyFrames.Add(keyFrame);

            so.ObjectList.Add(sprite);
            so.ObjectList.Add(pivot);
            so.ObjectList.Add(sprite2);
            so.ObjectList.Add(pivot2);

            so.StartAnimation();
            TimeManager.CurrentTime += .5;
            so.TimedActivity(.5f, 0f, 0f);

            Assert.AreEqual(5f, so.ObjectList[1].Position.Y);
            Assert.AreEqual(5f, so.ObjectList[3].Position.X);

            TimeManager.CurrentTime += .25;
            so.TimedActivity(.25f, 0f, 0f);
            Assert.AreEqual(7.5f, so.ObjectList[1].Position.Y);
            Assert.AreEqual(7.5f, so.ObjectList[3].Position.X);
        }
All Usage Examples Of FlatRedBall_Spriter.SpriterObject::TimedActivity