Ejemplo n.º 1
0
        /// <summary>
        /// Main Game loop
        /// </summary>
        /// <remarks>
        /// Shows information and board, moves player and checks their HP
        /// (twice), iterate all enemies to move them or damage player.
        /// </remarks>
        private void GameLoop()
        {
            // Shows a starting message for the level/floor.
            UI.ShowStartingMessage(gameValues.Level);

            // A boolean that describes if the player won or not.
            bool playerWon = false;

            // The game loop in and of itself. Will only be broken if the
            // player dies, leaves the game, or if he finishes the level.
            while (currentPlayer.Health > 0)
            {
                // Prints game information for the player's turn
                UI.ShowCurrentInformation(
                    currentPlayer.Health, "Player", gameValues.Level);
                UI.ShowBoard(board);
                UI.ShowBoardInstructions();

                // Moves player and returns their state, observing if he won
                // or not.
                playerWon = MovePlayer();

                // Checks if player is dead or if he won.
                if (currentPlayer.Damage(1) <= 0)
                {
                    continue;
                }
                if (playerWon)
                {
                    break;
                }

                // Prints game info once again.
                UI.ShowCurrentInformation(
                    currentPlayer.Health, "Player", gameValues.Level);
                UI.ShowBoard(board);
                UI.ShowBoardInstructions();

                // Moves player again and returns their state
                playerWon = MovePlayer();

                // Checks if player is dead or if he won, once again.
                if (currentPlayer.Damage(1) <= 0)
                {
                    continue;
                }
                if (playerWon)
                {
                    break;
                }

                // Creates a list of enemies that will be present on the board.
                List <Enemy> enemies = new List <Enemy>();

                // Iterates through all enemies on board and adds their
                // references to the list.
                foreach (Entity e in board.CurrentBoard)
                {
                    if (e is Enemy)
                    {
                        enemies.Add((Enemy)e);
                    }
                }

                // Will do actions for every enemy on board.
                foreach (Enemy enemy in enemies)
                {
                    // Prints current enemy information.
                    UI.ShowCurrentInformation(
                        currentPlayer.Health, "Enemy", gameValues.Level);
                    UI.ShowBoard(board);

                    // Observes if enemy is adjacent to player, and if so,
                    // damages him
                    if (enemy.AdjacentToPlayer(board))
                    {
                        int damage = 5;
                        // bosses deal double the damage
                        if (enemy.kind == EntityKind.Boss)
                        {
                            damage *= 2;
                        }
                        currentPlayer.Damage(damage);

                        UI.WriteMessage($"You took {damage} damage!");
                    }

                    // If he is not adjacent to player, he moves towards him
                    else
                    {
                        // gets target move coordinate
                        Coord dest = enemy.WhereToMove(board);

                        // gets original coordinate
                        Coord source = enemy.Pos;

                        // checks if the is powerup on dest coordinate, and
                        // stores it
                        if (board.IsPowerUp(dest) > 0)
                        {
                            board.StorePowerUp(dest);
                        }

                        // moves enemy
                        board.MoveEntity(enemy, dest);

                        // restores power ups if needed
                        board.RestorePowerUp(source);

                        // show where the enemy moved
                        UI.ShowBoardInformation(
                            enemy.Pos, enemy.kind);
                    }

                    if (currentPlayer.Health <= 0)
                    {
                        continue;
                    }

                    // Pauses for 2 seconds so the player can process what
                    // happened.
                    Thread.Sleep(2000);
                }
            }

            // After leaving the loop, it is observed if the player won or not,
            // and the game updates values and enters methods accordingly.
            if (playerWon)
            {
                gameValues.Hp = currentPlayer.Health;
                SaveProgress();
                NewLevel();
            }
            else
            {
                EndGame();
            }
        }