From 5cfda221dd3c468935fb96c78fb075b470ec8266 Mon Sep 17 00:00:00 2001 From: Ipagaxi Date: Thu, 30 May 2024 23:27:29 +0200 Subject: [PATCH] Observer finally works! --- src/include/FightEnv.hpp | 4 +- src/include/ObserverPattern/Observer.hpp | 2 +- src/include/UIElements/UIStats.hpp | 2 +- src/include/UIObjects/UIPlayerOverview.hpp | 2 +- src/main/Activities/FightActivity.cpp | 7 +-- src/main/Actors/Player.cpp | 2 +- src/main/FightStates/EnemiesTurn.cpp | 6 +-- src/main/ObserverPattern/Observer.cpp | 3 +- src/main/UIElements/UIStats.cpp | 60 +--------------------- src/main/UIObjects/UIEnemyOverview.cpp | 52 +------------------ src/main/UIObjects/UIPlayerOverview.cpp | 29 +++++++++-- 11 files changed, 39 insertions(+), 130 deletions(-) diff --git a/src/include/FightEnv.hpp b/src/include/FightEnv.hpp index 846670a..4fcbb7a 100644 --- a/src/include/FightEnv.hpp +++ b/src/include/FightEnv.hpp @@ -15,10 +15,8 @@ class FightEnv { sf::Texture backgroundTX; sf::Sprite backgroundSP; - //UIStats playerStatsBox; - //UIEnemyOverview* enemyOverview; std::unique_ptr enemyOverview; - UIPlayerOverview playerOverview; + std::unique_ptr playerOverview; TextFadingManager textFadingManager; sf::Texture playersTurnTX; sf::Texture enemiesTurnTX; diff --git a/src/include/ObserverPattern/Observer.hpp b/src/include/ObserverPattern/Observer.hpp index 4e2431d..670cbe1 100644 --- a/src/include/ObserverPattern/Observer.hpp +++ b/src/include/ObserverPattern/Observer.hpp @@ -15,7 +15,7 @@ class Observer { Observer(const Observer&) = delete; // rule of three Observer& operator=(const Observer&) = delete; - virtual void update(int newValue) const; + virtual void update(int newValue); void invalidateSubject(); bool subjectIsValid() const; diff --git a/src/include/UIElements/UIStats.hpp b/src/include/UIElements/UIStats.hpp index 18e214e..33ee5ec 100644 --- a/src/include/UIElements/UIStats.hpp +++ b/src/include/UIElements/UIStats.hpp @@ -20,7 +20,7 @@ class UIStats: public UIElement, Observer { sf::FloatRect getSize() override; void updateHealth(int value); - void update(int newValue) const override; + void update(int newValue) override; private: UIBox actorStatsBox = UIBox(sf::Color(51, 25, 0, 150), "borders/border_stats.png"); diff --git a/src/include/UIObjects/UIPlayerOverview.hpp b/src/include/UIObjects/UIPlayerOverview.hpp index 4a55aaa..6843159 100644 --- a/src/include/UIObjects/UIPlayerOverview.hpp +++ b/src/include/UIObjects/UIPlayerOverview.hpp @@ -11,7 +11,7 @@ class UIPlayerOverview { public: - UIPlayerOverview(); + UIPlayerOverview(std::shared_ptr player); UIBorderedImage playerFrame; Player player; diff --git a/src/main/Activities/FightActivity.cpp b/src/main/Activities/FightActivity.cpp index e10b386..ea0421f 100644 --- a/src/main/Activities/FightActivity.cpp +++ b/src/main/Activities/FightActivity.cpp @@ -5,7 +5,7 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std:: Game& game = Game::getInstance(); this->enemy = std::make_shared(this->initEnemy()); this->fightEnv.enemyOverview = std::make_unique(this->enemy); - this->fightEnv.playerOverview.init(); + this->fightEnv.playerOverview = std::make_unique(Game::getInstance().player); this->fightEnv.backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/background_fight.png"); this->fightEnv.backgroundSP.setTexture(this->fightEnv.backgroundTX); @@ -20,9 +20,6 @@ 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); - std::random_device randSeed; std::mt19937 gen(randSeed()); std::uniform_int_distribution dist(0, 1); @@ -71,7 +68,7 @@ ActivityEnum FightActivity::executeActivity() { game.gameWindow.draw(this->fightEnv.turnSP); game.gameWindow.draw(this->fightEnv.backgroundSP); - this->fightEnv.playerOverview.draw(); + this->fightEnv.playerOverview->draw(); this->fightEnv.enemyOverview->draw(); this->exitButton.draw(); this->fightEnv.textFadingManager.run(); diff --git a/src/main/Actors/Player.cpp b/src/main/Actors/Player.cpp index 3b94787..b1c65fd 100644 --- a/src/main/Actors/Player.cpp +++ b/src/main/Actors/Player.cpp @@ -17,5 +17,5 @@ Player::Player(std::string _name, int _health, int _attackStrength, RGB _defense } void Player::notify(int newValue) const { - std::cout << "Got a notification" << std::endl; + std::cout << "Player changed!" << std::endl; } diff --git a/src/main/FightStates/EnemiesTurn.cpp b/src/main/FightStates/EnemiesTurn.cpp index d749233..1fa1e1d 100644 --- a/src/main/FightStates/EnemiesTurn.cpp +++ b/src/main/FightStates/EnemiesTurn.cpp @@ -14,12 +14,12 @@ FightStateEnum EnemiesTurn::run(FightEnv &fightEnv) { std::uniform_int_distribution dist(minDamage, maxDamage); int enemyDamage = dist(gen); int millSecToLive = 600; - sf::Vector2f playerIconPos = fightEnv.playerOverview.playerFrame.getPosition(); - sf::FloatRect playerIconSize = fightEnv.playerOverview.playerFrame.getSize(); + sf::Vector2f playerIconPos = fightEnv.playerOverview->playerFrame.getPosition(); + sf::FloatRect playerIconSize = fightEnv.playerOverview->playerFrame.getSize(); sf::Vector2f damagePos = sf::Vector2f(playerIconPos.x + (playerIconSize.width * 0.5), playerIconPos.y + (playerIconSize.height * 0.5)); fightEnv.textFadingManager.startAnimation(std::to_string(enemyDamage), damagePos, sf::Color::Yellow, game.gameWindow.getSize().y * 0.05, AnimationPath::Parabel, millSecToLive); - fightEnv.playerOverview.changeHealth(enemyDamage); + fightEnv.playerOverview->changeHealth(enemyDamage); fightEnv.enemyDamageCalculated = true; } if (fightEnv.textFadingManager.fadingText.pastMillSec >= fightEnv.textFadingManager.fadingText.millSecToLive) { diff --git a/src/main/ObserverPattern/Observer.cpp b/src/main/ObserverPattern/Observer.cpp index 195b354..9c20781 100644 --- a/src/main/ObserverPattern/Observer.cpp +++ b/src/main/ObserverPattern/Observer.cpp @@ -11,7 +11,7 @@ Observer::~Observer() { } } -void Observer::update(int newValue) const { +void Observer::update(int newValue) { std::cout << "Got a notification" << std::endl; } @@ -43,7 +43,6 @@ void Subject::attachObserver(Observer& observer) { } void Subject::detachObserver(Observer &observer) { - std::cout << "Size: " << this->observers.size() << std::endl; this->observers.remove_if([&observer] (const RefObserver& obs) { return &obs.get() == &observer; diff --git a/src/main/UIElements/UIStats.cpp b/src/main/UIElements/UIStats.cpp index d4778e2..e0ebd88 100644 --- a/src/main/UIElements/UIStats.cpp +++ b/src/main/UIElements/UIStats.cpp @@ -1,12 +1,9 @@ #include "UIElements/UIStats.hpp" UIStats::~UIStats() { - std::cout << "~UIStats" << std::endl; } UIStats::UIStats(std::shared_ptr actor): Observer(*actor) { - std::cout << "UIStats(actor)" << std::endl; - //actor->attachObserver(*this); Game& game = Game::getInstance(); sf::Vector2u windowSize = game.gameWindow.getSize(); sf::Color statsValueFontColor = sf::Color::Yellow; @@ -57,59 +54,6 @@ UIStats::UIStats(std::shared_ptr actor): Observer(*actor) { this->setPosition(0., 0.); } -/* -void UIStats::init(Actor actor) { - actor.attachObserver(*this); - Game& game = Game::getInstance(); - sf::Vector2u windowSize = game.gameWindow.getSize(); - sf::Color statsValueFontColor = sf::Color::Yellow; - sf::Color statsLabelFontColor = sf::Color::White; - this->statsTextHeight = windowSize.y * 0.015; - - float scale = (windowSize.y * 0.4) / this->actorStatsBox.getSize().height; - this->actorStatsBox.scale(scale, scale); - - sf::FloatRect actorStatsBoxSize = this->actorStatsBox.getSize(); - this->actorStatsBox.setBackgroundMargin(actorStatsBoxSize.width * 0.1, actorStatsBoxSize.height * 0.04); - - this->actorName.setFont(game.mainFont); - this->actorName.setString(actor.name); - this->actorName.setCharacterSize(windowSize.y*0.02); - this->actorName.setFillColor(sf::Color::White); - - this->actorHealthLabel.setFont(game.mainFont); - this->actorHealthLabel.setString("Health:"); - this->actorHealthLabel.setCharacterSize(statsTextHeight); - this->actorHealthLabel.setFillColor(statsLabelFontColor); - - this->actorHealthValue.setFont(game.mainFont); - this->actorHealthValue.setString(std::to_string(actor.health)); - this->actorHealthValue.setCharacterSize(statsTextHeight); - this->actorHealthValue.setFillColor(statsValueFontColor); - - this->actorAttackStrengthLabel.setFont(game.mainFont); - this->actorAttackStrengthLabel.setString("ATK:"); - this->actorAttackStrengthLabel.setCharacterSize(statsTextHeight); - this->actorAttackStrengthLabel.setFillColor(statsLabelFontColor); - - this->actorAttackStrengthValue.setFont(game.mainFont); - this->actorAttackStrengthValue.setString(std::to_string(actor.attackStrength)); - this->actorAttackStrengthValue.setCharacterSize(statsTextHeight); - this->actorAttackStrengthValue.setFillColor(statsValueFontColor); - - this->actorRGBDefenseLabel.setFont(game.mainFont); - this->actorRGBDefenseLabel.setString("DEF:"); - this->actorRGBDefenseLabel.setCharacterSize(statsTextHeight); - 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.setCharacterSize(statsTextHeight); - this->actorRGBDefenseValues.setFillColor(statsValueFontColor); - - this->setPosition(0., 0.); -}*/ - void UIStats::draw() { Game& game = Game::getInstance(); this->actorStatsBox.draw(); @@ -154,6 +98,6 @@ void UIStats::updateHealth(int value) { this->actorHealthValue.setString(std::to_string(value)); } -void UIStats::update(int newValue) const { - //this->actorHealthValue.setString(std::to_string(newValue)); +void UIStats::update(int newValue) { + this->actorHealthValue.setString(std::to_string(newValue)); } diff --git a/src/main/UIObjects/UIEnemyOverview.cpp b/src/main/UIObjects/UIEnemyOverview.cpp index 30d1b10..5c8a952 100644 --- a/src/main/UIObjects/UIEnemyOverview.cpp +++ b/src/main/UIObjects/UIEnemyOverview.cpp @@ -44,56 +44,6 @@ UIEnemyOverview::UIEnemyOverview(std::shared_ptr _enemy): enemyBorderedIm this->pickedColorText.setPosition(colorPickerPos.x + colorPickerSize.width*0.5, colorPickerPos.y + colorPickerSize.height + windowSize.y*0.02); } -/* -void UIEnemyOverview::init(Enemy _enemy) { - Game& game = Game::getInstance(); - this->enemy = _enemy; - this->enemyBorderedImage.init("monster_landscape_cut/" + enemy.picPath, "actor_borders/fight_border.png"); - std::cout << "before enemy stats init" << std::endl; - this->enemyStats.init(_enemy); - std::cout << "after enemy stats init" << std::endl; - - sf::Vector2u windowSize = game.gameWindow.getSize(); - sf::FloatRect boxRect = this->box.getSize(); - sf::Vector2f overviewPos = sf::Vector2f(windowSize.x * 0.51, windowSize.y * 0.1); - this->box.setPosition(overviewPos.x, overviewPos.y); - - float relativeOuterPaddingStatBoxes = 0.05; - sf::FloatRect statsBoxSize = this->enemyStats.getSize(); - this->enemyStats.setPosition(windowSize.x * (1.0 - relativeOuterPaddingStatBoxes) - statsBoxSize.width, windowSize.y * 0.35); - - sf::FloatRect creatureFrameRect = this->enemyBorderedImage.getSize(); - float creatureBoxScale = (windowSize.x*0.22)/creatureFrameRect.width; - this->enemyBorderedImage.scale(creatureBoxScale, creatureBoxScale); - creatureFrameRect = this->enemyBorderedImage.getSize(); - float creatureFrameMargin = boxRect.width * 0.05; - sf::Vector2f creatureFramePos = sf::Vector2f(overviewPos.x + creatureFrameMargin, overviewPos.y + creatureFrameMargin); - this->enemyBorderedImage.setPosition(creatureFramePos.x, creatureFramePos.y); - - this->enemyIconBackgroundTX.loadFromFile(RESOURCE_PATH "actor_landscape_backgrounds/forest.png"); - this->enemyIconBackgroundSP.setTexture(this->enemyIconBackgroundTX); - sf::FloatRect creatureBackgroundRect = this->enemyIconBackgroundSP.getGlobalBounds(); - this->enemyIconBackgroundSP.setOrigin(creatureBackgroundRect.width / 2.f, creatureBackgroundRect.height / 2.f); - this->enemyIconBackgroundSP.setPosition(creatureFramePos.x + creatureFrameRect.width / 2.f, creatureFramePos.y + creatureFrameRect.height / 2.f); - this->enemyIconBackgroundSP.scale(creatureBoxScale, creatureBoxScale); - - float colorPickerScale = creatureFrameRect.width / colorPicker.getSize().width; - this->colorPicker.scale(colorPickerScale, colorPickerScale); - this->colorPicker.setColorBox(this->enemy.colorPicPath, this->enemy.colorPicBorderPath); - sf::Vector2f colorPickerPos = sf::Vector2f(creatureFramePos.x, creatureFramePos.y + creatureFrameRect.height + boxRect.height * 0); - this->colorPicker.setPosition(colorPickerPos.x, colorPickerPos.y); - sf::FloatRect colorPickerSize = this->colorPicker.getSize(); - - this->pickedColorText.setFont(game.mainFont); - this->pickedColorText.setString("(0, 0, 0)"); - this->pickedColorText.setCharacterSize(game.gameWindow.getSize().y * 0.04); - this->pickedColorText.setFillColor(sf::Color::Yellow); - sf::FloatRect textRec = this->pickedColorText.getGlobalBounds(); - this->pickedColorText.setOrigin(textRec.width/2, textRec.height/2); - this->pickedColorText.setPosition(colorPickerPos.x + colorPickerSize.width*0.5, colorPickerPos.y + colorPickerSize.height + windowSize.y*0.02); -} -*/ - void UIEnemyOverview::draw() { Game& game = Game::getInstance(); this->box.draw(); @@ -107,7 +57,7 @@ void UIEnemyOverview::draw() { void UIEnemyOverview::changeHealth(int value) { int newHealth = std::max(this->enemy.health - value, 0); this->enemy.health = newHealth; - this->enemyStats.updateHealth(newHealth); + this->enemyStats.update(newHealth); } void UIEnemyOverview::updatePickedColorText(std::string newText, sf::Color pickedColor) { diff --git a/src/main/UIObjects/UIPlayerOverview.cpp b/src/main/UIObjects/UIPlayerOverview.cpp index c0f11fd..de141e5 100644 --- a/src/main/UIObjects/UIPlayerOverview.cpp +++ b/src/main/UIObjects/UIPlayerOverview.cpp @@ -1,14 +1,35 @@ #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(std::shared_ptr _player): uiPlayerStats(_player), playerFrame("monster_landscape_cut/" + _player->picPath, "actor_borders/fight_border.png") { + Game& game = Game::getInstance(); + this->player = *_player; + + sf::Vector2u windowSize = game.gameWindow.getSize(); + sf::FloatRect boxRect = this->backgroundBox.getSize(); + this->backgroundBox.setPosition((windowSize.x * 0.49) - boxRect.width, windowSize.y * 0.1); + + this->uiPlayerStats.setPosition(windowSize.x * 0.08, windowSize.y * 0.35); + + sf::Vector2f statsPos = this->uiPlayerStats.getPosition(); + sf::FloatRect statsSize = this->uiPlayerStats.getSize(); + sf::FloatRect playerFrameRect = this->playerFrame.getSize(); + float playerBoxScale = (windowSize.x*0.22)/playerFrameRect.width; + this->playerFrame.scale(playerBoxScale, playerBoxScale); + playerFrameRect = this->playerFrame.getSize(); + this->playerFrame.setPosition(statsPos.x + statsSize.width*0.5 - playerFrameRect.width/2.f, windowSize.y * 0.14); + sf::Vector2f playerFramePos = this->playerFrame.getPosition(); + this->playerBackgroundTX.loadFromFile(RESOURCE_PATH "actor_landscape_backgrounds/forest.png"); + this->playerBackgroundSP.setTexture(this->playerBackgroundTX); + sf::FloatRect playerBackgroundRect = this->playerBackgroundSP.getGlobalBounds(); + this->playerBackgroundSP.setOrigin(playerBackgroundRect.width/2.f, playerBackgroundRect.height/2.f); + this->playerBackgroundSP.setPosition(playerFramePos.x + playerFrameRect.width/2.f, playerFramePos.y + playerFrameRect.height/2.f); + this->playerBackgroundSP.scale(playerBoxScale, playerBoxScale); } void UIPlayerOverview::init() { Game& game = Game::getInstance(); - //uiPlayerStats.init(game.player); - //playerFrame.init("monster_landscape_cut/" + Game::getInstance().player.picPath, "actor_borders/fight_border.png"); sf::Vector2u windowSize = game.gameWindow.getSize(); sf::FloatRect boxRect = this->backgroundBox.getSize(); @@ -36,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.updateHealth(newHealth); + this->uiPlayerStats.update(newHealth); } void UIPlayerOverview::draw() {