diff --git a/src/include/Activities/FightActivity.hpp b/src/include/Activities/FightActivity.hpp index 970b074..047b375 100644 --- a/src/include/Activities/FightActivity.hpp +++ b/src/include/Activities/FightActivity.hpp @@ -9,21 +9,8 @@ #include #include "Activities/Activity.hpp" -#include "ActivityEnum.hpp" -#include "Animations/TextFadingManager.hpp" -#include "Animations/IncomingBanner.hpp" -#include "UIElements/UIStats.hpp" -#include "UIObjects/UIEnemyOverview.hpp" -#include "UIObjects/UIPlayerOverview.hpp" -#include "UIElements/UIColorPicker.hpp" -#include "UIElements/UIButton.hpp" -#include "UIElements/UIBox.hpp" -#include "Actors/Player.hpp" -#include "PerlinNoise.hpp" #include "Game.hpp" #include "Defines.hpp" -#include "Actors/Enemy.hpp" -#include "Color.hpp" #include "FightEnv.hpp" #include "FightStates/PlayersTurn.hpp" #include "FightStates/EnemiesTurn.hpp" diff --git a/src/include/Actors/Actor.hpp b/src/include/Actors/Actor.hpp index 8ca485b..ad8af63 100644 --- a/src/include/Actors/Actor.hpp +++ b/src/include/Actors/Actor.hpp @@ -12,7 +12,8 @@ struct RGB { int blue = 0; }; -class Actor: public Subject { + +class Actor: public Subject { public: std::string name; int health; diff --git a/src/include/Actors/Enemy.hpp b/src/include/Actors/Enemy.hpp index 958a0f1..1188cc5 100644 --- a/src/include/Actors/Enemy.hpp +++ b/src/include/Actors/Enemy.hpp @@ -10,8 +10,6 @@ class Enemy: public Actor { Enemy(); Enemy(std::string name, int health, int attackStrength, RGB defense, std::string picPath, std::string colorPicPath, std::string colorPicBorderPath); - void notify(int newValue) const override; - void init(); std::string colorPicPath; std::string colorPicBorderPath; diff --git a/src/include/Actors/Player.hpp b/src/include/Actors/Player.hpp index 91257bf..689b84e 100644 --- a/src/include/Actors/Player.hpp +++ b/src/include/Actors/Player.hpp @@ -11,7 +11,7 @@ class Player: public Actor { Player(); Player(std::string name, int health, int attackStrength, RGB defense, std::string picFilePath); - void notify(int newValue) const override; + }; #endif \ No newline at end of file diff --git a/src/include/ObserverPattern/Observer.hpp b/src/include/ObserverPattern/Observer.hpp index 670cbe1..3202344 100644 --- a/src/include/ObserverPattern/Observer.hpp +++ b/src/include/ObserverPattern/Observer.hpp @@ -5,38 +5,70 @@ #include #include +template class Subject; +template class Observer { public: - Observer(Subject& subject); - virtual ~Observer(); + Observer(Subject& _subject): subject(_subject) { + this->subject.attachObserver(*this); + std::cout << "Observer" << std::endl; + } + + virtual ~Observer() { + if (this->valid) { + subject.detachObserver(*this); + } + } Observer(const Observer&) = delete; // rule of three Observer& operator=(const Observer&) = delete; - virtual void update(int newValue); - void invalidateSubject(); - bool subjectIsValid() const; + virtual void update(T entity) { + std::cout << "Got a notification" << std::endl; + } + + void invalidateSubject() { + this->valid = false; + } private: bool valid = true; - Subject& subject; + Subject& subject; }; +template class Subject { public: - ~Subject(); - using RefObserver = std::reference_wrapper; - void attachObserver(Observer& observer); - void detachObserver(Observer& observer); + virtual ~Subject() { + this->valid = false; + for (auto& obs : this->observers) { + obs.get().invalidateSubject(); + } + }; + using RefObserver = std::reference_wrapper>; + void attachObserver(Observer& observer) { + this->observers.push_front(observer); + } + + void detachObserver(Observer &observer) { + this->observers.remove_if([&observer] (const RefObserver& obs) { + + return &obs.get() == &observer; + }); + } bool valid = true; private: std::list observers; protected: - virtual void notify(int newValue) const = 0; + void notify(T entity) const { + for (const auto& obs: observers) { + obs.get().update(entity); + } + } }; #endif \ No newline at end of file diff --git a/src/include/ObserverPattern/Subject.hpp b/src/include/ObserverPattern/Subject.hpp deleted file mode 100644 index 47d6d13..0000000 --- a/src/include/ObserverPattern/Subject.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SUBJECT_HPP -#define SUBJECT_HPP - -#include "ObserverPattern/Observer.hpp" - - - -#endif \ No newline at end of file diff --git a/src/include/UIElements/UIStats.hpp b/src/include/UIElements/UIStats.hpp index 33ee5ec..1c6eb47 100644 --- a/src/include/UIElements/UIStats.hpp +++ b/src/include/UIElements/UIStats.hpp @@ -9,18 +9,16 @@ #include "Game.hpp" #include "ObserverPattern/Observer.hpp" -class UIStats: public UIElement, Observer { +class UIStats: public UIElement, Observer { public: ~UIStats(); UIStats(std::shared_ptr actor); - //void init(Actor actor); void draw() override; sf::Vector2f getPosition() override; void setPosition(float x, float y) override; sf::FloatRect getSize() override; - void updateHealth(int value); - void update(int newValue) override; + void update(Actor newActor) override; private: UIBox actorStatsBox = UIBox(sf::Color(51, 25, 0, 150), "borders/border_stats.png"); diff --git a/src/include/UIObjects/UIEnemyOverview.hpp b/src/include/UIObjects/UIEnemyOverview.hpp index 4f85aab..795684c 100644 --- a/src/include/UIObjects/UIEnemyOverview.hpp +++ b/src/include/UIObjects/UIEnemyOverview.hpp @@ -19,7 +19,7 @@ class UIEnemyOverview { sf::Text pickedColorText; //void init(Enemy enemy); - void changeHealth(int value); + void changeHealth(int lostHealth); void updatePickedColorText(std::string newText, sf::Color pickedColor); void draw(); diff --git a/src/main/Actors/Actor.cpp b/src/main/Actors/Actor.cpp index 1dd9288..0db391d 100644 --- a/src/main/Actors/Actor.cpp +++ b/src/main/Actors/Actor.cpp @@ -1,9 +1,3 @@ #include "Actors/Actor.hpp" -Actor::Actor() {} - - -/*Observer::Observer(Actor& subject_): subject(subject_) { - this->subject.attachObserver(*this); -} -*/ \ No newline at end of file +Actor::Actor() {} \ No newline at end of file diff --git a/src/main/Actors/Enemy.cpp b/src/main/Actors/Enemy.cpp index cf5d996..35671a1 100644 --- a/src/main/Actors/Enemy.cpp +++ b/src/main/Actors/Enemy.cpp @@ -20,10 +20,6 @@ Enemy::Enemy(std::string _name, int _health, int _attackStrength, RGB _defense, this->colorPicBorderPath = _colorPicBorderPath; } -void Enemy::notify(int newValue) const { - std::cout << "Got a notification" << std::endl; -} - void Enemy::init() { diff --git a/src/main/Actors/Player.cpp b/src/main/Actors/Player.cpp index b1c65fd..15af727 100644 --- a/src/main/Actors/Player.cpp +++ b/src/main/Actors/Player.cpp @@ -14,8 +14,4 @@ Player::Player(std::string _name, int _health, int _attackStrength, RGB _defense this->attackStrength = _attackStrength; this->defense = _defense; this->picPath = _picFilePath; -} - -void Player::notify(int newValue) const { - std::cout << "Player changed!" << std::endl; -} +} \ No newline at end of file diff --git a/src/main/ObserverPattern/Observer.cpp b/src/main/ObserverPattern/Observer.cpp index 9c20781..1366e2a 100644 --- a/src/main/ObserverPattern/Observer.cpp +++ b/src/main/ObserverPattern/Observer.cpp @@ -1,50 +1 @@ -#include "ObserverPattern/Observer.hpp" - -Observer::Observer(Subject& _subject): subject(_subject) { - subject.attachObserver(*this); - std::cout << "Observer" << std::endl; -} - -Observer::~Observer() { - if (this->valid) { - subject.detachObserver(*this); - } -} - -void Observer::update(int newValue) { - std::cout << "Got a notification" << std::endl; -} - - -void Observer::invalidateSubject() { - this->valid = false; -} - -bool Observer::subjectIsValid() const { - return this->valid; -} - -Subject::~Subject() { - this->valid = false; - for (auto& obs : observers) { - obs.get().invalidateSubject(); - } -} - - -void Subject::notify(int newValue) const { - for (const auto& obs: observers) { - obs.get().update(newValue); - } -} - -void Subject::attachObserver(Observer& observer) { - this->observers.push_front(observer); -} - -void Subject::detachObserver(Observer &observer) { - this->observers.remove_if([&observer] (const RefObserver& obs) { - - return &obs.get() == &observer; - }); -} \ No newline at end of file +#include "ObserverPattern/Observer.hpp" \ No newline at end of file diff --git a/src/main/ObserverPattern/Subject.cpp b/src/main/ObserverPattern/Subject.cpp deleted file mode 100644 index b23bf96..0000000 --- a/src/main/ObserverPattern/Subject.cpp +++ /dev/null @@ -1,2 +0,0 @@ -#include "ObserverPattern/Subject.hpp" - diff --git a/src/main/UIElements/UIStats.cpp b/src/main/UIElements/UIStats.cpp index e0ebd88..6fd32ef 100644 --- a/src/main/UIElements/UIStats.cpp +++ b/src/main/UIElements/UIStats.cpp @@ -3,7 +3,7 @@ UIStats::~UIStats() { } -UIStats::UIStats(std::shared_ptr actor): Observer(*actor) { +UIStats::UIStats(std::shared_ptr actor): Observer(reinterpret_cast &>(*actor)) { Game& game = Game::getInstance(); sf::Vector2u windowSize = game.gameWindow.getSize(); sf::Color statsValueFontColor = sf::Color::Yellow; @@ -94,10 +94,6 @@ sf::FloatRect UIStats::getSize() { return this->actorStatsBox.getSize(); } -void UIStats::updateHealth(int value) { - this->actorHealthValue.setString(std::to_string(value)); -} - -void UIStats::update(int newValue) { - this->actorHealthValue.setString(std::to_string(newValue)); +void UIStats::update(Actor newActor) { + this->actorHealthValue.setString(std::to_string(newActor.health)); } diff --git a/src/main/UIObjects/UIEnemyOverview.cpp b/src/main/UIObjects/UIEnemyOverview.cpp index 5c8a952..3f229fd 100644 --- a/src/main/UIObjects/UIEnemyOverview.cpp +++ b/src/main/UIObjects/UIEnemyOverview.cpp @@ -54,10 +54,10 @@ void UIEnemyOverview::draw() { this->enemyBorderedImage.draw(); } -void UIEnemyOverview::changeHealth(int value) { - int newHealth = std::max(this->enemy.health - value, 0); +void UIEnemyOverview::changeHealth(int lostHealth) { + int newHealth = std::max(this->enemy.health - lostHealth, 0); this->enemy.health = newHealth; - this->enemyStats.update(newHealth); + this->enemyStats.update(this->enemy); } void UIEnemyOverview::updatePickedColorText(std::string newText, sf::Color pickedColor) { diff --git a/src/main/UIObjects/UIPlayerOverview.cpp b/src/main/UIObjects/UIPlayerOverview.cpp index de141e5..527968e 100644 --- a/src/main/UIObjects/UIPlayerOverview.cpp +++ b/src/main/UIObjects/UIPlayerOverview.cpp @@ -57,7 +57,7 @@ void UIPlayerOverview::init() { void UIPlayerOverview::changeHealth(int value) { int newHealth = std::max(this->player.health - value, 0); this->player.health = newHealth; - this->uiPlayerStats.update(newHealth); + this->uiPlayerStats.update(this->player); } void UIPlayerOverview::draw() {