Example #1
0
        public static void TestCondition()
        {
            CommandQueue queue = new CommandQueue();

            int testCount            = 0;
            int conditionCalledCount = 0;

            queue.Enqueue(
                Cmd.Repeat(4,
                           Cmd.Condition(() => testCount % 2 == 0,
                                         Cmd.Do(() => testCount += 1),
                                         Cmd.Do(() => {
                Assert.IsTrue(testCount % 2 == 1, "Unexpected state in condition branch.");
                conditionCalledCount += 1;
                testCount            += 1;
            })
                                         )
                           ),
                Cmd.Do(() => Assert.AreEqual(conditionCalledCount, 2))
                );

            while (!queue.Update(DELTA_TIME_RATE))
            {
            }
        }
Example #2
0
        public static void TestRecursiveUpdateFail()
        {
            CommandQueue queue = new CommandQueue();

            queue.Enqueue(
                Cmd.Queue(queue)
                );

            bool threwException = false;

            try {
                queue.Update(DELTA_TIME_RATE);
            } catch (InvalidOperationException) {
                threwException = true;
            }

            Assert.IsTrue(threwException, "Failed to throw exception from invalid state.");

            queue = new CommandQueue();
            queue.Enqueue(
                Cmd.Do(() => {
                queue.Update(DELTA_TIME_RATE);
            })
                );

            try {
                queue.Update(DELTA_TIME_RATE);
            } catch (InvalidOperationException) {
                threwException = true;
            }

            Assert.IsTrue(threwException, "Failed to throw exception from invalid state.");
        }
Example #3
0
        public static void TestRepeatForever()
        {
            CommandQueue queue            = new CommandQueue();
            const int    MAX_REPEAT_COUNT = 100;
            int          repeatCount      = 0;

            queue.Enqueue(
                Cmd.RepeatForever(
                    Cmd.Do(() => {
                if (repeatCount < MAX_REPEAT_COUNT)
                {
                    repeatCount++;
                }
                else
                {
                    // Well there is one way to stop execution
                    throw new Exception();
                }
            })
                    )
                );

            bool threwException = false;

            try {
                while (!queue.Update(DELTA_TIME_RATE))
                {
                }
            } catch (Exception) {
                threwException = true;
            }

            Assert.IsTrue(threwException);
            Assert.AreEqual(repeatCount, MAX_REPEAT_COUNT);
        }
Example #4
0
        public static CommandDelegate ChangeTo(Ref <Rect> rect, Rect endRect, double duration, Vector2 anchorPoint,
                                               CommandEase ease = null)
        {
            CheckArgumentNonNull(rect, "rect");

            Rect    startRect        = new Rect();
            Vector2 startAnchorPoint = Vector2.zero;
            Vector2 endAnchorPoint   = Vector2.zero;

            return(Cmd.Sequence(
                       Cmd.Do(delegate()
            {
                startRect = rect.Value;
                startAnchorPoint = new Vector2(
                    startRect.x + startRect.width * anchorPoint.x,
                    startRect.y + startRect.height * anchorPoint.y
                    );
                endAnchorPoint = new Vector2(
                    endRect.x + endRect.width * anchorPoint.x,
                    endRect.y + endRect.height * anchorPoint.y
                    );
            }),
                       Cmd.Duration(delegate(double t)
            {
                Rect newRect = new Rect();
                newRect.width = (endRect.width - startRect.width) * (float)t + startRect.width;
                newRect.height = (endRect.height - startRect.height) * (float)t + startRect.height;
                Vector2 newAnchorPoint = Vector2.Lerp(startAnchorPoint, endAnchorPoint, (float)t);
                newRect.x = newAnchorPoint.x - anchorPoint.x * newRect.width;
                newRect.y = newAnchorPoint.y - anchorPoint.y * newRect.height;
                rect.Value = newRect;
            }, duration, ease)
                       ));
        }
Example #5
0
        /// <summary>
        /// Oscilates a quaternion.
        /// </summary>
        /// <param name="amount"> The amount to wiggle by in degrees.</param>
        /// <param name="axis"> The axis to wiggle around </param>
        public static CommandDelegate Wiggle(Ref <Quaternion> rotation, float amount, double duration, Vector3 axis)
        {
            CheckArgumentNonNull(rotation, "rotation");

            Quaternion startQuaternion = Quaternion.identity;

            float val = 0.0f;

            Ref <float> floatRef = new Ref <float>(
                () => val,
                (t) =>
            {
                val            = t;
                rotation.Value = startQuaternion * Quaternion.AngleAxis(val, axis);
            }
                );

            return(Cmd.Sequence(
                       Cmd.Do(() =>
            {
                startQuaternion = rotation.Value;
                val = 0.0f;
            }),

                       Cmd.Oscillate(
                           floatRef, amount, duration, Ease.Smooth()
                           )
                       ));
        }
Example #6
0
        public static CommandDelegate TintTo(Ref <Color> color, Color endColor, double duration, CommandEase ease = null)
        {
            CheckArgumentNonNull(color, "color");

            Color startColor = Color.white;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { startColor = color.Value; }),
                       Cmd.Duration(delegate(double t) { color.Value = Color.Lerp(startColor, endColor, (float)t); },
                                    duration, ease)
                       ));
        }
Example #7
0
        public static CommandDelegate ChangeTo(Ref <Vector2> vector, Vector2 endVector, double duration,
                                               CommandEase ease = null)
        {
            CheckArgumentNonNull(vector, "vector");
            Vector2 startVector = Vector2.zero;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { startVector = vector.Value; }),
                       Cmd.Duration(delegate(double t) { vector.Value = (endVector - startVector) * (float)t + startVector; },
                                    duration, ease)
                       ));
        }
Example #8
0
        /// <summary>
        /// Oscillates around a value. This will animation from
        ///  startValue > startValue + amount > startValue - amount- > startValue,
        /// in a smooth circular motion.
        /// </summary>
        /// <param name="amount">
        /// The maximum amount to oscillate away from the default value.
        /// </param>
        public static CommandDelegate Oscillate(Ref <float> single, float amount, double duration,
                                                CommandEase ease = null)
        {
            CheckArgumentNonNull(single, "single");
            float baseValue = 0f;

            return(Cmd.Sequence(
                       Cmd.Do(() => baseValue = single.Value),
                       Cmd.Duration(t => { single.Value = baseValue + Mathf.Sin((float)t * 2f * Mathf.PI) * amount; },
                                    duration, ease)
                       ));
        }
Example #9
0
        public static void TestTiming()
        {
            CommandQueue queue = new CommandQueue();

            const double FIRST_Command_DURATION  = 4.5;
            const double SECOND_Command_DURATION = 1.0;
            const double WAIT_DURATION           = 1.5;
            const int    REPEAT_COUNT            = 8640;
            double       lastT = 0.0;

            // This test ensures that between alternating CommandDurations,
            // there is no accumulation of error in timing. We use a repeat
            // here to accentuate the error.
            queue.Enqueue(
                Cmd.Repeat(REPEAT_COUNT,
                           Cmd.Sequence(
                               Cmd.WaitForSeconds(WAIT_DURATION),
                               Cmd.Do(() => lastT = 0.0),
                               Cmd.Duration((t) => {
                Assert.IsTrue(t <= 1.0);
                Assert.IsTrue(lastT <= t);
                lastT = t;
            }, FIRST_Command_DURATION),
                               Cmd.Do(() => lastT = 0.0),
                               Cmd.Parallel(
                                   Cmd.Duration((t) => {}, SECOND_Command_DURATION / 2.0),
                                   // The following two  Duration Cmd should finish in the same Update call.
                                   Cmd.Duration((t) => {}, SECOND_Command_DURATION - (DELTA_TIME_RATE / 2.0)),
                                   Cmd.Duration((t) => {
                Assert.IsTrue(t <= 1.0);
                Assert.IsTrue(lastT <= t);
                lastT = t;
            }, SECOND_Command_DURATION)
                                   )
                               )
                           )
                );

            double totalTime = 0.0;

            while (!queue.Update(DELTA_TIME_RATE))
            {
                totalTime += DELTA_TIME_RATE;
            }

            const double EXPECTED_TIME = (FIRST_Command_DURATION + SECOND_Command_DURATION + WAIT_DURATION) * REPEAT_COUNT;

            Assert.AreEqual(totalTime, EXPECTED_TIME, DELTA_TIME_RATE, "Time delta accumulation too large.");
        }
Example #10
0
        public static CommandDelegate TintBy(Ref <Color> color, Color offset, double duration, CommandEase ease = null)
        {
            CheckArgumentNonNull(color, "color");

            double lastT = 0.0;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { lastT = 0.0; }),
                       Cmd.Duration(delegate(double t)
            {
                color.Value += offset * (float)(t - lastT);
                lastT = t;
            }, duration, ease)
                       ));
        }
Example #11
0
        /// <summary>
        /// Performs a squash and stretch animation, while changing from a target scale.
        /// </summary>
        /// <param name="scale">The value to animate.</param>
        /// <param name="startScale">The scale to animate from.</param>
        /// <param name="amplitude">The amplitude of a squash and strech</param>
        /// <param name="duration">The duration of the animation</param>
        /// <param name="normal"> The normal of the animation. </param>
        /// <param name="tangent"> The tangent of the animation. </param>
        public static CommandDelegate ScaleSquashAndStretchFrom(Ref <Vector3> scale, Vector3 startScale, float amplitude,
                                                                double duration, Vector3 normal, Vector3 tangent)
        {
            CheckArgumentNonNull(scale, "scale");
            Vector3 targetScale = Vector3.zero;

            return(Cmd.Sequence(
                       Cmd.Do(() =>
            {
                targetScale = scale.Value;
                scale.Value = startScale;
            }),
                       Cmd.Defer(() => Cmd.ScaleSquashAndStretchTo(scale, targetScale, amplitude, duration, normal, tangent))
                       ));
        }
Example #12
0
        public static CommandDelegate RotateFrom(Ref <Quaternion> rotation, Quaternion startRotation, double duration,
                                                 CommandEase ease = null)
        {
            CheckArgumentNonNull(rotation, "rotation");

            Quaternion endRotation = Quaternion.identity;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { endRotation = rotation.Value; }),
                       Cmd.Duration(
                           delegate(double t)
            {
                rotation.Value = Quaternion.LerpUnclamped(startRotation, endRotation, (float)t);
            }, duration, ease)
                       ));
        }
Example #13
0
        public static CommandDelegate ChangeBy(Ref <Vector2> vector, Vector2 offset, double duration,
                                               CommandEase ease = null)
        {
            CheckArgumentNonNull(vector, "vector");

            double lastT = 0.0;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { lastT = 0.0; }),
                       Cmd.Duration(delegate(double t)
            {
                vector.Value += offset * (float)(t - lastT);
                lastT = t;
            }, duration, ease)
                       ));
        }
Example #14
0
        /// <summary>
        /// Pulsates a value.
        /// </summary>
        /// <param name="amount">The amount to increase the value by.</param>
        public static CommandDelegate PulsateScale(Ref <float> scale, float amount, double duration)
        {
            CheckArgumentNonNull(scale, "scale");
            CommandDelegate tweenBack = null;

            return(Cmd.Sequence(
                       Cmd.Do(() =>
            {
                // Because we don't know what the original scale is at this point,
                // we have to recreate the scale back tween every time.
                tweenBack = Cmd.ChangeTo(scale, scale.Value, duration / 2.0, Ease.Smooth());
            }),
                       Cmd.ChangeBy(scale, amount, duration / 2.0, Ease.Smooth()),
                       Cmd.Defer(() => tweenBack)
                       ));
        }
Example #15
0
        public static void TestRequire()
        {
            CommandQueue queue = new CommandQueue();

            bool shouldStop = false;
            bool didFinish  = false;
            int  callCount  = 0;

            queue.Enqueue(
                Cmd.RepeatForever(
                    Cmd.Require(() => !shouldStop,
                                () => Cmd.RepeatForever(
                                    Cmd.Sequence(
                                        Cmd.Do(() => callCount++),
                                        Cmd.WaitForFrames(1)
                                        )
                                    )
                                ),
                    Cmd.Do(() => didFinish = true),
                    Cmd.WaitForFrames(1)
                    )
                );

            Assert.AreEqual(callCount, 0);
            queue.Update(1.0f);
            Assert.AreEqual(callCount, 1);
            queue.Update(1.0f);
            Assert.AreEqual(callCount, 2);
            queue.Update(1.0f);
            Assert.AreEqual(callCount, 3);

            // Require should only re-evaluate on next update.
            shouldStop = true;
            Assert.AreEqual(didFinish, false);
            queue.Update(1.0f);
            Assert.AreEqual(callCount, 3);
            Assert.AreEqual(didFinish, true);

            queue.Update(1.0f);
            Assert.AreEqual(callCount, 3);
            Assert.AreEqual(didFinish, true);
            shouldStop = false;

            queue.Update(1.0f);
            Assert.AreEqual(callCount, 4);
            Assert.AreEqual(didFinish, true);
        }
Example #16
0
        /// <summary>
        /// Performs a squash and stretch animation, while changing to a target scale.
        /// </summary>
        /// <param name="scale">The value to animate.</param>
        /// <param name="endScale">The final scale.</param>
        /// <param name="amplitude">The amplitude of a squash and strech</param>
        /// <param name="duration">The duration of the animation</param>
        /// <param name="normal"> The normal of the animation. </param>
        /// <param name="tangent"> The tangent of the animation. </param>
        public static CommandDelegate ScaleSquashAndStretchTo(Ref <Vector3> scale, Vector3 endScale, float amplitude,
                                                              double duration, Vector3 normal, Vector3 tangent)
        {
            CheckArgumentNonNull(scale, "scale");
            var squashRef = Ref <Vector3> .Create(Vector3.one);

            var scaleRef = Ref <Vector3> .Create();

            return(Cmd.Sequence(
                       Cmd.Do(() => scaleRef.Value = scale.Value),
                       Cmd.Parallel(
                           SquashAndStretch(squashRef, amplitude, duration, normal, tangent),
                           Cmd.ChangeTo(scaleRef, endScale, duration / 4, Ease.Smooth()),
                           Cmd.Duration(t => scale.Value = Vector3.Scale(squashRef.Value, scaleRef.Value), duration)
                           )
                       ));
        }
Example #17
0
        public static void TestWhile()
        {
            CommandQueue queue = new CommandQueue();

            int i = 0;
            int c = 0;

            queue.Enqueue(
                Cmd.Repeat(5,
                           Cmd.Do(() => ++ i),
                           Cmd.While(() => i % 5 != 0,
                                     Cmd.Do(() => ++ i),
                                     Cmd.WaitForFrames(1),
                                     Cmd.Do(() => ++ c)
                                     ),
                           Cmd.WaitForFrames(1)
                           )
                );

            System.Action Update5 = () => {
                for (int j = 0; j < 5; ++j)
                {
                    queue.Update(0f);
                }
            };

            Update5();
            Assert.AreEqual(i, 5);
            Assert.AreEqual(c, 4);

            Update5();
            Assert.AreEqual(i, 10);
            Assert.AreEqual(c, 8);

            Update5();
            Assert.AreEqual(i, 15);
            Assert.AreEqual(c, 12);

            Update5();
            Assert.AreEqual(i, 20);
            Assert.AreEqual(c, 16);

            Update5();
            Assert.AreEqual(i, 25);
            Assert.AreEqual(c, 20);
        }
Example #18
0
        public static CommandDelegate RotateBy(Ref <Quaternion> rotation, Quaternion offsetRotation, double duration,
                                               CommandEase ease = null)
        {
            CheckArgumentNonNull(rotation, "rotation");

            double lastT = 0.0;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { lastT = 0.0; }),
                       Cmd.Duration(delegate(double t)
            {
                rotation.Value *= Quaternion.LerpUnclamped(Quaternion.identity, offsetRotation, (float)t) *
                                  Quaternion.Inverse(Quaternion.LerpUnclamped(Quaternion.identity, offsetRotation,
                                                                              (float)lastT));
                lastT = t;
            }, duration, ease)
                       ));
        }
Example #19
0
        public static void TestChooseRandom()
        {
            const int    NUM_SELECTIONS = 1000;
            CommandQueue queue          = new CommandQueue();

            int[] selections = new int[NUM_SELECTIONS];
            int   i          = 0;

            queue.Enqueue(
                Cmd.Repeat(NUM_SELECTIONS,
                           Cmd.Do(() => selections[i] = 0),
                           Cmd.ChooseRandom(
                               Cmd.Do(() => selections[i] = 1),
                               Cmd.Do(() => selections[i] = 2),
                               Cmd.Do(() => selections[i] = 3),
                               Cmd.Do(() => selections[i] = 4),
                               Cmd.Do(() => selections[i] = 5),
                               Cmd.Do(() => selections[i] = 6),
                               Cmd.Do(() => selections[i] = 7),
                               Cmd.Do(() => selections[i] = 8),
                               Cmd.Do(() => selections[i] = 9),
                               null
                               ),
                           Cmd.Do(() => i++)
                           )
                );

            queue.Update(1.0f);

            // The chance of every number being the same is 1 / 10 ^ (NUM_SELECTIONS - 1).
            bool allEqual = true;

            for (int c = 0; c < NUM_SELECTIONS - 1; c++)
            {
                if (selections[c] != selections[c + 1])
                {
                    allEqual = false;
                    break;
                }
            }

            Assert.IsTrue(!allEqual, "All numbers were equal, this is either extremely unlikely, or a bug.");
        }
Example #20
0
        public static CommandDelegate ScaleBy(Ref <Vector2> scale, Vector2 scaleFactor, double duration,
                                              CommandEase ease = null)
        {
            CheckArgumentNonNull(scale, "scale");

            Vector2 lastScaleFactor = Vector2.one;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { lastScaleFactor = Vector2.one; }),
                       Cmd.Duration(delegate(double t)
            {
                Vector2 newScaleFactor = (float)t * (scaleFactor - Vector2.one) + Vector2.one;
                scale.Value = new Vector2(
                    scale.Value.x * newScaleFactor.x / lastScaleFactor.x,
                    scale.Value.y * newScaleFactor.y / lastScaleFactor.y
                    );
                lastScaleFactor = newScaleFactor;
            }, duration, ease)
                       ));
        }
Example #21
0
        public static void TestRunToEnd()
        {
            CommandQueue queue = new CommandQueue();

            double time     = 0.0;
            bool   started  = false;
            bool   finished = false;

            queue.Enqueue(
                Cmd.Do(() => started   = true),
                Cmd.Duration(t => time = t, 1.0),
                Cmd.Do(() => finished  = true)
                );

            Assert.AreEqual(false, started);
            queue.RunToEnd();
            Assert.AreEqual(true, started);
            Assert.AreEqual(1.0, time);
            Assert.AreEqual(true, finished);
        }
Example #22
0
        public static void TestPausing()
        {
            CommandQueue queue = new CommandQueue();

            bool shouldBePaused      = true;
            bool secondCommandCalled = false;

            queue.Enqueue(
                Cmd.Do(() => queue.Paused = true),
                Cmd.Do(() => {
                Assert.AreEqual(shouldBePaused, false, "Executed Command while CommandQueue paused.");
                secondCommandCalled = true;
            })
                );

            queue.Update(10.0);
            queue.Paused   = false;
            shouldBePaused = false;
            queue.Update(10.0);
            Assert.AreEqual(secondCommandCalled, true, "Second Command never called.");
        }
Example #23
0
        public static CommandDelegate TintBy(Ref <Color32> color, Color32 offset, double duration,
                                             CommandEase ease = null)
        {
            CheckArgumentNonNull(color, "color");

            double lastT = 0.0;

            return(Cmd.Sequence(
                       Cmd.Do(delegate() { lastT = 0.0; }),
                       Cmd.Duration(delegate(double t)
            {
                color.Value = new Color32(
                    (byte)(color.Value.r + offset.r * (t - lastT)),
                    (byte)(color.Value.g + offset.g * (t - lastT)),
                    (byte)(color.Value.b + offset.b * (t - lastT)),
                    (byte)(color.Value.a + offset.a * (t - lastT))
                    );
                lastT = t;
            }, duration, ease)
                       ));
        }
Example #24
0
        /// <summary>
        /// Wobble a value. This oscilates a value, with a decay.
        /// </summary>
        /// <param name="amplitude">Amplitude.</param>
        /// <param name="duration">Duration.</param>
        public static CommandDelegate Wobble(Ref <float> single, float amount, double duration)
        {
            CheckArgumentNonNull(single, "val");

            float intervals = 3f;
            float decay     = Mathf.Log(100f * amount);

            float baseVal = 0f;

            return(Cmd.Sequence(
                       Cmd.Do(() => { baseVal = single.Value; }),
                       Cmd.Duration(
                           t =>
            {
                float decayCoeef = Mathf.Exp((float)t * decay);
                single.Value = baseVal + amount * Mathf.Sin(intervals * (float)t * 2 * Mathf.PI) / decayCoeef;
            },
                           duration
                           ),
                       Cmd.Do(() => { single.Value = baseVal; })
                       ));
        }
Example #25
0
        /// <summary>
        /// Squashes the y axis, while inversely stretching the x axis.
        /// </summary>
        /// <param name="val">The value to animate.</param>
        /// <param name="amplitude">The size of the squash.</param>
        /// <param name="duration">The duration of the squash.</param>
        public static CommandDelegate SquashAndStretch(Ref <Vector2> scale, float amplitude, double duration)
        {
            CheckArgumentNonNull(scale, "scale");

            Vector2     startScale = Vector2.zero;
            float       area       = 0f;
            Ref <float> widthRef   = new Ref <float>(
                () => scale.Value.x,
                (t) =>
            {
                Vector2 tempVal = scale.Value;
                tempVal.x       = t;
                scale.Value     = tempVal;
            }
                );

            return(Cmd.Sequence(
                       Cmd.Do(() =>
            {
                area = scale.Value.x * scale.Value.y;
                startScale = scale.Value;
            }),
                       Cmd.Parallel(
                           Wobble(widthRef, amplitude, duration),
                           Cmd.Duration((t) =>
            {
                Vector2 tempVal = scale.Value;
                if (tempVal.x != 0f)
                {
                    tempVal.y = area / tempVal.x;
                }

                scale.Value = tempVal;
            }, duration)
                           ),
                       Cmd.Do(() => { scale.Value = startScale; })
                       ));
        }
Example #26
0
        public static void TestWaitFrames()
        {
            int             count = 0;
            CommandDelegate incr  = Cmd.Do(() => ++ count);
            CommandQueue    queue = new CommandQueue();

            queue.Enqueue(
                Cmd.WaitForFrames(1),
                incr,
                Cmd.WaitForFrames(2),
                incr,
                Cmd.Repeat(3,
                           Cmd.Sequence(
                               Cmd.WaitForFrames(2),
                               incr
                               )
                           )
                );

            queue.Update(0.1);
            Assert.AreEqual(count, 0);
            queue.Update(0.1);
            Assert.AreEqual(count, 1);
            queue.Update(0.1);
            Assert.AreEqual(count, 1);
            queue.Update(0.1);
            Assert.AreEqual(count, 2);
            queue.Update(0.1);
            Assert.AreEqual(count, 2);
            queue.Update(0.1);
            Assert.AreEqual(count, 3);
            queue.Update(0.1);
            Assert.AreEqual(count, 3);
            queue.Update(0.1);
            Assert.AreEqual(count, 4);
            queue.Update(0.1);
            Assert.AreEqual(count, 4);
        }
Example #27
0
        public static void TestOrdering()
        {
            CommandQueue queue = new CommandQueue();

            string lastCalled = "";

            queue.Enqueue(
                Cmd.Do(() => {
                Assert.AreEqual(lastCalled, "");
                lastCalled = "a";
            }),
                Cmd.Do(() => {
                Assert.AreEqual(lastCalled, "a");
                lastCalled = "b";
            }),
                Cmd.Do(() => {
                Assert.AreEqual(lastCalled, "b");
                lastCalled = "c";
                // Any Command pushed to the queue now, should
                // execute after d.
                queue.Enqueue(Cmd.Do(() => {
                    Assert.AreEqual(lastCalled, "d");
                    lastCalled = "e";
                }));
            }),
                Cmd.Do(() => {
                Assert.AreEqual(lastCalled, "c");
                lastCalled = "d";
            })
                );

            // Fake time updating.
            while (!queue.Update(DELTA_TIME_RATE))
            {
            }

            Assert.AreEqual(lastCalled, "e");
        }
        public static void TestOrdering()
        {
            CommandScheduler scheduler = new CommandScheduler();

            int a = 0;

            scheduler.Add(
                Cmd.Sequence(
                    Cmd.WaitForSeconds(1.0f),
                    Cmd.Do(() => ++ a)
                    )
                );

            int b = 0;

            scheduler.Add(
                Cmd.Sequence(
                    Cmd.WaitForSeconds(1.0f),
                    Cmd.Do(() => ++ b)
                    )
                );

            scheduler.Add(
                Cmd.Sequence(
                    Cmd.WaitForSeconds(1.5f),
                    Cmd.Do(() => ++ b)
                    )
                );
            Assert.AreEqual(a, 0);
            Assert.AreEqual(b, 0);
            scheduler.Update(1.0f);
            Assert.AreEqual(a, 1);
            Assert.AreEqual(b, 1);
            scheduler.Update(0.5f);
            Assert.AreEqual(a, 1);
            Assert.AreEqual(b, 2);
        }
Example #29
0
        public static void TestQueue()
        {
            CommandQueue mainQueue   = new CommandQueue();
            CommandQueue secondQueue = new CommandQueue();

            bool firstCommandTriggered  = false;
            bool secondCommandTriggered = false;
            bool thirdCommandTriggered  = false;

            secondQueue.Enqueue(
                Cmd.Do(() => {
                Assert.IsTrue(!firstCommandTriggered);
                firstCommandTriggered = true;
            }),
                Cmd.Do(() => {
                Assert.IsTrue(firstCommandTriggered);
                Assert.IsTrue(!secondCommandTriggered);
                secondCommandTriggered = true;
            }),
                Cmd.Do(() => {
                Assert.IsTrue(firstCommandTriggered);
                Assert.IsTrue(secondCommandTriggered);
                Assert.IsTrue(!thirdCommandTriggered);
                thirdCommandTriggered = true;
            })
                );

            mainQueue.Enqueue(
                Cmd.Repeat(2,
                           Cmd.Queue(secondQueue)
                           )
                );

            while (!mainQueue.Update(DELTA_TIME_RATE))
            {
            }
        }
Example #30
0
        public static void TestChangeTo()
        {
            const float floatEnd   = 4.8f;
            const float floatStart = 1.2f;
            float       floatVal   = floatStart;
            Ref <float> floatRef   = new Ref <float>(
                () => floatVal,
                t => floatVal = t
                );

            const double doubleEnd   = 3.2;
            const double doubleStart = 9.2;
            double       doubleVal   = doubleStart;
            Ref <double> doubleRef   = new Ref <double>(
                () => doubleVal,
                t => doubleVal = t
                );

            Vector2       vec2End   = new Vector2(9.5f, 2.0f);
            Vector2       vec2Start = new Vector2(4.0f, 5.0f);
            Vector2       vec2Val   = vec2Start;
            Ref <Vector2> vec2Ref   = new Ref <Vector2>(
                () => vec2Val,
                t => vec2Val = t
                );

            Vector3       vec3End   = new Vector3(4.0f, 19.0f, 2.0f);
            Vector3       vec3Start = new Vector3(92.0f, 0.5f, 34.0f);
            Vector3       vec3Val   = vec3Start;
            Ref <Vector3> vec3Ref   = new Ref <Vector3>(
                () => vec3Val,
                t => vec3Val = t
                );

            Vector4       vec4End   = new Vector4(92.0f, 0.5f, 14.0f, 7.0f);
            Vector4       vec4Start = new Vector4(0.4f, 10.0f, 3.0f, 82.0f);
            Vector4       vec4Val   = vec4Start;
            Ref <Vector4> vec4Ref   = new Ref <Vector4>(
                () => vec4Val,
                t => vec4Val = t
                );


            CommandQueue queue = new CommandQueue();

            queue.Enqueue(
                Cmd.Repeat(2,
                           Cmd.Parallel(
                               Cmd.ChangeTo(floatRef, floatEnd, 1.0),
                               Cmd.ChangeTo(doubleRef, doubleEnd, 1.0),
                               Cmd.ChangeTo(vec2Ref, vec2End, 1.0),
                               Cmd.ChangeTo(vec3Ref, vec3End, 1.0),
                               Cmd.ChangeTo(vec4Ref, vec4End, 1.0)
                               )
                           )
                );

            queue.Update(0.3);
            // Check basic lerping works.
            Assert.AreEqual(floatVal, floatEnd * 0.3f + floatStart * 0.7f, 0.01);
            Assert.AreEqual(doubleVal, doubleEnd * 0.3 + doubleStart * 0.7, 0.01);
            AreEqual(vec2Val, vec2End * 0.3f + vec2Start * 0.7f, 0.01f);
            AreEqual(vec3Val, vec3End * 0.3f + vec3Start * 0.7f, 0.01f);
            AreEqual(vec4Val, vec4End * 0.3f + vec4Start * 0.7f, 0.01f);
            // Reset the vals to zero. Checks that 'ChangeTo' will force itself back on
            // track.
            floatVal  = 0.0f;
            doubleVal = 0.0;
            vec2Val   = Vector2.zero;
            vec3Val   = Vector3.zero;
            vec4Val   = Vector4.zero;
            queue.Update(0.2);
            // Completes the offset
            Assert.AreEqual(floatVal, floatEnd * 0.5f + floatStart * 0.5f, 0.01);
            Assert.AreEqual(doubleVal, doubleEnd * 0.5 + doubleStart * 0.5, 0.01);
            AreEqual(vec2Val, vec2End * 0.5f + vec2Start * 0.5f, 0.01f);
            AreEqual(vec3Val, vec3End * 0.5f + vec3Start * 0.5f, 0.01f);
            AreEqual(vec4Val, vec4End * 0.5f + vec4Start * 0.5f, 0.01f);
            queue.Update(0.5);

            Assert.AreEqual(floatVal, floatEnd, 0.01f);
            Assert.AreEqual(doubleVal, doubleEnd, 0.01);
            AreEqual(vec2Val, vec2End, 0.01f);
            AreEqual(vec3Val, vec3End, 0.01f);
            AreEqual(vec4Val, vec4End, 0.01f);
            queue.Update(0.5);
            // Check that it doesn't move once it has reached it's final position.
            Assert.AreEqual(floatVal, floatEnd, 0.01f);
            Assert.AreEqual(doubleVal, doubleEnd, 0.01);
            AreEqual(vec2Val, vec2End, 0.01f);
            AreEqual(vec3Val, vec3End, 0.01f);
            AreEqual(vec4Val, vec4End, 0.01f);

            Rect       rectEnd   = new Rect(-1.0f, 1.0f, 5.0f, 5.0f);
            Rect       rectStart = new Rect(0.0f, 2.0f, 6.0f, 6.0f);
            Rect       rectVal   = new Rect();
            Ref <Rect> rectRef   = new Ref <Rect>(
                () => { return(rectVal); },
                t => { rectVal = t; }
                );

            Vector2 firstAnchor  = new Vector2(0.0f, 0.0f);
            Vector2 secondAnchor = new Vector2(1.0f, 0.0f);
            Vector3 thirdAnchor  = new Vector2(0.0f, 1.0f);
            Vector2 forthAnchor  = new Vector2(1.0f, 1.0f);

            CommandDelegate reset = Cmd.Do(() => { rectVal = rectStart; });

            queue = new CommandQueue();
            queue.Enqueue(
                reset,
                Cmd.ChangeTo(rectRef, rectEnd, 1.0, firstAnchor),
                Cmd.WaitForFrames(1),
                reset,
                Cmd.ChangeTo(rectRef, rectEnd, 1.0, secondAnchor),
                Cmd.WaitForFrames(1),
                reset,
                Cmd.ChangeTo(rectRef, rectEnd, 1.0, thirdAnchor),
                Cmd.WaitForFrames(1),
                reset,
                Cmd.ChangeTo(rectRef, rectEnd, 1.0, forthAnchor)
                );

            // Test the top left corner.
            queue.Update(0.5);
            AreEqual(rectVal, new Rect(
                         -0.5f, 1.5f,
                         (rectStart.width + rectEnd.width) * 0.5f,
                         (rectStart.height + rectEnd.height) * 0.5f), 0.001f
                     );
            queue.Update(0.5);
            AreEqual(rectVal, rectEnd, 0.001f);
            queue.Update(0.0f);

            // Test the top right corner.
            queue.Update(0.3);
            AreEqual(rectVal, new Rect(
                         5.4f - 5.7f, 1.7f,
                         rectStart.width * 0.7f + rectEnd.width * 0.3f,
                         rectStart.height * 0.7f + rectEnd.height * 0.3f), 0.001f
                     );
            queue.Update(0.7);
            AreEqual(rectVal, rectEnd, 0.001f);
            queue.Update(0.0f);

            // Test the bottom left corner.
            queue.Update(0.4);
            AreEqual(rectVal, new Rect(
                         -0.4f, 7.2f - 5.6f,
                         rectStart.width * 0.6f + rectEnd.width * 0.4f,
                         rectStart.height * 0.6f + rectEnd.height * 0.4f), 0.001f
                     );
            queue.Update(0.6);
            AreEqual(rectVal, rectEnd, 0.001f);
            queue.Update(0.0f);

            // Test the bottom right corner.
            queue.Update(0.4);
            AreEqual(rectVal, new Rect(
                         5.2f - 5.6f, 7.2f - 5.6f,
                         rectStart.width * 0.6f + rectEnd.width * 0.4f,
                         rectStart.height * 0.6f + rectEnd.height * 0.4f), 0.001f
                     );
            queue.Update(0.6);
            AreEqual(rectVal, rectEnd, 0.001f);
            queue.Update(0.0f);
        }