From 36984b859f35086a55a67e224e0fab4ff9bf8083 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Thu, 19 Dec 2024 15:14:14 +0100 Subject: [PATCH 1/5] =?UTF-8?q?Naprawiono=20laser,=20przerobiono=20na=20ws?= =?UTF-8?q?ka=C5=BAniki.=20Do=20refactoringu=20i=20poprawy=20sprite'a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- headers/Beam.h | 2 +- headers/Wiazkowiec.h | 4 ++-- sources/Beam.cpp | 4 ++-- sources/Plansza.cpp | 18 +++++++++--------- sources/Wiazkowiec.cpp | 22 +++++++++++----------- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/headers/Beam.h b/headers/Beam.h index 704d15d..94eb345 100644 --- a/headers/Beam.h +++ b/headers/Beam.h @@ -6,7 +6,7 @@ class Beam { public: - Beam(float x, float y, float width, float height, const sf::Color& color); + Beam(float x, float y, float width, float height); void draw(sf::RenderWindow &window); diff --git a/headers/Wiazkowiec.h b/headers/Wiazkowiec.h index cf996ad..ab8b428 100644 --- a/headers/Wiazkowiec.h +++ b/headers/Wiazkowiec.h @@ -31,7 +31,7 @@ public: void takeDamage(); void updateDirection(); bool isShooting() const; - const Beam& getBeam() const; + const Beam* getBeam() const; void setPlanszaHeight(float height, float width); void setMapBounds(float width, float height); @@ -44,7 +44,7 @@ private: float movementSpeed = 2.0f; bool alive = true; DirectionW direction = DirectionW::Down; - Beam beam; // Wiązka + Beam *beam; // Wiązka bool shooting = false; sf::Clock shootingClock; float beamDuration = 1.0f; diff --git a/sources/Beam.cpp b/sources/Beam.cpp index c0771a6..3fc062b 100644 --- a/sources/Beam.cpp +++ b/sources/Beam.cpp @@ -2,11 +2,11 @@ #include -Beam::Beam(float x, float y, float width, float height, const sf::Color& color) +Beam::Beam(float x, float y, float width, float height) : visible(false) { beamShape.setPosition(x, y); beamShape.setSize({width, height}); - beamShape.setFillColor(color); +// beamShape.setFillColor(color); if (!beamTexture.loadFromFile("../assets/img/wiazka/wiazka.png")) { std::cerr << "Błąd! Nie można załadować tekstury wiazka.png" << std::endl; diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 5413583..a167482 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -111,13 +111,13 @@ void Plansza::update() { ship->update(); // migotanie statku update_score(); // naliczanie punktów // Sprawnowanie wszystkich rodzajów wrogów - spawn_meteor(); - spawn_hearts(); - spawn_enemy(); - spawn_advanced_enemy(); +// spawn_meteor(); +// spawn_hearts(); +// spawn_enemy(); +// spawn_advanced_enemy(); spawn_wiazkowiec(); - spawn_bomber(); - spawn_kamikadze(); +// spawn_bomber(); +// spawn_kamikadze(); // utrzymanie meteorów i pocisków w ruchu for (auto &meteor: meteors) { @@ -445,8 +445,8 @@ void Plansza::update() { for (auto &wiazkowiec: WEnemies) { wiazkowiec.update(); - if (wiazkowiec.isShooting() && wiazkowiec.getBeam().isVisible()) { - if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam().getBounds())) { + if (wiazkowiec.isShooting() && wiazkowiec.getBeam()->isVisible()) { + if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getBounds())) { ship->takeDamage(); // Gracz otrzymuje obrażenia } } @@ -775,7 +775,7 @@ void Plansza::spawn_kamikadze() { } void Plansza::spawn_wiazkowiec() { - if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 50) { // Spawn co 10 sekund + if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture); wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna diff --git a/sources/Wiazkowiec.cpp b/sources/Wiazkowiec.cpp index d97d0c1..5d25552 100644 --- a/sources/Wiazkowiec.cpp +++ b/sources/Wiazkowiec.cpp @@ -5,7 +5,7 @@ #include "../headers/Plansza.h" -Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(0, 0, 50.f, 50.f, sf::Color::Red) { +Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(new Beam(0, 0, 50.f, 50.f)) { actorSprite.setTexture(texture); hp = 2; // 2 punkty życia firerate = 5000; // Strzela co 10 @@ -27,29 +27,29 @@ void Wiazkowiec::spawnBeam() { case DirectionW::Up: beamHeight = position.y; beamY -= beamHeight; - beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); + beam = new Beam(beamX, beamY, beamWidth, beamHeight); break; case DirectionW::Down: beamHeight = planszaHeight - position.y; - beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); + beam = new Beam(beamX, beamY, beamWidth, beamHeight); break; case DirectionW::Left: beamHeight = 50.f; beamWidth = position.x; beamX -= beamWidth; beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; - beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); + beam = new Beam(beamX, beamY, beamWidth, beamHeight); break; case DirectionW::Right: beamHeight = 50.f; beamWidth = 800 - position.x; beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; - beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); + beam = new Beam(beamX, beamY, beamWidth, beamHeight); break; } - beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); - beam.setVisible(true); +// beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); + beam->setVisible(true); shooting = true; shootingClock.restart(); @@ -133,7 +133,7 @@ void Wiazkowiec::update() { if (shooting) { // Kontrola zakończenia strzału if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) { - beam.setVisible(false); + beam->setVisible(false); shooting = false; setRandomDirection(); // Zmień kierunek po strzale } @@ -164,8 +164,8 @@ void Wiazkowiec::update() { // Ustawianie widoczności wiązki podczas renderowania void Wiazkowiec::render(sf::RenderWindow& window) { - if (beam.isVisible()) { - beam.render(window); + if (beam->isVisible()) { + beam->render(window); } } @@ -184,6 +184,6 @@ bool Wiazkowiec::isShooting() const { return shooting; } -const Beam& Wiazkowiec::getBeam() const { +const Beam* Wiazkowiec::getBeam() const { return beam; } \ No newline at end of file From 76203a8b296f203879fb22f91e95f8b512a8018b Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Thu, 19 Dec 2024 16:01:21 +0100 Subject: [PATCH 2/5] refactor --- headers/AdvancedEnemy.h | 2 -- headers/Beam.h | 1 - headers/Bullet.h | 3 -- headers/Heart.hpp | 3 +- headers/Meteor.h | 2 +- headers/ObjectItem.hpp | 2 +- headers/Wiazkowiec.h | 2 -- sources/Bullet.cpp | 3 -- sources/Meteor.cpp | 2 +- sources/ObjectItem.cpp | 8 +++--- sources/Plansza.cpp | 32 +++++++++++---------- sources/Player.cpp | 3 -- sources/Rocket.cpp | 2 +- sources/Wiazkowiec.cpp | 61 +++++++++++++++++++++-------------------- 14 files changed, 58 insertions(+), 68 deletions(-) diff --git a/headers/AdvancedEnemy.h b/headers/AdvancedEnemy.h index 769fa74..f3dae6e 100644 --- a/headers/AdvancedEnemy.h +++ b/headers/AdvancedEnemy.h @@ -3,8 +3,6 @@ #include "Enemy.h" -#include - enum class DirectionA { Up, Down, diff --git a/headers/Beam.h b/headers/Beam.h index 94eb345..1d37d57 100644 --- a/headers/Beam.h +++ b/headers/Beam.h @@ -2,7 +2,6 @@ #define LOTOSTATEK_BEAM_H #include -#include "Position.h" class Beam { public: diff --git a/headers/Bullet.h b/headers/Bullet.h index cc29809..f521be2 100644 --- a/headers/Bullet.h +++ b/headers/Bullet.h @@ -2,14 +2,11 @@ #define LOTOSTATEK_BULLET_H #include "Projectile.h" -#include "SFML/Graphics/Texture.hpp" class Bullet : public Projectile { public: Bullet(float x, float y, sf::Texture &texture) : Projectile(x,y, texture) {}; void update() override; -private: - float directionY; }; diff --git a/headers/Heart.hpp b/headers/Heart.hpp index ecaf8b6..14c785c 100644 --- a/headers/Heart.hpp +++ b/headers/Heart.hpp @@ -2,13 +2,12 @@ #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(); + void update() override; }; #endif //LOTOSTATEK_HEART_HPP diff --git a/headers/Meteor.h b/headers/Meteor.h index 1a01eec..4439eb7 100644 --- a/headers/Meteor.h +++ b/headers/Meteor.h @@ -8,7 +8,7 @@ class Meteor : public ObjectItem { public: - Meteor(float x, float y, sf::Texture &texture); + Meteor(float x, float y, sf::Texture &texture_); void update(); }; diff --git a/headers/ObjectItem.hpp b/headers/ObjectItem.hpp index 18157e7..bfcdaee 100644 --- a/headers/ObjectItem.hpp +++ b/headers/ObjectItem.hpp @@ -12,7 +12,7 @@ public: bool getStatus(); virtual void update() = 0; protected: - sf::Texture texture; + sf::Texture texture_; sf::Sprite sprite; Position position; float rotationSpeed; diff --git a/headers/Wiazkowiec.h b/headers/Wiazkowiec.h index ab8b428..c0c0ec2 100644 --- a/headers/Wiazkowiec.h +++ b/headers/Wiazkowiec.h @@ -33,8 +33,6 @@ public: bool isShooting() const; const Beam* getBeam() const; void setPlanszaHeight(float height, float width); - void setMapBounds(float width, float height); - private: float planszaHeight = 800.f; diff --git a/sources/Bullet.cpp b/sources/Bullet.cpp index 226401f..5b766c7 100644 --- a/sources/Bullet.cpp +++ b/sources/Bullet.cpp @@ -1,8 +1,5 @@ #include "../headers/Bullet.h" -#include -#include - void Bullet::update() { //std::cout << "Start update: speed = " << speed << ", position.y = " << position.y << std::endl; sprite.move(0.0f, speed); diff --git a/sources/Meteor.cpp b/sources/Meteor.cpp index 6e0e2eb..edbe41e 100644 --- a/sources/Meteor.cpp +++ b/sources/Meteor.cpp @@ -2,7 +2,7 @@ Meteor::Meteor(float x, float y, sf::Texture &texture) : ObjectItem(x, y, texture) { outOfBounds = false; - texture = texture; + texture_ = texture; sprite.setTexture(texture); sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite sprite.setPosition(x, y); diff --git a/sources/ObjectItem.cpp b/sources/ObjectItem.cpp index 7e4bb22..4d80017 100644 --- a/sources/ObjectItem.cpp +++ b/sources/ObjectItem.cpp @@ -1,11 +1,11 @@ #include "../headers/ObjectItem.hpp" ObjectItem::ObjectItem(float x, float y, sf::Texture &texture) { - Position position_; - position_.x = x; - position_.y = y; + Position position_ = {0,0}; + position_.x = static_cast(x); + position_.y = static_cast(y); position = position_; - this->texture = texture; + this->texture_ = texture; } bool ObjectItem::getStatus() { diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index a167482..050fd81 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -140,7 +140,7 @@ void Plansza::update() { window->draw(rocket.getSprite()); } - // Sprawdzenie czy meteory i pociski są poza granicami ekranu + // Sprawdzenie, czy meteory i pociski są poza granicami ekranu update_meteors(); update_hearts(); ship->updateBullets(); @@ -406,7 +406,7 @@ void Plansza::update() { if (playerBulletIt->getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) { // Usuń pocisk Bombera i pocisk gracza it = bomberEnemy.getBullets().erase(it); - playerBulletIt = ship->getBullets().erase(playerBulletIt); + ship->getBullets().erase(playerBulletIt); bulletDestroyed = true; break; } else { @@ -420,7 +420,7 @@ void Plansza::update() { if (rocketIt->getSprite().getGlobalBounds().intersects(it->getSprite().getGlobalBounds())) { // Usuń pocisk Bombera i rakietę gracza it = bomberEnemy.getBullets().erase(it); - rocketIt = ship->getRockets().erase(rocketIt); + ship->getRockets().erase(rocketIt); bulletDestroyed = true; break; } else { @@ -474,7 +474,7 @@ void Plansza::update() { bool hit = false; for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { if (enemyIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { - bulletIt = ship->getBullets().erase(bulletIt); + ship->getBullets().erase(bulletIt); enemyIt->takeDamage(); hit = true; break; @@ -493,7 +493,7 @@ void Plansza::update() { bool hit = false; for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { if (advancedIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { - bulletIt = ship->getBullets().erase(bulletIt); + ship->getBullets().erase(bulletIt); advancedIt->takeDamage(); hit = true; break; @@ -512,7 +512,7 @@ void Plansza::update() { bool hit = false; for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { if (bomberIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { - bulletIt = ship->getBullets().erase(bulletIt); + ship->getBullets().erase(bulletIt); bomberIt->takeDamage(); hit = true; break; @@ -531,7 +531,7 @@ void Plansza::update() { bool hit = false; for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { if (kamikadzeIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { - bulletIt = ship->getBullets().erase(bulletIt); + ship->getBullets().erase(bulletIt); kamikadzeIt->takeDamage(); hit = true; break; @@ -550,7 +550,7 @@ void Plansza::update() { bool hit = false; for (auto bulletIt = ship->getBullets().begin(); bulletIt != ship->getBullets().end();) { if (wiazkowiecIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) { - bulletIt = ship->getBullets().erase(bulletIt); + ship->getBullets().erase(bulletIt); wiazkowiecIt->takeDamage(); hit = true; break; @@ -572,7 +572,7 @@ void Plansza::update() { bool hit = false; for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { if (enemyIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { - rocketIt = ship->getRockets().erase(rocketIt); + ship->getRockets().erase(rocketIt); enemyIt->takeDamage(); hit = true; break; @@ -591,7 +591,7 @@ void Plansza::update() { bool hit = false; for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { if (advancedIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { - rocketIt = ship->getRockets().erase(rocketIt); + ship->getRockets().erase(rocketIt); advancedIt->takeDamage(); hit = true; break; @@ -610,7 +610,7 @@ void Plansza::update() { bool hit = false; for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { if (bomberIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { - rocketIt = ship->getRockets().erase(rocketIt); + ship->getRockets().erase(rocketIt); bomberIt->takeDamage(); hit = true; break; @@ -629,7 +629,7 @@ void Plansza::update() { bool hit = false; for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { if (kamikadzeIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { - rocketIt = ship->getRockets().erase(rocketIt); + ship->getRockets().erase(rocketIt); kamikadzeIt->takeDamage(); hit = true; break; @@ -648,7 +648,7 @@ void Plansza::update() { bool hit = false; for (auto rocketIt = ship->getRockets().begin(); rocketIt != ship->getRockets().end();) { if (wiazkowiecIt->getSprite().getGlobalBounds().intersects(rocketIt->getSprite().getGlobalBounds())) { - rocketIt = ship->getRockets().erase(rocketIt); + ship->getRockets().erase(rocketIt); wiazkowiecIt->takeDamage(); hit = true; break; @@ -687,6 +687,8 @@ void Plansza::update() { heartStats[1].setTexture(heartTexture); heartStats[2].setTexture(heartTexture); break; + default: + break; } @@ -698,7 +700,7 @@ void Plansza::update() { void Plansza::update_meteors() { - // usuwanie meteorów które wyleciały poza ekran + // usuwanie meteorów, które wyleciały poza ekran for (auto &meteor: meteors) { if (meteor.getStatus()) { meteors.erase(meteors.begin()); @@ -787,7 +789,7 @@ void Plansza::spawn_wiazkowiec() { void Plansza::update_hearts() { - // usuwanie serduszek które wyleciały poza ekran + // usuwanie serduszek, które wyleciały poza ekran for (auto& heart : hearts) { if(heart.getStatus()) { hearts.erase(hearts.begin()); diff --git a/sources/Player.cpp b/sources/Player.cpp index 56a2ce7..a64b2be 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -1,10 +1,7 @@ -#include - #include "../headers/Player.h" #include #include -#include #include #include "../headers/Plansza.h" diff --git a/sources/Rocket.cpp b/sources/Rocket.cpp index 39af7af..2d7970a 100644 --- a/sources/Rocket.cpp +++ b/sources/Rocket.cpp @@ -2,7 +2,7 @@ void Rocket::update() { sprite.move(0.0f, speed); - position.y += int(speed); + position.y += static_cast(speed); if(position.y < -100) { outOfBounds = true; } diff --git a/sources/Wiazkowiec.cpp b/sources/Wiazkowiec.cpp index 5d25552..f21f28e 100644 --- a/sources/Wiazkowiec.cpp +++ b/sources/Wiazkowiec.cpp @@ -5,7 +5,8 @@ #include "../headers/Plansza.h" -Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture& texture) : Actor(x, y, texture), beam(new Beam(0, 0, 50.f, 50.f)) { +Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture) : Actor(x, y, texture), + beam(new Beam(0, 0, 50.f, 50.f)) { actorSprite.setTexture(texture); hp = 2; // 2 punkty życia firerate = 5000; // Strzela co 10 @@ -26,29 +27,29 @@ void Wiazkowiec::spawnBeam() { switch (direction) { case DirectionW::Up: beamHeight = position.y; - beamY -= beamHeight; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); - break; + beamY -= beamHeight; + beam = new Beam(beamX, beamY, beamWidth, beamHeight); + break; case DirectionW::Down: beamHeight = planszaHeight - position.y; beam = new Beam(beamX, beamY, beamWidth, beamHeight); - break; + break; case DirectionW::Left: beamHeight = 50.f; - beamWidth = position.x; - beamX -= beamWidth; - beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); - break; + beamWidth = position.x; + beamX -= beamWidth; + beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; + beam = new Beam(beamX, beamY, beamWidth, beamHeight); + break; case DirectionW::Right: beamHeight = 50.f; - beamWidth = 800 - position.x; - beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); - break; + beamWidth = 800 - position.x; + beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; + beam = new Beam(beamX, beamY, beamWidth, beamHeight); + break; } -// beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); + // beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); beam->setVisible(true); shooting = true; @@ -65,21 +66,22 @@ void Wiazkowiec::shoot() { void Wiazkowiec::setRandomDirection() { // Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right - int directionIndex = std::rand() % 4; - switch (directionIndex) { + switch (std::rand() % 4) { case 0: direction = DirectionW::Up; - break; + break; case 1: direction = DirectionW::Down; - break; + break; case 2: direction = DirectionW::Left; - break; + break; case 3: direction = DirectionW::Right; - break; + break; + default: + break; } } @@ -143,19 +145,20 @@ void Wiazkowiec::update() { switch (direction) { case DirectionW::Up: moveUp(); - break; + break; case DirectionW::Down: moveDown(); - break; + break; case DirectionW::Left: moveLeft(); - break; + break; case DirectionW::Right: moveRight(); - break; + break; } - if (shootClock.getElapsedTime().asSeconds() >= 3.0f) { // Co 3 sekundy + if (shootClock.getElapsedTime().asSeconds() >= 3.0f) { + // Co 3 sekundy shoot(); shootClock.restart(); } @@ -163,7 +166,7 @@ void Wiazkowiec::update() { } // Ustawianie widoczności wiązki podczas renderowania -void Wiazkowiec::render(sf::RenderWindow& window) { +void Wiazkowiec::render(sf::RenderWindow &window) { if (beam->isVisible()) { beam->render(window); } @@ -184,6 +187,6 @@ bool Wiazkowiec::isShooting() const { return shooting; } -const Beam* Wiazkowiec::getBeam() const { +const Beam *Wiazkowiec::getBeam() const { return beam; -} \ No newline at end of file +} From 0a5a26208abbb3f034a92dc253aabc1ec3ebb213 Mon Sep 17 00:00:00 2001 From: hamx01 Date: Fri, 20 Dec 2024 19:39:18 +0100 Subject: [PATCH 3/5] wip --- headers/Wiazkowiec.h | 2 +- sources/Wiazkowiec.cpp | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/headers/Wiazkowiec.h b/headers/Wiazkowiec.h index c0c0ec2..ba7f449 100644 --- a/headers/Wiazkowiec.h +++ b/headers/Wiazkowiec.h @@ -29,7 +29,7 @@ public: bool isAlive() const; void takeDamage(); - void updateDirection(); + void checkIfBeamShootOutOfBounds(); bool isShooting() const; const Beam* getBeam() const; void setPlanszaHeight(float height, float width); diff --git a/sources/Wiazkowiec.cpp b/sources/Wiazkowiec.cpp index f21f28e..511f36a 100644 --- a/sources/Wiazkowiec.cpp +++ b/sources/Wiazkowiec.cpp @@ -1,8 +1,8 @@ #include "../headers/Wiazkowiec.h" +#include "../headers/Plansza.h" +#include "../headers/RandomNumberGenerator.h" #include -#include -#include "../headers/Plansza.h" Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture) : Actor(x, y, texture), @@ -49,7 +49,6 @@ void Wiazkowiec::spawnBeam() { break; } - // beam = Beam(beamX, beamY, beamWidth, beamHeight, sf::Color::Red); beam->setVisible(true); shooting = true; @@ -67,7 +66,7 @@ void Wiazkowiec::shoot() { void Wiazkowiec::setRandomDirection() { // Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right - switch (std::rand() % 4) { + switch (RandomNumberGenerator::getRandomNumber(0, 3)) { case 0: direction = DirectionW::Up; break; @@ -85,7 +84,7 @@ void Wiazkowiec::setRandomDirection() { } } -void Wiazkowiec::updateDirection() { +void Wiazkowiec::checkIfBeamShootOutOfBounds() { auto spriteBounds = actorSprite.getGlobalBounds(); // Pobierz rozmiar i pozycję sprite'a // Kontrola górnej i dolnej krawędzi @@ -140,7 +139,7 @@ void Wiazkowiec::update() { setRandomDirection(); // Zmień kierunek po strzale } } else { - updateDirection(); + checkIfBeamShootOutOfBounds(); switch (direction) { case DirectionW::Up: From c4c83382c3f84e3004fb7aa13a25e46916a28bf8 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Fri, 20 Dec 2024 23:38:37 +0100 Subject: [PATCH 4/5] Do poprawy centrowanie lasera i strzelanie w lewo i w prawo --- headers/Actor.h | 3 +- headers/Beam.h | 18 ++----- headers/Position.h | 10 ++++ headers/Wiazkowiec.h | 39 +++++++++------ sources/Beam.cpp | 44 ++--------------- sources/Plansza.cpp | 19 +++---- sources/Player.cpp | 10 +++- sources/Wiazkowiec.cpp | 110 ++++++++++++++++++++++++----------------- 8 files changed, 125 insertions(+), 128 deletions(-) diff --git a/headers/Actor.h b/headers/Actor.h index 741cf42..0433254 100644 --- a/headers/Actor.h +++ b/headers/Actor.h @@ -11,8 +11,7 @@ class Actor { public: Actor(int x, int y, const sf::Texture& texture); - - + virtual ~Actor() = default; sf::Sprite& getSprite(); unsigned int getHP(); diff --git a/headers/Beam.h b/headers/Beam.h index 1d37d57..5dd5852 100644 --- a/headers/Beam.h +++ b/headers/Beam.h @@ -5,23 +5,11 @@ class Beam { public: - Beam(float x, float y, float width, float height); - - void draw(sf::RenderWindow &window); - - void update(); - void render(sf::RenderWindow& window); - - sf::FloatRect getBounds() const; - - bool isVisible() const; - void setVisible(bool visible); - + Beam() = default; + Beam(int x, int y, const sf::Texture &texture); + sf::Sprite getSprite(); private: - sf::RectangleShape beamShape; - bool visible; - sf::Texture beamTexture; sf::Sprite beamSprite; }; diff --git a/headers/Position.h b/headers/Position.h index 6f69670..a43976a 100644 --- a/headers/Position.h +++ b/headers/Position.h @@ -6,4 +6,14 @@ struct Position { int y; }; +struct PositionU { + unsigned int x; + unsigned int y; +}; + +struct PositionF { + float x; + float y; +}; + #endif //LOTOSTATEK_POSITION_H diff --git a/headers/Wiazkowiec.h b/headers/Wiazkowiec.h index ba7f449..23e7752 100644 --- a/headers/Wiazkowiec.h +++ b/headers/Wiazkowiec.h @@ -13,40 +13,47 @@ enum class DirectionW { class Wiazkowiec : public Actor { public: - Wiazkowiec(int x, int y, const sf::Texture& texture); + Wiazkowiec(int x, int y, const sf::Texture& texture, sf::RenderWindow *window); - void shoot() override; void move(float deltaX, float deltaY) override; void moveLeft() override; void moveRight() override; void moveUp() override; void moveDown() override; + + void takeDamage(); + void setRandomDirection(); + void checkIfBeamShootOutOfBounds(); void update(); - + void shoot() override; void render(sf::RenderWindow &window); bool isAlive() const; - void takeDamage(); - void checkIfBeamShootOutOfBounds(); bool isShooting() const; - const Beam* getBeam() const; - void setPlanszaHeight(float height, float width); + + void setPlanszaHeight(int height, int width); + Beam* getBeam() const; private: - float planszaHeight = 800.f; - float planszaWidth = 600.f; - sf::Clock shootClock; sf::Texture WiazkaTexture; - float movementSpeed = 2.0f; - bool alive = true; - DirectionW direction = DirectionW::Down; - Beam *beam; // Wiązka - bool shooting = false; + sf::Texture beamTexture; + sf::Clock shootClock; sf::Clock shootingClock; - float beamDuration = 1.0f; + DirectionW direction = DirectionW::Down; + + Beam *beam; // wskaźnik na wiązkę + sf::RenderWindow *window_ptr; + void spawnBeam(); // Tworzy wiązkę + + int planszaHeight = 800; + int planszaWidth = 600; + float beamDuration = 1.0f; + float movementSpeed = 2.0f; + bool shooting = false; + bool alive = true; }; #endif //WIAZKOWIEC_H diff --git a/sources/Beam.cpp b/sources/Beam.cpp index 3fc062b..f14de49 100644 --- a/sources/Beam.cpp +++ b/sources/Beam.cpp @@ -2,50 +2,16 @@ #include -Beam::Beam(float x, float y, float width, float height) - : visible(false) { - beamShape.setPosition(x, y); - beamShape.setSize({width, height}); -// beamShape.setFillColor(color); - - if (!beamTexture.loadFromFile("../assets/img/wiazka/wiazka.png")) { - std::cerr << "Błąd! Nie można załadować tekstury wiazka.png" << std::endl; - } - beamSprite.setTexture(beamTexture); - - if (beamTexture.getSize().x > 0 && beamTexture.getSize().y > 0) { - float scaleX = width / beamTexture.getSize().x; - float scaleY = height / beamTexture.getSize().y; - beamSprite.setScale(scaleX, scaleY); +Beam::Beam(int x, int y, const sf::Texture& texture) { + if (texture.getSize().x > 0 && texture.getSize().y > 0) { beamSprite.setPosition(x, y); } else { std::cerr << "Błąd: Tekstura wiązki nie została poprawnie załadowana." << std::endl; } - + beamSprite.setTexture(texture); } -void Beam::draw(sf::RenderWindow& window) { - - window.draw(beamSprite); - -} - -void Beam::update() { -} - -void Beam::render(sf::RenderWindow& window) { - window.draw(beamSprite); -} - -sf::FloatRect Beam::getBounds() const { - return beamShape.getGlobalBounds(); -} - -bool Beam::isVisible() const { - return visible; -} - -void Beam::setVisible(bool visible) { - this->visible = visible; +sf::Sprite Beam::getSprite() { + return beamSprite; } diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 050fd81..1a3b7fd 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -445,14 +445,13 @@ void Plansza::update() { for (auto &wiazkowiec: WEnemies) { wiazkowiec.update(); - if (wiazkowiec.isShooting() && wiazkowiec.getBeam()->isVisible()) { - if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getBounds())) { + if (wiazkowiec.isShooting()) { + if (ship->getSprite().getGlobalBounds().intersects(wiazkowiec.getBeam()->getSprite().getGlobalBounds())) { ship->takeDamage(); // Gracz otrzymuje obrażenia } } window->draw(wiazkowiec.getSprite()); - wiazkowiec.render(*window); } // TODO: naprawić to co średnio działa @@ -778,12 +777,14 @@ void Plansza::spawn_kamikadze() { void Plansza::spawn_wiazkowiec() { if (WiazkowiecSpawnClock.getElapsedTime().asSeconds() >= 5) { // Spawn co 10 sekund - int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); - Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture); - wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna - WEnemies.push_back(wiazkowiec); - std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl; - WiazkowiecSpawnClock.restart(); + if (WEnemies.size() < 1) { + int spawnX = RandomNumberGenerator::getRandomNumber(50, size.width - 50); + Wiazkowiec wiazkowiec(spawnX, -50, WiazkowiecTexture, window); + wiazkowiec.setPlanszaHeight(size.height, size.width); // Przekazanie wysokości i szerokosci okna + WEnemies.push_back(wiazkowiec); + std::cout << "Spawned Wiazkowiec Enemy at X: " << spawnX << std::endl; + WiazkowiecSpawnClock.restart(); + } } } diff --git a/sources/Player.cpp b/sources/Player.cpp index a64b2be..7170b2f 100644 --- a/sources/Player.cpp +++ b/sources/Player.cpp @@ -21,8 +21,14 @@ Player* Player::getInstance(int x, int y, const sf::Texture& texture) { } void Player::loadTexture() { - bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png"); - rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png"); + try { + bulletTexture.loadFromFile("../assets/img/bullets/bullet_pink.png"); + rocketTexture.loadFromFile("../assets/img/rockets/Rocket_111.png"); + } catch (std::exception &e) { + std::cerr << "Failed to load textures: " << e.what() << std::endl; + exit(-500); + } + damageDealClock.restart(); originalColor = actorSprite.getColor(); } diff --git a/sources/Wiazkowiec.cpp b/sources/Wiazkowiec.cpp index 511f36a..407cc1b 100644 --- a/sources/Wiazkowiec.cpp +++ b/sources/Wiazkowiec.cpp @@ -5,52 +5,54 @@ #include -Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture) : Actor(x, y, texture), - beam(new Beam(0, 0, 50.f, 50.f)) { +Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture, sf::RenderWindow *window) : Actor(x, y, texture), + beam(nullptr) +{ + window_ptr = window; actorSprite.setTexture(texture); + + try { + beamTexture.loadFromFile("../assets/img/wiazka/laser.png"); + } catch (std::exception &e) { + std::cerr << "Failed to load textures: " << e.what() << std::endl; + exit(-500); + } + hp = 2; // 2 punkty życia firerate = 5000; // Strzela co 10 moving_speed = 5.0f; // Prędkość } -void Wiazkowiec::setPlanszaHeight(float height, float width) { +void Wiazkowiec::setPlanszaHeight(int height, int width) { planszaHeight = height; planszaWidth = width; } void Wiazkowiec::spawnBeam() { - float beamX = position.x; - float beamY = position.y; - float beamWidth = 50.f; - float beamHeight = 0.f; + int beamX = actorSprite.getPosition().x; + int beamY = actorSprite.getPosition().y; switch (direction) { - case DirectionW::Up: - beamHeight = position.y; - beamY -= beamHeight; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); + case DirectionW::Up: { + beam = new Beam(beamX, beamY - beamTexture.getSize().y, beamTexture); break; - case DirectionW::Down: - beamHeight = planszaHeight - position.y; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); + } + case DirectionW::Down: { + beam = new Beam(beamX, beamY + actorSprite.getGlobalBounds().height, beamTexture); break; - case DirectionW::Left: - beamHeight = 50.f; - beamWidth = position.x; - beamX -= beamWidth; - beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); + } + case DirectionW::Left: { + beam = new Beam(beamX - beamTexture.getSize().x, beamY, beamTexture); break; - case DirectionW::Right: - beamHeight = 50.f; - beamWidth = 800 - position.x; - beamY = position.y + (actorSprite.getGlobalBounds().height / 2) - 25.f; - beam = new Beam(beamX, beamY, beamWidth, beamHeight); + } + case DirectionW::Right: { + beam = new Beam(beamX + actorSprite.getGlobalBounds().width, beamY, beamTexture); + break; + } + default: break; } - beam->setVisible(true); - shooting = true; shootingClock.restart(); } @@ -60,13 +62,35 @@ void Wiazkowiec::shoot() { if (!shooting) { spawnBeam(); std::cout << "Wiazkowiec shot a beam!" << std::endl; + switch (direction) { + case DirectionW::Up: { + std::cout << "Direction is up" << std::endl; + break; + } + case DirectionW::Down: { + std::cout << "Direction is down" << std::endl; + break; + } + case DirectionW::Left: { + std::cout << "Direction is left" << std::endl; + break; + } + case DirectionW::Right: { + std::cout << "Direction is right" << std::endl; + break; + } + default: + break; + } } } void Wiazkowiec::setRandomDirection() { // Losowanie kierunku: 0 = Up, 1 = Down, 2 = Left, 3 = Right - switch (RandomNumberGenerator::getRandomNumber(0, 3)) { + int whatNumber = rand() % 4; + + switch (whatNumber) { case 0: direction = DirectionW::Up; break; @@ -107,17 +131,17 @@ void Wiazkowiec::move(float deltaX, float deltaY) { auto spriteBounds = actorSprite.getGlobalBounds(); // Rozmiar i pozycja sprite'a // Zapobiegaj wyjściu poza poziome granice - if (position.x + deltaX < 0) { - deltaX = -position.x; - } else if (position.x + spriteBounds.width + deltaX > planszaWidth) { - deltaX = planszaWidth - (position.x + spriteBounds.width); + if (static_cast(position.x) + deltaX < 0) { + deltaX = static_cast(-position.x); + } else if (static_cast(position.x) + spriteBounds.width + deltaX > static_cast(planszaWidth)) { + deltaX = static_cast(planszaWidth) - (static_cast(position.x) + spriteBounds.width); } // Zapobiegaj wyjściu poza pionowe granice - if (position.y + deltaY < 0) { - deltaY = -position.y; - } else if (position.y + spriteBounds.height + deltaY > planszaHeight) { - deltaY = planszaHeight - (position.y + spriteBounds.height); + if (static_cast(position.y) + deltaY < 0) { + deltaY = static_cast(-position.y); + } else if (static_cast(position.y) + spriteBounds.height + deltaY > static_cast(planszaHeight)) { + deltaY = static_cast(planszaHeight) - (static_cast(position.y) + spriteBounds.height); } actorSprite.move(deltaX, deltaY); @@ -134,9 +158,12 @@ void Wiazkowiec::update() { if (shooting) { // Kontrola zakończenia strzału if (shootingClock.getElapsedTime().asSeconds() >= beamDuration) { - beam->setVisible(false); shooting = false; + delete beam; + beam = nullptr; setRandomDirection(); // Zmień kierunek po strzale + } else { + window_ptr->draw(beam->getSprite()); } } else { checkIfBeamShootOutOfBounds(); @@ -164,13 +191,6 @@ void Wiazkowiec::update() { } } -// Ustawianie widoczności wiązki podczas renderowania -void Wiazkowiec::render(sf::RenderWindow &window) { - if (beam->isVisible()) { - beam->render(window); - } -} - bool Wiazkowiec::isAlive() const { return alive; } @@ -186,6 +206,6 @@ bool Wiazkowiec::isShooting() const { return shooting; } -const Beam *Wiazkowiec::getBeam() const { +Beam* Wiazkowiec::getBeam() const { return beam; } From 6b45443c75b9bffa6cfcb48139965d89900d7837 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Sun, 5 Jan 2025 18:20:19 +0100 Subject: [PATCH 5/5] Laser naprawiono --- headers/Beam.h | 2 ++ sources/AdvancedEnemy.cpp | 1 - sources/Beam.cpp | 5 ++++ sources/Bomber.cpp | 1 - sources/Enemy.cpp | 1 - sources/Kamikadze.cpp | 1 - sources/Plansza.cpp | 12 ++++----- sources/Wiazkowiec.cpp | 51 ++++++++++++++++++++------------------- 8 files changed, 39 insertions(+), 35 deletions(-) diff --git a/headers/Beam.h b/headers/Beam.h index 5dd5852..ce08a41 100644 --- a/headers/Beam.h +++ b/headers/Beam.h @@ -9,6 +9,8 @@ public: Beam(int x, int y, const sf::Texture &texture); sf::Sprite getSprite(); + + void setRotation(float angle); private: sf::Sprite beamSprite; }; diff --git a/sources/AdvancedEnemy.cpp b/sources/AdvancedEnemy.cpp index 516454b..746189a 100644 --- a/sources/AdvancedEnemy.cpp +++ b/sources/AdvancedEnemy.cpp @@ -3,7 +3,6 @@ #include "../headers/Plansza.h" AdvancedEnemy::AdvancedEnemy(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) { - actorSprite.setTexture(texture); enemyBulletTexture = bulletTexture; hp = 2; // 2 punkty życia firerate = 2000; // Strzela co 2 diff --git a/sources/Beam.cpp b/sources/Beam.cpp index f14de49..3ccf6d7 100644 --- a/sources/Beam.cpp +++ b/sources/Beam.cpp @@ -3,6 +3,7 @@ #include Beam::Beam(int x, int y, const sf::Texture& texture) { + beamSprite.setOrigin(beamSprite.getLocalBounds().width/2, beamSprite.getLocalBounds().height/2); if (texture.getSize().x > 0 && texture.getSize().y > 0) { beamSprite.setPosition(x, y); } else { @@ -15,3 +16,7 @@ sf::Sprite Beam::getSprite() { return beamSprite; } +void Beam::setRotation(float angle) { + beamSprite.setRotation(angle); +} + diff --git a/sources/Bomber.cpp b/sources/Bomber.cpp index 9d29b9a..d56bd99 100644 --- a/sources/Bomber.cpp +++ b/sources/Bomber.cpp @@ -4,7 +4,6 @@ #include Bomber::Bomber(int x, int y, const sf::Texture& texture, const sf::Texture& bulletTexture) : Actor(x, y, texture) { - actorSprite.setTexture(texture); BombaTexture = bulletTexture; hp = 2; // 2 punkty życia firerate = 10000; // Strzela co 10 diff --git a/sources/Enemy.cpp b/sources/Enemy.cpp index 484fba0..731281a 100644 --- a/sources/Enemy.cpp +++ b/sources/Enemy.cpp @@ -3,7 +3,6 @@ #include "../headers/Plansza.h" Enemy::Enemy(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { - actorSprite.setTexture(texture); hp = 1; // Przeciwnik ma 1 punkt życia firerate = 2000; // Strzela co 2 moving_speed = 2.0f; // Prędkość diff --git a/sources/Kamikadze.cpp b/sources/Kamikadze.cpp index f78833a..f2fd0c2 100644 --- a/sources/Kamikadze.cpp +++ b/sources/Kamikadze.cpp @@ -7,7 +7,6 @@ Kamikadze::Kamikadze(int x, int y, const sf::Texture& texture) : Actor(x, y, texture) { - actorSprite.setTexture(texture); hp = 3; // 3 punkty życia moving_speed = 2.0f; // Prędkość } diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 1a3b7fd..0e9c294 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -111,13 +111,13 @@ void Plansza::update() { ship->update(); // migotanie statku update_score(); // naliczanie punktów // Sprawnowanie wszystkich rodzajów wrogów -// spawn_meteor(); -// spawn_hearts(); -// spawn_enemy(); -// spawn_advanced_enemy(); + // spawn_meteor(); + // spawn_hearts(); + // spawn_enemy(); + // spawn_advanced_enemy(); spawn_wiazkowiec(); -// spawn_bomber(); -// spawn_kamikadze(); + // spawn_bomber(); + // spawn_kamikadze(); // utrzymanie meteorów i pocisków w ruchu for (auto &meteor: meteors) { diff --git a/sources/Wiazkowiec.cpp b/sources/Wiazkowiec.cpp index 407cc1b..f73de42 100644 --- a/sources/Wiazkowiec.cpp +++ b/sources/Wiazkowiec.cpp @@ -9,7 +9,6 @@ Wiazkowiec::Wiazkowiec(int x, int y, const sf::Texture &texture, sf::RenderWindo beam(nullptr) { window_ptr = window; - actorSprite.setTexture(texture); try { beamTexture.loadFromFile("../assets/img/wiazka/laser.png"); @@ -34,19 +33,22 @@ void Wiazkowiec::spawnBeam() { switch (direction) { case DirectionW::Up: { - beam = new Beam(beamX, beamY - beamTexture.getSize().y, beamTexture); + beam = new Beam(beamX, beamY, beamTexture); + beam->setRotation(180); break; } case DirectionW::Down: { - beam = new Beam(beamX, beamY + actorSprite.getGlobalBounds().height, beamTexture); + beam = new Beam(beamX, beamY, beamTexture); break; } case DirectionW::Left: { - beam = new Beam(beamX - beamTexture.getSize().x, beamY, beamTexture); + beam = new Beam(beamX, beamY, beamTexture); + beam->setRotation(90); break; } case DirectionW::Right: { - beam = new Beam(beamX + actorSprite.getGlobalBounds().width, beamY, beamTexture); + beam = new Beam(beamX, beamY, beamTexture); + beam->setRotation(270); break; } default: @@ -61,27 +63,26 @@ void Wiazkowiec::spawnBeam() { void Wiazkowiec::shoot() { if (!shooting) { spawnBeam(); - std::cout << "Wiazkowiec shot a beam!" << std::endl; - switch (direction) { - case DirectionW::Up: { - std::cout << "Direction is up" << std::endl; - break; - } - case DirectionW::Down: { - std::cout << "Direction is down" << std::endl; - break; - } - case DirectionW::Left: { - std::cout << "Direction is left" << std::endl; - break; - } - case DirectionW::Right: { - std::cout << "Direction is right" << std::endl; - break; - } - default: - break; + } + switch (direction) { + case DirectionW::Up: { + std::cout << "Direction is up" << std::endl; + break; } + case DirectionW::Down: { + std::cout << "Direction is down" << std::endl; + break; + } + case DirectionW::Left: { + std::cout << "Direction is left" << std::endl; + break; + } + case DirectionW::Right: { + std::cout << "Direction is right" << std::endl; + break; + } + default: + break; } }