Skip to content

Commit

Permalink
refactor: continued implementing observer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed May 13, 2024
1 parent b07a6c4 commit bfa6414
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 43 deletions.
1 change: 0 additions & 1 deletion src/include/Actors/Actor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <list>

#include "ObserverPattern/Observer.hpp"
#include "ObserverPattern/Subject.hpp"

struct RGB {
int red = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/include/FightEnv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class FightEnv {

sf::Texture backgroundTX;
sf::Sprite backgroundSP;
UIStats playerStatsBox;
//UIStats playerStatsBox;
UIEnemyOverview enemyOverview;
UIPlayerOverview playerOverview;
TextFadingManager textFadingManager;
Expand Down
22 changes: 19 additions & 3 deletions src/include/ObserverPattern/Observer.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
#ifndef OBSERVER_HPP
#define OBSERVER_HPP

class Actor;
#include <list>
#include <iostream>

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<Observer> observers;

protected:
virtual void notify(int newValue);
};

#endif
12 changes: 0 additions & 12 deletions src/include/ObserverPattern/Subject.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
#ifndef SUBJECT_HPP
#define SUBJECT_HPP

#include <list>

#include "ObserverPattern/Observer.hpp"

class Subject {
public:
void attachObserver(Observer& observer);
void detachObserver(Observer& observer);

private:
std::list<Observer> observers;

protected:
virtual void notify(int newValue);
};

#endif
2 changes: 2 additions & 0 deletions src/include/UIElements/UIStats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class UIStats: public UIElement, Observer {
public:
UIStats();
~UIStats();
void init(Actor actor);
void draw() override;
sf::Vector2f getPosition() override;
Expand Down
4 changes: 3 additions & 1 deletion src/include/UIObjects/UIPlayerOverview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

class UIPlayerOverview {
public:
UIBorderedImage playerFrame;
UIPlayerOverview() {}

UIBorderedImage playerFrame;
Player player;

void init();
Expand Down
4 changes: 2 additions & 2 deletions src/main/Activities/FightActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
7 changes: 2 additions & 5 deletions src/main/Actors/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
*/
2 changes: 1 addition & 1 deletion src/main/FightEnv.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "FightEnv.hpp"

FightEnv::FightEnv() {
playerStatsBox.init(Game::getInstance().player);
//playerStatsBox.init(Game::getInstance().player);
}
28 changes: 27 additions & 1 deletion src/main/ObserverPattern/Observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,30 @@

//constructor and destructor defined in ObserverPattern/Subject.cpp

void Observer::onNotify(int newValue) {}
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;
});
}
15 changes: 0 additions & 15 deletions src/main/ObserverPattern/Subject.cpp
Original file line number Diff line number Diff line change
@@ -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;
});
}
6 changes: 5 additions & 1 deletion src/main/UIElements/UIStats.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "UIElements/UIStats.hpp"

UIStats::UIStats() {
}

UIStats::~UIStats() {
}

void UIStats::init(Actor actor) {
actor.attachObserver(*this);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit bfa6414

Please sign in to comment.