diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a16cba..8877bca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,9 @@ add_executable(LotoStatek main.cpp headers/Size.h headers/Position.h headers/ObjectItem.hpp - sources/ObjectItem.cpp) + sources/ObjectItem.cpp + headers/Heart.hpp + sources/Heart.cpp) if(WIN32) set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML") diff --git a/headers/Heart.hpp b/headers/Heart.hpp new file mode 100644 index 0000000..ecaf8b6 --- /dev/null +++ b/headers/Heart.hpp @@ -0,0 +1,14 @@ +#ifndef LOTOSTATEK_HEART_HPP +#define LOTOSTATEK_HEART_HPP + +#include "SFML/Graphics/Texture.hpp" +#include "SFML/Graphics/Sprite.hpp" +#include "ObjectItem.hpp" + +class Heart : public ObjectItem { +public: + Heart(float x, float y, sf::Texture &texture); + void update(); +}; + +#endif //LOTOSTATEK_HEART_HPP diff --git a/headers/Plansza.h b/headers/Plansza.h index 0971823..76dc689 100644 --- a/headers/Plansza.h +++ b/headers/Plansza.h @@ -12,6 +12,7 @@ #include "AudioManager.h" #include "Meteor.h" #include "Plansza.h" +#include "Heart.hpp" class Plansza { public: @@ -19,7 +20,9 @@ public: Size getSize(); std::vector &getMeteors(); void spawn_meteor(); + void spawn_hearts(); void update_meteors(); + void update_hearts(); void update(); private: Size size; @@ -28,9 +31,13 @@ private: AudioManager audioManager; sf::Texture meteorTexture1; sf::Texture meteorTexture2; - sf::Clock spawnClock; + sf::Texture heartTexture; + sf::Clock meteorSpawnClock; + sf::Clock heartSpawnClock; std::vector meteors; + std::vector hearts; sf::RenderWindow *window; + }; #endif //PLANSZA_H diff --git a/sources/Heart.cpp b/sources/Heart.cpp new file mode 100644 index 0000000..4a61998 --- /dev/null +++ b/sources/Heart.cpp @@ -0,0 +1,23 @@ +#include "../headers/Heart.hpp" + +Heart::Heart(float x, float y, sf::Texture &texture) : ObjectItem(x,y,texture) { + outOfBounds = false; + texture = texture; + sprite.setTexture(texture); + sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite + sprite.setPosition(x, y); + movingSpeed = 3.0f; +// sprite.scale(0.05f, 0.05f); + position.x = x; + position.y = y; + rotationSpeed = 0; +} + +void Heart::update() { + sprite.move(0.0f, movingSpeed); // przesunięcie sprajta + position.y += int(movingSpeed); // przesunięcie pozycji +// sprite.rotate(rotationSpeed); // obracanie tym meteorkiem pięknym + if(position.y > 900) { + outOfBounds = true; // jeżeli wyszedł poza granice ekranu ustaw tą zmienną + } +} \ No newline at end of file diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 5183a91..93bd57a 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -24,7 +24,10 @@ ship(static_cast(mainWindow->getSize().x) / 2, static_cast(mainWindow- meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png"); meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png"); - spawnClock.restart(); + + heartTexture.loadFromFile("../assets/img/hearts/heart.png"); + + meteorSpawnClock.restart(); } @@ -67,14 +70,20 @@ void Plansza::update() { // generowanie nowego meteoru spawn_meteor(); + spawn_hearts(); // utrzymanie meteorów i pocisków w ruchu - for (auto& meteor : getMeteors()) { + for (auto& meteor : meteors) { meteor.update(); window->draw(meteor.getSprite()); } + for (auto& heart : hearts) { + heart.update(); + window->draw(heart.getSprite()); + } + for (auto& bullet : ship.getBullets()) { bullet.update(); window->draw(bullet.getSprite()); @@ -87,10 +96,12 @@ void Plansza::update() { // Sprawdzenie czy meteory i pociski są poza granicami ekranu update_meteors(); + update_hearts(); ship.updateBullets(); window->draw(ship.getSprite()); + // trochę dziwny sposób ale jednak działa for (auto& meteor : getMeteors()) { if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) { @@ -162,7 +173,7 @@ void Plansza::update() { // Meteor-related niżej void Plansza::spawn_meteor() { - if (spawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy + if (meteorSpawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy if (meteors.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy if(rand() % 2 == 1) { meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50,499), -100, meteorTexture2); @@ -170,7 +181,16 @@ void Plansza::spawn_meteor() { meteors.emplace_back(RandomNumberGenerator::getRandomNumber(50,499), -100, meteorTexture1); } } - spawnClock.restart(); + meteorSpawnClock.restart(); + } +} + +void Plansza::spawn_hearts() { + if (heartSpawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy + if (hearts.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy + hearts.emplace_back(RandomNumberGenerator::getRandomNumber(50, 499), -100, heartTexture); + } + heartSpawnClock.restart(); } } @@ -183,6 +203,15 @@ void Plansza::update_meteors() { } } +void Plansza::update_hearts() { + // usuwanie serduszek które wyleciały poza ekran + for (auto& heart : hearts) { + if(heart.getStatus()) { + hearts.erase(hearts.begin()); + } + } +} + Size Plansza::getSize() { return size; }