Animatroller.SceneRunner.XmasScene2.Start C# (CSharp) Метод

Start() публичный Метод

public Start ( ) : void
Результат void
        public override void Start()
        {
            pulsatingEffect1.AddDevice(lightHatsRight);
            pulsatingEffect2
                .AddDevice(lightCeiling1)
                .AddDevice(lightCeiling2)
                .AddDevice(lightCeiling3);

            candyCane
                .WhenExecuted
                .SetUp(() => allPixels.TurnOff())
                .Execute(instance =>
                {
                    const int spacing = 4;

                    while (true)
                    {
                        for (int i = 0; i < spacing; i++)
                        {
                            allPixels.Inject((i % spacing) == 0 ? Color.Red : Color.White, 1.0);

                            instance.WaitFor(S(0.2), true);
                        }
                    }
                })
                .TearDown(() => allPixels.TurnOff());

            starwarsCane
                .WhenExecuted
                .Execute(instance =>
                {
                    const int spacing = 4;

                    allPixels.TurnOff();

                    while (!instance.CancelToken.IsCancellationRequested)
                    {
                        for (int i = 0; i < spacing; i++)
                        {
                            switch (i % spacing)
                            {
                                case 0:
                                case 1:
                                    allPixels.InjectRev(Color.Yellow, 1.0);
                                    break;
                                case 2:
                                case 3:
                                    allPixels.InjectRev(Color.Orange, 0.2);
                                    break;
                            }

                            instance.WaitFor(S(0.1));

                            if (instance.IsCancellationRequested)
                                break;
                        }
                    }
                    allPixels.TurnOff();
                });

            backgroundLoop
                .WhenExecuted
                .SetUp(() =>
                    {
                        pulsatingEffect1.Start();
                        lightGarlandLeft.Brightness = 1;
                        lightGarlandRight.Brightness = 1;
                        lightIcicles.Brightness = 1;
                        lightTreesRight.Brightness = 1;
                        lightHatsRight.Brightness = 1;
                        lightNetLeft.Brightness = 0.5;
                        lightNetRight.Brightness = 0.5;
                        lightReindeers.Brightness = 1;
                        lightTree.Brightness = 1;
                        lightCeiling1.SetColor(Color.Red, 0.5);
                        lightCeiling2.SetColor(Color.Red, 0.5);
                        lightCeiling3.SetColor(Color.Red, 0.5);

                        Executor.Current.Execute(candyCane);
                    })
                .TearDown(() =>
                    {
                        Executor.Current.Cancel(candyCane);

                        EverythingOff();
                    });

            musicSeq
                .Loop
                .WhenExecuted
                .SetUp(() =>
                    {
                        audioPlayer.CueTrack("21 Christmas Canon Rock");
                        // Make sure it's ready
                        System.Threading.Thread.Sleep(800);

                        EverythingOff();
                    })
                .Execute(instance =>
                    {
                        audioPlayer.PlayTrack();
                        var task = timeline.Start();

                        try
                        {
                            task.Wait(instance.CancelToken);

                            instance.WaitFor(S(10));
                        }
                        finally
                        {
                            timeline.Stop();
                            audioPlayer.PauseTrack();
                        }

                        if (!instance.IsCancellationRequested)
                            instance.WaitFor(S(2));
                        EverythingOff();

                        instance.WaitFor(S(2), true);

                        Executor.Current.Execute(backgroundLoop);
                        instance.WaitFor(S(30));
                        Executor.Current.Cancel(backgroundLoop);
                        EverythingOff();
                        instance.WaitFor(S(1));
                    })
                    .TearDown(() =>
                        {
                            EverythingOff();
                        });

            buttonSeq
                .Loop
                .WhenExecuted
                .Execute(instance =>
                {
                    buttonLightBlue.SetPower(true);
                    buttonLightRed.SetPower(false);
                    instance.WaitFor(S(0.2));
                    buttonLightBlue.SetPower(false);
                    buttonLightRed.SetPower(true);
                    instance.WaitFor(S(0.2));
                })
                .TearDown(() =>
                    {
                        buttonLightBlue.SetPower(false);
                        buttonLightRed.SetPower(false);
                    });

            fatherSeq
                .WhenExecuted
                .Execute(instance =>
                {
                    Executor.Current.Execute(starwarsCane);

                    EverythingOff();

                    audioPlayer.CueTrack("01 Star Wars_ Main Title");
                    // Make sure it's ready
                    instance.WaitFor(S(0.5));
                    audioPlayer.PlayTrack();

                    lightCeiling1.SetOnlyColor(Color.Yellow);
                    lightCeiling2.SetOnlyColor(Color.Yellow);
                    lightCeiling3.SetOnlyColor(Color.Yellow);
                    pulsatingEffect2.Start();
                    instance.WaitFor(S(16));
                    pulsatingEffect2.Stop();
                    audioPlayer.PauseTrack();
                    Executor.Current.Cancel(starwarsCane);
                    allPixels.TurnOff();
                    instance.WaitFor(S(0.5));

                    elJesus.SetPower(true);
                    lightJesus.SetColor(Color.White, 0.3);

                    instance.WaitFor(S(1.5));

                    elLightsaber.SetPower(true);
                    audioPlayer.PlayEffect("saberon");
                    instance.WaitFor(S(1));

                    lightVader.SetColor(Color.Red, 1.0);
                    audioPlayer.PlayEffect("father");
                    instance.WaitFor(S(3));

                    lightVader.TurnOff();
                    audioPlayer.PlayEffect("saberoff");
                    instance.WaitFor(S(0.5));
                    elLightsaber.SetPower(false);
                    instance.WaitFor(S(1));

                    lightJesus.TurnOff();
                    elLightsaber.TurnOff();
                    elJesus.TurnOff();
                });

            breathSeq
                .WhenExecuted
                .Execute(instance =>
                {
                    audioPlayer.PlayEffect("Darth Breathing");
                    instance.WaitFor(S(4));
                });

            laserSeq
                .WhenExecuted
                .SetUp(() =>
                    {
                        allPixels.TurnOff();
                    })
                .Execute(instance =>
                {
                    audioPlayer.PlayEffect("lazer");

                    var cb = new ColorBrightness[6];
                    cb[0] = new ColorBrightness(Color.Black, 1.0);
                    cb[1] = new ColorBrightness(Color.Red, 1.0);
                    cb[2] = new ColorBrightness(Color.Orange, 1.0);
                    cb[3] = new ColorBrightness(Color.Yellow, 1.0);
                    cb[4] = new ColorBrightness(Color.Blue, 1.0);
                    cb[5] = new ColorBrightness(Color.White, 1.0);

                    for (int i = -6; i < allPixels.Pixels; i++)
                    {
                        allPixels.SetColors(i, cb);
                        System.Threading.Thread.Sleep(25);
                    }

                    instance.WaitFor(S(1));
                })
                .TearDown(() =>
                    {
                        allPixels.TurnOff();
                    });

            stateMachine.ForFromSequence(States.Background, backgroundLoop);

            stateMachine.ForFromSequence(States.Music, musicSeq);

            stateMachine.ForFromSequence(States.Vader, fatherSeq);

            // Start Reindeer
            buttonStartReindeer.ActiveChanged += (sender, e) =>
                {
                    if (!e.NewState)
                        return;

                    if (hours.IsOpen)
                        bigReindeer.SetPower(true);
                    else
                    {
                        TestAllPixels(Color.Red, 1.0, S(1));
                        TestAllPixels(Color.Red, 0.5, S(1));

                        TestAllPixels(Color.Green, 1.0, S(1));
                        TestAllPixels(Color.Green, 0.5, S(1));

                        TestAllPixels(Color.Blue, 1.0, S(1));
                        TestAllPixels(Color.Blue, 0.5, S(1));

                        TestAllPixels(Color.Purple, 1.0, S(1));
                        TestAllPixels(Color.Purple, 0.5, S(1));

                        TestAllPixels(Color.White, 1.0, S(1));
                        TestAllPixels(Color.White, 0.5, S(1));

                        allPixels.TurnOff();
                    }
                };

            // Red Button
            buttonRed.ActiveChanged += (sender, e) =>
            {
                if (!e.NewState)
                    return;

                if (hours.IsOpen)
                {
                    stateMachine.SetMomentaryState(States.Vader);
                }
                else
                {
                    Executor.Current.Execute(laserSeq);
                }
            };

            // Blue Button
            buttonBlue.ActiveChanged += (sender, e) =>
            {
                if (!e.NewState)
                    return;

                if (hours.IsOpen)
                {
                    if (stateMachine.CurrentState == States.Background)
                        stateMachine.SetMomentaryState(States.Music);
                }
                else
                {
                    Executor.Current.Execute(breathSeq);
                }
            };

            // Hours
            hours.OpenHoursChanged += (sender, e) =>
            {
                if (e.IsOpenNow)
                {
                    stateMachine.SetState(States.Background);

                    Executor.Current.Execute(buttonSeq);
                }
                else
                {
                    Executor.Current.Cancel(buttonSeq);

                    stateMachine.Hold();
                    bigReindeer.SetPower(false);
                }
            };
        }