Skip to content

Commit

Permalink
generic Observer is working?
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed Jun 11, 2024
1 parent d28ba82 commit f5e915c
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 120 deletions.
13 changes: 0 additions & 13 deletions src/include/Activities/FightActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,8 @@
#include <SFML/Graphics.hpp>

#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"
Expand Down
3 changes: 2 additions & 1 deletion src/include/Actors/Actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ struct RGB {
int blue = 0;
};

class Actor: public Subject {

class Actor: public Subject<Actor> {
public:
std::string name;
int health;
Expand Down
2 changes: 0 additions & 2 deletions src/include/Actors/Enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/include/Actors/Player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
54 changes: 43 additions & 11 deletions src/include/ObserverPattern/Observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,70 @@
#include <iostream>
#include <memory>

template <typename T>
class Subject;

template <typename T>
class Observer {
public:
Observer(Subject& subject);
virtual ~Observer();
Observer(Subject<T>& _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<T>& subject;
};

template <typename T>
class Subject {
public:
~Subject();
using RefObserver = std::reference_wrapper<Observer>;
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<Observer<T>>;
void attachObserver(Observer<T>& observer) {
this->observers.push_front(observer);
}

void detachObserver(Observer<T> &observer) {
this->observers.remove_if([&observer] (const RefObserver& obs) {

return &obs.get() == &observer;
});
}
bool valid = true;

private:
std::list<RefObserver> observers;

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

#endif
8 changes: 0 additions & 8 deletions src/include/ObserverPattern/Subject.hpp

This file was deleted.

6 changes: 2 additions & 4 deletions src/include/UIElements/UIStats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@
#include "Game.hpp"
#include "ObserverPattern/Observer.hpp"

class UIStats: public UIElement, Observer {
class UIStats: public UIElement, Observer<Actor> {
public:
~UIStats();
UIStats(std::shared_ptr<Actor> 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");
Expand Down
2 changes: 1 addition & 1 deletion src/include/UIObjects/UIEnemyOverview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
8 changes: 1 addition & 7 deletions src/main/Actors/Actor.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
#include "Actors/Actor.hpp"

Actor::Actor() {}


/*Observer::Observer(Actor& subject_): subject(subject_) {
this->subject.attachObserver(*this);
}
*/
Actor::Actor() {}
4 changes: 0 additions & 4 deletions src/main/Actors/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down
6 changes: 1 addition & 5 deletions src/main/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
51 changes: 1 addition & 50 deletions src/main/ObserverPattern/Observer.cpp
Original file line number Diff line number Diff line change
@@ -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;
});
}
#include "ObserverPattern/Observer.hpp"
2 changes: 0 additions & 2 deletions src/main/ObserverPattern/Subject.cpp

This file was deleted.

10 changes: 3 additions & 7 deletions src/main/UIElements/UIStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
UIStats::~UIStats() {
}

UIStats::UIStats(std::shared_ptr<Actor> actor): Observer(*actor) {
UIStats::UIStats(std::shared_ptr<Actor> actor): Observer(reinterpret_cast<Subject<Actor> &>(*actor)) {
Game& game = Game::getInstance();
sf::Vector2u windowSize = game.gameWindow.getSize();
sf::Color statsValueFontColor = sf::Color::Yellow;
Expand Down Expand Up @@ -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));
}
6 changes: 3 additions & 3 deletions src/main/UIObjects/UIEnemyOverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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(newHealth);
this->uiPlayerStats.update(this->player);
}

void UIPlayerOverview::draw() {
Expand Down

0 comments on commit f5e915c

Please sign in to comment.