diff --git a/bullet_factory.cpp b/bullet_factory.cpp new file mode 100644 index 0000000..449db0a --- /dev/null +++ b/bullet_factory.cpp @@ -0,0 +1,63 @@ + #include "bullet_factory.h" + #include "globals.h" + #include + #include + + std::vector BulletFactory::SprayProjectiles(ProjectileType type, point_2d origin, vector_2d direction, int count, float spread) { + std::vector 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 + }; + } \ No newline at end of file diff --git a/bullet_factory.h b/bullet_factory.h new file mode 100644 index 0000000..9d3d55a --- /dev/null +++ b/bullet_factory.h @@ -0,0 +1,22 @@ +#ifndef BULLET_FACTORY_H +#define BULLET_FACTORY_H + +#include "splashkit.h" +#include + +enum class ProjectileType { + NORMAL, + FAST, + EXPLOSIVE +}; + +class BulletFactory { +public: + static std::vector 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 \ No newline at end of file diff --git a/game.exe b/game.exe index 0dee3e1..5e00ee8 100644 Binary files a/game.exe and b/game.exe differ diff --git a/globals.h b/globals.h index fbdcb68..a976510 100644 --- a/globals.h +++ b/globals.h @@ -4,12 +4,14 @@ 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 spawn_interval; extern int WINDOW_WIDTH ; extern int WINDOW_HEIGHT ; extern int spawn_interval; -extern float BEE_SCALE; \ No newline at end of file +extern float BEE_SCALE; diff --git a/images/bullet.png b/images/bullet.png new file mode 100644 index 0000000..e2bc886 Binary files /dev/null and b/images/bullet.png differ diff --git a/program.cpp b/program.cpp index 9c36c87..4538c31 100644 --- a/program.cpp +++ b/program.cpp @@ -1,25 +1,29 @@ // import #include "splashkit.h" #include "globals.h" // <- added this import -//#include #include #include "player.h" #include "obstacle.h" #include "Observer.h" #include "Subject.h" #include +#include #include -//skm g++ program.cpp player.cpp obstacle.cpp -o game.exe +#include +// Bitmap declarations bitmap background = bitmap_named("images/Background.jpg"); bitmap bee = bitmap_named("images/Bee.png"); bitmap box = bitmap_named("images/box.png"); -float player_posx = 550.0f; -float player_posy = 650.0f; -int RIGHT_BOUNDARY = 1200; +bitmap bullet = bitmap_named("images/bullet.png"); + +// Game settings +float player_posx = 480.0f; +float player_posy = 550.0f; +int RIGHT_BOUNDARY = 1020; int LEFT_BOUNDARY = 0; int GRAVITY = 3; -int spawn_interval = 60;// Spawn obstacles at a rate of 1 per second +int spawn_interval = 60; // Spawn obstacles at a rate of 1 per second int WINDOW_WIDTH = 1280; int WINDOW_HEIGHT = 960; float BEE_SCALE = 0.6; @@ -37,7 +41,7 @@ void display_start_screen(); void player_move(Player* player); void Spawn_obstacle(std::vector>& obstacles, Player* player, int& spawn_timer); void render(std::vector>& obstacles, Player& player); -void check_game_over(std::vector>& obstacles,Player& player); +void check_game_over(std::vector>& obstacles, Player& player); void display_game_over_screen(); void start_game() { @@ -60,15 +64,13 @@ void display_timer() { } void display_start_screen() { - draw_text("Press SPACE to Start", COLOR_BLACK, "Arial", 200, 550, 200); } -void check_game_over(std::vector>& obstacles,Player& player) { +void check_game_over(std::vector>& obstacles, Player& player) { if (Player::get_HP() == 1) { player.notify_all_observers(); - } - else if (Player::get_HP() <= 0) { + } else if (Player::get_HP() <= 0) { game_over = true; game_started = false; // Stop the game obstacles.clear(); @@ -132,22 +134,11 @@ void Spawn_obstacle(std::vector>& obstacles, Player* p } } - void render(std::vector>& obstacles, 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); draw_bitmap(background, 0, 0, option_to_screen()); - drawing_options scale_options = option_scale_bmp(BEE_SCALE+0.1, BEE_SCALE+0.1); // Scale to 70% of original size - draw_bitmap(bee, player.get_x()-50, player.get_y()-50,scale_options); - - // Get the circle that encompasses the scaled bitmap - - point_2d bee_position = point_at(center_x,center_y); - circle scaled_bee_circle = bitmap_cell_circle(bee, bee_position,BEE_SCALE); - - // Draw the circle for debugging - draw_circle(COLOR_RED,scaled_bee_circle); + drawing_options scale_options = option_scale_bmp(BEE_SCALE + 0.1, BEE_SCALE + 0.1); // Scale to 70% of original size + draw_bitmap(bee, player.get_x(), player.get_y(), scale_options); // Update and draw obstacles for (const auto& obstacle_ptr : obstacles) { @@ -159,7 +150,6 @@ void render(std::vector>& obstacles, Player& player) { } } - 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 @@ -178,11 +168,10 @@ int main() { if (!game_started) { draw_bitmap(background, 0, 0, option_to_screen()); - drawing_options scale_options = option_scale_bmp(BEE_SCALE+0.1, BEE_SCALE+0.1); // Scale to 40% of original size - draw_bitmap(bee, player.get_x()-50, player.get_y()-50,scale_options); + drawing_options scale_options = option_scale_bmp(BEE_SCALE + 0.1, BEE_SCALE + 0.1); // Scale to 40% of original size + draw_bitmap(bee, player.get_x() - 50, player.get_y() - 50, scale_options); display_start_screen(); if (key_down(SPACE_KEY)) { - start_game(); } refresh_screen(60); @@ -190,9 +179,7 @@ int main() { } if (game_over) { - display_game_over_screen(); - if (key_down(SPACE_KEY)) { start_game(); // Restart game } @@ -211,7 +198,7 @@ int main() { // Update game elements update_timer(); display_timer(); - check_game_over(obstacles,player); + check_game_over(obstacles, player); refresh_screen(60); }