Skip to content

Commit

Permalink
refactored Game class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed Apr 29, 2024
1 parent 5f94e6b commit 2db7bd3
Show file tree
Hide file tree
Showing 19 changed files with 127 additions and 118 deletions.
4 changes: 4 additions & 0 deletions src/include/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ class Game {
Player player = Player("Ipagaxi", 100, 12, {100, 100, 100}, "default_actor_quer.png");

static Game& getInstance();

Game(const Game&) = delete;
Game& operator=(const Game&) = delete;
private:
Game();
~Game();
static Game* instance;
};

Expand Down
2 changes: 1 addition & 1 deletion src/main/Activities/Activity.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Activities/Activity.hpp"

Activity::Activity() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
sf::Vector2u windowSize = game.gameWindow.getSize();
sf::FloatRect buttonSize = this->exitButton.getSize();
this->exitButton.setPosition(windowSize.x * 0.99 - buttonSize.width, windowSize.x * 0.01);
Expand Down
2 changes: 1 addition & 1 deletion src/main/Activities/CharacterActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CharacterActivity::CharacterActivity(): Activity() {
}

ActivityEnum CharacterActivity::executeActivity() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
ActivityEnum currentActivity = ActivityEnum::Character;
game.gameWindow.draw(this->backgroundSP);
this->exitButton.draw();
Expand Down
2 changes: 1 addition & 1 deletion src/main/Activities/FightActivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void FightActivity::runCurrentState(Game &game) {
}

ActivityEnum FightActivity::executeActivity() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
ActivityEnum currentActivity = ActivityEnum::Fight;

game.gameWindow.draw(this->fightEnv.turnSP);
Expand Down
6 changes: 3 additions & 3 deletions src/main/Activities/MenuActivity.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Activities/MenuActivity.hpp"

MenuActivity::MenuActivity(): Activity() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
this->backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/backgroundMenu.png");
this->backgroundSP.setTexture(this->backgroundTX);

Expand Down Expand Up @@ -33,7 +33,7 @@ MenuActivity::~MenuActivity() {
}

ActivityEnum MenuActivity::executeActivity() {
Game game = Game::getInstance();
Game& game = Game::getInstance();

ActivityEnum currentActivity = ActivityEnum::Menu;

Expand All @@ -55,7 +55,7 @@ ActivityEnum MenuActivity::executeActivity() {
}

if (buttonExit.clickListener()) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
this->backgroundMusic.stop();
game.gameWindow.close();
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/Animations/IncomingBanner.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Animations/IncomingBanner.hpp"

void IncomingBanner::init() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
sf::Vector2u windowSize = game.gameWindow.getSize();

this->banner.setSize(sf::Vector2f(windowSize.x, windowSize.y * 0.2));
Expand Down Expand Up @@ -35,7 +35,7 @@ void IncomingBanner::setNewLabel(std::string newLabel) {
}

bool IncomingBanner::runAnimation() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
int changeTimeMillSec = 1000; // The entire screen time of the banner
int bannerMovementime = 300; // The time only for movement of the banner => after movement ended banner is still on screen shortly

Expand All @@ -62,7 +62,7 @@ bool IncomingBanner::runAnimation() {
}

void IncomingBanner::drawAnimation() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
game.gameWindow.draw(this->banner);
game.gameWindow.draw(this->bannerText);
}
4 changes: 2 additions & 2 deletions src/main/Animations/TextFadingManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ TextFading::TextFading(std::string text, sf::Vector2f pos, sf::Color textColor,
}

void TextFading::draw() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
game.gameWindow.draw(this->text);
}

Expand Down Expand Up @@ -53,7 +53,7 @@ void TextFading::setNewParabelPos() {
}

void TextFadingManager::updateAnimationState() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
sf::Color oldColor = this->fadingText.text.getFillColor();
sf::Vector2f oldPos = this->fadingText.text.getPosition();
float pastRatio = game.gameStatus.elapsedTime.asMilliseconds() / static_cast<float>(this->fadingText.millSecToLive);
Expand Down
2 changes: 1 addition & 1 deletion src/main/FightStates/EnemiesTurn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ EnemiesTurn::~EnemiesTurn() {
}

FightStateEnum EnemiesTurn::run(FightEnv &fightEnv) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
FightStateEnum currentState = FightStateEnum::ENEMY_STATE;
if (!fightEnv.enemyDamageCalculated) {
std::random_device randSeed;
Expand Down
4 changes: 2 additions & 2 deletions src/main/FightStates/PlayersTurn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ PlayersTurn::~PlayersTurn() {
}

FightStateEnum PlayersTurn::run(FightEnv &fightEnv) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
FightStateEnum currentState = FightStateEnum::PLAYER_STATE;
switch (this->playerPhase) {
case PlayerPhase::PICK_COLOR:
Expand Down Expand Up @@ -52,7 +52,7 @@ void PlayersTurn::processAttack(FightEnv &fightEnv) {
}

void PlayersTurn::changeColoPickerImage(FightEnv &fightEnv) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
static int changingMillSec = 2000;
float elapsedRatio = this->passedMillSec/changingMillSec;
for (int y = 0; y < GEN_IMG_HEIGHT; ++y) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/FightStates/TurnChangeState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ TurnChangeState::~TurnChangeState() {
}

FightStateEnum TurnChangeState::run(FightEnv &fightEnv) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
FightStateEnum currentFightState = FightStateEnum::TURN_CHANGE;
this->turnChangeBanner.drawAnimation();
if (!this->turnChangeBanner.runAnimation()) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ Game::Game(): gameWindow(*(new sf::RenderWindow(sf::VideoMode(), "Road of Ogden"
sf::RenderWindow window(sf::VideoMode(), "Road of Ogden", sf::Style::Fullscreen);
this->gameWindow.setFramerateLimit(60);
this->mainFont.loadFromFile(RESOURCE_PATH "fonts/Avara-Bold.otf");
}

Game::~Game() {
}
2 changes: 1 addition & 1 deletion src/main/UIElements/UIBorderedImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void UIBorderedImage::setImage(std::string imagePath) {
}

void UIBorderedImage::draw() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
game.gameWindow.draw(this->imageSP);
game.gameWindow.draw(this->borderSP);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/UIElements/UIBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ UIBox::UIBox(sf::Color fillColor, std::string borderFilePath) {
}

void UIBox::draw() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
game.gameWindow.draw(this->backgroundREC);
game.gameWindow.draw(this->borderSP);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/UIElements/UIButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void UIButton::scale(float x, float y) {
}

void UIButton::draw() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
game.gameWindow.draw(this->buttonSP);
game.gameWindow.draw(this->label);
}
Expand All @@ -68,14 +68,14 @@ sf::Vector2f UIButton::getPosition() {
}

bool UIButton::buttonContainsMouse() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
sf::Vector2i mousePos = sf::Mouse::getPosition(game.gameWindow);
sf::Vector2f mousePosF(static_cast<float>(mousePos.x), static_cast<float>(mousePos.y));
return this->buttonSP.getGlobalBounds().contains(mousePosF);
}

void UIButton::hoverListener() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
if (game.gameEvents.mouseMoved) {
if(this->buttonContainsMouse()) {
this->buttonSP.setTexture(this->hoveredTX);
Expand All @@ -90,7 +90,7 @@ void UIButton::hoverListener() {
}

bool UIButton::clickListener() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
this->hoverListener();
if (game.gameEvents.mousePressed && this->buttonSP.getGlobalBounds().contains(game.gameEvents.pressedPos)) {
this->pressSound.play();
Expand Down
4 changes: 2 additions & 2 deletions src/main/UIElements/UIColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ UIColorPicker::UIColorPicker(sf::Image image, std::string borderPath) {
}

void UIColorPicker::draw() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
game.gameWindow.draw(this->colorSP);
game.gameWindow.draw(this->borderSP);
}
Expand Down Expand Up @@ -75,7 +75,7 @@ sf::Color UIColorPicker::getPixelColor(sf::Vector2f pos) {
}

bool UIColorPicker::clickListener(sf::Vector2f &clickedPos) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
if (game.gameEvents.mousePressed && this->colorSP.getGlobalBounds().contains(game.gameEvents.pressedPos)) {
this->pressed = true;
} else if (game.gameEvents.mousePressed){
Expand Down
42 changes: 21 additions & 21 deletions src/main/UIElements/UIStats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


UIStats::UIStats(Actor actor) {
Game game = Game::getInstance();
Game& game = Game::getInstance();
sf::Vector2u windowSize = game.gameWindow.getSize();
sf::Color statsValueFontColor = sf::Color::Yellow;
sf::Color statsLabelFontColor = sf::Color::White;
Expand Down Expand Up @@ -62,7 +62,7 @@ void UIStats::setActor(Actor actor) {
}

void UIStats::draw() {
Game game = Game::getInstance();
Game& game = Game::getInstance();
this->actorStatsBox.draw();
game.gameWindow.draw(this->actorName);
game.gameWindow.draw(this->actorHealthLabel);
Expand All @@ -77,28 +77,28 @@ sf::Vector2f UIStats::getPosition() {
return this->actorStatsBox.getPosition();
}
void UIStats::setPosition(float x, float y) {
this->actorStatsBox.setPosition(x, y);

sf::Vector2f actorStatsBoxPosition = this->actorStatsBox.getPosition();
sf::FloatRect actorStatsBoxSize = this->actorStatsBox.getSize();
float statsLabelPosX = actorStatsBoxPosition.x + actorStatsBoxSize.width * 0.2;
float statsValuePosX = actorStatsBoxPosition.x + actorStatsBoxSize.width * 0.45;
float statsOffsetY = actorStatsBoxSize.height * 0.3;
float statsSeparationPaddingY = actorStatsBoxSize.height * 0.05;

sf::FloatRect actorNameRec = this->actorName.getGlobalBounds();
this->actorName.setPosition(x + (actorStatsBoxSize.width - actorNameRec.width)/2, y + actorStatsBoxSize.height * 0.15);
this->actorHealthLabel.setPosition(statsLabelPosX, y + statsOffsetY);
this->actorHealthValue.setPosition(statsValuePosX, y + statsOffsetY);
this->actorAttackStrengthLabel.setPosition(statsLabelPosX, y + statsOffsetY + statsTextHeight + statsSeparationPaddingY);
this->actorAttackStrengthValue.setPosition(statsValuePosX, y + statsOffsetY + statsTextHeight + statsSeparationPaddingY);
this->actorRGBDefenseLabel.setPosition(statsLabelPosX, y + statsOffsetY + (statsTextHeight + statsSeparationPaddingY)*2);
this->actorRGBDefenseValues.setPosition(statsValuePosX, y + statsOffsetY + (statsTextHeight + statsSeparationPaddingY)*2);
this->actorStatsBox.setPosition(x, y);

sf::Vector2f actorStatsBoxPosition = this->actorStatsBox.getPosition();
sf::FloatRect actorStatsBoxSize = this->actorStatsBox.getSize();
float statsLabelPosX = actorStatsBoxPosition.x + actorStatsBoxSize.width * 0.2;
float statsValuePosX = actorStatsBoxPosition.x + actorStatsBoxSize.width * 0.45;
float statsOffsetY = actorStatsBoxSize.height * 0.3;
float statsSeparationPaddingY = actorStatsBoxSize.height * 0.05;

sf::FloatRect actorNameRec = this->actorName.getGlobalBounds();
this->actorName.setPosition(x + (actorStatsBoxSize.width - actorNameRec.width)/2, y + actorStatsBoxSize.height * 0.15);
this->actorHealthLabel.setPosition(statsLabelPosX, y + statsOffsetY);
this->actorHealthValue.setPosition(statsValuePosX, y + statsOffsetY);
this->actorAttackStrengthLabel.setPosition(statsLabelPosX, y + statsOffsetY + statsTextHeight + statsSeparationPaddingY);
this->actorAttackStrengthValue.setPosition(statsValuePosX, y + statsOffsetY + statsTextHeight + statsSeparationPaddingY);
this->actorRGBDefenseLabel.setPosition(statsLabelPosX, y + statsOffsetY + (statsTextHeight + statsSeparationPaddingY)*2);
this->actorRGBDefenseValues.setPosition(statsValuePosX, y + statsOffsetY + (statsTextHeight + statsSeparationPaddingY)*2);
}
sf::FloatRect UIStats::getSize() {
return this->actorStatsBox.getSize();
return this->actorStatsBox.getSize();
}

void UIStats::updateHealth(int value) {
this->actorHealthValue.setString(std::to_string(value));
this->actorHealthValue.setString(std::to_string(value));
}
Loading

0 comments on commit 2db7bd3

Please sign in to comment.