Skip to content

Commit

Permalink
changed stuff back and forth. I think I am on a good way right now wi…
Browse files Browse the repository at this point in the history
…th the observer
  • Loading branch information
Ipagaxi committed May 28, 2024
1 parent 2d093cd commit aa7be0b
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 26 deletions.
3 changes: 3 additions & 0 deletions src/include/Activities/FightActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ class FightActivity: public Activity {
void runCurrentState(Game &game);

private:
/*
* fightEnv is a personal current view state of FightActivity. Therefore, can not be included in Game.hpp or a derived class
*/
FightEnv fightEnv;
FightStateEnum currentFightStateEnum = FightStateEnum::TURN_CHANGE;
std::shared_ptr<Enemy> enemy;
Expand Down
2 changes: 1 addition & 1 deletion src/include/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Game {
GameStatus gameStatus;
sf::RenderWindow& gameWindow;

Player player = Player("Ipagaxi", 100, 12, {100, 100, 100}, "default_actor_quer.png");
std::shared_ptr<Player> player = std::make_shared<Player>(Player("Ipagaxi", 100, 12, {100, 100, 100}, "default_actor_quer.png"));

static Game& getInstance();

Expand Down
1 change: 1 addition & 0 deletions src/include/GameStatus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <SFML/Graphics.hpp>


class GameStatus {
public:
sf::Time elapsedTime;
Expand Down
15 changes: 10 additions & 5 deletions src/include/ObserverPattern/Observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ class Observer {
//Observer(Actor& subject);
virtual ~Observer();
virtual void onNotify(int newValue);
void invalidateSubject();

private:
Subject* subject;
Subject* subject;
};

class Subject {
public:
void attachObserver(Observer& observer);
void detachObserver(Observer& observer);
~Subject();
using RefObserver = std::reference_wrapper<Observer>;
void attachObserver(Observer& observer);
void detachObserver(Observer& observer);
bool valid = true;

private:
std::list<Observer> observers;
std::list<RefObserver> observers;

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

#endif
2 changes: 1 addition & 1 deletion src/include/UIElements/UIStats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class UIStats: public UIElement, Observer {
public:
~UIStats();
UIStats(Actor& actor);
UIStats(std::shared_ptr<Actor> actor);
void init(Actor actor);
void draw() override;
sf::Vector2f getPosition() override;
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 @@ -13,7 +13,7 @@

class UIEnemyOverview {
public:
UIEnemyOverview(Enemy& enemy);
UIEnemyOverview(std::shared_ptr<Enemy> enemy);
Enemy enemy;
UIColorPicker colorPicker = UIColorPicker("colorPIC_default.png", "color_picker_border.png");
sf::Text pickedColorText;
Expand Down
3 changes: 1 addition & 2 deletions src/main/Activities/FightActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::make_unique<TurnChangeState>(fightEnv)) {
Game& game = Game::getInstance();
this->enemy = std::make_shared<Enemy>(this->initEnemy());
this->fightEnv.enemyOverview = std::make_unique<UIEnemyOverview>(*this->enemy);
this->fightEnv.enemyOverview = std::make_unique<UIEnemyOverview>(this->enemy);
this->fightEnv.playerOverview.init();

this->fightEnv.backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/background_fight.png");
Expand Down Expand Up @@ -42,7 +42,6 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::
}

FightActivity::~FightActivity() {
//delete this->fightEnv.enemyOverview;
this->fightEnv.backgroundMusic.stop();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/FightStates/PlayersTurn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FightStateEnum PlayersTurn::run(FightEnv &fightEnv) {
fightEnv.pickedColor = fightEnv.enemyOverview->colorPicker.getPixelColor(clickedPos);
fightEnv.enemyOverview->updatePickedColorText("(" + std::to_string(fightEnv.pickedColor.r) + ", " + std::to_string(fightEnv.pickedColor.g) + ", " + std::to_string(fightEnv.pickedColor.b) + ")", fightEnv.pickedColor);
float attackMultiplier = this->calculateAttackMult(fightEnv);
int damage = game.player.attackStrength * attackMultiplier;
int damage = game.player->attackStrength * attackMultiplier;
int millSecToLive = 600;
fightEnv.textFadingManager.startAnimation(std::to_string(damage), clickedPos, sf::Color::Yellow, game.gameWindow.getSize().y * 0.05, AnimationPath::Parabel, millSecToLive);
fightEnv.enemyOverview->changeHealth(damage);
Expand Down
22 changes: 18 additions & 4 deletions src/main/ObserverPattern/Observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,39 @@ Observer::Observer() {

Observer::~Observer() {
std::cout << "~Observer" << std::endl;
this->subject->detachObserver(*this);
if (subject && subject->valid) {
subject->detachObserver(*this);
}
}

void Observer::onNotify(int newValue) {}

void Observer::invalidateSubject() {
this->subject = nullptr;
}

Subject::~Subject() {
this->valid = false;
for (RefObserver& obs : observers) {
obs.get().invalidateSubject();
}
}


void Subject::notify(int newValue) {
for (Observer obs: observers) {
obs.onNotify(newValue);
}
}

void Subject::attachObserver(Observer &observer) {
std::cout << "Observer attached" << std::endl;
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;
this->observers.remove_if([&observer] (const RefObserver& obs) {

return &obs.get() == &observer;
});
}
12 changes: 6 additions & 6 deletions src/main/UIElements/UIStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ UIStats::~UIStats() {
std::cout << "~UIStats" << std::endl;
}

UIStats::UIStats(Actor& actor) {
UIStats::UIStats(std::shared_ptr<Actor> actor) {
std::cout << "UIStats(actor)" << std::endl;
actor.attachObserver(*this);
actor->attachObserver(*this);
Game& game = Game::getInstance();
sf::Vector2u windowSize = game.gameWindow.getSize();
sf::Color statsValueFontColor = sf::Color::Yellow;
Expand All @@ -20,7 +20,7 @@ UIStats::UIStats(Actor& actor) {
this->actorStatsBox.setBackgroundMargin(actorStatsBoxSize.width * 0.1, actorStatsBoxSize.height * 0.04);

this->actorName.setFont(game.mainFont);
this->actorName.setString(actor.name);
this->actorName.setString(actor->name);
this->actorName.setCharacterSize(windowSize.y*0.02);
this->actorName.setFillColor(sf::Color::White);

Expand All @@ -30,7 +30,7 @@ UIStats::UIStats(Actor& actor) {
this->actorHealthLabel.setFillColor(statsLabelFontColor);

this->actorHealthValue.setFont(game.mainFont);
this->actorHealthValue.setString(std::to_string(actor.health));
this->actorHealthValue.setString(std::to_string(actor->health));
this->actorHealthValue.setCharacterSize(statsTextHeight);
this->actorHealthValue.setFillColor(statsValueFontColor);

Expand All @@ -40,7 +40,7 @@ UIStats::UIStats(Actor& actor) {
this->actorAttackStrengthLabel.setFillColor(statsLabelFontColor);

this->actorAttackStrengthValue.setFont(game.mainFont);
this->actorAttackStrengthValue.setString(std::to_string(actor.attackStrength));
this->actorAttackStrengthValue.setString(std::to_string(actor->attackStrength));
this->actorAttackStrengthValue.setCharacterSize(statsTextHeight);
this->actorAttackStrengthValue.setFillColor(statsValueFontColor);

Expand All @@ -50,7 +50,7 @@ UIStats::UIStats(Actor& actor) {
this->actorRGBDefenseLabel.setFillColor(statsLabelFontColor);

this->actorRGBDefenseValues.setFont(game.mainFont);
this->actorRGBDefenseValues.setString("(" + std::to_string(actor.defense.red) + ", " + std::to_string(actor.defense.green) + ", " + std::to_string(actor.defense.blue) + ")");
this->actorRGBDefenseValues.setString("(" + std::to_string(actor->defense.red) + ", " + std::to_string(actor->defense.green) + ", " + std::to_string(actor->defense.blue) + ")");
this->actorRGBDefenseValues.setCharacterSize(statsTextHeight);
this->actorRGBDefenseValues.setFillColor(statsValueFontColor);

Expand Down
6 changes: 2 additions & 4 deletions src/main/UIObjects/UIEnemyOverview.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#include "UIObjects/UIEnemyOverview.hpp"

UIEnemyOverview::UIEnemyOverview(Enemy &_enemy): enemyBorderedImage("monster_landscape_cut/" + _enemy.picPath, "actor_borders/fight_border.png"), enemyStats(_enemy) {
UIEnemyOverview::UIEnemyOverview(std::shared_ptr<Enemy> _enemy): enemyBorderedImage("monster_landscape_cut/" + (*_enemy).picPath, "actor_borders/fight_border.png"), enemyStats(_enemy) {
Game& game = Game::getInstance();
this->enemy = _enemy;
//this->enemyBorderedImage.init("monster_landscape_cut/" + enemy.picPath, "actor_borders/fight_border.png");
//this->enemyStats.init(_enemy);
this->enemy = *_enemy;

sf::Vector2u windowSize = game.gameWindow.getSize();
sf::FloatRect boxRect = this->box.getSize();
Expand Down
2 changes: 1 addition & 1 deletion src/main/UIObjects/UIPlayerOverview.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "UIObjects/UIPlayerOverview.hpp"

UIPlayerOverview::UIPlayerOverview(): uiPlayerStats(Game::getInstance().player), playerFrame("monster_landscape_cut/" + Game::getInstance().player.picPath, "actor_borders/fight_border.png") {
UIPlayerOverview::UIPlayerOverview(): uiPlayerStats(Game::getInstance().player), playerFrame("monster_landscape_cut/" + Game::getInstance().player->picPath, "actor_borders/fight_border.png") {

}

Expand Down

0 comments on commit aa7be0b

Please sign in to comment.