epr24pr42-ojanssen2/Entity.cpp
2025-01-19 18:23:50 +01:00

112 lines
3.4 KiB
C++

#include "Entity.h"
#include "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;
}
}