From baced5f9714af164fca8a4c6e0297247a5bdc235 Mon Sep 17 00:00:00 2001 From: Kuba Date: Tue, 14 Jan 2025 13:52:05 +0100 Subject: [PATCH] Kolizja wiazki dodana --- headers/Boss.h | 11 ++++++- headers/Plansza.h | 5 +-- sources/Boss.cpp | 74 +++++++++++++++++++++++++++++++++++---------- sources/Plansza.cpp | 16 +++++++--- 4 files changed, 83 insertions(+), 23 deletions(-) diff --git a/headers/Boss.h b/headers/Boss.h index b8c2281..53bee8e 100644 --- a/headers/Boss.h +++ b/headers/Boss.h @@ -19,6 +19,8 @@ public: Boss(int x, int y, const sf::Texture &bossTexture, const sf::Texture &bulletTexture, sf::Texture BombaTexture, sf::RenderWindow *window); + void setPlanszaHeight(int height, int width); + void moveLeft() override; void moveRight() override; void moveUp() override; @@ -35,9 +37,11 @@ public: bool isAlive() const; std::vector& getBullets() { return bullets; } std::vector& getBombs() { return bombs; } + bool isShooting() const { return laserBeam != nullptr; } + Beam* getBeam() const { return laserBeam; } private: - float movementSpeed = 1.5f; + float movementSpeed = 5.5f; BossDirection direction = BossDirection::Down; sf::Clock shootClock; @@ -56,6 +60,11 @@ private: std::vector bullets; std::vector bombs; + int planszaHeight = 800; + int planszaWidth = 600; + + bool isStationary = false; + void setRandomDirection(); void handleBounds(); }; diff --git a/headers/Plansza.h b/headers/Plansza.h index 47e00f4..15422e8 100644 --- a/headers/Plansza.h +++ b/headers/Plansza.h @@ -56,8 +56,8 @@ public: void spawn_bomber(); void spawn_kamikadze(); ~Plansza() { - delete ship; // usuwanie wskaźnika ship - delete boss; // Usuwanie wskaźnika bossa + delete ship; + delete boss; } static ships selectedShip; @@ -115,6 +115,7 @@ private: sf::Clock bossSpawnClock; // Zegar do spawnowania bossa bool bossSpawned = false; // Flaga informująca, czy boss został już zespawnowany bool gameOver = false; + unsigned int nextBossScoreThreshold = 1; // Próg punktowy dla spawnu bossa }; #endif //PLANSZA_H diff --git a/sources/Boss.cpp b/sources/Boss.cpp index e9fea30..a6a5416 100644 --- a/sources/Boss.cpp +++ b/sources/Boss.cpp @@ -13,9 +13,15 @@ Boss::Boss(int x, int y, const sf::Texture& bossTexture, const sf::Texture& bull } hp = 100; firerate = 2000; + movementSpeed = 2.0f; } +void Boss::setPlanszaHeight(int height, int width) { + planszaHeight = height; + planszaWidth = width; +} + void Boss::shoot() { if (shootClock.getElapsedTime().asMilliseconds() >= firerate) { Bullet leftBullet(position.x - 20, position.y, bulletTexture); @@ -35,7 +41,7 @@ void Boss::shoot() { void Boss::dropBomb() { if (bombClock.getElapsedTime().asMilliseconds() >= 5000) { Bullet Bomb(position.x, position.y, BombaTexture); - Bomb.setSpeed(0.1f); + Bomb.setSpeed(0.5f); bullets.emplace_back(std::move(Bomb)); // Można zmienić na bombę std::cout << "Bombka lezy" << std::endl; bombClock.restart(); @@ -47,6 +53,7 @@ void Boss::shootLaser() { laserBeam = new Beam(position.x, position.y, beamTexture); beamDurationClock.restart(); laserClock.restart(); + isStationary = true; } if (laserBeam) { @@ -56,12 +63,16 @@ void Boss::shootLaser() { if (beamDurationClock.getElapsedTime().asSeconds() >= beamDuration) { delete laserBeam; laserBeam = nullptr; + isStationary = false; } } } void Boss::move(float deltaX, float deltaY) { + if (deltaX == 0 && deltaY == 0) { + std::cerr << "Boss stopped: deltaX and deltaY are both 0" << std::endl; + } actorSprite.move(deltaX, deltaY); position.x += static_cast(deltaX); position.y += static_cast(deltaY); @@ -69,16 +80,21 @@ void Boss::move(float deltaX, float deltaY) { } void Boss::update() { - if (directionClock.getElapsedTime().asSeconds() >= 3.0f) { - setRandomDirection(); - directionClock.restart(); - } + std::cout << "Current movementSpeed: " << movementSpeed << std::endl; + std::cout << "Boss position: (" << position.x << ", " << position.y << "), Direction: " << static_cast(direction) << std::endl; - switch (direction) { - case BossDirection::Up: move(0, -movementSpeed); break; - case BossDirection::Down: move(0, movementSpeed); break; - case BossDirection::Left: move(-movementSpeed, 0); break; - case BossDirection::Right: move(movementSpeed, 0); break; + if (!isStationary) { + if (directionClock.getElapsedTime().asSeconds() >= 3.0f) { + setRandomDirection(); + directionClock.restart(); + } + + switch (direction) { + case BossDirection::Up: moveUp(); break; + case BossDirection::Down: moveDown(); break; + case BossDirection::Left: moveLeft(); break; + case BossDirection::Right: moveRight(); break; + } } shoot(); @@ -125,9 +141,11 @@ void Boss::moveDown() { } + void Boss::takeDamage() { if (--hp <= 0) { hp = 0; + std::cout << "HP bossa" << hp << std::endl; } } @@ -136,14 +154,38 @@ bool Boss::isAlive() const { } void Boss::setRandomDirection() { - int randomValue = RandomNumberGenerator::getRandomNumber(0, 3); - direction = static_cast(randomValue); + BossDirection previousDirection = direction; + do { + int randomValue = RandomNumberGenerator::getRandomNumber(0, 3); + direction = static_cast(randomValue); + } while ( + (direction == BossDirection::Left && position.x <= 0) || + (direction == BossDirection::Right && position.x >= planszaWidth) || + (direction == BossDirection::Up && position.y <= 0) || + (direction == BossDirection::Down && position.y >= planszaHeight) + ); + if (previousDirection == direction) { + std::cerr << "Boss kept the same direction: " << static_cast(direction) << std::endl; + } } + void Boss::handleBounds() { - if (position.x <= 0) direction = BossDirection::Right; - if (position.x >= 300) direction = BossDirection::Left; - if (position.y <= 0) direction = BossDirection::Down; - if (position.y >= 300) direction = BossDirection::Up; + if (position.x < 0) { + position.x = 0; + direction = BossDirection::Right; + } else if (position.x > planszaWidth - actorSprite.getGlobalBounds().width) { + position.x = planszaWidth - actorSprite.getGlobalBounds().width; + direction = BossDirection::Left; + } + + if (position.y < 0) { + position.y = 0; + direction = BossDirection::Down; + } else if (position.y > planszaHeight - actorSprite.getGlobalBounds().height) { + position.y = planszaHeight - actorSprite.getGlobalBounds().height; + direction = BossDirection::Up; + } } + diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index d34184f..f91a751 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -126,7 +126,7 @@ void Plansza::update() { spawn_hearts(); spawn_power_up(); // spawn_enemy(); - // spawn_advanced_enemy(); + spawn_advanced_enemy(); // spawn_wiazkowiec(); // spawn_bomber(); // spawn_kamikadze(); @@ -422,6 +422,12 @@ void Plansza::update() { } } + if (boss->isShooting() && boss->getBeam() != nullptr) { + if (ship->getSprite().getGlobalBounds().intersects(boss->getBeam()->getSprite().getGlobalBounds())) { + ship->takeDamage(); // Gracz otrzymuje obrażenia + } + } + } @@ -851,7 +857,7 @@ void Plansza::spawn_enemy() { } void Plansza::spawn_advanced_enemy() { - if (AenemySpawnClock.getElapsedTime().asSeconds() >= 20) { // Spawn co 10 sekund + if (AenemySpawnClock.getElapsedTime().asSeconds() >= 8) { // Spawn co 10 sekund int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); AEnemies.emplace_back(spawnX, -50, advancedEnemyTexture, enemyBulletTexture); std::cout << "Spawned Advanced Enemy at X: " << spawnX << std::endl; @@ -893,10 +899,12 @@ void Plansza::spawn_wiazkowiec() { } void Plansza::spawn_boss() { - if (!bossSpawned && bossSpawnClock.getElapsedTime().asSeconds() >= 3) { // Spawn po 60 sekundach + if (!bossSpawned && score >= nextBossScoreThreshold) { // Spawn po 60 sekundach boss = new Boss(size.width / 2, -100, BossTexture, enemyBulletTexture, BombaTexture, window); + boss->setPlanszaHeight(size.width, size.height); bossSpawned = true; - std::cout << "Boss spawned!" << std::endl; + nextBossScoreThreshold += 1000; + std::cout << "Boss spawned at score: " << score << std::endl; } }