diff --git a/CMakeLists.txt b/CMakeLists.txt index 10ceef7..419e8b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,7 +19,11 @@ add_executable(LotoStatek main.cpp sources/Bullet.cpp headers/Meteor.h sources/Meteor.cpp - headers/RandomNumberGenerator.h) + headers/RandomNumberGenerator.h + headers/Projectile.h + sources/Projectile.cpp + headers/Rocket.h + sources/Rocket.cpp) if(WIN32) set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML") diff --git a/headers/Bullet.h b/headers/Bullet.h index 3050af6..d6bc301 100644 --- a/headers/Bullet.h +++ b/headers/Bullet.h @@ -1,28 +1,13 @@ #ifndef LOTOSTATEK_BULLET_H #define LOTOSTATEK_BULLET_H -#include "SFML/Graphics/Sprite.hpp" +#include "Projectile.h" #include "SFML/Graphics/Texture.hpp" -class Bullet { - struct Position { - int x; - int y; - }; - +class Bullet : public Projectile { public: - Bullet(float x, float y); - sf::Sprite &getSprite(); - void setSpeed(float speed); - bool getStatus() const; - void update(); - static sf::Texture bulletTexture; - static sf::Texture rocketTexture; -private: - sf::Sprite bulletSprite; - Position bulletPosition; - float bulletSpeed; - bool outOfBounds; + Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {}; + void update() override; }; diff --git a/headers/Player.h b/headers/Player.h index 8138296..e6b4c01 100644 --- a/headers/Player.h +++ b/headers/Player.h @@ -4,6 +4,7 @@ #include #include "Actor.h" +#include "Rocket.h" class Player : public Actor { public: @@ -16,10 +17,12 @@ public: void moveRight() override; void moveUp() override; void moveDown() override; + std::vector& getRockets(); private: std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now(); - std::vector rightBullets; - + std::vector rockets; + sf::Texture rocketTexture; + sf::Texture bulletTexture; }; diff --git a/headers/Projectile.h b/headers/Projectile.h new file mode 100644 index 0000000..5b15978 --- /dev/null +++ b/headers/Projectile.h @@ -0,0 +1,27 @@ +#ifndef PROJECTILE_H +#define PROJECTILE_H + +#include + +class Projectile { + struct Position { + int x; + int y; + }; + +public: + Projectile(float x, float y, sf::Texture &texture); + sf::Sprite &getSprite(); + void setSpeed(float speed); + bool isOutOfBounds() const; + virtual void update() = 0; + static sf::Texture texture; +protected: + ~Projectile() = default; + sf::Sprite sprite; + Position position{}; + float speed; + bool outOfBounds; +}; + +#endif //PROJECTILE_H diff --git a/headers/Rocket.h b/headers/Rocket.h new file mode 100644 index 0000000..37503f7 --- /dev/null +++ b/headers/Rocket.h @@ -0,0 +1,14 @@ +#ifndef ROCKET_H +#define ROCKET_H +#include "Projectile.h" + + +class Rocket : public Projectile{ +public: + Rocket(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {}; + void update() override; +}; + + + +#endif //ROCKET_H diff --git a/main.cpp b/main.cpp index 03aef75..417c87c 100644 --- a/main.cpp +++ b/main.cpp @@ -102,6 +102,11 @@ int main() mainWindow.draw(bullet.getSprite()); } + for (auto& rocket : ship.getRockets()) { + rocket.update(); + mainWindow.draw(rocket.getSprite()); + } + // Sprawdzenie czy meteory i pociski są poza granicami ekranu plansza.update_meteors(); ship.updateBullets(); @@ -143,14 +148,31 @@ int main() for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ) { bool meteorHit = false; - for (auto bulletIt = ship.getBullets().begin(); bulletIt != ship.getBullets().end(); ) { - if (meteorIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { - bulletIt = ship.getBullets().erase(bulletIt); + for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end(); ) { + if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { + ship.getBullets().erase(rocketIt); meteorIt = plansza.getMeteors().erase(meteorIt); meteorHit = true; break; } else { - ++bulletIt; + ++rocketIt; + } + } + if (!meteorHit) { + ++meteorIt; + } + } + + for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ) { + bool meteorHit = false; + for (auto rocketIt = ship.getRockets().begin(); rocketIt != ship.getRockets().end(); ) { + if (meteorIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { + ship.getRockets().erase(rocketIt); + meteorIt = plansza.getMeteors().erase(meteorIt); + meteorHit = true; + break; + } else { + ++rocketIt; } } if (!meteorHit) { diff --git a/sources/Actor.cpp b/sources/Actor.cpp index c6a785d..d454bad 100644 --- a/sources/Actor.cpp +++ b/sources/Actor.cpp @@ -30,7 +30,7 @@ std::vector &Actor::getBullets() { void Actor::updateBullets() { for (auto& bullet : bullets) { - if(bullet.getStatus()) { + if(bullet.isOutOfBounds()) { bullets.erase(bullets.begin()); } } diff --git a/sources/Bullet.cpp b/sources/Bullet.cpp index d2a362e..17ff7a2 100644 --- a/sources/Bullet.cpp +++ b/sources/Bullet.cpp @@ -1,35 +1,9 @@ -#include #include "../headers/Bullet.h" -Bullet::Bullet(float x, float y) { - bulletPosition.x = x; - bulletPosition.y = y; - outOfBounds = false; - bulletSprite.setTexture(Bullet::bulletTexture); - bulletSprite.setOrigin(bulletSprite.getLocalBounds().width/2, bulletSprite.getLocalBounds().height/2); - bulletSprite.setPosition(x, y); - bulletSpeed = -10.0f; -} - -void Bullet::setSpeed(float speed) { - bulletSpeed = speed; -} - -sf::Sprite &Bullet::getSprite() { - return bulletSprite; -} - void Bullet::update() { - bulletSprite.move(0.0f, bulletSpeed); - bulletPosition.y += int(bulletSpeed); - if(bulletPosition.y < -100) { + sprite.move(0.0f, speed); + position.y += int(speed); + if(position.y < -100) { outOfBounds = true; } -} - -bool Bullet::getStatus() const { - return outOfBounds; -} - -sf::Texture Bullet::bulletTexture = sf::Texture(); // plain init of static field -sf::Texture Bullet::rocketTexture = sf::Texture(); +} \ No newline at end of file diff --git a/sources/Player.cpp b/sources/Player.cpp index aad1f56..7a15507 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -3,22 +3,22 @@ // TODO: Podzielić na klasy Bullet i Rocket. Player::Player(int x, int y, std::string path) : Actor(x, y, path) { - Bullet::bulletTexture.loadFromFile("../assets/img/bullet_left.png"); + bulletTexture.loadFromFile("../assets/img/bullet_left.png"); + rocketTexture.loadFromFile("../assets/img/Rocket_111.png"); }; void Player::shoot() { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { - bullets.emplace_back(position.x, position.y); + bullets.emplace_back(position.x, position.y, bulletTexture); lastShotTime = now; } } -// TODO: Czy strzał prawym musi mieć osobną tablicę z pociskami? void Player::alternate_shoot() { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { - rightBullets.emplace_back(position.x, position.y).getSprite().scale(1.5f, 1.5f); + rockets.emplace_back(position.x, position.y, rocketTexture).getSprite().scale(1.5f, 1.5f); lastShotTime = now; } } @@ -49,4 +49,8 @@ void Player::moveUp() { void Player::moveDown() { move(0.0f, moving_speed); position.y += static_cast(moving_speed); -} \ No newline at end of file +} + +std::vector &Player::getRockets() { + return rockets; +} diff --git a/sources/Projectile.cpp b/sources/Projectile.cpp new file mode 100644 index 0000000..ba26080 --- /dev/null +++ b/sources/Projectile.cpp @@ -0,0 +1,23 @@ +#include "../headers/Projectile.h" + +Projectile::Projectile(float x, float y, sf::Texture &texture) { + position.x = x; + position.y = y; + outOfBounds = false; + sprite.setTexture(texture); + sprite.setOrigin(sprite.getLocalBounds().width/2, sprite.getLocalBounds().height/2); + sprite.setPosition(x, y); + speed = -10.0f; +} + +sf::Sprite &Projectile::getSprite() { + return sprite; +} + +void Projectile::setSpeed(float speed) { + this->speed = speed; +} + +bool Projectile::isOutOfBounds() const { + return outOfBounds; +} diff --git a/sources/Rocket.cpp b/sources/Rocket.cpp new file mode 100644 index 0000000..a5ae223 --- /dev/null +++ b/sources/Rocket.cpp @@ -0,0 +1,9 @@ +#include "../headers/Rocket.h" + +void Rocket::update() { + sprite.move(0.0f, speed); + position.y += int(speed); + if(position.y < -100) { + outOfBounds = true; + } +} \ No newline at end of file