Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code base #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions BEE_PLAYER.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// BEE_PLAYER.h
#ifndef BEE_PLAYER_H
#define BEE_PLAYER_H
#include <vector>
#include <memory>
#include "RAIN.h" // Include the definition
class RAIN;
class Obstacle;

class BEE_PLAYER {
public:
virtual ~BEE_PLAYER() {}
virtual void attach(std::shared_ptr<RAIN> RAIN) = 0;
virtual void detach(RAIN* RAIN) =0;
virtual void notify(RAIN* RAIN, bool is_collision) =0;
};

#endif // BEE_PLAYER_H
52 changes: 52 additions & 0 deletions GameManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// GameManager.cpp
#include "GameManager.h"
#include "splashkit.h"

GameManager::GameManager() {
player = nullptr;
}

void GameManager::setPlayer(Player* player) {
this->player = player;
}

void GameManager::addObstacle(std::shared_ptr<Obstacle> obstacle_ptr) {
obstacles.push_back(obstacle_ptr);
player->attach(obstacle_ptr); // Use shared_ptr instead of raw pointer
}

void GameManager::checkCollisions() {
if (player == nullptr) return;

for (const auto& obstacle_ptr : obstacles) {
Obstacle& obstacle = *obstacle_ptr;
if (player->get_x() < obstacle.get_x() + obstacle.get_width() &&
player->get_x() + player->get_width() > obstacle.get_x() &&
player->get_y() < obstacle.get_y() + obstacle.get_height() &&
player->get_y() + player->get_height() > obstacle.get_y())
{
// Notify player and obstacle about the collision
if (!obstacle.get_collision()) { // Collision started
player->notify(&obstacle, true);
Player::set_HP(Player::get_HP()-1); // Decrease player health on collision
}
draw_text("Collision detected!", COLOR_BLACK, "Arial", 24, player->get_x() + 10, player->get_y() - 50);
}
else {
if (obstacle.get_collision()) { // Collision ended
player->notify(&obstacle, false);
}
}
}
}

void GameManager::updateGameObjects() {
for (const auto& obstacle_ptr : obstacles) {
Obstacle& obstacle = *obstacle_ptr;
obstacle.update();
obstacle.draw();
}
}



24 changes: 24 additions & 0 deletions GameManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// GameManager.h
#ifndef GAMEMANAGER_H
#define GAMEMANAGER_H

#include "player.h"
#include "obstacle.h"
#include <vector>
#include <memory>

class GameManager {
public:
GameManager();
void addObstacle(std::shared_ptr<Obstacle> obstacle_ptr);
void setPlayer(Player* player);
void checkCollisions();
void updateGameObjects();
void clear_Obstacles(){obstacles.clear();};

private:
Player* player;
std::vector<std::shared_ptr<Obstacle>> obstacles;
};

#endif // GAMEMANAGER_H
12 changes: 0 additions & 12 deletions Observer.h

This file was deleted.

12 changes: 12 additions & 0 deletions RAIN.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RAIN.h
#ifndef RAIN_H
#define RAIN_H

class RAIN {
public:
virtual ~RAIN() {}
virtual void CollisionUpdate(bool is_collision)=0;
virtual void deceaseSpeed(int newSpeed)=0;
};

#endif // RAIN_H
16 changes: 0 additions & 16 deletions Subject.h

This file was deleted.

Binary file modified game.exe
Binary file not shown.
2 changes: 1 addition & 1 deletion obstacle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void Obstacle::CollisionUpdate(bool is_collision) {

void Obstacle::deceaseSpeed(int newSpeed){
this->speed = newSpeed;
std::cout << "The Speed equal to 2 now" << std::endl;
//std::cout << "The Speed equal to 2 now" << std::endl;
}


Expand Down
4 changes: 2 additions & 2 deletions obstacle.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#ifndef OBSTACLE_H
#define OBSTACLE_H

#include"Observer.h"
#include "RAIN.h"
#include "player.h"
class Obstacle : public Observer {
class Obstacle : public RAIN {
public:
Obstacle(float x, float y,int speed);
float get_x() { return x; }
Expand Down
35 changes: 18 additions & 17 deletions player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,31 @@ void Player::move_left() {

}

void Player::attach(Observer* observer) {
observers.push_back(observer);
void Player::attach(std::shared_ptr<RAIN> RAIN) {
RAINs.push_back(RAIN);
}

void Player::detach(Observer* observer) {
auto it = std::remove(observers.begin(), observers.end(), observer);
if (it != observers.end()) {
std::cout << "Detaching observer" << std::endl;
observers.erase(it, observers.end());
}
void Player::detach(RAIN* Rain) {
auto it = std::remove_if(RAINs.begin(), RAINs.end(),
[&Rain](const std::shared_ptr<RAIN>& o) {
return o.get() == Rain; // Compare the raw pointer
});
RAINs.erase(it, RAINs.end());
}


void Player::notify(Observer* observer, bool is_collision) {
observer->CollisionUpdate(is_collision); // Call onCollision on the observer, passing this obstacle
void Player::notify(RAIN* RAIN, bool is_collision) {
RAIN->CollisionUpdate(is_collision); // Call onCollision on the RAIN, passing this obstacle
}

void Player::notify_all_observers() {
std::cout << "Notifying all observers..." << std::endl;
for (Observer* observer : observers) {
if (observer == nullptr) {
std::cout << "Observer is null!" << std::endl;
continue; // Skip null observers
void Player::notify_all_RAINs() {
//std::cout << "Notifying all RAINs..." << std::endl;
for (auto& RAIN : RAINs) {
if (!RAIN) {
std::cout << "RAIN is null!" << std::endl;
continue; // Skip null RAINs
}
observer->deceaseSpeed(1);
RAIN->deceaseSpeed(1);
}
}

17 changes: 9 additions & 8 deletions player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
#ifndef PLAYER_H
#define PLAYER_H

#include "Subject.h"
#include "BEE_PLAYER.h"
#include "obstacle.h"
#include <vector>
#include "Observer.h"
#include "RAIN.h"
#include <memory>
class Player : public Subject {
class Player : public BEE_PLAYER {

public:
Player(float x, float y, float speed);
Expand All @@ -20,14 +20,15 @@ class Player : public Subject {
float get_speed() { return speed; }
static int get_HP(){return HP;}
static void set_HP(int hp){HP = hp;}
void attach(Observer* observer) override;
void detach(class Observer* observer) ;
void notify(class Observer* observer, bool is_collision);
void notify_all_observers();
void attach(std::shared_ptr<RAIN> RAIN) override;
void detach(class RAIN* Rain) ;
void notify(class RAIN* RAIN, bool is_collision);
void notify_all_RAINs();
private:
float x, y, speed, width, height;
static int HP;
std::vector<Observer*> observers;
std::vector<std::shared_ptr<RAIN>> RAINs;

};

#endif // PLAYER_H
Loading