Skip to content

Commit

Permalink
Observer now used as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed Jun 11, 2024
1 parent f5e915c commit 4abfd9a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/include/FightEnv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#include "Animations/IncomingBanner.hpp"
#include "Game.hpp"

/*
* This class has the UI components and therefore provides these to the different fight phases
* Not an ideal solution
*/

class FightEnv {
public:
FightEnv();
Expand Down
12 changes: 7 additions & 5 deletions src/include/ObserverPattern/Observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,19 @@ class Subject {
return &obs.get() == &observer;
});
}

virtual void notify(T entity) const {
for (const auto& obs: observers) {
obs.get().update(entity);
}
}

bool valid = true;

private:
std::list<RefObserver> observers;

protected:
void notify(T entity) const {
for (const auto& obs: observers) {
obs.get().update(entity);
}
}
};

#endif
2 changes: 1 addition & 1 deletion src/main/UIObjects/UIEnemyOverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void UIEnemyOverview::draw() {
void UIEnemyOverview::changeHealth(int lostHealth) {
int newHealth = std::max(this->enemy.health - lostHealth, 0);
this->enemy.health = newHealth;
this->enemyStats.update(this->enemy);
this->enemy.notify(this->enemy);
}

void UIEnemyOverview::updatePickedColorText(std::string newText, sf::Color pickedColor) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/UIObjects/UIPlayerOverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(this->player);
this->player.notify(this->player);
}

void UIPlayerOverview::draw() {
Expand Down

0 comments on commit 4abfd9a

Please sign in to comment.