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
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.
1 change: 1 addition & 0 deletions globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern bitmap background;
extern bitmap bee;
extern bitmap box;
extern bitmap bullet;
extern float player_posx;
extern float player_posy;
extern int RIGHT_BOUNDARY ;
Expand Down
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.
Binary file added images/pollen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/raindrop2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion obstacle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ void Obstacle::update(){
}

void Obstacle::draw() {
draw_bitmap(box, x, y, option_to_screen());
double center_x = this->x + this->width/2;
double center_y = this->y + this->height/2;
point_2d box_position = point_at(center_x,center_y);
circle scaled_box_circle = bitmap_cell_circle(box, box_position,1);
draw_bitmap(box, x, y, option_to_screen());
draw_circle(COLOR_RED,scaled_box_circle);
}

void Obstacle::CollisionUpdate(bool is_collision) {
Expand Down
10 changes: 6 additions & 4 deletions program.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
// import
#include "splashkit.h"
#include "globals.h" // <- added this import
//#include <graphics.h>
#include <cstdlib>
#include "player.h"
#include "obstacle.h"
#include "Observer.h"
#include "Subject.h"
#include <iostream>
#include <memory>
//skm g++ program.cpp player.cpp obstacle.cpp -o game.exe
#include "bullet_factory.h"
#include <vector>
#include <cmath>
//skm g++ program.cpp player.cpp obstacle.cpp bullet_factory.cpp -o game.exe

bitmap background = bitmap_named("images/Background.jpg");
bitmap bee = bitmap_named("images/Bee.png");
bitmap box = bitmap_named("images/box.png");
bitmap box = bitmap_named("images/raindrop2.png");
bitmap bullet = bitmap_named("images/pollen.png");
float player_posx = 550.0f;
float player_posy = 650.0f;
int RIGHT_BOUNDARY = 1200;
Expand Down