Esempio n. 1
0
        public void exit_sets_onExit_action()
        {
            int timesExitCalled = 0;

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Exit(_ => timesExitCalled++)
                            .End()
                            .State <TestState>("bar")
                            .End()
                            .Build();

            rootState.ChangeState("foo");
            rootState.ChangeState("bar");
            rootState.ChangeState("foo");

            Assert.Equal(1, timesExitCalled);
        }
        public void state_with_type_and_string_has_specified_name()
        {
            IState expectedParent = null;

            var rootState = new StateMachineBuilder()
                .State<TestState>("test")
                    .Enter(state => expectedParent = state.Parent)
                .End()
                .Build();

            rootState.ChangeState("test");

            Assert.Equal(rootState, expectedParent);
        }
Esempio n. 3
0
        public void state_with_string_has_specified_name()
        {
            IState expectedParent = null;

            var rootState = new StateMachineBuilder()
                            .State("test")
                            .Enter(state => expectedParent = state.Parent)
                            .End()
                            .Build();

            rootState.ChangeState("test");

            Assert.Equal(rootState, expectedParent);
        }
Esempio n. 4
0
        public void build_returns_root_state_which_has_added_states_under_it()
        {
            IState expectedParent = null;

            var rootState = new StateMachineBuilder()
                            .State <TestState>()
                            .Enter(state => expectedParent = state.Parent)
                            .End()
                            .Build();

            rootState.ChangeState("TestState");

            Assert.Equal(rootState, expectedParent);
        }
        public void enter_sets_onEnter_action()
        {
            int timesEnterCalled = 0;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Enter(_ => timesEnterCalled++)
                .End()
                .Build();

            rootState.ChangeState("foo");

            Assert.Equal(1, timesEnterCalled);
        }
        public void build_returns_root_state_which_has_added_states_under_it()
        {
            IState expectedParent = null;

            var rootState = new StateMachineBuilder()
                .State<TestState>()
                    .Enter(state => expectedParent = state.Parent)
                .End()
                .Build();

            rootState.ChangeState("TestState");

            Assert.Equal(rootState, expectedParent);
        }
        public void event_sets_up_event()
        {
            var timesEventRaised = 0;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Event("newEvent", _ => timesEventRaised++)
                .End()
                .Build();
            rootState.ChangeState("foo");

            rootState.TriggerEvent("newEvent");

            Assert.Equal(1, timesEventRaised);
        }
Esempio n. 8
0
        public void update_sets_onUpdate_action()
        {
            int timesUpdateCalled = 0;

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Update((state, dt) => timesUpdateCalled++)
                            .End()
                            .Build();

            rootState.ChangeState("foo");
            rootState.Update(1f);

            Assert.Equal(1, timesUpdateCalled);
        }
Esempio n. 9
0
        public void event_sets_up_event()
        {
            var timesEventRaised = 0;

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Event("newEvent", _ => timesEventRaised++)
                            .End()
                            .Build();

            rootState.ChangeState("foo");

            rootState.TriggerEvent("newEvent");

            Assert.Equal(1, timesEventRaised);
        }
Esempio n. 10
0
        public void triggering_event_with_incorrect_type_of_EventArgs_throws_exception()
        {
            const string expectedString = "test";

            var testEventArgs = new TestEventArgs {
                TestString = expectedString
            };

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Event <SecondTestEventArgs>("newEvent", (state, eventArgs) => { })
                            .End()
                            .Build();

            rootState.ChangeState("foo");

            Assert.Throws <ApplicationException>(() => rootState.TriggerEvent("newEvent", testEventArgs));
        }
        public void event_passes_correct_arguments()
        {
            var expectedString = "test";
            var actualString = string.Empty;

            var testEventArgs = new TestEventArgs();
            testEventArgs.TestString = expectedString;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Event<TestEventArgs>("newEvent", (state, eventArgs) => actualString = eventArgs.TestString)
                .End()
                .Build();
            rootState.ChangeState("foo");

            rootState.TriggerEvent("newEvent", testEventArgs);

            Assert.Equal(expectedString, actualString);
        }
        public void condition_sets_action_for_condition()
        {
            var condition = false;
            var timesConditionActionCalled = 0;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Condition(() => condition, _ => timesConditionActionCalled++)
                .End()
                .Build();
            rootState.ChangeState("foo");

            rootState.Update(1f);

            condition = true;

            rootState.Update(1f);

            Assert.Equal(1, timesConditionActionCalled);
        }
Esempio n. 13
0
        public void state_adds_state_as_child_of_current_state()
        {
            IState expectedParent = null;
            IState actualParent   = null;

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Enter(state => {
                expectedParent = state;
                state.ChangeState("bar");
            })
                            .State("bar")
                            .Enter(state => actualParent = state.Parent)
                            .End()
                            .End()
                            .Build();

            rootState.ChangeState("foo");

            Assert.Equal(expectedParent, actualParent);
        }
Esempio n. 14
0
        public void named_state_with_type_is_added_with_correct_name()
        {
            IState expectedParent = null;
            IState actualParent   = null;

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Enter(state => {
                expectedParent = state;
                state.ChangeState("bar");
            })
                            .State <TestState>("bar")
                            .Enter(state => actualParent = state.Parent)
                            .End()
                            .End()
                            .Build();

            rootState.ChangeState("foo");

            Assert.Equal(expectedParent, actualParent);
        }
Esempio n. 15
0
        public void event_passes_correct_arguments()
        {
            const string expectedString = "test";
            var          actualString   = string.Empty;

            var testEventArgs = new TestEventArgs {
                TestString = expectedString
            };

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Event <TestEventArgs>("newEvent", (state, eventArgs) => actualString = eventArgs.TestString)
                            .End()
                            .Build();

            rootState.ChangeState("foo");

            rootState.TriggerEvent("newEvent", testEventArgs);

            Assert.Equal(expectedString, actualString);
        }
Esempio n. 16
0
        public void condition_sets_action_for_condition()
        {
            var condition = false;
            var timesConditionActionCalled = 0;

            var rootState = new StateMachineBuilder()
                            .State <TestState>("foo")
                            .Condition(() => condition, _ => timesConditionActionCalled++)
                            .End()
                            .Build();

            rootState.ChangeState("foo");

            rootState.Update(1f);

            condition = true;

            rootState.Update(1f);

            Assert.Equal(1, timesConditionActionCalled);
        }
Esempio n. 17
0
        static void Main(string[] args)
        {
            Console.WriteLine("Shark state machine. Press Ctrl-C to exit.");

            // Set up the state machine
            var rootState = new StateMachineBuilder()
                            .State <NormalState>("Swimming")
                            .Update((state, time) =>
            {
                state.OnUpdate();
                if (hunger > 5)
                {
                    state.PushState("Hunting");
                }
            })
                            .State <HungryState>("Hunting")
                            .Update((state, time) =>
            {
                state.OnUpdate();
                if (hunger <= 5)
                {
                    state.Parent.PopState();
                    return;
                }
            })
                            .End()
                            .End()
                            .Build();

            // Set the initial state.
            rootState.ChangeState("Swimming");

            // Update the state machine at a set interval.
            while (true)
            {
                rootState.Update(1.0f);
                Thread.Sleep(1000);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Shark state machine. Press Ctrl-C to exit.");

            // Set up the state machine
            var rootState = new StateMachineBuilder()
                .State<NormalState>("Swimming")
                    .Update((state, time) =>
                    {
                        state.OnUpdate();
                        if (hunger > 5)
                        {
                            state.PushState("Hunting");
                        }
                    })
                    .State<HungryState>("Hunting")
                        .Update((state, time) =>
                        {
                            state.OnUpdate();
                            if (hunger <= 5)
                            {
                                state.Parent.PopState();
                                return;
                            }
                        })
                    .End()
                .End()
                .Build();

            // Set the initial state.
            rootState.ChangeState("Swimming");

            // Update the state machine at a set interval.
            while (true)
            {
                rootState.Update(1.0f);
                Thread.Sleep(1000);
            }
        }
Esempio n. 19
0
        IEnumerator State()
        {
            y_push = sprite.Height;
            player.grid_entrance = MapUtilities.GetRoomUpperLeftPos(GlobalState.CurrentMapGrid) + Vector2.One * 20;

            GlobalState.SpawnEntity(new VolumeEvent(0, 3));

            while (MapUtilities.GetInGridPosition(player.Position).X < 48)
            {
                yield return(null);
            }

            GlobalState.Dialogue = Dialogue.DialogueManager.GetDialogue("redboss", "before_fight");

            float push_timer = 0f;

            loopSFX = true;
            while (!GlobalState.LastDialogueFinished)
            {
                push_timer += GameTimes.DeltaTime;
                if (push_timer >= push_tick_max)
                {
                    push_timer = 0f;
                    if (y_push > 0)
                    {
                        GlobalState.screenShake.Shake(0.021f, 0.1f);
                        y_push--;
                    }
                }
                yield return(null);
            }
            loopSFX = false;

            SoundManager.PlaySong("redcave-boss");
            Play("bob");

            IState state = new StateMachineBuilder()
                           .State <SplashState>("Splash")
                           .Enter((s) => amp = 5)
                           .Event("Splash", (s) =>
            {
                splash_bullets.Spawn(b => b.Spawn(), 4);
            })
                           .Event("Tentacles", (s) =>
            {
                SpawnTentacles();
                if (proximity_hits != Touching.NONE)
                {
                    s.got_too_close++;
                    if (s.got_too_close == 2)
                    {
                        s.got_too_close = 0;
                        s.Parent.ChangeState("Stun");
                    }
                }
            })
                           .End()
                           .State <DashState>("Dash")
                           .Enter((s) =>
            {
                amp      = 0;
                velocity = new Vector2(30, 20);
                Play("bob");
            })
                           .Update((s, _) =>
            {
                Drawing.Effects.ScreenShake.Directions dirs = new();
                Vector2 tl = MapUtilities.GetInGridPosition(Position);
                Vector2 br = MapUtilities.GetInGridPosition(Position + new Vector2(width, height));
                if (tl.Y < 2 * 16)
                {
                    velocity.Y = 60;
                    dirs      |= Drawing.Effects.ScreenShake.Directions.Vertical;
                }
                else if (br.Y > 16 * 8)
                {
                    velocity.Y = -60;
                    dirs      |= Drawing.Effects.ScreenShake.Directions.Vertical;
                }

                if (tl.X < 2 * 16)
                {
                    velocity.X = 60;
                    dirs      |= Drawing.Effects.ScreenShake.Directions.Horizontal;
                }
                else if (br.X > 16 * 8)
                {
                    velocity.X = -60;
                    dirs      |= Drawing.Effects.ScreenShake.Directions.Horizontal;
                }
                GlobalState.screenShake.Shake(0.05f, 0.1f, dirs);
            })
                           .Event("Tentacles", (s) =>
            {
                SpawnTentacles();
                if (proximity_hits != Touching.NONE)
                {
                    s.got_too_close++;
                    if (s.got_too_close == 2)
                    {
                        s.got_too_close = 0;
                        s.Parent.ChangeState("Splash");
                    }
                }
            })
                           .Event("EndDash", (s) =>
            {
                s.Parent.ChangeState("Stun");
            })
                           .Exit((s) => velocity = Vector2.Zero)
                           .End()
                           .State <StunState>("Stun")
                           .Enter((s) => s.stateLogic = StunStateLogic())
                           .Update((s, _) =>
            {
                if (!s.stateLogic.MoveNext())
                {
                    s.Parent.ChangeState("Dash");
                }
            })
                           .End()
                           .Build();

            state.ChangeState("Splash");
            state.TriggerEvent("Splash"); //First time instantly fires splash bullets

            while (health > 0)
            {
                state.Update(GameTimes.DeltaTime);

                pushdown_timer += GameTimes.DeltaTime * 3;
                y_push          = (int)(amp + MathF.Sin(pushdown_timer) * amp);
                yield return(null);
            }

            velocity = Vector2.Zero;

            SoundManager.StopSong();
            GlobalState.Dialogue = Dialogue.DialogueManager.GetDialogue("redboss", "after_fight");
            GlobalState.screenShake.Shake(0.05f, 0.1f);
            GlobalState.flash.Flash(1f, Color.Red);

            while (!GlobalState.LastDialogueFinished)
            {
                yield return(null);
            }

            Play("die");
            SoundManager.PlaySoundEffect("redboss_death");
            GlobalState.wave.active = true;

            y_push = 0;

            while (y_push < sprite.Height)
            {
                MathUtilities.MoveTo(ref ripple.opacity, 0, 0.3f);

                push_timer += GameTimes.DeltaTime;
                if (push_timer >= push_tick_max)
                {
                    push_timer = 0f;
                    y_push++;
                }
                yield return(null);
            }

            float final_timer = 2f;

            while (final_timer > 0f)
            {
                final_timer -= GameTimes.DeltaTime;
                yield return(null);
            }

            preset.Alive            = exists = false;
            GlobalState.wave.active = false;
            SoundManager.PlaySong("redcave");
            GlobalState.events.BossDefeated.Add("REDCAVE");
            yield break;
        }
        public void named_state_with_type_is_added_with_correct_name()
        {
            IState expectedParent = null;
            IState actualParent = null;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Enter(state => {
                        expectedParent = state;
                        state.ChangeState("bar");
                    })
                    .State<TestState>("bar")
                        .Enter(state => actualParent = state.Parent)
                    .End()
                .End()
                .Build();

            rootState.ChangeState("foo");

            Assert.Equal(expectedParent, actualParent);
        }
        public void state_with_type_adds_state_as_child_of_current_state()
        {
            IState expectedParent = null;
            IState actualParent = null;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Enter(state => {
                        expectedParent = state;
                        state.ChangeState("TestState");
                    })
                    .State<TestState>()
                        .Enter(state => actualParent = state.Parent)
                    .End()
                .End()
                .Build();

            rootState.ChangeState("foo");

            Assert.Equal(expectedParent, actualParent);
        }
        public void triggering_event_with_incorrect_type_of_EventArgs_throws_exception()
        {
            var expectedString = "test";
            var actualString = string.Empty;

            var testEventArgs = new TestEventArgs();
            testEventArgs.TestString = expectedString;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Event<SecondTestEventArgs>("newEvent", (state, eventArgs) => { })
                .End()
                .Build();
            rootState.ChangeState("foo");

            Assert.Throws<ApplicationException>(() => rootState.TriggerEvent("newEvent", testEventArgs));
        }
        public void update_sets_onUpdate_action()
        {
            int timesUpdateCalled = 0;

            var rootState = new StateMachineBuilder()
                .State<TestState>("foo")
                    .Update((state, dt) => timesUpdateCalled++)
                .End()
                .Build();

            rootState.ChangeState("foo");
            rootState.Update(1f);

            Assert.Equal(1, timesUpdateCalled);
        }