From dff0c95b45dd1f7c444af90cfdbc71de94e46830 Mon Sep 17 00:00:00 2001 From: Ipagaxi Date: Tue, 30 Apr 2024 19:15:14 +0200 Subject: [PATCH] refactored FightActivity (some UIElements are initialized with init()) --- src/include/Actors/Enemy.hpp | 13 +++++----- src/include/UIElements/UIBorderedImage.hpp | 3 +-- src/include/UIElements/UIStats.hpp | 4 +-- src/include/UIObjects/UIEnemyOverview.hpp | 5 +--- src/include/UIObjects/UIPlayerOverview.hpp | 28 ++++++++++----------- src/main/Activities/FightActivity.cpp | 4 ++- src/main/Actors/Enemy.cpp | 4 +++ src/main/FightEnv.cpp | 3 ++- src/main/UIElements/UIBorderedImage.cpp | 29 +++++++++++----------- src/main/UIElements/UIStats.cpp | 11 +------- src/main/UIObjects/UIEnemyOverview.cpp | 16 ++++-------- src/main/UIObjects/UIPlayerOverview.cpp | 5 +++- 12 files changed, 57 insertions(+), 68 deletions(-) diff --git a/src/include/Actors/Enemy.hpp b/src/include/Actors/Enemy.hpp index 8d01b75..1188cc5 100644 --- a/src/include/Actors/Enemy.hpp +++ b/src/include/Actors/Enemy.hpp @@ -6,14 +6,15 @@ #include "Defines.hpp" class Enemy: public Actor { - public: - Enemy(); - Enemy(std::string name, int health, int attackStrength, RGB defense, std::string picPath, std::string colorPicPath, std::string colorPicBorderPath); + public: + Enemy(); + Enemy(std::string name, int health, int attackStrength, RGB defense, std::string picPath, std::string colorPicPath, std::string colorPicBorderPath); - std::string colorPicPath; - std::string colorPicBorderPath; + void init(); + std::string colorPicPath; + std::string colorPicBorderPath; - private: + private: }; diff --git a/src/include/UIElements/UIBorderedImage.hpp b/src/include/UIElements/UIBorderedImage.hpp index df9e09b..714816c 100644 --- a/src/include/UIElements/UIBorderedImage.hpp +++ b/src/include/UIElements/UIBorderedImage.hpp @@ -11,8 +11,7 @@ class UIBorderedImage: public UIElement { public: - UIBorderedImage(std::string imageFilePath, std::string borderFilePath); - + void init(std::string imageFilePath, std::string borderFilePath); void setImage(std::string imagePath); void draw() override; void setPosition(float x, float y) override; diff --git a/src/include/UIElements/UIStats.hpp b/src/include/UIElements/UIStats.hpp index 32f48f3..8aa31f1 100644 --- a/src/include/UIElements/UIStats.hpp +++ b/src/include/UIElements/UIStats.hpp @@ -9,9 +9,7 @@ class UIStats: public UIElement { public: - UIStats(Actor actor); - - void setActor(Actor actor); + void init(Actor actor); void draw() override; sf::Vector2f getPosition() override; void setPosition(float x, float y) override; diff --git a/src/include/UIObjects/UIEnemyOverview.hpp b/src/include/UIObjects/UIEnemyOverview.hpp index 62876d0..372fd38 100644 --- a/src/include/UIObjects/UIEnemyOverview.hpp +++ b/src/include/UIObjects/UIEnemyOverview.hpp @@ -13,14 +13,11 @@ class UIEnemyOverview { public: - UIEnemyOverview(Enemy enemy); - UIEnemyOverview(); - Enemy enemy; UIColorPicker colorPicker = UIColorPicker("colorPIC_default.png", "color_picker_border.png"); sf::Text pickedColorText; - void initEnemy(Enemy enemy); + void init(Enemy enemy); void changeHealth(int value); void updatePickedColorText(std::string newText, sf::Color pickedColor); void draw(); diff --git a/src/include/UIObjects/UIPlayerOverview.hpp b/src/include/UIObjects/UIPlayerOverview.hpp index 46f2344..dcf3b0c 100644 --- a/src/include/UIObjects/UIPlayerOverview.hpp +++ b/src/include/UIObjects/UIPlayerOverview.hpp @@ -9,22 +9,20 @@ #include "UIElements/UIBorderedImage.hpp" #include "UIElements/UIStats.hpp" - class UIPlayerOverview { - public: - UIPlayerOverview(); - - UIBorderedImage playerFrame; - Player player; - - void changeHealth(int value); - void draw(); - - private: - UIBox backgroundBox = UIBox(sf::Color(51, 25, 0, 150), "borders/metal_border_900x900.png"); - sf::Texture playerBackgroundTX; - sf::Sprite playerBackgroundSP; - UIStats statsComponent; + public: + UIBorderedImage playerFrame; + Player player; + + void init(); + void changeHealth(int value); + void draw(); + + private: + UIBox backgroundBox = UIBox(sf::Color(51, 25, 0, 150), "borders/metal_border_900x900.png"); + sf::Texture playerBackgroundTX; + sf::Sprite playerBackgroundSP; + UIStats statsComponent; }; #endif \ No newline at end of file diff --git a/src/main/Activities/FightActivity.cpp b/src/main/Activities/FightActivity.cpp index d34fded..1f9d4d3 100644 --- a/src/main/Activities/FightActivity.cpp +++ b/src/main/Activities/FightActivity.cpp @@ -3,7 +3,9 @@ FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::make_unique(fightEnv)) { Game& game = Game::getInstance(); - this->fightEnv.enemyOverview = UIEnemyOverview(initEnemy()); + this->fightEnv.enemyOverview.init(this->initEnemy()); + this->fightEnv.playerOverview.init(); + this->fightEnv.backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/background_fight.png"); this->fightEnv.backgroundSP.setTexture(this->fightEnv.backgroundTX); diff --git a/src/main/Actors/Enemy.cpp b/src/main/Actors/Enemy.cpp index c242f92..2848f40 100644 --- a/src/main/Actors/Enemy.cpp +++ b/src/main/Actors/Enemy.cpp @@ -18,4 +18,8 @@ Enemy::Enemy(std::string _name, int _health, int _attackStrength, RGB _defense, this->picPath = _picPath; this->colorPicPath = _colorPicPath; this->colorPicBorderPath = _colorPicBorderPath; +} + +void Enemy::init() { + } \ No newline at end of file diff --git a/src/main/FightEnv.cpp b/src/main/FightEnv.cpp index e8bb71f..ac3a6d2 100644 --- a/src/main/FightEnv.cpp +++ b/src/main/FightEnv.cpp @@ -1,4 +1,5 @@ #include "FightEnv.hpp" -FightEnv::FightEnv(): playerStatsBox(Game::getInstance().player) { +FightEnv::FightEnv() { + playerStatsBox.init(Game::getInstance().player); } diff --git a/src/main/UIElements/UIBorderedImage.cpp b/src/main/UIElements/UIBorderedImage.cpp index bb86fb2..6c9d946 100644 --- a/src/main/UIElements/UIBorderedImage.cpp +++ b/src/main/UIElements/UIBorderedImage.cpp @@ -1,19 +1,20 @@ #include "UIElements/UIBorderedImage.hpp" -UIBorderedImage::UIBorderedImage(std::string imageFilePath, std::string borderFilePath) { - this->imageTX.loadFromFile(RESOURCE_PATH + imageFilePath); - this->imageSP.setTexture(this->imageTX); - this->borderTX.loadFromFile(RESOURCE_PATH + borderFilePath); - this->borderSP.setTexture(this->borderTX); - - sf::FloatRect imageRect = this->imageSP.getGlobalBounds(); - sf::Vector2f borderPos = this->borderSP.getPosition(); - sf::FloatRect borderSize = this->borderSP.getGlobalBounds(); - this->imageSP.setOrigin(imageRect.width/2.f, imageRect.height/2.f); - this->imageSP.setPosition(borderPos.x + (borderSize.width/2.f), borderPos.y + (borderSize.height/2.f)); - - // For a save small overlap - this->borderSP.scale(0.97, 0.97); + +void UIBorderedImage::init(std::string imageFilePath, std::string borderFilePath) { + this->imageTX.loadFromFile(RESOURCE_PATH + imageFilePath); + this->imageSP.setTexture(this->imageTX); + this->borderTX.loadFromFile(RESOURCE_PATH + borderFilePath); + this->borderSP.setTexture(this->borderTX); + + sf::FloatRect imageRect = this->imageSP.getGlobalBounds(); + sf::Vector2f borderPos = this->borderSP.getPosition(); + sf::FloatRect borderSize = this->borderSP.getGlobalBounds(); + this->imageSP.setOrigin(imageRect.width/2.f, imageRect.height/2.f); + this->imageSP.setPosition(borderPos.x + (borderSize.width/2.f), borderPos.y + (borderSize.height/2.f)); + + // For a save small overlap + this->borderSP.scale(0.97, 0.97); } void UIBorderedImage::setImage(std::string imagePath) { diff --git a/src/main/UIElements/UIStats.cpp b/src/main/UIElements/UIStats.cpp index b7eb9d2..5c1be10 100644 --- a/src/main/UIElements/UIStats.cpp +++ b/src/main/UIElements/UIStats.cpp @@ -1,13 +1,11 @@ #include "UIElements/UIStats.hpp" -UIStats::UIStats(Actor actor) { +void UIStats::init(Actor actor) { Game& game = Game::getInstance(); sf::Vector2u windowSize = game.gameWindow.getSize(); sf::Color statsValueFontColor = sf::Color::Yellow; sf::Color statsLabelFontColor = sf::Color::White; - int numStats = 2; - sf::Vector2f actorStatsBoxPosition = this->actorStatsBox.getPosition(); this->statsTextHeight = windowSize.y * 0.015; float scale = (windowSize.y * 0.4) / this->actorStatsBox.getSize().height; @@ -54,13 +52,6 @@ UIStats::UIStats(Actor actor) { this->setPosition(0., 0.); } -void UIStats::setActor(Actor actor) { - this->actorName.setString(actor.name); - this->actorHealthValue.setString(std::to_string(actor.health)); - this->actorAttackStrengthValue.setString(std::to_string(actor.attackStrength)); - this->actorRGBDefenseValues.setString("(" + std::to_string(actor.defense.red) + ", " + std::to_string(actor.defense.green) + ", " + std::to_string(actor.defense.blue) + ")"); -} - void UIStats::draw() { Game& game = Game::getInstance(); this->actorStatsBox.draw(); diff --git a/src/main/UIObjects/UIEnemyOverview.cpp b/src/main/UIObjects/UIEnemyOverview.cpp index 5f5832d..f3a4980 100644 --- a/src/main/UIObjects/UIEnemyOverview.cpp +++ b/src/main/UIObjects/UIEnemyOverview.cpp @@ -1,10 +1,11 @@ #include "UIObjects/UIEnemyOverview.hpp" -UIEnemyOverview::UIEnemyOverview() { -} - -UIEnemyOverview::UIEnemyOverview(Enemy enemy): enemyStats(enemy), enemy(enemy), enemyBorderedImage("monster_landscape_cut/" + enemy.picPath, "actor_borders/fight_border.png") { +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"); + this->enemyStats.init(_enemy); + 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); @@ -45,13 +46,6 @@ UIEnemyOverview::UIEnemyOverview(Enemy enemy): enemyStats(enemy), enemy(enemy), this->pickedColorText.setPosition(colorPickerPos.x + colorPickerSize.width*0.5, colorPickerPos.y + colorPickerSize.height + windowSize.y*0.02); } -void UIEnemyOverview::initEnemy(Enemy enemy) { - this->enemy = enemy; - this->enemyStats.setActor(enemy); - this->enemyBorderedImage.setImage("monster_landscape_cut/" + enemy.picPath); - this->colorPicker.setColorImage(enemy.colorPicPath); -} - void UIEnemyOverview::draw() { Game& game = Game::getInstance(); this->box.draw(); diff --git a/src/main/UIObjects/UIPlayerOverview.cpp b/src/main/UIObjects/UIPlayerOverview.cpp index d87a9b4..2846b2c 100644 --- a/src/main/UIObjects/UIPlayerOverview.cpp +++ b/src/main/UIObjects/UIPlayerOverview.cpp @@ -1,7 +1,10 @@ #include "UIObjects/UIPlayerOverview.hpp" -UIPlayerOverview::UIPlayerOverview(): statsComponent(Game::getInstance().player), player(Game::getInstance().player), playerFrame("monster_landscape_cut/" + Game::getInstance().player.picPath, "actor_borders/fight_border.png") { +void UIPlayerOverview::init() { Game& game = Game::getInstance(); + statsComponent.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(); this->backgroundBox.setPosition((windowSize.x * 0.49) - boxRect.width, windowSize.y * 0.1);