diff --git a/CMakeLists.txt b/CMakeLists.txt index c9b5e55..b71462b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,9 @@ add_executable(LotoStatek main.cpp sources/Player.cpp headers/Player.h headers/Bullet.h - sources/Bullet.cpp) + sources/Bullet.cpp + headers/Meteor.h + sources/Meteor.cpp) if(WIN32) set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML") diff --git a/assets/img/meteor.png b/assets/img/meteor.png new file mode 100644 index 0000000..512c494 Binary files /dev/null and b/assets/img/meteor.png differ diff --git a/headers/Bullet.h b/headers/Bullet.h index e4c34d4..5829536 100644 --- a/headers/Bullet.h +++ b/headers/Bullet.h @@ -11,7 +11,7 @@ class Bullet { }; public: - Bullet(float x, float y, sf::Texture &bulletTexture); + Bullet(float x, float y, sf::Texture &texture); void update(); sf::Sprite& getSprite(); void setSpeed(float speed); diff --git a/headers/Meteor.h b/headers/Meteor.h new file mode 100644 index 0000000..af6e1b3 --- /dev/null +++ b/headers/Meteor.h @@ -0,0 +1,26 @@ +#ifndef LOTOSTATEK_METEOR_H +#define LOTOSTATEK_METEOR_H + +#include "SFML/Graphics/Texture.hpp" +#include "SFML/Graphics/Sprite.hpp" + +class Meteor { + struct Position { + int x; + int y; + }; +public: + Meteor(int x, int y, sf::Texture &texture); + sf::Sprite & getSprite(); + bool getStatus(); + void update(); +private: + sf::Texture meteorTexture; + sf::Sprite meteorSprite; + Position position; + float meteorSpeed; + bool outOfBounds; +}; + + +#endif //LOTOSTATEK_METEOR_H diff --git a/main.cpp b/main.cpp index 6e883d3..54ef343 100644 --- a/main.cpp +++ b/main.cpp @@ -1,9 +1,9 @@ #include -#include +#include #include "SFML/Graphics.hpp" #include "headers/Player.h" -#include "headers/Bullet.h" +#include "headers/Meteor.h" int main() { @@ -12,6 +12,15 @@ int main() window.setVerticalSyncEnabled(true); window.setFramerateLimit(60); + // Ustawienia randomizera + std::random_device rd; + std::mt19937 gen(rd()); + std::uniform_int_distribution<> dis(0, 599); + // Koniec ustawień randomizera + + sf::Texture meteorTexture; + meteorTexture.loadFromFile("../assets/img/meteor.png"); + sf::Texture backgroundTexture; backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła @@ -20,6 +29,9 @@ int main() ship.setMovingSpeed(8); ship.setFirerate(200); + std::vector meteors; + std::srand(static_cast(std::time(nullptr))); + while (window.isOpen()) { window.clear(); @@ -33,16 +45,6 @@ int main() if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { window.close(); } - if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { - ship.shoot(); - } - } - - if(event.type == sf::Event::MouseButtonPressed) { - if(event.mouseButton.button == sf::Mouse::Left) - ship.shoot(); - else - ship.alternate_shoot(); } if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { @@ -50,11 +52,6 @@ int main() ship.moveLeft(); } } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { - if(ship.getPosition().x < 480) { - ship.moveRight(); - } - } if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { if(ship.getPosition().y > 0) { ship.moveUp(); @@ -65,6 +62,40 @@ int main() ship.moveDown(); } } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { + if(ship.getPosition().x < 480) { + ship.moveRight(); + } + } + if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) { + ship.shoot(); + } + + + if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) ship.shoot(); + if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) ship.alternate_shoot(); + + + // TODO: Meteory na jednym poziomie ze statkiem + // TODO: Kolizje + // Generate a new meteor at a random position at the top of the screen + if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) { + int randomX = dis(gen); + meteors.emplace_back(randomX, -100, meteorTexture); + } + + // Update and draw meteors + for (auto& meteor : meteors) { + meteor.update(); + window.draw(meteor.getSprite()); + } + + // Remove meteors that are out of bounds + for (auto& meteor : meteors) { + if(meteor.getStatus()) { + meteors.erase(meteors.begin()); + } + } for (auto& bullet : ship.getBullets()) { bullet.update(); @@ -73,6 +104,7 @@ int main() ship.updateBullets(); window.draw(ship.getSprite()); + window.display(); } diff --git a/sources/Bullet.cpp b/sources/Bullet.cpp index c897f7d..3f92a17 100644 --- a/sources/Bullet.cpp +++ b/sources/Bullet.cpp @@ -1,9 +1,10 @@ #include #include "../headers/Bullet.h" -Bullet::Bullet(float x, float y, sf::Texture &bulletTexture) { +Bullet::Bullet(float x, float y, sf::Texture &texture) { outOfBounds = false; - bulletSprite.setTexture(bulletTexture); + bulletTexture = texture; + bulletSprite.setTexture(texture); bulletSprite.setPosition(x, y); bulletSpeed = -10.0f; bulletPosition.x = x; diff --git a/sources/Meteor.cpp b/sources/Meteor.cpp new file mode 100644 index 0000000..e587b92 --- /dev/null +++ b/sources/Meteor.cpp @@ -0,0 +1,30 @@ +#include +#include "../headers/Meteor.h" + +Meteor::Meteor(int x, int y, sf::Texture &texture) { + position.x = x; + position.y = y; + outOfBounds = false; + meteorTexture = texture; + meteorSprite.setTexture(texture); + meteorSpeed = 10.0f; + meteorSprite.setPosition(x, y); + meteorSprite.scale(0.05f, 0.05f); +} + +sf::Sprite &Meteor::getSprite() { + return meteorSprite; +} + +void Meteor::update() { + meteorSprite.move(0.0f, meteorSpeed); + position.y += int(meteorSpeed); + if(position.y > 900) { + outOfBounds = true; + } +} + +bool Meteor::getStatus() { + return outOfBounds; +} +