From bfa6414c99e4066d288a1eb3c7a4e62df3302661 Mon Sep 17 00:00:00 2001 From: Ipagaxi Date: Tue, 14 May 2024 01:53:08 +0200 Subject: [PATCH] refactor: continued implementing observer --- src/include/Actors/Actor.hpp | 1 - src/include/FightEnv.hpp | 2 +- src/include/ObserverPattern/Observer.hpp | 22 ++++++++++++++--- src/include/ObserverPattern/Subject.hpp | 12 ---------- src/include/UIElements/UIStats.hpp | 2 ++ src/include/UIObjects/UIPlayerOverview.hpp | 4 +++- src/main/Activities/FightActivity.cpp | 4 ++-- src/main/Actors/Actor.cpp | 7 ++---- src/main/FightEnv.cpp | 2 +- src/main/ObserverPattern/Observer.cpp | 28 +++++++++++++++++++++- src/main/ObserverPattern/Subject.cpp | 15 ------------ src/main/UIElements/UIStats.cpp | 6 ++++- 12 files changed, 62 insertions(+), 43 deletions(-) diff --git a/src/include/Actors/Actor.hpp b/src/include/Actors/Actor.hpp index fe7bb30..8ca485b 100644 --- a/src/include/Actors/Actor.hpp +++ b/src/include/Actors/Actor.hpp @@ -5,7 +5,6 @@ #include #include "ObserverPattern/Observer.hpp" -#include "ObserverPattern/Subject.hpp" struct RGB { int red = 0; diff --git a/src/include/FightEnv.hpp b/src/include/FightEnv.hpp index 2b2bfc6..5006400 100644 --- a/src/include/FightEnv.hpp +++ b/src/include/FightEnv.hpp @@ -15,7 +15,7 @@ class FightEnv { sf::Texture backgroundTX; sf::Sprite backgroundSP; - UIStats playerStatsBox; + //UIStats playerStatsBox; UIEnemyOverview enemyOverview; UIPlayerOverview playerOverview; TextFadingManager textFadingManager; diff --git a/src/include/ObserverPattern/Observer.hpp b/src/include/ObserverPattern/Observer.hpp index 6451f82..125739a 100644 --- a/src/include/ObserverPattern/Observer.hpp +++ b/src/include/ObserverPattern/Observer.hpp @@ -1,15 +1,31 @@ #ifndef OBSERVER_HPP #define OBSERVER_HPP -class Actor; +#include +#include + +class Subject; class Observer { public: - Observer(Actor& subject); + Observer(); + //Observer(Actor& subject); virtual ~Observer(); virtual void onNotify(int newValue); private: - Actor& subject; + Subject* subject; +}; + +class Subject { + public: + void attachObserver(Observer& observer); + void detachObserver(Observer& observer); + + private: + std::list observers; + + protected: + virtual void notify(int newValue); }; #endif \ No newline at end of file diff --git a/src/include/ObserverPattern/Subject.hpp b/src/include/ObserverPattern/Subject.hpp index ba5b343..47d6d13 100644 --- a/src/include/ObserverPattern/Subject.hpp +++ b/src/include/ObserverPattern/Subject.hpp @@ -1,20 +1,8 @@ #ifndef SUBJECT_HPP #define SUBJECT_HPP -#include - #include "ObserverPattern/Observer.hpp" -class Subject { - public: - void attachObserver(Observer& observer); - void detachObserver(Observer& observer); - - private: - std::list observers; - protected: - virtual void notify(int newValue); -}; #endif \ No newline at end of file diff --git a/src/include/UIElements/UIStats.hpp b/src/include/UIElements/UIStats.hpp index d9a67e7..d2749a3 100644 --- a/src/include/UIElements/UIStats.hpp +++ b/src/include/UIElements/UIStats.hpp @@ -11,6 +11,8 @@ class UIStats: public UIElement, Observer { public: + UIStats(); + ~UIStats(); void init(Actor actor); void draw() override; sf::Vector2f getPosition() override; diff --git a/src/include/UIObjects/UIPlayerOverview.hpp b/src/include/UIObjects/UIPlayerOverview.hpp index dcf3b0c..8c08942 100644 --- a/src/include/UIObjects/UIPlayerOverview.hpp +++ b/src/include/UIObjects/UIPlayerOverview.hpp @@ -11,7 +11,9 @@ class UIPlayerOverview { public: - UIBorderedImage playerFrame; + UIPlayerOverview() {} + + UIBorderedImage playerFrame; Player player; void init(); diff --git a/src/main/Activities/FightActivity.cpp b/src/main/Activities/FightActivity.cpp index 1f9d4d3..3a9d625 100644 --- a/src/main/Activities/FightActivity.cpp +++ b/src/main/Activities/FightActivity.cpp @@ -19,8 +19,8 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std:: sf::Vector2f backgroundScale = sf::Vector2f(windowSize.x / backgroundSize.x, windowSize.y / backgroundSize.y); this->fightEnv.backgroundSP.scale(backgroundScale); - float relativeOuterPaddingStatBoxes = 0.02; - this->fightEnv.playerStatsBox.setPosition(windowSize.x * relativeOuterPaddingStatBoxes, (windowSize.y - this->fightEnv.playerStatsBox.getSize().height)/2); + //float relativeOuterPaddingStatBoxes = 0.02; + //this->fightEnv.playerStatsBox.setPosition(windowSize.x * relativeOuterPaddingStatBoxes, (windowSize.y - this->fightEnv.playerStatsBox.getSize().height)/2); std::random_device randSeed; std::mt19937 gen(randSeed()); diff --git a/src/main/Actors/Actor.cpp b/src/main/Actors/Actor.cpp index c60ef35..1dd9288 100644 --- a/src/main/Actors/Actor.cpp +++ b/src/main/Actors/Actor.cpp @@ -3,10 +3,7 @@ Actor::Actor() {} -Observer::Observer(Actor& subject_): subject(subject_) { +/*Observer::Observer(Actor& subject_): subject(subject_) { this->subject.attachObserver(*this); } - -Observer::~Observer() { - this->subject.detachObserver(*this); -} \ No newline at end of file +*/ \ No newline at end of file diff --git a/src/main/FightEnv.cpp b/src/main/FightEnv.cpp index ac3a6d2..c91cfa2 100644 --- a/src/main/FightEnv.cpp +++ b/src/main/FightEnv.cpp @@ -1,5 +1,5 @@ #include "FightEnv.hpp" FightEnv::FightEnv() { - playerStatsBox.init(Game::getInstance().player); + //playerStatsBox.init(Game::getInstance().player); } diff --git a/src/main/ObserverPattern/Observer.cpp b/src/main/ObserverPattern/Observer.cpp index 8ea9d55..b2c203b 100644 --- a/src/main/ObserverPattern/Observer.cpp +++ b/src/main/ObserverPattern/Observer.cpp @@ -2,4 +2,30 @@ //constructor and destructor defined in ObserverPattern/Subject.cpp -void Observer::onNotify(int newValue) {} \ No newline at end of file +Observer::Observer() { + std::cout << "Observer" << std::endl; +} + +Observer::~Observer() { + std::cout << "~Observer" << std::endl; + this->subject->detachObserver(*this); +} + +void Observer::onNotify(int newValue) {} + +void Subject::notify(int newValue) { + for (Observer obs: observers) { + obs.onNotify(newValue); + } +} + +void Subject::attachObserver(Observer &observer) { + this->observers.push_front(observer); +} + +void Subject::detachObserver(Observer &observer) { + std::cout << "Size: " << this->observers.size() << std::endl; + this->observers.remove_if([&observer] (const Observer obs) { + return &obs == &observer; + }); +} \ No newline at end of file diff --git a/src/main/ObserverPattern/Subject.cpp b/src/main/ObserverPattern/Subject.cpp index 3789aaa..b23bf96 100644 --- a/src/main/ObserverPattern/Subject.cpp +++ b/src/main/ObserverPattern/Subject.cpp @@ -1,17 +1,2 @@ #include "ObserverPattern/Subject.hpp" -void Subject::notify(int newValue) { - for (Observer obs: observers) { - obs.onNotify(newValue); - } -} - -void Subject::attachObserver(Observer &observer) { - this->observers.push_front(observer); -} - -void Subject::detachObserver(Observer &observer) { - this->observers.remove_if([&observer] (const Observer obs) { - return &obs == &observer; - }); -} \ No newline at end of file diff --git a/src/main/UIElements/UIStats.cpp b/src/main/UIElements/UIStats.cpp index 49e2891..9eef58c 100644 --- a/src/main/UIElements/UIStats.cpp +++ b/src/main/UIElements/UIStats.cpp @@ -1,5 +1,10 @@ #include "UIElements/UIStats.hpp" +UIStats::UIStats() { +} + +UIStats::~UIStats() { +} void UIStats::init(Actor actor) { actor.attachObserver(*this); @@ -79,7 +84,6 @@ void UIStats::setPosition(float x, float y) { float statsSeparationPaddingY = actorStatsBoxSize.height * 0.05; std::string currentActor = this->actorName.getString(); - std::cout << currentActor << std::endl; sf::FloatRect actorNameRec = this->actorName.getGlobalBounds(); this->actorName.setPosition(x + ((actorStatsBoxSize.width - actorNameRec.width)/2), y + actorStatsBoxSize.height * 0.15); this->actorHealthLabel.setPosition(statsLabelPosX, y + statsOffsetY);