Skip to content
Merged
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
15 changes: 15 additions & 0 deletions code/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void Bullet::Display(const Pair& position, sf::RenderWindow& window, sf::FloatRe
}
}
void Bullet::Update(sf::Time dt,Pair player,sf::RenderWindow& window,sf::FloatRect& viewRect) {
lifetime-=dt.asSeconds();
physics.UpdatePosition(dt);
Display(player,window,viewRect);
}
Expand All @@ -28,7 +29,21 @@ Physics& Bullet::getPhysics() {
float Bullet::getDamage() const {
return damage;
}
float Bullet::getLifetime() const {
return lifetime;
}
const float Bullet::speed = 200.f;
Bullet &Bullet::operator=(const Bullet &other) {
if (this!=&other) {
shape = other.shape;
physics = other.physics;
collider = other.collider;
damage = other.damage;
radius = other.radius;
lifetime = other.lifetime;
}
return *this;
}
std::ostream& operator<<(std::ostream& out,const Bullet& bullet) {
out<<"BULLET\n";
out<<bullet.physics<<'\n';
Expand Down
9 changes: 6 additions & 3 deletions code/Bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ class Bullet {
sf::CircleShape shape;
Physics physics;
Collider collider;
const float damage=10;
const float radius =5;
float damage=10;
float radius =5;
float lifetime=10;
public:
static const float speed;
explicit Bullet(const Physics& physics) : collider(5.f) {
Expand All @@ -27,8 +28,10 @@ class Bullet {
sf::CircleShape& getShape();
Physics& getPhysics();
float getDamage() const;
void Update(sf::Time dt,Pair player,sf::RenderWindow& window,sf::FloatRect& viewRect);
float getLifetime() const;
void Update(sf::Time dt,Pair player,sf::RenderWindow& window,sf::FloatRect& viewRects);
void Display(const Pair& position, sf::RenderWindow& window, sf::FloatRect& viewRect);
Bullet &operator=(const Bullet &other);
friend std::ostream& operator<<(std::ostream& out,const Bullet& bullet);
};
#endif //OOP_BULLET_H
7 changes: 5 additions & 2 deletions code/SpaceShip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,11 @@ void SpaceShip::alignToPlanet(const Physics& planetPhys) {

}
void SpaceShip::UpdateBullets(sf::Time dt,sf::RenderWindow& window,sf::FloatRect& viewRect) {
for (auto& i:bullets) {
i.Update(dt,getPhysics().getPosition(),window,viewRect);
for (int i=0;i<bullets.size();i++) {
bullets[i].Update(dt,getPhysics().getPosition(),window,viewRect);
if (bullets[i].getLifetime()<0) {
bullets.erase(bullets.begin()+i);
}
}
}
Collider SpaceShip::getCollider() {
Expand Down
Loading