#include "Player.h" #include "../Environment/Maze.h" #include "../Exceptions/MovementNotPossible.h" #include "../Util/GameState.h" #include "../Environment/Game.h" using game::GameState; using game::Game; using game_exceptions::MovementNotPossible; namespace game { Player::Player(const int target_x, const int target_y): pos(target_x, target_y), keys_in_inventory(0) { } Player::Player(const PositionVector pos) : pos(pos), keys_in_inventory(0) { } PositionVector Player::get_pos() const { return this->pos; } void Player::update_position(const PositionVector& target) { this->pos = target; } Maze Player::handle_move(Maze& maze, const PositionVector& move_vector) { const PositionVector target_position = PositionVector(this->get_pos().x + move_vector.x, this->get_pos().y + move_vector.y); if (maze.is_pos_free(target_position, this->has_key_available())) { this->update_position(target_position); } else throw MovementNotPossible("Bewegung nicht moeglich!"); return maze; } Maze Player::handle_user_input(Maze& maze, const char& input) { PositionVector move_vector = {0, 0}; switch (input) { case 'w': move_vector = {-1, 0}; break; case 's': move_vector = {1, 0}; break; case 'a': move_vector = {0, -1}; break; case 'd': move_vector = {0, 1}; break; } this->handle_move(maze, move_vector); return maze; } void Player::handle_keys(Maze& maze) { switch (maze.get_field(this->pos)) { case 'K': ++this->keys_in_inventory; maze.update_field(this->get_pos(), '.'); break; case 'T': --this->keys_in_inventory; maze.update_field(this->get_pos(), '.'); break; default: ; } } GameState Player::handle_collisions(const Game& game, const Maze& maze) { char field_at_pos = maze.get_field(this->get_pos()); // Game state sanity check if (game.get_state() != GameState::RUNNING) return game.get_state(); if (field_at_pos == '.'){ if (game.is_enemy_at_pos(this->get_pos())) return GameState::HIT_BY_GHOST; return GameState::RUNNING; } // You are not supposed to be here! return game.get_state(); } void Player::tick(const Game& game, Maze& maze) { this->handle_keys(maze); this->handle_collisions(game, maze); } bool Player::has_key_available() const { return this->keys_in_inventory > 0; } } // game