From b6830c305bba6851d72bba31b21180d0f9297857 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Thu, 5 Dec 2024 12:46:03 +0100 Subject: [PATCH] ObjectItem class added to accordance of diagram class --- CMakeLists.txt | 4 +++- assets/img/hearts/heart.png | Bin 0 -> 782 bytes headers/Meteor.h | 14 ++---------- headers/ObjectItem.hpp | 24 +++++++++++++++++++++ sources/Meteor.cpp | 41 +++++++++++++----------------------- sources/ObjectItem.cpp | 17 +++++++++++++++ sources/Plansza.cpp | 1 + 7 files changed, 62 insertions(+), 39 deletions(-) create mode 100644 assets/img/hearts/heart.png create mode 100644 headers/ObjectItem.hpp create mode 100644 sources/ObjectItem.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 99ca51a..1a16cba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,9 @@ add_executable(LotoStatek main.cpp headers/Rocket.h sources/Rocket.cpp headers/Size.h - headers/Position.h) + headers/Position.h + headers/ObjectItem.hpp + sources/ObjectItem.cpp) if(WIN32) set(SFML_ROOT "${CMAKE_SOURCE_DIR}/lib/SFML") diff --git a/assets/img/hearts/heart.png b/assets/img/hearts/heart.png new file mode 100644 index 0000000000000000000000000000000000000000..b028939c9703e012db6637bafb66ac3494381924 GIT binary patch literal 782 zcmV+p1M&QcP)z>%8FWQhbW?9;ba!ELWdL_~cP?peYja~^ zaAhuUa%Y?FJQ@H10*gsRK~z|U)t13)6G0Tlf1AWwZNZv`P&^bpNWJt@yog7k2TvY) z^-^-xtKN%<7KHu*Rr?nxcqk1BB8Xt2RC_5@urZfLqiK>_Bgy7@nAzRg&1MrqNZJpE z8Qz=s_U-$=ogHY!A|MeX9}?pr-ywD}7!Zp{HV|0G`ZaK40upy22@nUd{d?Gge;=R> z`~YUPc?U?}gbQm18a*@GJJ5x5g9#s(w}A`v6wZ>a-8`-1zoK_2Td z=f&Ah;EA9>09HV5gVg}8&CIB`8ygdB+v4z0$mL=HIgmP7-+@~?$y;C=7y+3MP*wst z(6(2o{^vnn8v6v0b+dI28jXgCh%7BNgRLzYRFpc<=8Pwriu*?FP$)3PlW6hAjGCsV z;uMPzXe+=(AW=6M2P(jh3bkOsBCP89it$2j2Wn1L@i=5M%4pker3S)mQ9jq^D1Op# zx&@3nf%Q79t~%zh2eo^6$Nfz`pEsNXrNF7AY1rHXYL2FoN!Zv>V__joXHnXV+&`-4 zNyFAkK_UV3^U9db_LYJW;4g5_unm-gSYQT!#WoO21BE_5v;e%&>qo$mS~h`ru$ZC1 zJPFIdMFIJ$JCg#L=%cMI*NlQQtJEU~3k|X(xOJ*7bS?IPeN?+v%ZG z&@I-`LfQ`z+%qk>2Y$h=b(47L9S9R)77w9fsppyv0AOKhK zKI-SdUXM9X4Fx7X0hg;V{zhMJs&I^AVZV>Hb;Kr~;x~IuGIR?511Xn5g`;$mQUCw| M07*qoM6N<$f&^nxHvj+t literal 0 HcmV?d00001 diff --git a/headers/Meteor.h b/headers/Meteor.h index e283b82..1a01eec 100644 --- a/headers/Meteor.h +++ b/headers/Meteor.h @@ -4,22 +4,12 @@ #include "SFML/Graphics/Texture.hpp" #include "SFML/Graphics/Sprite.hpp" #include "Position.h" +#include "ObjectItem.hpp" -class Meteor { +class Meteor : public ObjectItem { public: Meteor(float x, float y, sf::Texture &texture); - sf::Sprite &getSprite(); - bool getStatus(); void update(); -// ~Meteor(); -private: - sf::Texture meteorTexture; - sf::Sprite meteorSprite; - Position meteorPosition; - float meteorRotationSpeed; - float meteorSpeed; - bool outOfBounds; - static unsigned int counter; }; diff --git a/headers/ObjectItem.hpp b/headers/ObjectItem.hpp new file mode 100644 index 0000000..18157e7 --- /dev/null +++ b/headers/ObjectItem.hpp @@ -0,0 +1,24 @@ +#ifndef LOTOSTATEK_OBJECTITEM_HPP +#define LOTOSTATEK_OBJECTITEM_HPP + +#include "SFML/Graphics/Sprite.hpp" +#include "Position.h" +#include "SFML/Graphics/Texture.hpp" + +class ObjectItem { +public: + ObjectItem(float x, float y, sf::Texture &texture); + sf::Sprite &getSprite(); + bool getStatus(); + virtual void update() = 0; +protected: + sf::Texture texture; + sf::Sprite sprite; + Position position; + float rotationSpeed; + float movingSpeed; + bool outOfBounds; + static unsigned int counter; +}; + +#endif //LOTOSTATEK_OBJECTITEM_HPP diff --git a/sources/Meteor.cpp b/sources/Meteor.cpp index 996d402..6e0e2eb 100644 --- a/sources/Meteor.cpp +++ b/sources/Meteor.cpp @@ -1,40 +1,29 @@ -#include #include "../headers/Meteor.h" -Meteor::Meteor(float x, float y, sf::Texture &texture) { +Meteor::Meteor(float x, float y, sf::Texture &texture) : ObjectItem(x, y, 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); + texture = texture; + sprite.setTexture(texture); + sprite.setOrigin(sprite.getLocalBounds().width / 2, sprite.getLocalBounds().height / 2); // wycentrowanie sprite + sprite.setPosition(x, y); + movingSpeed = 5.0f; + sprite.scale(0.05f, 0.05f); + position.x = x; + position.y = y; + rotationSpeed = static_cast(rand() % 2 + 1) * (rand() % 2 == 0 ? 1 : -1); } -sf::Sprite &Meteor::getSprite() { - return meteorSprite; -} + void Meteor::update() { - 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) { + 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ą } -// std::cout << "x: " << meteorSprite.getPosition().x << std::endl; -// std::cout << "y: " << meteorSprite.getPosition().y << std::endl; } -bool Meteor::getStatus() { - return outOfBounds; -} - -unsigned int Meteor::counter = 0; - // było użyte do testowania czy meteoryt jest kasowany //Meteor::~Meteor() { // Meteor::counter++; diff --git a/sources/ObjectItem.cpp b/sources/ObjectItem.cpp new file mode 100644 index 0000000..7e4bb22 --- /dev/null +++ b/sources/ObjectItem.cpp @@ -0,0 +1,17 @@ +#include "../headers/ObjectItem.hpp" + +ObjectItem::ObjectItem(float x, float y, sf::Texture &texture) { + Position position_; + position_.x = x; + position_.y = y; + position = position_; + this->texture = texture; +} + +bool ObjectItem::getStatus() { + return outOfBounds; +} + +sf::Sprite &ObjectItem::getSprite() { + return sprite; +} \ No newline at end of file diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 90acec2..5183a91 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -1,6 +1,7 @@ #include #include #include "../headers/Plansza.h" +#include "../headers/ObjectItem.hpp" Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth, sf::RenderWindow *mainWindow) : background("../assets/img/background/background.png", 2.0f),