forked from University/epr24pr42-ojanssen2
89 lines
2.5 KiB
C++
89 lines
2.5 KiB
C++
|
#include "Game.h"
|
||
|
#include "../Entities/Player.h"
|
||
|
#include "Maze.h"
|
||
|
#include "../Exceptions/MovementNotPossible.h"
|
||
|
#include "../Exceptions/UnkownAction.h"
|
||
|
#include "../Util/PositionVector.h"
|
||
|
|
||
|
using game::Player;
|
||
|
using game::Maze;
|
||
|
using game_exceptions::UnkownAction;
|
||
|
using game_exceptions::MovementNotPossible;
|
||
|
|
||
|
namespace game
|
||
|
{
|
||
|
Game::Game(Maze& maze): maze(maze), player(0, 0), infomode_enabled(false), state(GameState::RUNNING)
|
||
|
{
|
||
|
PositionVector player_start_position = this->maze.get_player_start_position();
|
||
|
|
||
|
this->player = Player(player_start_position);
|
||
|
}
|
||
|
|
||
|
GameState Game::get_state() {
|
||
|
return this->state;
|
||
|
}
|
||
|
|
||
|
void Game::handle_user_input(const char& input)
|
||
|
{
|
||
|
switch (input)
|
||
|
{
|
||
|
case 'w':
|
||
|
case 'a':
|
||
|
case 's':
|
||
|
case 'd':
|
||
|
break;
|
||
|
case 'q':
|
||
|
this->state = GameState::QUITTING;
|
||
|
case 'h':
|
||
|
cout <<
|
||
|
"Du wurdest von einem Zauberer in ein Labyrinth gesperrt, nachdem du seine Künste beleidigt hast.\n"
|
||
|
<< "Er laesst dich leben, wenn du es schaffst den Ausgang (Z) zu finden. Solltest du keinen Erfolg haben, laesst er dich verhungern.\n"
|
||
|
<< "Auf deinem Abenteuer wirst du dabei boesen Geistern (A) begegnen und mit Schluesseln (K) Tueren (T) aufschliessen.\n"
|
||
|
<< "Bewege dich mit 'w', 'a', 's' und 'd'.\n";
|
||
|
break;
|
||
|
default:
|
||
|
throw UnkownAction("Diese Eingabe kenne ich nicht. Gib 'h' ein, um eine Hilfe zu erhalten.");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool Game::should_end_game() {
|
||
|
return this->state != GameState::RUNNING;
|
||
|
}
|
||
|
|
||
|
void Game::run_game()
|
||
|
{
|
||
|
char game_input;
|
||
|
|
||
|
// Hauptschleife
|
||
|
while (true)
|
||
|
{
|
||
|
this->maze.render(this->player, this->enemies);
|
||
|
|
||
|
if (this->maze.is_player_at_goal(this->player))
|
||
|
{
|
||
|
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
cin >> game_input;
|
||
|
|
||
|
try
|
||
|
{
|
||
|
this->handle_user_input(game_input);
|
||
|
this->player.handle_user_input(this->maze, game_input);
|
||
|
} catch (UnkownAction& err)
|
||
|
{
|
||
|
cout << err.what() << "\n";
|
||
|
} catch (MovementNotPossible& err)
|
||
|
{
|
||
|
cout << err.what() << "\n";
|
||
|
}
|
||
|
|
||
|
this->player.tick(this->state, this->maze);
|
||
|
|
||
|
if (this->should_end_game())
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
} // game
|