100 lines
2.8 KiB
C++
100 lines
2.8 KiB
C++
#include "Game.h"
|
|
#include "Player.h"
|
|
#include "Maze.h"
|
|
#include "Exceptions/ExitGame.h"
|
|
#include "Exceptions/MovementNotPossible.h"
|
|
#include "Exceptions/UnkownAction.h"
|
|
|
|
using game::Player;
|
|
using game::Maze;
|
|
using game_exceptions::UnkownAction;
|
|
using game_exceptions::MovementNotPossible;
|
|
using game_exceptions::ExitGame;
|
|
|
|
namespace game
|
|
{
|
|
Game::Game(Maze& maze): maze(maze), player(0, 0)
|
|
{
|
|
PositionVector player_start_position = this->maze.get_player_start_position();
|
|
|
|
this->player = Player(player_start_position);
|
|
}
|
|
|
|
PositionVector Game::handle_user_input(const char& input)
|
|
{
|
|
PositionVector movement_vector = PositionVector(0, 0);
|
|
|
|
switch (input)
|
|
{
|
|
case 'w':
|
|
movement_vector.update(0, -1);
|
|
break;
|
|
case 'a':
|
|
movement_vector.update(-1, 0);
|
|
break;
|
|
case 's':
|
|
movement_vector.update(0, 1);
|
|
break;
|
|
case 'd':
|
|
movement_vector.update(1, 0);
|
|
break;
|
|
case 'q':
|
|
throw ExitGame("Schoenen Tag noch!");
|
|
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.");
|
|
}
|
|
|
|
return movement_vector;
|
|
}
|
|
|
|
void Game::run_game()
|
|
{
|
|
char game_input;
|
|
|
|
// Hauptschleife
|
|
while (true)
|
|
{
|
|
if (this->maze.was_player_killed_by_ghost(player))
|
|
{
|
|
cout << "Sie haben einen Geist getroffen! Game Over!\n";
|
|
break;
|
|
}
|
|
|
|
this->maze.render(player);
|
|
|
|
if (this->maze.is_player_at_goal(player))
|
|
{
|
|
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
|
|
break;
|
|
}
|
|
|
|
cin >> game_input;
|
|
|
|
PositionVector movement_vector = PositionVector(0, 0);
|
|
|
|
try
|
|
{
|
|
movement_vector = handle_user_input(game_input);
|
|
} catch (UnkownAction& err)
|
|
{
|
|
cout << err.what() << "\n";
|
|
}
|
|
|
|
try
|
|
{
|
|
player.move(this->maze, movement_vector);
|
|
}
|
|
catch (MovementNotPossible& err)
|
|
{
|
|
cout << err.what() << "\n";
|
|
}
|
|
}
|
|
}
|
|
} // game
|