Skip to content

Commit

Permalink
refactored FightActivity (some UIElements are initialized with init())
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed Apr 30, 2024
1 parent f17728a commit dff0c95
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 68 deletions.
13 changes: 7 additions & 6 deletions src/include/Actors/Enemy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:

};

Expand Down
3 changes: 1 addition & 2 deletions src/include/UIElements/UIBorderedImage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/include/UIElements/UIStats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 1 addition & 4 deletions src/include/UIObjects/UIEnemyOverview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
28 changes: 13 additions & 15 deletions src/include/UIObjects/UIPlayerOverview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion src/main/Activities/FightActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::make_unique<TurnChangeState>(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);

Expand Down
4 changes: 4 additions & 0 deletions src/main/Actors/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

}
3 changes: 2 additions & 1 deletion src/main/FightEnv.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "FightEnv.hpp"

FightEnv::FightEnv(): playerStatsBox(Game::getInstance().player) {
FightEnv::FightEnv() {
playerStatsBox.init(Game::getInstance().player);
}
29 changes: 15 additions & 14 deletions src/main/UIElements/UIBorderedImage.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
11 changes: 1 addition & 10 deletions src/main/UIElements/UIStats.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand Down
16 changes: 5 additions & 11 deletions src/main/UIObjects/UIEnemyOverview.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 4 additions & 1 deletion src/main/UIObjects/UIPlayerOverview.cpp
Original file line number Diff line number Diff line change
@@ -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);
Expand Down

0 comments on commit dff0c95

Please sign in to comment.