Skip to content
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
Binary file modified .vscode/.BROWSE.VC.DB
Binary file not shown.
Binary file modified .vscode/.BROWSE.VC.DB-shm
Binary file not shown.
4 changes: 3 additions & 1 deletion Observer.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// observer.h
#ifndef OBSERVER_H
#define OBSERVER_H
#include "obstacle.h"

class Obstacle;

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

#endif // OBSERVER_H
112 changes: 0 additions & 112 deletions Observer_pattern_tutorial/README.md

This file was deleted.

63 changes: 63 additions & 0 deletions bullet_factory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "bullet_factory.h"
#include "globals.h"
#include <cmath>
#include <iostream>

std::vector<sprite> BulletFactory::SprayProjectiles(ProjectileType type, point_2d origin, vector_2d direction, int count, float spread) {
std::vector<sprite> bullets;

float angleStep = spread / (count - 1);
float startAngle = -spread / 2;

for (int i = 0; i < count; i++) {
float angle = startAngle + i * angleStep;
vector_2d rotatedDirection = RotateVector(direction, angle);

sprite bullet = CreateBullet(type, origin, rotatedDirection);
if (bullet) {
bullets.push_back(bullet);
std::cout << "Created bullet at (" << sprite_x(bullet) << ", " << sprite_y(bullet) << ")" << std::endl;
}
}

return bullets;
}

sprite BulletFactory::CreateBullet(ProjectileType type, point_2d origin, vector_2d direction) {
if (!bitmap_valid(bullet)) {
std::cout << "Error: Could not load bullet bitmap in CreateBullet" << std::endl;
return nullptr;
}

sprite bullet_sprite = create_sprite(bullet);

// Scale the sprite
sprite_set_scale(bullet_sprite,0.2); // Scale by 50%

sprite_set_position(bullet_sprite, origin);

vector_2d velocity;
switch (type) {
case ProjectileType::NORMAL:
velocity = vector_multiply(direction, 5);
break;
case ProjectileType::FAST:
velocity = vector_multiply(direction, 10);
break;
case ProjectileType::EXPLOSIVE:
velocity = vector_multiply(direction, 3);
break;
}
sprite_set_velocity(bullet_sprite, velocity);

return bullet_sprite;
}

vector_2d BulletFactory::RotateVector(vector_2d vec, float angle) {
float cos_a = std::cos(angle);
float sin_a = std::sin(angle);
return vector_2d{
vec.x * cos_a - vec.y * sin_a,
vec.x * sin_a + vec.y * cos_a
};
}
22 changes: 22 additions & 0 deletions bullet_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef BULLET_FACTORY_H
#define BULLET_FACTORY_H

#include "splashkit.h"
#include <vector>

enum class ProjectileType {
NORMAL,
FAST,
EXPLOSIVE
};

class BulletFactory {
public:
static std::vector<sprite> SprayProjectiles(ProjectileType type, point_2d origin, vector_2d direction, int count, float spread);

private:
static sprite CreateBullet(ProjectileType type, point_2d origin, vector_2d direction);
static vector_2d RotateVector(vector_2d vec, float angle);
};

#endif // BULLET_FACTORY_H
Binary file modified game.exe
Binary file not shown.
6 changes: 2 additions & 4 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
extern bitmap background;
extern bitmap bee;
extern bitmap box;
extern bitmap bullet;
extern float player_posx;
extern float player_posy;
extern int RIGHT_BOUNDARY ;
extern int LEFT_BOUNDARY ;
extern int GRAVITY;
extern int WINDOW_WIDTH ;
extern int WINDOW_HEIGHT ;
extern int spawn_interval;
extern float BEE_SCALE;
extern int spawn_interval;
Binary file added images/bullet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 0 additions & 5 deletions obstacle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,5 @@ void Obstacle::CollisionUpdate(bool is_collision) {

}

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



2 changes: 1 addition & 1 deletion obstacle.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Obstacle : public Observer {
void update();
void draw();
void CollisionUpdate(bool is_collision);
void deceaseSpeed(int newSpeed);


private:
float x, y, width, height, speed;
Expand Down
29 changes: 4 additions & 25 deletions player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
#include "globals.h"
#include <iostream>
#include <algorithm>
#include <memory>
#include <vector>

Player::Player(float x, float y, float speed) {
this->x = x;
this->y = y;
this->speed = speed;
this->width = bitmap_width(bee)*BEE_SCALE; // Assuming 'bee' is the bitmap for the player
this->height = bitmap_height(bee)*BEE_SCALE;
this->width = bitmap_width(bee); // Assuming 'bee' is the bitmap for the player
this->height = bitmap_height(bee);
}

int Player::HP = 3; // Initialize the static HP variable

void Player::move_right() {

x += speed;
Expand All @@ -32,25 +27,9 @@ void Player::attach(Observer* observer) {
}

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());
}
observers.erase(std::remove(observers.begin(), observers.end(), observer), observers.end());
}


void Player::notify(Observer* observer, bool is_collision) {
observer->CollisionUpdate(is_collision); // Call onCollision on the observer, 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
}
observer->deceaseSpeed(1);
}
}
}
9 changes: 2 additions & 7 deletions player.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include "obstacle.h"
#include <vector>
#include "Observer.h"
#include <memory>
class Player : public Subject {

public:
Player(float x, float y, float speed);
void move_right();
Expand All @@ -18,15 +16,12 @@ class Player : public Subject {
float get_width() { return width; }
float get_height() { return height; }
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 attach(class Observer* observer) ;
void detach(class Observer* observer) ;
void notify(class Observer* observer, bool is_collision);
void notify_all_observers();

private:
float x, y, speed, width, height;
static int HP;
std::vector<Observer*> observers;
};

Expand Down
Loading