epr24pr42-ojanssen2/Game.cpp

109 lines
3.1 KiB
C++
Raw Permalink Normal View History

2025-01-19 18:23:50 +01:00
#include "Game.h"
#include "Maze.h"
2025-01-19 18:20:22 +01:00
#include "MovementNotPossible.h"
2025-01-19 18:23:50 +01:00
#include "UnkownAction.h"
2025-01-19 18:20:22 +01:00
#include "Vector2d.h"
2025-01-19 17:59:26 +01:00
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), enemies({}), state(GameState::RUNNING), infomode_enabled(false)
{
Vector2d player_start_position = this->maze.get_player_start_position();
this->player = Player(player_start_position);
this->enemies = maze.get_entities();
}
GameState Game::get_state() const {
return this->state;
}
void Game::handle_user_input(const char& input)
{
switch (input)
{
case 'w':
case 'a':
case 's':
case 'd':
break;
case 'i':
this->infomode_enabled = !this->infomode_enabled;
break;
case 'q':
this->state = GameState::QUITTING;
break;
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::is_enemy_at_pos(const Vector2d& position) const {
for (Entity e : this->enemies)
if (e.is_at_position(position))
return true;
return false;
}
bool Game::should_end_game() const
{
return this->state != GameState::RUNNING;
}
void Game::run_game()
{
char game_input;
// Hauptschleife
while (true)
{
this->maze.render(this->player, this->enemies, this->infomode_enabled);
if (this->maze.is_player_at_goal(this->player))
{
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
break;
}
cin >> game_input;
if (!cin)
this->state = GameState::QUITTING;
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->state = this->player.tick(*this, this->maze);
for (Entity& e : this->enemies)
e.tick(this->maze, this->player.get_pos());
this->state = this->player.tick(*this, this->maze);
if (this->should_end_game())
return;
}
}
} // game