예제 #1
0
        // 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);
            }
        }
예제 #2
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);
            }
        }
예제 #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);
            }
        }