From a81cf284d0a9172dd891a7347fcf1bd33620dee1 Mon Sep 17 00:00:00 2001 From: Kuba Date: Mon, 9 Dec 2024 23:09:44 +0100 Subject: [PATCH] Strzela z kolizja --- CMakeLists.txt | 2 -- headers/EnemyBullet.h | 14 ----------- headers/Player.h | 3 +++ sources/Actor.cpp | 2 +- sources/Enemy.cpp | 23 ++++++++--------- sources/EnemyBullet.cpp | 18 ------------- sources/Plansza.cpp | 56 ++++++++++++++++++++++++++++++++++++++--- sources/Player.cpp | 24 ++++++++++++++++++ 8 files changed, 91 insertions(+), 51 deletions(-) delete mode 100644 headers/EnemyBullet.h delete mode 100644 sources/EnemyBullet.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 56d75e3..d09862e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,8 +30,6 @@ add_executable(LotoStatek main.cpp sources/ObjectItem.cpp sources/Enemy.cpp headers/Enemy.h - headers/EnemyBullet.h - sources/EnemyBullet.cpp ) if(WIN32) diff --git a/headers/EnemyBullet.h b/headers/EnemyBullet.h deleted file mode 100644 index ce733db..0000000 --- a/headers/EnemyBullet.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef ENEMY_BULLET_H -#define ENEMY_BULLET_H - -#include "Projectile.h" - -class EnemyBullet : public Projectile { -public: - EnemyBullet(float x, float y, sf::Texture &texture); - void update() override; - - void setOutOfBounds(bool status); -}; - -#endif // ENEMY_BULLET_H diff --git a/headers/Player.h b/headers/Player.h index e6b4c01..b4e0fd3 100644 --- a/headers/Player.h +++ b/headers/Player.h @@ -17,11 +17,14 @@ public: void moveRight() override; void moveUp() override; void moveDown() override; + void takeDamage(); + bool isAlive() const; std::vector& getRockets(); private: std::chrono::steady_clock::time_point lastShotTime = std::chrono::steady_clock::now(); std::vector rockets; sf::Texture rocketTexture; + int health = 3; // Liczba punktów życia gracza sf::Texture bulletTexture; }; diff --git a/sources/Actor.cpp b/sources/Actor.cpp index 6ee0d0b..cff3f5d 100644 --- a/sources/Actor.cpp +++ b/sources/Actor.cpp @@ -38,7 +38,7 @@ void Actor::updateBullets() { ++it; // Przechodzi do następnego elementu } } - std::cout << "Liczba pociskow: " << bullets.size() << std::endl; + //std::cout << "Liczba pociskow: " << bullets.size() << std::endl; } diff --git a/sources/Enemy.cpp b/sources/Enemy.cpp index 7404e50..0aca9ab 100644 --- a/sources/Enemy.cpp +++ b/sources/Enemy.cpp @@ -3,8 +3,8 @@ Enemy::Enemy(int x, int y, std::string path) : Actor(x, y, path) { hp = 1; // Przeciwnik ma 1 punkt życia - firerate = 2000; // Strzela co 1 sekundę - moving_speed = 2.0f; // Prędkość ruchu przeciwnika + firerate = 2000; // Strzela co 2 + moving_speed = 2.0f; // Prędkość enemyBulletTexture.loadFromFile("../assets/img/bullets/enemy_bullet.png"); } @@ -19,17 +19,17 @@ void Enemy::shoot() { void Enemy::updateDirection() { // Zmieniamy kierunek przeciwnika, gdy dotrze do krawędzi - if (position.y <= 0) { // Górna krawędź ekranu - direction = Direction::Down; // Zmieniamy na ruch w dół - } else if (position.y >= 800) { // Dolna krawędź ekranu - direction = Direction::Up; // Zmieniamy na ruch w górę + if (position.y <= 0) { + direction = Direction::Down; + } else if (position.y >= 800) { + direction = Direction::Up; } - // Podobna logika dla kierunku lewo/prawo, jeśli przeciwnik będzie się poruszał w poziomie - if (position.x <= 0) { // Lewa krawędź - direction = Direction::Right; // Zmieniamy na ruch w prawo - } else if (position.x >= 1200) { // Prawa krawędź - direction = Direction::Left; // Zmieniamy na ruch w lewo + // logika dla kierunku lewo/prawo + if (position.x <= 0) { + direction = Direction::Right; + } else if (position.x >= 1200) { + direction = Direction::Left; } } @@ -50,7 +50,6 @@ void Enemy::update() { // Sprawdzamy, czy przeciwnik dotarł do krawędzi i zmieniamy kierunek updateDirection(); - // Zgodnie z kierunkiem, poruszamy przeciwnikiem switch (direction) { case Direction::Up: moveUp(); diff --git a/sources/EnemyBullet.cpp b/sources/EnemyBullet.cpp deleted file mode 100644 index 67955fd..0000000 --- a/sources/EnemyBullet.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "../headers/EnemyBullet.h" - -EnemyBullet::EnemyBullet(float x, float y, sf::Texture &texture) - : Projectile(x, y, texture) { - speed = 5.0f; // Ruch w dół (dodatnia prędkość) -} - -void EnemyBullet::update() { - sprite.move(0.0f, speed); // Przesuwanie pocisku w dół - position.y += speed; - - if (position.y > 800) { - outOfBounds = true; - } -} -void EnemyBullet::setOutOfBounds(bool status) { - outOfBounds = status; -} \ No newline at end of file diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index d9f3a7b..2a9d147 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -22,6 +22,7 @@ ship(static_cast(mainWindow->getSize().x) / 2, static_cast(mainWindow- audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg"); audioManager.loadSoundEffect("fail", "../assets/sounds/fail.mp3"); + meteorTexture1.loadFromFile("../assets/img/meteors/meteor-1.png"); meteorTexture2.loadFromFile("../assets/img/meteors/meteor-2.png"); spawnClock.restart(); @@ -124,6 +125,38 @@ void Plansza::update() { } } + + if (!ship.isAlive()) { + std::cout << "Game Over! Player is dead." << std::endl; + std::cout << "You lost the game!\n"; + sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end"); + sf::Font font; + if (!font.loadFromFile("../assets/fonts/arial.ttf")) { + std::cerr << "Error loading font\n"; + exit(-500); + } + sf::Text text("Your ship is destroyed!", font, 24); + text.setFillColor(sf::Color::Red); + text.setPosition(50, 80); + + // zatrzymanie muzyki i odtworzenie dźwięku przegranej + audioManager.playSoundEffect("fail", 70.f); + audioManager.stopBackgroundMusic(); + sf::Event event{}; + while (errorWindow.isOpen()) { + while (errorWindow.pollEvent(event)) { + if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { + errorWindow.close(); + window->close(); + exit(-2); + } + } + errorWindow.clear(); + errorWindow.draw(text); + errorWindow.display(); + } + } + for (auto meteorIt = getMeteors().begin(); meteorIt != getMeteors().end(); ) { bool meteorHit = false; for (auto rocketIt = ship.getBullets().begin(); rocketIt != ship.getBullets().end(); ) { @@ -157,12 +190,11 @@ void Plansza::update() { ++meteorIt; } } - // Ruch i renderowanie przeciwników + // Ruch i render przeciwnika for (auto it = enemies.begin(); it != enemies.end();) { - it->update(); // Aktualizacja kierunku i ruchu przeciwnika + it->update(); // Aktualizacja kierunku i ruchu it->shoot(); - // Rysowanie przeciwników window->draw(it->getSprite()); // Usunięcie martwych przeciwników @@ -173,7 +205,23 @@ void Plansza::update() { } } - for (auto& enemy : enemies) { // Lista przeciwników w grze + for (auto& enemy : enemies) { + for (auto it = enemy.getBullets().begin(); it != enemy.getBullets().end();) { + if (ship.getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) { + // Kolizja wykryta + std::cout << "Player hit by enemy bullet!\n"; + + ship.takeDamage(); + + // Usuwanie pocisku + it = enemy.getBullets().erase(it); + } else { + ++it; // Brak kolizji, przechodzimy do kolejnego pocisku + } + } + } + + for (auto& enemy : enemies) { enemy.shoot(); enemy.updateBullets(); for (auto& bullet : enemy.getBullets()) { diff --git a/sources/Player.cpp b/sources/Player.cpp index 91117cf..e5315b0 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -1,4 +1,10 @@ #include "../headers/Player.h" + +#include +#include +#include +#include + #include "../headers/Bullet.h" Player::Player(int x, int y, std::string path) : Actor(x, y, path) { @@ -22,6 +28,24 @@ void Player::alternate_shoot() { } } +void Player::takeDamage() { + if (health > 0) { + health--; + std::cout << "Player hit! Remaining health: " << health << "\n"; + + + if (health <= 0) { + std::cout << "Player has been destroyed!\n"; + std::cout << "You lost the game!\n"; + + } + } +} + +bool Player::isAlive() const { + return health > 0; +} + void Player::setFirerate(unsigned int firerate) { this->firerate = firerate; }