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

Tutorial combining design patterns #10

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
37 changes: 26 additions & 11 deletions GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,43 @@ void GameManager::setPlayer(Player* player) {
this->player = player;
}

void GameManager::addObstacle(Obstacle* obstacle) {
obstacles.push_back(std::move(obstacle));
player->attach(obstacle.get()); // Attach the newly created obstacle
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 (auto* obstacle : obstacles) {
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())
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
player->notify(obstacle, true); // The player has collided with the obstacle
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 (auto* obstacle : obstacles) {
obstacle->update();
for (const auto& obstacle_ptr : obstacles) {
Obstacle& obstacle = *obstacle_ptr;
obstacle.update();
obstacle.draw();
}
}



5 changes: 3 additions & 2 deletions GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
class GameManager {
public:
GameManager();
void addObstacle(Obstacle* obstacle);
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::unique_ptr<Obstacle>> obstacles;
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
77 changes: 21 additions & 56 deletions program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <cstdlib>
#include "player.h"
#include "obstacle.h"
#include "Observer.h"
#include "Subject.h"
#include "RAIN.h"
#include "BEE_PLAYER.h"
#include <iostream>
#include <memory>
#include "GameManager.h"
//skm g++ program.cpp player.cpp obstacle.cpp -o game.exe
//skm g++ program.cpp player.cpp obstacle.cpp GameManager.cpp -o game.exe

bitmap background = bitmap_named("images/Background.jpg");
bitmap bee = bitmap_named("images/Bee.png");
Expand All @@ -36,9 +36,9 @@ void update_timer();
void display_timer();
void display_start_screen();
void player_move(Player* player);
void Spawn_obstacle(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player* player, int& spawn_timer);
void render(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player);
void check_game_over(std::vector<std::unique_ptr<Obstacle>>& obstacles,Player& player);
void Spawn_obstacle(GameManager& gameManager, int& spawn_timer);
void render(GameManager& gameManager, Player& player);
void check_game_over(GameManager& gameManager,Player& player);
void display_game_over_screen();

void start_game() {
Expand All @@ -65,14 +65,14 @@ void display_start_screen() {
draw_text("Press SPACE to Start", COLOR_BLACK, "Arial", 200, 550, 200);
}

void check_game_over(std::vector<std::unique_ptr<Obstacle>>& obstacles,Player& player) {
void check_game_over(GameManager& gameManager,Player& player) {
if (Player::get_HP() == 1) {
player.notify_all_observers();
player.notify_all_RAINs();
}
else if (Player::get_HP() <= 0) {
game_over = true;
game_started = false; // Stop the game
obstacles.clear();
gameManager.clear_Obstacles();
}
}

Expand All @@ -81,38 +81,6 @@ void display_game_over_screen() {
draw_text("Press SPACE to Restart", COLOR_WHITE, "Arial", 32, 510, 500);
}

template <typename T, typename U>
bool is_colliding(T& obj1, U& obj2) {
float x1 = obj1.get_x();
float y1 = obj1.get_y();
float w1 = obj1.get_width();
float h1 = obj1.get_height();

float x2 = obj2.get_x();
float y2 = obj2.get_y();
float w2 = obj2.get_width();
float h2 = obj2.get_height();

// Check for collision
return (x1 < x2 + w2 && x1 + w1 > x2 && y1 < y2 + h2 && y1 + h1 > y2);
}

// Function to handle collision between two objects
template <typename T, typename U>
void handle_collision(T& subject, U& observer) {
if (is_colliding(subject, observer)) {
if (!observer.get_collision()) { // Collision started
subject.notify(&observer, true);
Player::set_HP(Player::get_HP()-1); // Decrease player health on collision
}
draw_text("Collision detected!", COLOR_BLACK, "Arial", 24, subject.get_x() + 10, subject.get_y() - 50);
} else {
if (observer.get_collision()) { // Collision ended
subject.notify(&observer, false);
}
}
}

void player_move(Player* player) {
if (key_down(RIGHT_KEY) && player->get_x() <= RIGHT_BOUNDARY) {
player->move_right();
Expand All @@ -127,13 +95,14 @@ void Spawn_obstacle(GameManager& gameManager, int& spawn_timer) {
if (spawn_timer >= spawn_interval) {
spawn_timer = 0;
int spawn_x = rand() % RIGHT_BOUNDARY; // Random x-coordinate between 0 and RIGHT_BOUNDARY
auto newObstacle = std::make_unique<Obstacle>(spawn_x, 0, 2);
gameManager.addObstacle(newObstacle);
std::shared_ptr<Obstacle> obstacle = std::make_shared<Obstacle>(spawn_x, 0, 2);
gameManager.addObstacle(obstacle);

}
}


void render(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player) {
void render(GameManager& gameManager,Player& player) {
// Redrawing the bitmap after every clear background and bee
double center_x = player.get_x()+(player.get_width()/2);
double center_y = player.get_y()+(player.get_height()/2);
Expand All @@ -149,22 +118,14 @@ void render(std::vector<std::unique_ptr<Obstacle>>& obstacles, Player& player) {
// Draw the circle for debugging
draw_circle(COLOR_RED,scaled_bee_circle);

// Update and draw obstacles
for (const auto& obstacle_ptr : obstacles) {
// Dereference the unique_ptr to access the Obstacle object
Obstacle& obstacle = *obstacle_ptr;
obstacle.update();
obstacle.draw();
handle_collision(player, obstacle);
}
gameManager.updateGameObjects();
}


int main() {
open_window("BeeFall", WINDOW_WIDTH, WINDOW_HEIGHT); // Named window beefall and window size
hide_mouse(); // Hide mouse while cursor is over the game window
Player player(player_posx, player_posy, 10.0f); // Initialize player
std::vector<std::unique_ptr<Obstacle>> obstacles; // List of obstacles
// Create game manager and add game objects
GameManager gameManager;
gameManager.setPlayer(&player);
Expand Down Expand Up @@ -206,15 +167,19 @@ int main() {
player_move(&player);

// Spawn obstacles
Spawn_obstacle(obstacles, &player, spawn_timer);
Spawn_obstacle(gameManager, spawn_timer);



// Render game objects
render(obstacles, player);
render(gameManager,player);

gameManager.checkCollisions();
// Update game elements

update_timer();
display_timer();
check_game_over(obstacles,player);
check_game_over(gameManager,player);

refresh_screen(60);
}
Expand Down
Loading