Skip to content

Commit

Permalink
Observer finally works!
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed May 30, 2024
1 parent bde1c06 commit 5cfda22
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 130 deletions.
4 changes: 1 addition & 3 deletions src/include/FightEnv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ class FightEnv {

sf::Texture backgroundTX;
sf::Sprite backgroundSP;
//UIStats playerStatsBox;
//UIEnemyOverview* enemyOverview;
std::unique_ptr<UIEnemyOverview> enemyOverview;
UIPlayerOverview playerOverview;
std::unique_ptr<UIPlayerOverview> playerOverview;
TextFadingManager textFadingManager;
sf::Texture playersTurnTX;
sf::Texture enemiesTurnTX;
Expand Down
2 changes: 1 addition & 1 deletion src/include/ObserverPattern/Observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/include/UIElements/UIStats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion src/include/UIObjects/UIPlayerOverview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class UIPlayerOverview {
public:
UIPlayerOverview();
UIPlayerOverview(std::shared_ptr<Player> player);

UIBorderedImage playerFrame;
Player player;
Expand Down
7 changes: 2 additions & 5 deletions src/main/Activities/FightActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::
Game& game = Game::getInstance();
this->enemy = std::make_shared<Enemy>(this->initEnemy());
this->fightEnv.enemyOverview = std::make_unique<UIEnemyOverview>(this->enemy);
this->fightEnv.playerOverview.init();
this->fightEnv.playerOverview = std::make_unique<UIPlayerOverview>(Game::getInstance().player);

this->fightEnv.backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/background_fight.png");
this->fightEnv.backgroundSP.setTexture(this->fightEnv.backgroundTX);
Expand All @@ -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<int> dist(0, 1);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/main/Actors/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 3 additions & 3 deletions src/main/FightStates/EnemiesTurn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ FightStateEnum EnemiesTurn::run(FightEnv &fightEnv) {
std::uniform_int_distribution<int> 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) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/ObserverPattern/Observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Observer::~Observer() {
}
}

void Observer::update(int newValue) const {
void Observer::update(int newValue) {
std::cout << "Got a notification" << std::endl;
}

Expand Down Expand Up @@ -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;
Expand Down
60 changes: 2 additions & 58 deletions src/main/UIElements/UIStats.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
#include "UIElements/UIStats.hpp"

UIStats::~UIStats() {
std::cout << "~UIStats" << std::endl;
}

UIStats::UIStats(std::shared_ptr<Actor> 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;
Expand Down Expand Up @@ -57,59 +54,6 @@ UIStats::UIStats(std::shared_ptr<Actor> 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();
Expand Down Expand Up @@ -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));
}
52 changes: 1 addition & 51 deletions src/main/UIObjects/UIEnemyOverview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,56 +44,6 @@ UIEnemyOverview::UIEnemyOverview(std::shared_ptr<Enemy> _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();
Expand All @@ -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) {
Expand Down
29 changes: 25 additions & 4 deletions src/main/UIObjects/UIPlayerOverview.cpp
Original file line number Diff line number Diff line change
@@ -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> _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();
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 5cfda22

Please sign in to comment.