Пример #1
0
    public void RunsEnterInitExitFunctionsInProperOrder()
    {
        BehaviorMap   behaviors = new BehaviorMap();
        List <string> list      = new List <string>();
        Callback      callback  = (stateName) => {
            list.Add(stateName);
        };

        MakeCallbacks(
            behaviors,
            new[] { "Root", "opened", "closed", "locked", "unlocked" },
            new[] { "init", "enter", "exit" },
            callback
            );

        StateChart chart = GetChart(behaviors);

        Assert.That(list, Is.EquivalentTo(new[] {
            "Root:init", "Root:enter",
            "closed:init", "closed:enter",
            "locked:init", "locked:enter"
        }));
        list.Clear();
        chart.Trigger(new Evt_DoorUnlock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "locked:exit", "unlocked:init", "unlocked:enter"
        }));
        list.Clear();
        chart.Trigger(new Evt_DoorLock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "unlocked:exit", "locked:enter"
        }));
    }
Пример #2
0
    public void StartNotInOpenedOrUnlocked()
    {
        StateChart chart = GetChart();

        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsFalse(chart.IsInState("unlocked"));
    }
Пример #3
0
    public void StartInClosedAndLocked()
    {
        StateChart chart = GetChart();

        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsTrue(chart.IsInState("locked"));
    }
Пример #4
0
    public void TransitionsToAnotherState()
    {
        StateChart chart = new StateChart((builder) => {
            Action <string, Action> State = builder.State;

            State("State-1", () => {
                builder.Transition <SomeEvt>("State-1-2");

                State("State-1-1", () => {
                    State("State-1-1-1", () => { });
                    State("State-1-1-2", () => { });
                });

                State("State-1-2", () => { });
            });
        });

        chart.Trigger(new SomeEvt());
        chart.Tick();
        Assert.IsTrue(chart.IsInState("State-1"));
        Assert.IsFalse(chart.IsInState("State-1-1"));
        Assert.IsFalse(chart.IsInState("State-1-1-1"));
        Assert.IsFalse(chart.IsInState("State-1-1-2"));
        Assert.IsTrue(chart.IsInState("State-1-2"));
    }
        private void SetupStateChart(StateChart chart)
        {
            var playerTurnState           = CreateState <PlayerTurnState>(Match3State.PlayerTurn);
            var swapInProgressState       = CreateState <SwapInProgressState>(Match3State.SwapInProgress);
            var revertSwapInProgressState = CreateState <RevertSwapInProgressState>(Match3State.RevertSwapInProgress);
            var matchesSearchState        = CreateState <MatchesSearchState>(Match3State.MatchesSearch);
            var fallingState  = CreateState <FallingState>(Match3State.MatchesFall);
            var gameOverState = CreateState <GameOverState>(Match3State.GameOver);

            var editor = new StateMachineEditor(chart);

            editor.Initial().Transition().Target(playerTurnState);

            playerTurnState.Event(BoardStateEvents.StartSwapEvent).Target(swapInProgressState);

            swapInProgressState.Event(BoardStateEvents.SwapCompleteEvent).Target(matchesSearchState);
            revertSwapInProgressState.Event(BoardStateEvents.SwapCompleteEvent).Target(playerTurnState);

            matchesSearchState.Event(BoardStateEvents.RevertSwapEvent).Target(revertSwapInProgressState);
            matchesSearchState.Event(BoardStateEvents.NoMatchesFoundEvent).Target(playerTurnState);
            matchesSearchState.Event(BoardStateEvents.MatchesFoundEvent).Target(fallingState);

            playerTurnState.Event(BoardStateEvents.NoTurnsLeftEvent).Target(gameOverState);

            fallingState.Event(BoardStateEvents.FallingCompleteEvent).Target(matchesSearchState);

            gameOverState.Event(BoardStateEvents.ExitBoardEvent).Target(editor.Final());
        }
Пример #6
0
    public void ProperlyQueuesEvents()
    {
        StateChart chart = GetChart2();

        Assert.IsTrue(chart.IsInState("closed"));
        chart.Tick();
        Assert.IsTrue(chart.IsInState("opened"));
    }
        public void Initialize()
        {
            stateChart = new StateChart();
            SetupStateChart(stateChart);
            stateChart.Start();

            signalBus.Subscribe <Match3Signals.StateChartSignal>(OnStateChartSignal);
        }
Пример #8
0
        public void Initialize()
        {
            stateChart = new StateChart();
            SetupStateChart(stateChart);
            stateChart.Start();

            signalBus.Subscribe <ChangeLevelSignal>(OnLevelChange);
            signalBus.Subscribe <ExitToMapSignal>(OnExitToMap);
        }
Пример #9
0
    public void MovesToUnlockedFromClosedWithUnlockEvent()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorUnlock());
        chart.Tick();
        Assert.IsTrue(chart.IsInState("unlocked"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsFalse(chart.IsInState("locked"));
    }
Пример #10
0
    public void DoesNotTransitionIfGuardFnIsFalse()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorUnlock(false));
        chart.Tick();
        Assert.IsFalse(chart.IsInState("unlocked"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsTrue(chart.IsInState("locked"));
    }
Пример #11
0
    public void DoesNotTransitionIfNoTransitionHandlerExistsForCongiuration()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorOpen());
        chart.Tick();
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsTrue(chart.IsInState("locked"));
        Assert.IsFalse(chart.IsInState("unlocked"));
    }
Пример #12
0
        private void SetupStateChart(StateChart chart)
        {
            var bootState  = new StateVertex(diContainer.Instantiate <BootState>());
            var mapState   = new StateVertex(diContainer.Instantiate <LevelSelectionState>());
            var boardState = new StateVertex(diContainer.Instantiate <BoardState>());

            var editor = new StateMachineEditor(chart);

            editor.Initial().Transition().Target(mapState);
            bootState.Transition().Target(mapState);
            mapState.Event(GameStateEvents.StartLevelEvent).Target(boardState);
            boardState.Event(GameStateEvents.ExitBoardEvent).Target(mapState);
            mapState.Event(GameStateEvents.DisposedEvent).Target(editor.Final());
        }
Пример #13
0
    public void MovesFromClosedAndUnlockedToOpenedAndBackToClosedLocked()
    {
        StateChart chart = GetChart();

        chart.Trigger(new Evt_DoorUnlock());
        chart.Trigger(new Evt_DoorOpen());
        chart.Tick();
        Assert.IsTrue(chart.IsInState("opened"));
        Assert.IsFalse(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("locked"));
        Assert.IsFalse(chart.IsInState("unlocked"));
        chart.Trigger(new Evt_DoorClose());
        chart.Tick();
        Assert.IsFalse(chart.IsInState("opened"));
        Assert.IsTrue(chart.IsInState("closed"));
        Assert.IsFalse(chart.IsInState("locked"));
        Assert.IsTrue(chart.IsInState("unlocked"));
    }
Пример #14
0
    public void CallsEventHandlers()
    {
        List <string> list = new List <string>();
        Action <string, StateChartEvent> callback = (key, evt) => {
            list.Add(key);
        };

        EvtPair[] pairs =
        {
            new EvtPair("Root",     typeof(Evt_DoorUnlock), callback),
            new EvtPair("closed",   typeof(Evt_DoorUnlock), callback),
            new EvtPair("locked",   typeof(Evt_DoorUnlock), callback),
            new EvtPair("unlocked", typeof(Evt_DoorUnlock), callback),
            new EvtPair("opened",   typeof(Evt_DoorUnlock), callback),
            new EvtPair("Root",     typeof(Evt_DoorLock),   callback),
            new EvtPair("closed",   typeof(Evt_DoorLock),   callback),
            new EvtPair("locked",   typeof(Evt_DoorLock),   callback),
            new EvtPair("unlocked", typeof(Evt_DoorLock),   callback)
        };

        StateChart chart = GetChart(null, pairs);

        chart.Trigger(new Evt_DoorUnlock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "Root:Evt_DoorUnlock",
            "closed:Evt_DoorUnlock",
            "locked:Evt_DoorUnlock"
        }));
        list.Clear();
        chart.Trigger(new Evt_DoorUnlock());
        chart.Trigger(new Evt_DoorLock());
        chart.Tick();
        Assert.That(list, Is.EquivalentTo(new[] {
            "Root:Evt_DoorUnlock",
            "closed:Evt_DoorUnlock",
            "unlocked:Evt_DoorUnlock",
            "Root:Evt_DoorLock",
            "closed:Evt_DoorLock",
            "unlocked:Evt_DoorLock"
        }));
    }
Пример #15
0
    public void StateChart_SpecSimplePasses()
    {
        StateChart chart = new StateChart((builder) => {
            Action <string, Action> State = builder.State;

            State("State-1", () => {
                State("State-1-1", () => {
                    State("State-1-1-1", () => { });
                    State("State-1-1-2", () => { });
                });
                State("State-1-2", () => { });
            });
        });

        Assert.IsTrue(chart.IsInState("State-1"));
        Assert.IsTrue(chart.IsInState("State-1-1"));
        Assert.IsTrue(chart.IsInState("State-1-1-1"));
        Assert.IsFalse(chart.IsInState("State-1-1-2"));
        Assert.IsFalse(chart.IsInState("State-1-2"));
    }
 public void Dispose()
 {
     signalBus.Unsubscribe <Match3Signals.StateChartSignal>(OnStateChartSignal);
     stateChart = null;
 }
Пример #17
0
 public void Start()
 {
     stateChart = new StateChart(BuildStateChart);
 }