From 2bdfbb193c8161f0a4261f03429edd63b582dbac Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Thu, 21 Nov 2024 15:50:25 +0100 Subject: [PATCH] =?UTF-8?q?Multiple=20fixes=20Obracaj=C4=85ce=20si=C4=99?= =?UTF-8?q?=20meteoryty,=20koniec=20gry=20przy=20natrafieniu=20na=20meteor?= =?UTF-8?q?yt.=20Poprawione=20sprajty=20statku,=20meteorytu=20i=20pocisku?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- headers/Actor.h | 4 +--- headers/Meteor.h | 1 + headers/Plansza.h | 2 +- main.cpp | 21 ++++++++++++++++++++- sources/Actor.cpp | 6 +++++- sources/Bullet.cpp | 1 + sources/Meteor.cpp | 13 +++++++++---- sources/Plansza.cpp | 6 ++---- sources/Player.cpp | 4 ++-- 9 files changed, 42 insertions(+), 16 deletions(-) diff --git a/headers/Actor.h b/headers/Actor.h index 70319be..56b704a 100644 --- a/headers/Actor.h +++ b/headers/Actor.h @@ -31,9 +31,7 @@ public: void updateBullets(); - void setMovingSpeed(float speed) { - moving_speed = speed; - } + void setMovingSpeed(float speed); protected: sf::Sprite actorSprite; diff --git a/headers/Meteor.h b/headers/Meteor.h index 44b5e0d..8b42356 100644 --- a/headers/Meteor.h +++ b/headers/Meteor.h @@ -20,6 +20,7 @@ private: sf::Texture meteorTexture; sf::Sprite meteorSprite; Position meteorPosition; + float meteorRotationSpeed; float meteorSpeed; bool outOfBounds; static unsigned int counter; diff --git a/headers/Plansza.h b/headers/Plansza.h index b793ac9..c8e0915 100644 --- a/headers/Plansza.h +++ b/headers/Plansza.h @@ -12,7 +12,7 @@ class Plansza { int width; }; public: - Plansza(int windowHeight, int windowWidth); + Plansza(unsigned int windowHeight, unsigned int windowWidth); void spawn_meteor(); Size getSize(); std::vector &getMeteors(); diff --git a/main.cpp b/main.cpp index 205a4f2..68e3b29 100644 --- a/main.cpp +++ b/main.cpp @@ -17,7 +17,8 @@ int main() backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła - Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku + // TODO: Przenieść tworzenie statku wewnątrz klasy Plansza + Player ship(window.getSize().x/2,window.getSize().y - 100, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku ship.setMovingSpeed(8); ship.setFirerate(200); @@ -83,6 +84,24 @@ int main() ship.updateBullets(); window.draw(ship.getSprite()); + // trochę dziwny sposób ale jednak działa + for (auto& meteor : plansza.getMeteors()) { + if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) { + std::cout << "You lost the game!\n"; + window.close(); + exit(-2); // Kod -2 oznacza uderzenie się w meteoryt + } + } + + // TODO: Poprawić kolizję pocisku z meteorem, tak aby był kasowany tylko ten konkretny meteoryt +// for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ++meteorIt) { +// for (auto& bullet : ship.getBullets()) { +// if (meteorIt->getSprite().getGlobalBounds().intersects(bullet.getSprite().getGlobalBounds())) { +// meteorIt = plansza.getMeteors().erase(meteorIt); +// break; // Exit the inner loop to avoid invalidating the iterator +// } +// } +// } window.display(); } diff --git a/sources/Actor.cpp b/sources/Actor.cpp index afe34e4..91edb2f 100644 --- a/sources/Actor.cpp +++ b/sources/Actor.cpp @@ -1,10 +1,10 @@ #include "../headers/Actor.h" -// TODO: Naprawić krzywy sprite statku Actor::Actor(int x, int y, std::string path) { loadTexture(path); position.x = x; position.y = y; + actorSprite.setOrigin(actorSprite.getLocalBounds().width / 2, actorSprite.getLocalBounds().height / 2); // wycentrowanie sprite actorSprite.setPosition(x, y); } @@ -39,3 +39,7 @@ void Actor::updateBullets() { } } } + +void Actor::setMovingSpeed(float speed) { + moving_speed = speed; +} diff --git a/sources/Bullet.cpp b/sources/Bullet.cpp index 3f92a17..d17a356 100644 --- a/sources/Bullet.cpp +++ b/sources/Bullet.cpp @@ -5,6 +5,7 @@ Bullet::Bullet(float x, float y, sf::Texture &texture) { outOfBounds = false; bulletTexture = texture; bulletSprite.setTexture(texture); + bulletSprite.setOrigin(bulletSprite.getLocalBounds().width/2, bulletSprite.getLocalBounds().height/2); bulletSprite.setPosition(x, y); bulletSpeed = -10.0f; bulletPosition.x = x; diff --git a/sources/Meteor.cpp b/sources/Meteor.cpp index 16d656c..996d402 100644 --- a/sources/Meteor.cpp +++ b/sources/Meteor.cpp @@ -5,11 +5,13 @@ Meteor::Meteor(float x, float y, sf::Texture &texture) { outOfBounds = false; meteorTexture = texture; meteorSprite.setTexture(texture); + meteorSprite.setOrigin(meteorSprite.getLocalBounds().width / 2, meteorSprite.getLocalBounds().height / 2); // wycentrowanie sprite meteorSprite.setPosition(x, y); meteorSpeed = 5.0f; meteorSprite.scale(0.05f, 0.05f); meteorPosition.x = x; meteorPosition.y = y; + meteorRotationSpeed = static_cast(rand() % 2 + 1) * (rand() % 2 == 0 ? 1 : -1); } sf::Sprite &Meteor::getSprite() { @@ -17,11 +19,14 @@ sf::Sprite &Meteor::getSprite() { } void Meteor::update() { - meteorSprite.move(0.0f, meteorSpeed); - meteorPosition.y += int(meteorSpeed); - if(meteorPosition.y > 800) { - outOfBounds = true; + meteorSprite.move(0.0f, meteorSpeed); // przesunięcie sprajta + meteorPosition.y += int(meteorSpeed); // przesunięcie pozycji + meteorSprite.rotate(meteorRotationSpeed); // obracanie tym meteorkiem pięknym + if(meteorPosition.y > 900) { + outOfBounds = true; // jeżeli wyszedł poza granice ekranu ustaw tą zmienną } +// std::cout << "x: " << meteorSprite.getPosition().x << std::endl; +// std::cout << "y: " << meteorSprite.getPosition().y << std::endl; } bool Meteor::getStatus() { diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 2da5a20..305cead 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -2,17 +2,15 @@ #include #include "../headers/Plansza.h" -Plansza::Plansza(int windowHeight, int windowWidth) { +Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth) { size.height = windowHeight; size.width = windowWidth; meteorTexture.loadFromFile("../assets/img/meteor.png"); spawnClock.restart(); } -// TODO: Meteory na jednym poziomie ze statkiem -// TODO: Kolizje void Plansza::spawn_meteor() { - if (spawnClock.getElapsedTime().asSeconds() > 1.0f) { // spawn co 1 sekunde + if (spawnClock.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 meteors.emplace_back(random.getRandomNumber(), -100, meteorTexture); } diff --git a/sources/Player.cpp b/sources/Player.cpp index eca9dc5..78257e5 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -5,7 +5,7 @@ Player::Player(int x, int y, std::string path) : Actor(x, y, path) {}; void Player::shoot() { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { - bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTextureLeft); + bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2, position.y, bulletTextureLeft); lastShotTime = now; } } @@ -13,7 +13,7 @@ void Player::shoot() { void Player::alternate_shoot() { auto now = std::chrono::steady_clock::now(); if (std::chrono::duration_cast(now - lastShotTime).count() >= firerate) { - bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2-62, position.y, bulletTextureRight); + bullets.emplace_back(float(position.x) + actorSprite.getGlobalBounds().width / 2, position.y, bulletTextureRight); lastShotTime = now; } }