forked from University/epr24pr42-ojanssen2
big bang v2
This commit is contained in:
parent
d48e9d7b28
commit
27589cc385
18 changed files with 2847 additions and 378 deletions
112
Entities/Entity.cpp
Normal file
112
Entities/Entity.cpp
Normal file
|
@ -0,0 +1,112 @@
|
|||
#include "Entity.h"
|
||||
#include "../Environment/Maze.h"
|
||||
|
||||
namespace game {
|
||||
Entity::Entity(const Vector2d starting_position, const char display_character): pos(starting_position), display_character(display_character), move_left(true){}
|
||||
|
||||
bool Entity::is_at_position(const Vector2d& position) const {
|
||||
return this->pos.eq(position);
|
||||
}
|
||||
|
||||
void Entity::tick(const Maze& maze, const Vector2d& player_position){
|
||||
switch (this->display_character) {
|
||||
case 'A':
|
||||
// No thoughts, head empty :P
|
||||
return;
|
||||
case 'B':
|
||||
this->handle_bowie(maze);
|
||||
return;
|
||||
case 'C':
|
||||
this->handle_connelly(maze, player_position);
|
||||
return;
|
||||
default:
|
||||
cout << "ERR: THIS IS NOT A GHOST!";
|
||||
/// This case will never happen
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Entity::handle_bowie(const Maze& maze) {
|
||||
Vector2d target_position = this->pos;
|
||||
if (this->move_left)
|
||||
target_position.change_x(-1);
|
||||
else
|
||||
target_position.change_x(1);
|
||||
if (maze.is_pos_free(target_position, false))
|
||||
this->pos = target_position;
|
||||
else
|
||||
{
|
||||
this->move_left = !this->move_left;
|
||||
if (this->move_left)
|
||||
target_position.change_x(-1);
|
||||
else
|
||||
target_position.change_x(1);
|
||||
if (maze.is_pos_free(target_position, false))
|
||||
this->pos = target_position;
|
||||
}
|
||||
}
|
||||
|
||||
bool Entity::connelly_move_up(const Maze& maze) {
|
||||
Vector2d top = this->pos.get_new_updated(0, 1);
|
||||
if (maze.is_pos_free(top, false)) {
|
||||
this->pos = top;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Entity::connelly_move_down(const Maze& maze) {
|
||||
Vector2d bottom = this->pos.get_new_updated(0, -1);
|
||||
if (maze.is_pos_free(bottom, false)) {
|
||||
this-> pos = bottom;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Entity::connelly_move_left(const Maze& maze) {
|
||||
Vector2d left = this->pos.get_new_updated(-1, 0);
|
||||
if (maze.is_pos_free(left, false)) {
|
||||
this->pos = left;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Entity::connelly_move_right(const Maze& maze) {
|
||||
Vector2d right = this->pos.get_new_updated(1, 0);
|
||||
if (maze.is_pos_free(right, false)) {
|
||||
this->pos = right;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void Entity::handle_connelly(const Maze& maze, const Vector2d& player_position) {
|
||||
Vector2d diff = maze.get_delta_vector(player_position, this->pos);
|
||||
Vector2d normalized = diff.normalize();
|
||||
|
||||
if ((normalized.y == normalized.x || normalized.y > normalized.x) && normalized.y != 0) {
|
||||
if (diff.y > 0 ) {
|
||||
if (this->connelly_move_up(maze))
|
||||
return;
|
||||
} else {
|
||||
if (this->connelly_move_down(maze))
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (normalized.x != 0) {
|
||||
if (diff.x > 0){
|
||||
if (this->connelly_move_right(maze))
|
||||
return;
|
||||
}
|
||||
else {
|
||||
bool _ = this->connelly_move_left(maze);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char Entity::get_display_character() const {
|
||||
return this->display_character;
|
||||
}
|
||||
}
|
66
Entities/Entity.h
Normal file
66
Entities/Entity.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
#include "../Util/Vector2d.h"
|
||||
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
namespace game
|
||||
{
|
||||
class Maze;
|
||||
/// Eine Entität, z.B. ein Geist
|
||||
class Entity {
|
||||
private:
|
||||
/// Die aktuelle Position des Geistes
|
||||
Vector2d pos;
|
||||
/// Wie der Geist dargestellt werden sollte
|
||||
char display_character;
|
||||
/// Ist wahr, wenn der Bowie nach Links läuft, falsch, wenn er nach Rechts läuft.
|
||||
bool move_left;
|
||||
|
||||
/// Behandle Bowie Bewegungen
|
||||
/// @param maze Das Labyrinth
|
||||
void handle_bowie(const Maze& maze);
|
||||
|
||||
/// Behandle Connelly Bewegungen
|
||||
/// @param maze Das Labyrinth
|
||||
/// @param player_position Die Position des Spielers
|
||||
void handle_connelly(const Maze& maze, const Vector2d& player_position);
|
||||
|
||||
/// Versuche den Connelly nach oben zu bewegen
|
||||
/// @param maze Das Labyrinth
|
||||
/// @returns Ob die Bewegung geglückt hat
|
||||
bool connelly_move_up(const Maze& maze);
|
||||
|
||||
/// Versuche den Connelly nach unten zu bewegen
|
||||
/// @param maze Das Labyrinth
|
||||
/// @returns Ob die Bewegung geglückt hat
|
||||
bool connelly_move_down(const Maze& maze);
|
||||
|
||||
/// Versuche den Connelly nach links zu bewegen
|
||||
/// @param maze Das Labyrinth
|
||||
/// @returns Ob die Bewegung geglückt hat
|
||||
bool connelly_move_left(const Maze& maze);
|
||||
|
||||
/// Versuche den Connelly nach Rechts zu bewegen
|
||||
/// @param maze Das Labyrinth
|
||||
/// @returns Ob die Bewegung geglückt hat
|
||||
bool connelly_move_right(const Maze& maze);
|
||||
public:
|
||||
Entity(Vector2d starting_position, char display_character);
|
||||
|
||||
/// Halte den Entität bzw. den Geist up to date, bewege ihn
|
||||
/// @param maze Das Labyrinth
|
||||
/// @param player_position Die Position des Spielers
|
||||
void tick(const Maze& maze, const Vector2d& player_position);
|
||||
|
||||
/// Kontrolliere, ob sich das Entity auf einer Position befindet
|
||||
/// @param position Die Position
|
||||
/// @returns Ob das sich das Entity auf der Position befindet
|
||||
bool is_at_position(const Vector2d& position) const;
|
||||
|
||||
/// Besorge dir das Zeichen, was auf dem Labyrinth angezeigt werden soll
|
||||
/// @returns Das Zeichen
|
||||
char get_display_character() const;
|
||||
};
|
||||
} // game
|
||||
|
||||
#endif //ENTITY_H
|
108
Entities/Player.cpp
Normal file
108
Entities/Player.cpp
Normal file
|
@ -0,0 +1,108 @@
|
|||
#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 Vector2d pos) : pos(pos), keys_in_inventory(0)
|
||||
{
|
||||
}
|
||||
|
||||
Vector2d Player::get_pos() const
|
||||
{
|
||||
return this->pos;
|
||||
}
|
||||
|
||||
void Player::update_position(const Vector2d& target)
|
||||
{
|
||||
this->pos = target;
|
||||
}
|
||||
|
||||
Maze Player::handle_move(Maze& maze, const Vector2d& move_vector)
|
||||
{
|
||||
const Vector2d target_position = Vector2d(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) {
|
||||
Vector2d move_vector = {0, 0};
|
||||
|
||||
switch (input) {
|
||||
case 'w':
|
||||
move_vector = {0, -1};
|
||||
break;
|
||||
case 's':
|
||||
move_vector = {0, 1};
|
||||
break;
|
||||
case 'a':
|
||||
move_vector = {-1, 0};
|
||||
break;
|
||||
case 'd':
|
||||
move_vector = {1, 0};
|
||||
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) const {
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
GameState Player::tick(const Game& game, Maze& maze) {
|
||||
this->handle_keys(maze);
|
||||
return this->handle_collisions(game, maze);
|
||||
}
|
||||
|
||||
bool Player::has_key_available() const
|
||||
{
|
||||
return this->keys_in_inventory > 0;
|
||||
}
|
||||
} // game
|
74
Entities/Player.h
Normal file
74
Entities/Player.h
Normal file
|
@ -0,0 +1,74 @@
|
|||
#include "../Util/Vector2d.h"
|
||||
#include "../Util/GameState.h"
|
||||
|
||||
#ifndef PLAYER_H
|
||||
#define PLAYER_H
|
||||
|
||||
using game::GameState;
|
||||
|
||||
namespace game
|
||||
{
|
||||
class Maze;
|
||||
class Game;
|
||||
|
||||
/// Ein Spieler.
|
||||
/// Besitzt einen veränderbaren Positionsvektor
|
||||
class Player
|
||||
{
|
||||
private:
|
||||
/// Die Position des Spielers
|
||||
Vector2d pos;
|
||||
int keys_in_inventory;
|
||||
|
||||
/// Bewege den Splieler um den Bewegungsvektor, insofern die Zielposition begehbar ist
|
||||
/// @param maze Das Maze
|
||||
/// @param move_vector Die gewollte Bewegung
|
||||
/// @return Die neue Position des Spielers
|
||||
Maze handle_move(Maze& maze, const Vector2d& move_vector);
|
||||
|
||||
/// Gebe oder Nehme dem Spieler Schlüssel
|
||||
/// Updated auch das Maze (Reference!)
|
||||
/// @param maze Das Maze
|
||||
void handle_keys(Maze& maze);
|
||||
|
||||
/// Kontrolliere, ob der Spieler gerade in einem Geist steht
|
||||
/// @param maze Das Maze
|
||||
GameState handle_collisions(const Game& game, const Maze& maze) const;
|
||||
|
||||
public:
|
||||
/// Ein Spieler.
|
||||
/// Besitzt einen veränderbaren Positionsvektor
|
||||
/// @param target_x Die Startposition des Spielers (X-Koordinate)
|
||||
/// @param target_y Die Startposition des Spielers (Y-Koordinate)
|
||||
Player(int target_x, int target_y);
|
||||
|
||||
/// Ein Spieler
|
||||
/// @param pos Die Startposition des Spielers
|
||||
explicit Player(Vector2d pos);
|
||||
|
||||
/// Kriege die Position des Spielers
|
||||
/// @return Die Position des Spielers
|
||||
Vector2d get_pos() const;
|
||||
|
||||
/// Aktuallisiere die Position des Spielers ohne weitere Checks
|
||||
/// @param target Das ziel
|
||||
void update_position(const Vector2d& target);
|
||||
|
||||
/// Behandle die eingabe des Nutzers für den Spieler
|
||||
/// @param maze Das Labyrinth
|
||||
/// @param input Die Eingabe des Nutzers
|
||||
Maze handle_user_input(Maze& maze, const char& input);
|
||||
|
||||
/// Halte den Spieler aktuell
|
||||
/// @param game Das Spiel
|
||||
/// @param maze Das Labyrinth
|
||||
/// @returns Der Status des Spiels
|
||||
GameState tick(const Game& game, Maze& maze);
|
||||
|
||||
/// Check, if a player has at least one key
|
||||
/// @return If the player as an available key
|
||||
bool has_key_available() const;
|
||||
};
|
||||
} // maze
|
||||
|
||||
#endif //PLAYER_H
|
Loading…
Add table
Add a link
Reference in a new issue