コード例 #1
0
        // Runs a tick of the food spawning facade, while doing some occasional logging to the console.
        public void RunTick()
        {
            if (_ticksLeft > 0)
            {
                if (_ticksLeft % 10 == 0)
                {
                    string message = $"[GAME STATE] Game will end in {_ticksLeft} ticks.";
                    Logger.Instance.LogWithColor(ConsoleColor.Blue, message);
                }

                _arena.FoodSpawningFacade.ExecuteTick();
                _ticksLeft--;

                // If all players disconnected, save the current state then wait for at least one to join
                if (!_arena.Players.Any())
                {
                    // Save the state, so that when a player joins, the game will resume.
                    var memento = _context.CreateGameStateMemento();
                    _context.Caretaker.Memento = memento;

                    var waitState = new WaitingForPlayersToConnectState(_arena, _context);
                    _context.ChangeState(waitState);
                }
            }
            else
            {
                // Game has ended, go to post-game.
                var postGameCountdownState = new PostGameCountdownState(_arena, _context);
                _context.ChangeState(postGameCountdownState);
            }
        }
コード例 #2
0
ファイル: GameInProgressState.cs プロジェクト: GedasFX/Snake
        // Runs a tick of the food spawning facade, while doing some occasional logging to the console.
        public void RunTick()
        {
            if (_ticksUntilGameEndingSoonCountdown > 0)
            {
                if (_ticksUntilGameEndingSoonCountdown % 10 == 0)
                {
                    int    totalGameTicksLeft = _ticksUntilGameEndingSoonCountdown + _context.GameEndingSoonStateDuration;
                    string message            =
                        $"[GAME STATE] {_ticksUntilGameEndingSoonCountdown} ticks left until end of game soon countdown. Ticks left until end of game: {totalGameTicksLeft}";
                    Logger.Instance.LogWithColor(ConsoleColor.Blue, message);
                }

                _arena.FoodSpawningFacade.ExecuteTick();
                _ticksUntilGameEndingSoonCountdown--;

                // If all players disconnected, save the current state then wait for a player to join.
                if (!_arena.Players.Any())
                {
                    // By saving this state to the context's caretaker, the game will resume when a new player joins again.
                    var memento = _context.CreateGameStateMemento();
                    _context.Caretaker.Memento = memento;

                    var waitState = new WaitingForPlayersToConnectState(_arena, _context);
                    _context.ChangeState(waitState);
                }
            }
            else
            {
                // Ending countdown has started.
                var gameEndingSoonState = new GameEndingSoonState(_arena, _context);
                _context.ChangeState(gameEndingSoonState);
            }
        }
コード例 #3
0
        public void RunTick()
        {
            if (_ticksLeftUntilGameStarts > 0)
            {
                if (_ticksLeftUntilGameStarts % 10 == 0)
                {
                    Logger.Instance.LogWithColor(ConsoleColor.Blue,
                                                 $"[GAME STATE] {_ticksLeftUntilGameStarts} ticks left until the game starts!");
                }

                _ticksLeftUntilGameStarts--;

                // If no players are connected during the countdown, wait for at least one to join, then restart.
                if (!_arena.Players.Any())
                {
                    var waitState = new WaitingForPlayersToConnectState(_arena, _context);
                    _context.ChangeState(waitState);
                }
            }
            else
            {
                // Countdown elapsed, game has started.
                var gameInProgressState = new GameInProgressState(_arena, _context);
                _context.ChangeState(gameInProgressState);
            }
        }
コード例 #4
0
        public void RunTick()
        {
            // Switch to pregame state if there are players connected, otherwise wait.
            if (!_arena.Players.Any())
            {
                return;
            }

            // If there is no memento in the caretaker, assume that a new game will start,
            // otherwise, restore state.
            if (_context.Caretaker.Memento == null)
            {
                var pregameCountdownState = new PregameCountdownState(_arena, _context);
                _context.ChangeState(pregameCountdownState);
            }
            else
            {
                var previousStateMemento = _context.Caretaker.Memento;
                _context.RestoreFromGameStateMemento(previousStateMemento);
            }
        }
コード例 #5
0
        public void RunTick()
        {
            if (_ticksLeftUntilPregameStart > 0)
            {
                if (_ticksLeftUntilPregameStart % 10 == 0)
                {
                    var message =
                        $"[GAME STATE] If any players are left, the pregame countdown will start in {_ticksLeftUntilPregameStart} ticks.";
                    Logger.Instance.LogWithColor(ConsoleColor.Blue, message);
                }

                _ticksLeftUntilPregameStart--;

                // If no players are connected, let the state run its course.
            }
            else
            {
                // Post-game ended, go to pregame.
                var pregameCountdownState = new PregameCountdownState(_arena, _context);
                _context.ChangeState(pregameCountdownState);
            }
        }