diff --git a/src/include/Activities/Activity.hpp b/src/include/Activities/Activity.hpp index bebadc2..3fb93f6 100644 --- a/src/include/Activities/Activity.hpp +++ b/src/include/Activities/Activity.hpp @@ -12,9 +12,9 @@ class Game; class Activity { public: - Activity(Game &game); + Activity(); virtual ~Activity(); - virtual ActivityEnum executeActivity(Game &game); + virtual ActivityEnum executeActivity(); private: diff --git a/src/include/Activities/CharacterActivity.hpp b/src/include/Activities/CharacterActivity.hpp index 47b7238..0d6012c 100644 --- a/src/include/Activities/CharacterActivity.hpp +++ b/src/include/Activities/CharacterActivity.hpp @@ -7,9 +7,9 @@ class CharacterActivity : public Activity { public : - CharacterActivity(Game &game); + CharacterActivity(); - ActivityEnum executeActivity(Game &game) override; + ActivityEnum executeActivity() override; private : diff --git a/src/include/Activities/FightActivity.hpp b/src/include/Activities/FightActivity.hpp index cc0c8ce..0c031b0 100644 --- a/src/include/Activities/FightActivity.hpp +++ b/src/include/Activities/FightActivity.hpp @@ -33,10 +33,10 @@ class FightActivity: public Activity { public: - FightActivity(Game &game); + FightActivity(); ~FightActivity(); - ActivityEnum executeActivity(Game &game) override; + ActivityEnum executeActivity() override; void runCurrentState(Game &game); private: @@ -46,10 +46,10 @@ class FightActivity: public Activity { std::unique_ptr currentFightState; Enemy initEnemy(); - void runPlayersTurn(Game &game); - void runEnemiesTurn(Game &game); - void runDefeat(Game &game); - void runVictory(Game &game); + void runPlayersTurn(); + void runEnemiesTurn(); + void runDefeat(); + void runVictory(); }; #endif \ No newline at end of file diff --git a/src/include/Activities/MenuActivity.hpp b/src/include/Activities/MenuActivity.hpp index 6949b17..c4c1f60 100644 --- a/src/include/Activities/MenuActivity.hpp +++ b/src/include/Activities/MenuActivity.hpp @@ -13,10 +13,10 @@ class MenuActivity: public Activity { public: - MenuActivity(Game &game); + MenuActivity(); ~MenuActivity(); - ActivityEnum executeActivity(Game &game) override; + ActivityEnum executeActivity() override; private: sf::Texture backgroundTX; diff --git a/src/include/Animations/IncomingBanner.hpp b/src/include/Animations/IncomingBanner.hpp index e7742ad..4f853c8 100644 --- a/src/include/Animations/IncomingBanner.hpp +++ b/src/include/Animations/IncomingBanner.hpp @@ -9,12 +9,12 @@ class IncomingBanner { public: - IncomingBanner(Game &game); - IncomingBanner(Game &game, std::string label); + IncomingBanner(); + IncomingBanner(std::string label); void setNewLabel(std::string newLabel); - bool runAnimation(Game &game); - void drawAnimation(sf::RenderWindow* gameWindow); + bool runAnimation(); + void drawAnimation(); private: int pastTimeInMillSec = 0; @@ -23,7 +23,7 @@ class IncomingBanner { sf::RectangleShape banner; sf::Text bannerText; - void init(Game &game); + void init(); }; #endif \ No newline at end of file diff --git a/src/include/Animations/TextFadingManager.hpp b/src/include/Animations/TextFadingManager.hpp index b696022..b2c20ee 100644 --- a/src/include/Animations/TextFadingManager.hpp +++ b/src/include/Animations/TextFadingManager.hpp @@ -33,7 +33,7 @@ class TextFading { float initPosY; float initPosX; - void draw(sf::RenderWindow* gameWindow); + void draw(); float computeParabel(float value); void setNewParabelPos(); @@ -43,9 +43,9 @@ class TextFading { class TextFadingManager: Animation { public: - void run(sf::RenderWindow* gameWindow, GameStatus &gameStatus); + void run(); void startAnimation(std::string text, sf::Vector2f pos, sf::Color textColor, int textSize, AnimationPath animationPath, int millSecToLive); - void updateAnimationState(GameStatus &gameStatus); + void updateAnimationState(); TextFading fadingText; bool isRunning = false; }; diff --git a/src/include/FightEnv.hpp b/src/include/FightEnv.hpp index 8fdecd6..2b2bfc6 100644 --- a/src/include/FightEnv.hpp +++ b/src/include/FightEnv.hpp @@ -11,7 +11,7 @@ class FightEnv { public: - FightEnv(Game &game); + FightEnv(); sf::Texture backgroundTX; sf::Sprite backgroundSP; diff --git a/src/include/FightStates/EnemiesTurn.hpp b/src/include/FightStates/EnemiesTurn.hpp index 3dc9675..84d4c54 100644 --- a/src/include/FightStates/EnemiesTurn.hpp +++ b/src/include/FightStates/EnemiesTurn.hpp @@ -8,7 +8,7 @@ class EnemiesTurn: public FightState { public: ~EnemiesTurn(); - FightStateEnum run(Game &game, FightEnv &fightEnv) override; + FightStateEnum run(FightEnv &fightEnv) override; private: }; diff --git a/src/include/FightStates/FightState.hpp b/src/include/FightStates/FightState.hpp index 27931ed..dd7f44b 100644 --- a/src/include/FightStates/FightState.hpp +++ b/src/include/FightStates/FightState.hpp @@ -10,7 +10,7 @@ class FightState { public: virtual ~FightState(); - virtual FightStateEnum run(Game &game, FightEnv &fightEnv); + virtual FightStateEnum run(FightEnv &fightEnv); }; #endif \ No newline at end of file diff --git a/src/include/FightStates/PlayersTurn.hpp b/src/include/FightStates/PlayersTurn.hpp index 79fa228..0b85825 100644 --- a/src/include/FightStates/PlayersTurn.hpp +++ b/src/include/FightStates/PlayersTurn.hpp @@ -20,7 +20,7 @@ class PlayersTurn: public FightState { public: PlayersTurn(FightEnv &fightEnv); ~PlayersTurn(); - FightStateEnum run(Game &game, FightEnv &fightEnv) override; + FightStateEnum run(FightEnv &fightEnv) override; private: PlayerPhase playerPhase = PlayerPhase::PICK_COLOR; @@ -31,8 +31,8 @@ class PlayersTurn: public FightState { bool newColorImageSet = false; float passedMillSec = 0.0; - void processAttack(FightEnv &fightEnv, Game &game); - void changeColoPickerImage(Game &game, FightEnv &fightEnv); + void processAttack(FightEnv &fightEnv); + void changeColoPickerImage(FightEnv &fightEnv); double computeCurrentPixel(double formerPixel, double newPixel, float elapsedRatio); // Compute damage multiplier diff --git a/src/include/FightStates/TurnChangeState.hpp b/src/include/FightStates/TurnChangeState.hpp index 18f3912..40904e6 100644 --- a/src/include/FightStates/TurnChangeState.hpp +++ b/src/include/FightStates/TurnChangeState.hpp @@ -8,9 +8,9 @@ class TurnChangeState: public FightState { public: - TurnChangeState(Game &gam, FightEnv &fightEnv); + TurnChangeState(FightEnv &fightEnv); ~TurnChangeState(); - FightStateEnum run(Game &game, FightEnv &fightEnv) override; + FightStateEnum run(FightEnv &fightEnv) override; private: IncomingBanner turnChangeBanner; diff --git a/src/include/Game.hpp b/src/include/Game.hpp index c69a8a7..68a1d7f 100644 --- a/src/include/Game.hpp +++ b/src/include/Game.hpp @@ -17,14 +17,16 @@ class Game { public: sf::Font mainFont; - //sf::Music backgroundMusic; GameEvents gameEvents; - RenderEngine renderEngine; GameStatus gameStatus; + sf::RenderWindow& gameWindow; Player player = Player("Ipagaxi", 100, 12, {100, 100, 100}, "default_actor_quer.png"); - Game(sf::RenderWindow &window, ActivityEnum activity); + static Game& getInstance(); + private: + Game(); + static Game* instance; }; #endif \ No newline at end of file diff --git a/src/include/UIElements/UIBorderedImage.hpp b/src/include/UIElements/UIBorderedImage.hpp index 4005724..df9e09b 100644 --- a/src/include/UIElements/UIBorderedImage.hpp +++ b/src/include/UIElements/UIBorderedImage.hpp @@ -4,6 +4,8 @@ #include #include #include + +#include "Game.hpp" #include "UIElements/UIElement.hpp" #include "Defines.hpp" @@ -12,7 +14,7 @@ class UIBorderedImage: public UIElement { UIBorderedImage(std::string imageFilePath, std::string borderFilePath); void setImage(std::string imagePath); - void draw(sf::RenderWindow* gameWindow) override; + void draw() override; void setPosition(float x, float y) override; sf::Vector2f getPosition() override; sf::FloatRect getSize() override; diff --git a/src/include/UIElements/UIBox.hpp b/src/include/UIElements/UIBox.hpp index 60f4335..28046e8 100644 --- a/src/include/UIElements/UIBox.hpp +++ b/src/include/UIElements/UIBox.hpp @@ -7,22 +7,23 @@ #include "UIElements/UIElement.hpp" #include "Defines.hpp" +#include "Game.hpp" class UIBox : UIElement { - public: - UIBox(sf::Color fillColor, std::string borderFilePath); + public: + UIBox(sf::Color fillColor, std::string borderFilePath); - void draw(sf::RenderWindow* window) override; - void setPosition(float x, float y) override; - sf::Vector2f getPosition() override; - sf::FloatRect getSize() override; - void scale(float x, float y) override; - void setBackgroundMargin(float x, float y); + void draw() override; + void setPosition(float x, float y) override; + sf::Vector2f getPosition() override; + sf::FloatRect getSize() override; + void scale(float x, float y) override; + void setBackgroundMargin(float x, float y); - private: - sf::RectangleShape backgroundREC; - sf::Texture borderTX; - sf::Sprite borderSP; + private: + sf::RectangleShape backgroundREC; + sf::Texture borderTX; + sf::Sprite borderSP; }; diff --git a/src/include/UIElements/UIButton.hpp b/src/include/UIElements/UIButton.hpp index 4ac99fb..e814b1b 100644 --- a/src/include/UIElements/UIButton.hpp +++ b/src/include/UIElements/UIButton.hpp @@ -27,8 +27,8 @@ class UIButton: public UIElement { sf::Sound releaseSound; void init(std::string fileName); - void hoverListener(sf::RenderWindow* gameWindow, GameEvents &gameEvents); - bool buttonContainsMouse(sf::RenderWindow* gameWindow); + void hoverListener(); + bool buttonContainsMouse(); public: sf::Font font; @@ -38,13 +38,13 @@ class UIButton: public UIElement { UIButton(std::string labelText, std::string fileName); UIButton(std::string fileName); - bool clickListener(sf::RenderWindow* gameWindow, GameEvents &gameEvents); + bool clickListener(); void setPosition(float x, float y) override; sf::Vector2f getPosition() override; sf::FloatRect getSize() override; - void draw(sf::RenderWindow* window) override; + void draw() override; void scale(float x, float y) override; }; diff --git a/src/include/UIElements/UIColorPicker.hpp b/src/include/UIElements/UIColorPicker.hpp index 4bf408b..7a4bdad 100644 --- a/src/include/UIElements/UIColorPicker.hpp +++ b/src/include/UIElements/UIColorPicker.hpp @@ -28,13 +28,13 @@ class UIColorPicker: public UIElement { sf::Image colorIMG; sf::Sound releaseSound; - void draw(sf::RenderWindow* window) override; + void draw() override; void setPosition(float x, float y) override; sf::Vector2f getPosition() override; sf::FloatRect getSize() override; void scale(float x, float y) override; - bool clickListener(GameEvents &gameEvents, sf::Vector2f &clickedPos); + bool clickListener(sf::Vector2f &clickedPos); sf::Color getPixelColor(sf::Vector2f pos); void setColorBox(std::string picPath, std::string borderPath); void setColorImage(std::string picPath); diff --git a/src/include/UIElements/UIElement.hpp b/src/include/UIElements/UIElement.hpp index 72ce27c..01c845f 100644 --- a/src/include/UIElements/UIElement.hpp +++ b/src/include/UIElements/UIElement.hpp @@ -5,7 +5,7 @@ class UIElement { public: - virtual void draw(sf::RenderWindow* window); + virtual void draw(); virtual void setPosition(float x, float y); virtual sf::Vector2f getPosition(); virtual sf::FloatRect getSize(); diff --git a/src/include/UIElements/UIStats.hpp b/src/include/UIElements/UIStats.hpp index b10faa1..32f48f3 100644 --- a/src/include/UIElements/UIStats.hpp +++ b/src/include/UIElements/UIStats.hpp @@ -9,10 +9,10 @@ class UIStats: public UIElement { public: - UIStats(Game &game, Actor actor); + UIStats(Actor actor); void setActor(Actor actor); - void draw(sf::RenderWindow* gameWindow) override; + void draw() override; sf::Vector2f getPosition() override; void setPosition(float x, float y) override; sf::FloatRect getSize() override; diff --git a/src/include/UIObjects/UIEnemyOverview.hpp b/src/include/UIObjects/UIEnemyOverview.hpp index 537ae3f..231e23a 100644 --- a/src/include/UIObjects/UIEnemyOverview.hpp +++ b/src/include/UIObjects/UIEnemyOverview.hpp @@ -13,8 +13,8 @@ class UIEnemyOverview { public: - UIEnemyOverview(Game &game, Enemy enemy); - UIEnemyOverview(Game &game); + UIEnemyOverview(Enemy enemy); + UIEnemyOverview(); Enemy creature; UIColorPicker colorPicker = UIColorPicker("colorPIC_default.png", "color_picker_border.png"); @@ -23,7 +23,7 @@ class UIEnemyOverview { void setEnemy(Enemy enemy); void changeHealth(int value); void updatePickedColorText(std::string newText, sf::Color pickedColor); - void draw(sf::RenderWindow* gameWindow); + void draw(); private: UIBox backgroundBox = UIBox(sf::Color(51, 25, 0, 150), "borders/metal_border_900x900.png"); diff --git a/src/include/UIObjects/UIPlayerOverview.hpp b/src/include/UIObjects/UIPlayerOverview.hpp index 5d61f71..46f2344 100644 --- a/src/include/UIObjects/UIPlayerOverview.hpp +++ b/src/include/UIObjects/UIPlayerOverview.hpp @@ -12,13 +12,13 @@ class UIPlayerOverview { public: - UIPlayerOverview(Game &game); + UIPlayerOverview(); UIBorderedImage playerFrame; Player player; void changeHealth(int value); - void draw(sf::RenderWindow* gameWindow); + void draw(); private: UIBox backgroundBox = UIBox(sf::Color(51, 25, 0, 150), "borders/metal_border_900x900.png"); diff --git a/src/main/Activities/Activity.cpp b/src/main/Activities/Activity.cpp index de5e793..40673fd 100644 --- a/src/main/Activities/Activity.cpp +++ b/src/main/Activities/Activity.cpp @@ -1,7 +1,8 @@ #include "Activities/Activity.hpp" -Activity::Activity(Game &game) { - sf::Vector2u windowSize = game.renderEngine.gameWindow->getSize(); +Activity::Activity() { + 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); } @@ -9,6 +10,6 @@ Activity::Activity(Game &game) { Activity::~Activity() { } -ActivityEnum Activity::executeActivity(Game &game) { +ActivityEnum Activity::executeActivity() { return ActivityEnum::Menu; } \ No newline at end of file diff --git a/src/main/Activities/CharacterActivity.cpp b/src/main/Activities/CharacterActivity.cpp index 1b97392..4767d51 100644 --- a/src/main/Activities/CharacterActivity.cpp +++ b/src/main/Activities/CharacterActivity.cpp @@ -1,17 +1,16 @@ #include "Activities/CharacterActivity.hpp" -CharacterActivity::CharacterActivity(Game &game): Activity(game) { +CharacterActivity::CharacterActivity(): Activity() { this->backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/backgroundMenu.png"); this->backgroundSP.setTexture(this->backgroundTX); } -ActivityEnum CharacterActivity::executeActivity(Game &game) { +ActivityEnum CharacterActivity::executeActivity() { + Game game = Game::getInstance(); ActivityEnum currentActivity = ActivityEnum::Character; - - sf::RenderWindow *gameWindow = game.renderEngine.gameWindow; - gameWindow->draw(this->backgroundSP); - this->exitButton.draw(gameWindow); - if (this->exitButton.clickListener(gameWindow, game.gameEvents)) { + game.gameWindow.draw(this->backgroundSP); + this->exitButton.draw(); + if (this->exitButton.clickListener()) { currentActivity = ActivityEnum::Menu; } return currentActivity; diff --git a/src/main/Activities/FightActivity.cpp b/src/main/Activities/FightActivity.cpp index 42c9a1f..401fa07 100644 --- a/src/main/Activities/FightActivity.cpp +++ b/src/main/Activities/FightActivity.cpp @@ -1,7 +1,8 @@ #include "Activities/FightActivity.hpp" -FightActivity::FightActivity(Game &game) : Activity(game), fightEnv(game), currentFightState(std::make_unique(game, fightEnv)) { +FightActivity::FightActivity() : Activity(), fightEnv(), currentFightState(std::make_unique(fightEnv)) { + Game& game = Game::getInstance(); this->fightEnv.enemyOverview.setEnemy(initEnemy()); this->fightEnv.backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/background_fight.png"); this->fightEnv.backgroundSP.setTexture(this->fightEnv.backgroundTX); @@ -10,7 +11,7 @@ FightActivity::FightActivity(Game &game) : Activity(game), fightEnv(game), curre this->fightEnv.backgroundMusic.setLoop(true); this->fightEnv.backgroundMusic.play(); - sf::Vector2f windowSize = static_cast(game.renderEngine.gameWindow->getSize()); + sf::Vector2f windowSize = static_cast(game.gameWindow.getSize()); sf::Vector2f backgroundSize = static_cast(this->fightEnv.backgroundTX.getSize()); sf::Vector2f backgroundScale = sf::Vector2f(windowSize.x / backgroundSize.x, windowSize.y / backgroundSize.y); @@ -42,7 +43,7 @@ FightActivity::~FightActivity() { } void FightActivity::runCurrentState(Game &game) { - FightStateEnum newStateFightEnum = this->currentFightState->run(game, this->fightEnv); + FightStateEnum newStateFightEnum = this->currentFightState->run(this->fightEnv); if (newStateFightEnum != this->currentFightStateEnum) { switch (newStateFightEnum) { case FightStateEnum::PLAYER_STATE: @@ -52,7 +53,7 @@ void FightActivity::runCurrentState(Game &game) { this->currentFightState = std::move(std::make_unique()); break; case FightStateEnum::TURN_CHANGE: - this->currentFightState = std::move(std::make_unique(game, this->fightEnv)); + this->currentFightState = std::move(std::make_unique(this->fightEnv)); break; default: break; @@ -61,20 +62,20 @@ void FightActivity::runCurrentState(Game &game) { } } -ActivityEnum FightActivity::executeActivity(Game &game) { - sf::RenderWindow* gameWindow = game.renderEngine.gameWindow; +ActivityEnum FightActivity::executeActivity() { + Game game = Game::getInstance(); ActivityEnum currentActivity = ActivityEnum::Fight; - gameWindow->draw(this->fightEnv.turnSP); - gameWindow->draw(this->fightEnv.backgroundSP); - this->fightEnv.playerOverview.draw(gameWindow); - this->fightEnv.enemyOverview.draw(gameWindow); - this->exitButton.draw(gameWindow); - this->fightEnv.textFadingManager.run(gameWindow, game.gameStatus); + game.gameWindow.draw(this->fightEnv.turnSP); + game.gameWindow.draw(this->fightEnv.backgroundSP); + this->fightEnv.playerOverview.draw(); + this->fightEnv.enemyOverview.draw(); + this->exitButton.draw(); + this->fightEnv.textFadingManager.run(); this->runCurrentState(game); - if (this->exitButton.clickListener(gameWindow, game.gameEvents)) { + if (this->exitButton.clickListener()) { currentActivity = ActivityEnum::Menu; } diff --git a/src/main/Activities/MenuActivity.cpp b/src/main/Activities/MenuActivity.cpp index 6153054..f005604 100644 --- a/src/main/Activities/MenuActivity.cpp +++ b/src/main/Activities/MenuActivity.cpp @@ -1,6 +1,7 @@ #include "Activities/MenuActivity.hpp" -MenuActivity::MenuActivity(Game &game): Activity(game) { +MenuActivity::MenuActivity(): Activity() { + Game game = Game::getInstance(); this->backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/backgroundMenu.png"); this->backgroundSP.setTexture(this->backgroundTX); @@ -11,7 +12,7 @@ MenuActivity::MenuActivity(Game &game): Activity(game) { this->buttonsBackgroundTX.loadFromFile(RESOURCE_PATH "box_backgrounds/menu_border_with_name.png"); this->buttonsBackgroundSP.setTexture(this->buttonsBackgroundTX); - sf::Vector2f windowSize = static_cast(game.renderEngine.gameWindow->getSize()); + sf::Vector2f windowSize = static_cast(game.gameWindow.getSize()); sf::Vector2f backgroundSize = static_cast(this->backgroundTX.getSize()); sf::Vector2f backgroundScale = sf::Vector2f(windowSize.x / backgroundSize.x, windowSize.y / backgroundSize.y); this->backgroundSP.scale(backgroundScale); @@ -31,32 +32,32 @@ MenuActivity::MenuActivity(Game &game): Activity(game) { MenuActivity::~MenuActivity() { } -ActivityEnum MenuActivity::executeActivity(Game &game) { - sf::RenderWindow* gameWindow = game.renderEngine.gameWindow; +ActivityEnum MenuActivity::executeActivity() { + Game game = Game::getInstance(); ActivityEnum currentActivity = ActivityEnum::Menu; - gameWindow->draw(this->backgroundSP); - gameWindow->draw(this->buttonsBackgroundSP); + game.gameWindow.draw(this->backgroundSP); + game.gameWindow.draw(this->buttonsBackgroundSP); - this->buttonFight.draw(gameWindow); - this->buttonCharacter.draw(gameWindow); - this->buttonExit.draw(gameWindow); + this->buttonFight.draw(); + this->buttonCharacter.draw(); + this->buttonExit.draw(); - if (buttonFight.clickListener(gameWindow, game.gameEvents)) { + if (buttonFight.clickListener()) { this->backgroundMusic.stop(); currentActivity = ActivityEnum::Fight; } - if (buttonCharacter.clickListener(gameWindow, game.gameEvents)) { + if (buttonCharacter.clickListener()) { this->backgroundMusic.stop(); currentActivity = ActivityEnum::Character; } - if (buttonExit.clickListener(gameWindow, game.gameEvents)) { - //game.backgroundMusic.stop(); - this->backgroundMusic.stop(); - gameWindow->close(); + if (buttonExit.clickListener()) { + Game game = Game::getInstance(); + this->backgroundMusic.stop(); + game.gameWindow.close(); } return currentActivity; } \ No newline at end of file diff --git a/src/main/Animations/IncomingBanner.cpp b/src/main/Animations/IncomingBanner.cpp index 0dda7a8..05dcae8 100644 --- a/src/main/Animations/IncomingBanner.cpp +++ b/src/main/Animations/IncomingBanner.cpp @@ -1,66 +1,68 @@ #include "Animations/IncomingBanner.hpp" -void IncomingBanner::init(Game &game) { - sf::Vector2u windowSize = game.renderEngine.gameWindow->getSize(); +void IncomingBanner::init() { + Game game = Game::getInstance(); + sf::Vector2u windowSize = game.gameWindow.getSize(); - this->banner.setSize(sf::Vector2f(windowSize.x, windowSize.y * 0.2)); - this->banner.setFillColor(sf::Color(40, 40, 40, 210)); + this->banner.setSize(sf::Vector2f(windowSize.x, windowSize.y * 0.2)); + this->banner.setFillColor(sf::Color(40, 40, 40, 210)); - this->bannerText.setFont(game.mainFont); - this->bannerText.setCharacterSize(game.renderEngine.gameWindow->getSize().y * 0.1); - this->bannerText.setFillColor(sf::Color::White); - sf::FloatRect textRec = this->bannerText.getGlobalBounds(); - this->bannerText.setOrigin(textRec.width * 0.5, textRec.height * 0.5); + this->bannerText.setFont(game.mainFont); + this->bannerText.setCharacterSize(game.gameWindow.getSize().y * 0.1); + this->bannerText.setFillColor(sf::Color::White); + sf::FloatRect textRec = this->bannerText.getGlobalBounds(); + this->bannerText.setOrigin(textRec.width * 0.5, textRec.height * 0.5); - sf::FloatRect bannerSize = this->banner.getGlobalBounds(); - sf::Vector2f bannerPos = sf::Vector2f(-windowSize.x, (windowSize.y - bannerSize.height) * 0.5f); - this->banner.setPosition(bannerPos.x, bannerPos.y); - this->bannerText.setPosition(bannerPos.x + bannerSize.width * 0.5, bannerPos.y + bannerSize.height * 0.5); + sf::FloatRect bannerSize = this->banner.getGlobalBounds(); + sf::Vector2f bannerPos = sf::Vector2f(-windowSize.x, (windowSize.y - bannerSize.height) * 0.5f); + this->banner.setPosition(bannerPos.x, bannerPos.y); + this->bannerText.setPosition(bannerPos.x + bannerSize.width * 0.5, bannerPos.y + bannerSize.height * 0.5); } -IncomingBanner::IncomingBanner(Game &game) { - this->init(game); +IncomingBanner::IncomingBanner() { + this->init(); } -IncomingBanner::IncomingBanner(Game &game, std::string label) { - this->bannerText.setString(label); - this->init(game); +IncomingBanner::IncomingBanner(std::string label) { + this->bannerText.setString(label); + this->init(); } void IncomingBanner::setNewLabel(std::string newLabel) { - this->bannerText.setString(newLabel); - sf::FloatRect textRec = this->bannerText.getGlobalBounds(); - this->bannerText.setOrigin(textRec.width * 0.5, textRec.height * 0.5); + this->bannerText.setString(newLabel); + sf::FloatRect textRec = this->bannerText.getGlobalBounds(); + this->bannerText.setOrigin(textRec.width * 0.5, textRec.height * 0.5); } -bool IncomingBanner::runAnimation(Game &game) { +bool IncomingBanner::runAnimation() { + 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 - 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 + float pastTimeRatio = std::min(this->pastMovementTime/static_cast(bannerMovementime), 1.0f); + sf::Vector2f windowSize = static_cast(game.gameWindow.getSize()); + sf::FloatRect bannerSize = this->banner.getGlobalBounds(); + sf::Vector2f bannerPos = sf::Vector2f(-windowSize.x + pastTimeRatio * windowSize.x, (windowSize.y - bannerSize.height) * 0.5f); + this->banner.setPosition(bannerPos.x, bannerPos.y); + this->bannerText.setPosition(bannerPos.x + bannerSize.width * 0.5, bannerPos.y + bannerSize.height * 0.5); - float pastTimeRatio = std::min(this->pastMovementTime/static_cast(bannerMovementime), 1.0f); - sf::Vector2f windowSize = static_cast(game.renderEngine.gameWindow->getSize()); - sf::FloatRect bannerSize = this->banner.getGlobalBounds(); - sf::Vector2f bannerPos = sf::Vector2f(-windowSize.x + pastTimeRatio * windowSize.x, (windowSize.y - bannerSize.height) * 0.5f); + this->pastTimeInMillSec += game.gameStatus.elapsedTime.asMilliseconds(); + if (this->pastMovementTime < bannerMovementime) { + this->pastMovementTime += game.gameStatus.elapsedTime.asMilliseconds(); + } + if (this->pastTimeInMillSec >= changeTimeMillSec) { + this->animationStillActive = false; + bannerPos = sf::Vector2f(-windowSize.x, (windowSize.y - bannerSize.height) * 0.5f); this->banner.setPosition(bannerPos.x, bannerPos.y); this->bannerText.setPosition(bannerPos.x + bannerSize.width * 0.5, bannerPos.y + bannerSize.height * 0.5); - - this->pastTimeInMillSec += game.gameStatus.elapsedTime.asMilliseconds(); - if (this->pastMovementTime < bannerMovementime) { - this->pastMovementTime += game.gameStatus.elapsedTime.asMilliseconds(); - } - if (this->pastTimeInMillSec >= changeTimeMillSec) { - this->animationStillActive = false; - bannerPos = sf::Vector2f(-windowSize.x, (windowSize.y - bannerSize.height) * 0.5f); - this->banner.setPosition(bannerPos.x, bannerPos.y); - this->bannerText.setPosition(bannerPos.x + bannerSize.width * 0.5, bannerPos.y + bannerSize.height * 0.5); - this->pastTimeInMillSec = 0; - this->pastMovementTime = 0; - } - return animationStillActive; + this->pastTimeInMillSec = 0; + this->pastMovementTime = 0; + } + return animationStillActive; } -void IncomingBanner::drawAnimation(sf::RenderWindow* gameWindow) { - gameWindow->draw(this->banner); - gameWindow->draw(this->bannerText); +void IncomingBanner::drawAnimation() { + Game game = Game::getInstance(); + game.gameWindow.draw(this->banner); + game.gameWindow.draw(this->bannerText); } \ No newline at end of file diff --git a/src/main/Animations/TextFadingManager.cpp b/src/main/Animations/TextFadingManager.cpp index 7cc1f99..324d42e 100644 --- a/src/main/Animations/TextFadingManager.cpp +++ b/src/main/Animations/TextFadingManager.cpp @@ -23,8 +23,9 @@ TextFading::TextFading(std::string text, sf::Vector2f pos, sf::Color textColor, this->millSecToLive = _millSecToLive; } -void TextFading::draw(sf::RenderWindow* gameWindow) { - gameWindow->draw(this->text); +void TextFading::draw() { + Game game = Game::getInstance(); + game.gameWindow.draw(this->text); } float TextFading::computeParabel(float value) { @@ -51,47 +52,44 @@ void TextFading::setNewParabelPos() { this->text.setPosition(this->initPosX + newRelativePosX, this->initPosY + newRelativePosY); } -void TextFadingManager::updateAnimationState(GameStatus &gameStatus) { - //std::cout << "Remaining Visibility: " << std::to_string(this->fadingText.remainingVisibilty) << std::endl; - sf::Color oldColor = this->fadingText.text.getFillColor(); - sf::Vector2f oldPos = this->fadingText.text.getPosition(); - //std::cout << "Pos: " << std::to_string(oldPos.x) << ", " << std::to_string(oldPos.y) << std::endl; - float pastRatio = gameStatus.elapsedTime.asMilliseconds() / static_cast(this->fadingText.millSecToLive); - int visRatio = int(255*pastRatio); - this->fadingText.remainingVisibilty -= visRatio; - this->fadingText.text.setFillColor(sf::Color(oldColor.r, oldColor.g, oldColor.b, this->fadingText.remainingVisibilty)); - float distanceToTravel = (this->fadingText.pixelPerMillSec * gameStatus.elapsedTime.asMilliseconds()); - switch (this->fadingText.animationPath) { - case Left: - this->fadingText.text.setPosition(oldPos.x - distanceToTravel, oldPos.y); - break; - case Right: - this->fadingText.text.setPosition(oldPos.x + distanceToTravel, oldPos.y); - break; - case Up: - this->fadingText.text.setPosition(oldPos.x, oldPos.y - distanceToTravel); - break; - case Down: - this->fadingText.text.setPosition(oldPos.x, oldPos.y + distanceToTravel); - break; - case Parabel: - this->fadingText.setNewParabelPos(); - break; - default: - break; - } - this->fadingText.pastMillSec += gameStatus.elapsedTime.asMilliseconds(); - if (this->fadingText.pastMillSec >= this->fadingText.millSecToLive) { - //this->fadingText.pastMillSec = 0; - this->isRunning = false; - } - //std::cout << "####################" << std::endl; +void TextFadingManager::updateAnimationState() { + 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(this->fadingText.millSecToLive); + int visRatio = int(255*pastRatio); + this->fadingText.remainingVisibilty -= visRatio; + this->fadingText.text.setFillColor(sf::Color(oldColor.r, oldColor.g, oldColor.b, this->fadingText.remainingVisibilty)); + float distanceToTravel = (this->fadingText.pixelPerMillSec * game.gameStatus.elapsedTime.asMilliseconds()); + switch (this->fadingText.animationPath) { + case Left: + this->fadingText.text.setPosition(oldPos.x - distanceToTravel, oldPos.y); + break; + case Right: + this->fadingText.text.setPosition(oldPos.x + distanceToTravel, oldPos.y); + break; + case Up: + this->fadingText.text.setPosition(oldPos.x, oldPos.y - distanceToTravel); + break; + case Down: + this->fadingText.text.setPosition(oldPos.x, oldPos.y + distanceToTravel); + break; + case Parabel: + this->fadingText.setNewParabelPos(); + break; + default: + break; + } + this->fadingText.pastMillSec += game.gameStatus.elapsedTime.asMilliseconds(); + if (this->fadingText.pastMillSec >= this->fadingText.millSecToLive) { + this->isRunning = false; + } } -void TextFadingManager::run(sf::RenderWindow* gameWindow, GameStatus &gameStatus) { +void TextFadingManager::run() { if (this->isRunning) { - this->fadingText.draw(gameWindow); - this->updateAnimationState(gameStatus); + this->fadingText.draw(); + this->updateAnimationState(); } } diff --git a/src/main/FightEnv.cpp b/src/main/FightEnv.cpp index de8ae05..d5144f8 100644 --- a/src/main/FightEnv.cpp +++ b/src/main/FightEnv.cpp @@ -1,5 +1,4 @@ #include "FightEnv.hpp" -FightEnv::FightEnv(Game &game): playerStatsBox(game, game.player), enemyOverview(game), playerOverview(game) { - +FightEnv::FightEnv(): playerStatsBox(Game::getInstance().player), enemyOverview(), playerOverview() { } diff --git a/src/main/FightStates/EnemiesTurn.cpp b/src/main/FightStates/EnemiesTurn.cpp index 9239f97..0c4b718 100644 --- a/src/main/FightStates/EnemiesTurn.cpp +++ b/src/main/FightStates/EnemiesTurn.cpp @@ -3,7 +3,8 @@ EnemiesTurn::~EnemiesTurn() { } -FightStateEnum EnemiesTurn::run(Game &game, FightEnv &fightEnv) { +FightStateEnum EnemiesTurn::run(FightEnv &fightEnv) { + Game game = Game::getInstance(); FightStateEnum currentState = FightStateEnum::ENEMY_STATE; if (!fightEnv.enemyDamageCalculated) { std::random_device randSeed; @@ -17,7 +18,7 @@ FightStateEnum EnemiesTurn::run(Game &game, FightEnv &fightEnv) { 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.renderEngine.gameWindow->getSize().y * 0.05, AnimationPath::Parabel, millSecToLive); + 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.enemyDamageCalculated = true; } diff --git a/src/main/FightStates/FightState.cpp b/src/main/FightStates/FightState.cpp index d5b2b29..2ecce7e 100644 --- a/src/main/FightStates/FightState.cpp +++ b/src/main/FightStates/FightState.cpp @@ -3,6 +3,6 @@ FightState::~FightState() { } -FightStateEnum FightState::run(Game &game, FightEnv &fightEnv) { +FightStateEnum FightState::run(FightEnv &fightEnv) { return FightStateEnum::TURN_CHANGE; } \ No newline at end of file diff --git a/src/main/FightStates/PlayersTurn.cpp b/src/main/FightStates/PlayersTurn.cpp index c843bb1..3ed6f0d 100644 --- a/src/main/FightStates/PlayersTurn.cpp +++ b/src/main/FightStates/PlayersTurn.cpp @@ -9,30 +9,29 @@ PlayersTurn::PlayersTurn(FightEnv &fightEnv) { PlayersTurn::~PlayersTurn() { } -FightStateEnum PlayersTurn::run(Game &game, FightEnv &fightEnv) { +FightStateEnum PlayersTurn::run(FightEnv &fightEnv) { + Game game = Game::getInstance(); FightStateEnum currentState = FightStateEnum::PLAYER_STATE; switch (this->playerPhase) { case PlayerPhase::PICK_COLOR: - if (fightEnv.enemyOverview.colorPicker.clickListener(game.gameEvents, this->clickedPos) && !colorPicked) { + if (fightEnv.enemyOverview.colorPicker.clickListener(this->clickedPos) && !colorPicked) { this->colorPicked = true; fightEnv.turnSP.setTexture(fightEnv.playersTurnTX); fightEnv.pickedColor = fightEnv.enemyOverview.colorPicker.getPixelColor(clickedPos); fightEnv.enemyOverview.updatePickedColorText("(" + std::to_string(fightEnv.pickedColor.r) + ", " + std::to_string(fightEnv.pickedColor.g) + ", " + std::to_string(fightEnv.pickedColor.b) + ")", fightEnv.pickedColor); float attackMultiplier = this->calculateAttackMult(fightEnv); - //std::cout << "Attack Multiplier: " << std::to_string(attackMultiplier) << std::endl; int damage = game.player.attackStrength * attackMultiplier; - //std::cout << "Damage: " << damage << std::endl; int millSecToLive = 600; - fightEnv.textFadingManager.startAnimation(std::to_string(damage), clickedPos, sf::Color::Yellow, game.renderEngine.gameWindow->getSize().y * 0.05, AnimationPath::Parabel, millSecToLive); + fightEnv.textFadingManager.startAnimation(std::to_string(damage), clickedPos, sf::Color::Yellow, game.gameWindow.getSize().y * 0.05, AnimationPath::Parabel, millSecToLive); fightEnv.enemyOverview.changeHealth(damage); this->playerPhase = PlayerPhase::ANIMATE_ATTACK; } break; case PlayerPhase::ANIMATE_ATTACK: - this->processAttack(fightEnv, game); + this->processAttack(fightEnv); break; case PlayerPhase::CHANGE_COLOR: - this->changeColoPickerImage(game, fightEnv); + this->changeColoPickerImage(fightEnv); break; case PlayerPhase::END_TURN: currentState = FightStateEnum::TURN_CHANGE; @@ -41,7 +40,7 @@ FightStateEnum PlayersTurn::run(Game &game, FightEnv &fightEnv) { return currentState; } -void PlayersTurn::processAttack(FightEnv &fightEnv, Game &game) { +void PlayersTurn::processAttack(FightEnv &fightEnv) { if (fightEnv.textFadingManager.fadingText.pastMillSec >= fightEnv.textFadingManager.fadingText.millSecToLive) { fightEnv.textFadingManager.fadingText.pastMillSec = 0; fightEnv.isPlayersTurn = (fightEnv.isPlayersTurn + 1) % 2; @@ -52,7 +51,8 @@ void PlayersTurn::processAttack(FightEnv &fightEnv, Game &game) { } } -void PlayersTurn::changeColoPickerImage(Game &game, FightEnv &fightEnv) { +void PlayersTurn::changeColoPickerImage(FightEnv &fightEnv) { + Game game = Game::getInstance(); static int changingMillSec = 2000; float elapsedRatio = this->passedMillSec/changingMillSec; for (int y = 0; y < GEN_IMG_HEIGHT; ++y) { diff --git a/src/main/FightStates/TurnChangeState.cpp b/src/main/FightStates/TurnChangeState.cpp index 314e7e9..72eeebf 100644 --- a/src/main/FightStates/TurnChangeState.cpp +++ b/src/main/FightStates/TurnChangeState.cpp @@ -1,6 +1,6 @@ #include "FightStates/TurnChangeState.hpp" -TurnChangeState::TurnChangeState(Game &game, FightEnv &fightEnv): turnChangeBanner(game) { +TurnChangeState::TurnChangeState(FightEnv &fightEnv): turnChangeBanner() { if (fightEnv.isPlayersTurn) { fightEnv.turnSP.setTexture(fightEnv.playersTurnTX); this->turnChangeBanner.setNewLabel("Your Turn"); @@ -12,10 +12,11 @@ TurnChangeState::TurnChangeState(Game &game, FightEnv &fightEnv): turnChangeBann TurnChangeState::~TurnChangeState() { } -FightStateEnum TurnChangeState::run(Game &game, FightEnv &fightEnv) { +FightStateEnum TurnChangeState::run(FightEnv &fightEnv) { + Game game = Game::getInstance(); FightStateEnum currentFightState = FightStateEnum::TURN_CHANGE; - this->turnChangeBanner.drawAnimation(game.renderEngine.gameWindow); - if (!this->turnChangeBanner.runAnimation(game)) { + this->turnChangeBanner.drawAnimation(); + if (!this->turnChangeBanner.runAnimation()) { if (fightEnv.isPlayersTurn) { currentFightState = FightStateEnum::PLAYER_STATE; } else { diff --git a/src/main/Game.cpp b/src/main/Game.cpp index 7022956..c23c206 100644 --- a/src/main/Game.cpp +++ b/src/main/Game.cpp @@ -1,6 +1,14 @@ #include "Game.hpp" -Game::Game(sf::RenderWindow &window, ActivityEnum activity) : renderEngine(&window) { +Game &Game::getInstance() { + if (!instance) { + instance = new Game(); + } + return *instance; +} - this->mainFont.loadFromFile(RESOURCE_PATH "fonts/Avara-Bold.otf"); +Game::Game(): gameWindow(*(new sf::RenderWindow(sf::VideoMode(), "Road of Ogden", sf::Style::Fullscreen))) { + sf::RenderWindow window(sf::VideoMode(), "Road of Ogden", sf::Style::Fullscreen); + this->gameWindow.setFramerateLimit(60); + this->mainFont.loadFromFile(RESOURCE_PATH "fonts/Avara-Bold.otf"); } \ No newline at end of file diff --git a/src/main/UIElements/UIBorderedImage.cpp b/src/main/UIElements/UIBorderedImage.cpp index c71853f..0c7e765 100644 --- a/src/main/UIElements/UIBorderedImage.cpp +++ b/src/main/UIElements/UIBorderedImage.cpp @@ -21,9 +21,10 @@ void UIBorderedImage::setImage(std::string imagePath) { this->imageSP.setTexture(this->imageTX); } -void UIBorderedImage::draw(sf::RenderWindow* gameWindow) { - gameWindow->draw(this->imageSP); - gameWindow->draw(this->borderSP); +void UIBorderedImage::draw() { + Game game = Game::getInstance(); + game.gameWindow.draw(this->imageSP); + game.gameWindow.draw(this->borderSP); } void UIBorderedImage::setPosition(float x, float y) { diff --git a/src/main/UIElements/UIBox.cpp b/src/main/UIElements/UIBox.cpp index facb7a2..274ec0f 100644 --- a/src/main/UIElements/UIBox.cpp +++ b/src/main/UIElements/UIBox.cpp @@ -12,9 +12,10 @@ UIBox::UIBox(sf::Color fillColor, std::string borderFilePath) { this->backgroundREC.setOrigin(backgroundSize.width/2, backgroundSize.height/2); } -void UIBox::draw(sf::RenderWindow* window) { - window->draw(this->backgroundREC); - window->draw(this->borderSP); +void UIBox::draw() { + Game game = Game::getInstance(); + game.gameWindow.draw(this->backgroundREC); + game.gameWindow.draw(this->borderSP); } sf::FloatRect UIBox::getSize() { diff --git a/src/main/UIElements/UIButton.cpp b/src/main/UIElements/UIButton.cpp index 7271a0f..d66257a 100644 --- a/src/main/UIElements/UIButton.cpp +++ b/src/main/UIElements/UIButton.cpp @@ -1,115 +1,117 @@ #include "UIElements/UIButton.hpp" void UIButton::init(std::string filePath) { - std::string buttonsPath = RESOURCE_PATH "buttons/"; - int sep_pos = filePath.find("."); - std::string fileName = filePath.substr(0, sep_pos); - std::string fileType = filePath.substr(sep_pos + 1); - - this->basicTX.loadFromFile(buttonsPath + filePath); - this->clickedTX.loadFromFile(buttonsPath + fileName + "_click." + fileType); - this->hoveredTX.loadFromFile(buttonsPath + fileName + "_hover." + fileType); - this->buttonSP.setTexture(this->basicTX); - - this->hovered = false; - this->pressed = false; - - this->pressSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/tick.wav"); - //this->pressSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/buttonAlpha2.wav"); - this->pressSound.setBuffer(this->pressSoundBuffer); - //this->releaseSound.setBuffer(this->releaseSoundBuffer); + std::string buttonsPath = RESOURCE_PATH "buttons/"; + int sep_pos = filePath.find("."); + std::string fileName = filePath.substr(0, sep_pos); + std::string fileType = filePath.substr(sep_pos + 1); + + this->basicTX.loadFromFile(buttonsPath + filePath); + this->clickedTX.loadFromFile(buttonsPath + fileName + "_click." + fileType); + this->hoveredTX.loadFromFile(buttonsPath + fileName + "_hover." + fileType); + this->buttonSP.setTexture(this->basicTX); + + this->hovered = false; + this->pressed = false; + + this->pressSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/tick.wav"); + //this->pressSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/buttonAlpha2.wav"); + this->pressSound.setBuffer(this->pressSoundBuffer); + //this->releaseSound.setBuffer(this->releaseSoundBuffer); } UIButton::UIButton() {} UIButton::UIButton(std::string filePath) { - this->init(filePath); + this->init(filePath); } UIButton::UIButton(std::string labelText, std::string filePath) { - this->init(filePath); - - sf::Vector2u buttonSize = this->basicTX.getSize(); - this->font.loadFromFile(RESOURCE_PATH "fonts/Avara-Bold.otf"); - this->label.setFont(this->font); - this->label.setString(labelText); - this->label.setCharacterSize(buttonSize.y * 0.5); - this->label.setFillColor(sf::Color::Black); + this->init(filePath); + + sf::Vector2u buttonSize = this->basicTX.getSize(); + this->font.loadFromFile(RESOURCE_PATH "fonts/Avara-Bold.otf"); + this->label.setFont(this->font); + this->label.setString(labelText); + this->label.setCharacterSize(buttonSize.y * 0.5); + this->label.setFillColor(sf::Color::Black); } sf::FloatRect UIButton::getSize() { - return this->buttonSP.getGlobalBounds(); + return this->buttonSP.getGlobalBounds(); } void UIButton::scale(float x, float y) { - this->buttonSP.scale(sf::Vector2f(x, y)); - sf::FloatRect buttonSize = this->getSize(); - this->label.setCharacterSize(buttonSize.height * 0.5); + this->buttonSP.scale(sf::Vector2f(x, y)); + sf::FloatRect buttonSize = this->getSize(); + this->label.setCharacterSize(buttonSize.height * 0.5); } -void UIButton::draw(sf::RenderWindow* window) { - window->draw(this->buttonSP); - window->draw(this->label); +void UIButton::draw() { + Game game = Game::getInstance(); + game.gameWindow.draw(this->buttonSP); + game.gameWindow.draw(this->label); } void UIButton::setPosition(float x, float y) { - this->buttonSP.setPosition(x, y); + this->buttonSP.setPosition(x, y); - sf::Vector2u buttonSize = this->basicTX.getSize(); - sf::Vector2f buttonPos = this->buttonSP.getPosition(); - sf::FloatRect textRect = this->label.getLocalBounds(); - this->label.setOrigin(textRect.width/2, textRect.height/2); - this->label.setPosition(buttonPos.x + buttonSize.x/2, buttonPos.y + buttonSize.y/2); + sf::Vector2u buttonSize = this->basicTX.getSize(); + sf::Vector2f buttonPos = this->buttonSP.getPosition(); + sf::FloatRect textRect = this->label.getLocalBounds(); + this->label.setOrigin(textRect.width/2, textRect.height/2); + this->label.setPosition(buttonPos.x + buttonSize.x/2, buttonPos.y + buttonSize.y/2); } sf::Vector2f UIButton::getPosition() { return this->buttonSP.getPosition(); } - -bool UIButton::buttonContainsMouse(sf::RenderWindow* gameWindow) { - sf::Vector2i mousePos = sf::Mouse::getPosition(*gameWindow); - sf::Vector2f mousePosF(static_cast(mousePos.x), static_cast(mousePos.y)); - return this->buttonSP.getGlobalBounds().contains(mousePosF); +bool UIButton::buttonContainsMouse() { + Game game = Game::getInstance(); + sf::Vector2i mousePos = sf::Mouse::getPosition(game.gameWindow); + sf::Vector2f mousePosF(static_cast(mousePos.x), static_cast(mousePos.y)); + return this->buttonSP.getGlobalBounds().contains(mousePosF); } - -void UIButton::hoverListener(sf::RenderWindow* gameWindow, GameEvents &gameEvents) { - if (gameEvents.mouseMoved) { - if(this->buttonContainsMouse(gameWindow)) { - this->buttonSP.setTexture(this->hoveredTX); - this->hovered = true; - } else { - this->buttonSP.setTexture(this->basicTX); - this->hovered = false; - } - } else if(this->hovered) { - this->buttonSP.setTexture(this->hoveredTX); +void UIButton::hoverListener() { + Game game = Game::getInstance(); + if (game.gameEvents.mouseMoved) { + if(this->buttonContainsMouse()) { + this->buttonSP.setTexture(this->hoveredTX); + this->hovered = true; + } else { + this->buttonSP.setTexture(this->basicTX); + this->hovered = false; } + } else if(this->hovered) { + this->buttonSP.setTexture(this->hoveredTX); + } } -bool UIButton::clickListener(sf::RenderWindow* gameWindow, GameEvents &gameEvents) { - this->hoverListener(gameWindow, gameEvents); - if (gameEvents.mousePressed && this->buttonSP.getGlobalBounds().contains(gameEvents.pressedPos)) { - this->pressSound.play(); - this->pressed = true; - } else if (gameEvents.mousePressed){ - this->pressed = false; +bool UIButton::clickListener() { + Game game = Game::getInstance(); + this->hoverListener(); + if (game.gameEvents.mousePressed && this->buttonSP.getGlobalBounds().contains(game.gameEvents.pressedPos)) { + this->pressSound.play(); + this->pressed = true; + } else if (game.gameEvents.mousePressed){ + this->pressed = false; + } + if (this->pressed) { + if (this->buttonContainsMouse()) { + this->buttonSP.setTexture(this->clickedTX); } - if (this->pressed) { - if (this->buttonContainsMouse(gameWindow)) { - this->buttonSP.setTexture(this->clickedTX); - } - if (gameEvents.mouseReleased) { - this->pressed = false; - if (this->buttonSP.getGlobalBounds().contains(gameEvents.releasedPos)) { - // Sound is attached to button. Therefore, stops when activity changes - //this->releaseSound.play(); - return true; - } else { - return false; - } - } + if (game.gameEvents.mouseReleased) { + this->pressed = false; + if (this->buttonSP.getGlobalBounds().contains(game.gameEvents.releasedPos)) { + // Sound is attached to button. Therefore, stops when activity changes + //this->releaseSound.play(); + return true; + } else { + return false; + } } - return false; + } + return false; } \ No newline at end of file diff --git a/src/main/UIElements/UIColorPicker.cpp b/src/main/UIElements/UIColorPicker.cpp index ebd97d5..bba4577 100644 --- a/src/main/UIElements/UIColorPicker.cpp +++ b/src/main/UIElements/UIColorPicker.cpp @@ -37,9 +37,10 @@ UIColorPicker::UIColorPicker(sf::Image image, std::string borderPath) { this->colorIMG = image; } -void UIColorPicker::draw(sf::RenderWindow* window) { - window->draw(this->colorSP); - window->draw(this->borderSP); +void UIColorPicker::draw() { + Game game = Game::getInstance(); + game.gameWindow.draw(this->colorSP); + game.gameWindow.draw(this->borderSP); } void UIColorPicker::setPosition(float x, float y) { @@ -73,17 +74,18 @@ sf::Color UIColorPicker::getPixelColor(sf::Vector2f pos) { return this->colorIMG.getPixel(posImg.x, posImg.y); } -bool UIColorPicker::clickListener(GameEvents &gameEvents, sf::Vector2f &clickedPos) { - if (gameEvents.mousePressed && this->colorSP.getGlobalBounds().contains(gameEvents.pressedPos)) { +bool UIColorPicker::clickListener(sf::Vector2f &clickedPos) { + Game game = Game::getInstance(); + if (game.gameEvents.mousePressed && this->colorSP.getGlobalBounds().contains(game.gameEvents.pressedPos)) { this->pressed = true; - } else if (gameEvents.mousePressed){ + } else if (game.gameEvents.mousePressed){ this->pressed = false; } if (this->pressed) { - if (gameEvents.mouseReleased) { + if (game.gameEvents.mouseReleased) { this->pressed = false; - if (this->colorSP.getGlobalBounds().contains(gameEvents.releasedPos)) { - clickedPos = sf::Vector2f(gameEvents.releasedPos.x, gameEvents.releasedPos.y); + if (this->colorSP.getGlobalBounds().contains(game.gameEvents.releasedPos)) { + clickedPos = sf::Vector2f(game.gameEvents.releasedPos.x, game.gameEvents.releasedPos.y); this->releaseSound.play(); return true; } diff --git a/src/main/UIElements/UIElement.cpp b/src/main/UIElements/UIElement.cpp index 4647f98..77cfc97 100644 --- a/src/main/UIElements/UIElement.cpp +++ b/src/main/UIElements/UIElement.cpp @@ -1,6 +1,6 @@ #include "UIElements/UIElement.hpp" -void UIElement::draw(sf::RenderWindow* window) { +void UIElement::draw() { } diff --git a/src/main/UIElements/UIStats.cpp b/src/main/UIElements/UIStats.cpp index d236ffa..4302dba 100644 --- a/src/main/UIElements/UIStats.cpp +++ b/src/main/UIElements/UIStats.cpp @@ -1,56 +1,57 @@ #include "UIElements/UIStats.hpp" -UIStats::UIStats(Game &game, Actor actor) { - sf::Vector2u windowSize = game.renderEngine.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; +UIStats::UIStats(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; + 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); - float scale = (windowSize.y * 0.4) / this->actorStatsBox.getSize().height; - this->actorStatsBox.scale(scale, scale); + this->actorHealthLabel.setFont(game.mainFont); + this->actorHealthLabel.setString("Health:"); + this->actorHealthLabel.setCharacterSize(statsTextHeight); + this->actorHealthLabel.setFillColor(statsLabelFontColor); - 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.); + 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::setActor(Actor actor) { @@ -60,15 +61,16 @@ void UIStats::setActor(Actor actor) { this->actorRGBDefenseValues.setString("(" + std::to_string(actor.defense.red) + ", " + std::to_string(actor.defense.green) + ", " + std::to_string(actor.defense.blue) + ")"); } -void UIStats::draw(sf::RenderWindow* gameWindow) { - this->actorStatsBox.draw(gameWindow); - gameWindow->draw(this->actorName); - gameWindow->draw(this->actorHealthLabel); - gameWindow->draw(this->actorHealthValue); - gameWindow->draw(this->actorAttackStrengthLabel); - gameWindow->draw(this->actorAttackStrengthValue); - gameWindow->draw(this->actorRGBDefenseLabel); - gameWindow->draw(this->actorRGBDefenseValues); +void UIStats::draw() { + Game game = Game::getInstance(); + this->actorStatsBox.draw(); + game.gameWindow.draw(this->actorName); + game.gameWindow.draw(this->actorHealthLabel); + game.gameWindow.draw(this->actorHealthValue); + game.gameWindow.draw(this->actorAttackStrengthLabel); + game.gameWindow.draw(this->actorAttackStrengthValue); + game.gameWindow.draw(this->actorRGBDefenseLabel); + game.gameWindow.draw(this->actorRGBDefenseValues); } sf::Vector2f UIStats::getPosition() { diff --git a/src/main/UIObjects/UIEnemyOverview.cpp b/src/main/UIObjects/UIEnemyOverview.cpp index e2613ef..5fc0fdd 100644 --- a/src/main/UIObjects/UIEnemyOverview.cpp +++ b/src/main/UIObjects/UIEnemyOverview.cpp @@ -1,11 +1,11 @@ #include "UIObjects/UIEnemyOverview.hpp" -UIEnemyOverview::UIEnemyOverview(Game &game): UIEnemyOverview(game, Enemy()) { +UIEnemyOverview::UIEnemyOverview(): UIEnemyOverview(Enemy()) { } -UIEnemyOverview::UIEnemyOverview(Game &game, Enemy enemy): statsComponent(game, enemy), creature(enemy), creatureFrame("monster_landscape_cut/" + enemy.picPath, "actor_borders/fight_border.png") { - sf::Vector2u windowSize = game.renderEngine.gameWindow->getSize(); - +UIEnemyOverview::UIEnemyOverview(Enemy enemy): statsComponent(enemy), creature(enemy), creatureFrame("monster_landscape_cut/" + enemy.picPath, "actor_borders/fight_border.png") { + Game game = Game::getInstance(); + sf::Vector2u windowSize = game.gameWindow.getSize(); sf::FloatRect boxRect = this->backgroundBox.getSize(); sf::Vector2f overviewPos = sf::Vector2f(windowSize.x * 0.51, windowSize.y * 0.1); this->backgroundBox.setPosition(overviewPos.x, overviewPos.y); @@ -38,7 +38,7 @@ UIEnemyOverview::UIEnemyOverview(Game &game, Enemy enemy): statsComponent(game, this->pickedColorText.setFont(game.mainFont); this->pickedColorText.setString("(0, 0, 0)"); - this->pickedColorText.setCharacterSize(game.renderEngine.gameWindow->getSize().y * 0.04); + 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); @@ -52,13 +52,14 @@ void UIEnemyOverview::setEnemy(Enemy enemy) { this->colorPicker.setColorImage(enemy.colorPicPath); } -void UIEnemyOverview::draw(sf::RenderWindow* gameWindow) { - this->backgroundBox.draw(gameWindow); - this->statsComponent.draw(gameWindow); - this->colorPicker.draw(gameWindow); - gameWindow->draw(this->pickedColorText); - gameWindow->draw(this->creatureBackgroundSP); - this->creatureFrame.draw(gameWindow); +void UIEnemyOverview::draw() { + Game game = Game::getInstance(); + this->backgroundBox.draw(); + this->statsComponent.draw(); + this->colorPicker.draw(); + game.gameWindow.draw(this->pickedColorText); + game.gameWindow.draw(this->creatureBackgroundSP); + this->creatureFrame.draw(); } void UIEnemyOverview::changeHealth(int value) { diff --git a/src/main/UIObjects/UIPlayerOverview.cpp b/src/main/UIObjects/UIPlayerOverview.cpp index d552f14..586a009 100644 --- a/src/main/UIObjects/UIPlayerOverview.cpp +++ b/src/main/UIObjects/UIPlayerOverview.cpp @@ -1,8 +1,8 @@ #include "UIObjects/UIPlayerOverview.hpp" -UIPlayerOverview::UIPlayerOverview(Game &game): statsComponent(game, game.player), player(game.player), playerFrame("monster_landscape_cut/" + game.player.picPath, "actor_borders/fight_border.png") { - sf::Vector2u windowSize = game.renderEngine.gameWindow->getSize(); - +UIPlayerOverview::UIPlayerOverview(): statsComponent(Game::getInstance().player), player(Game::getInstance().player), playerFrame("monster_landscape_cut/" + Game::getInstance().player.picPath, "actor_borders/fight_border.png") { + Game game = Game::getInstance(); + 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); @@ -33,9 +33,10 @@ void UIPlayerOverview::changeHealth(int value) { this->statsComponent.updateHealth(newHealth); } -void UIPlayerOverview::draw(sf::RenderWindow* gameWindow) { - this->backgroundBox.draw(gameWindow); - this->statsComponent.draw(gameWindow); - gameWindow->draw(this->playerBackgroundSP); - this->playerFrame.draw(gameWindow); +void UIPlayerOverview::draw() { + Game game = Game::getInstance(); + this->backgroundBox.draw(); + this->statsComponent.draw(); + game.gameWindow.draw(this->playerBackgroundSP); + this->playerFrame.draw(); } \ No newline at end of file diff --git a/src/main/main.cpp b/src/main/main.cpp index 721f4ed..e7efeed 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -9,17 +9,17 @@ #include "ActivityEnum.hpp" -std::unique_ptr setCurrentActivity(Game &game, ActivityEnum newActivityEnum) { +std::unique_ptr setCurrentActivity(ActivityEnum newActivityEnum) { std::unique_ptr newActivity; switch (newActivityEnum) { case ActivityEnum::Menu: - newActivity = std::move(std::make_unique(game)); + newActivity = std::move(std::make_unique()); break; case ActivityEnum::Fight: - newActivity = std::move(std::make_unique(game)); + newActivity = std::move(std::make_unique()); break; case ActivityEnum::Character: - newActivity = std::move(std::make_unique(game)); + newActivity = std::move(std::make_unique()); break; default: break; @@ -29,36 +29,35 @@ std::unique_ptr setCurrentActivity(Game &game, ActivityEnum newActivit int main() { - sf::RenderWindow window(sf::VideoMode(), "Road of Ogden", sf::Style::Fullscreen); - window.setFramerateLimit(60); - Game game = Game(window, Fight); + + Game game = Game::getInstance(); sf::Vector2i mousePos; sf::Vector2f mousePosF; //generateTexture(); - std::unique_ptr currentActivity = std::make_unique(game); + std::unique_ptr currentActivity = std::make_unique(); ActivityEnum currentActivityEnum = ActivityEnum::Menu; ActivityEnum oldActivityEnum = ActivityEnum::Menu; sf::Clock clock; sf::Time time; - while (window.isOpen()) { + while (game.gameWindow.isOpen()) { sf::Event event; time = clock.restart(); game.gameStatus.elapsedTime = time; //std::cout << "Elapsed Time: " << std::to_string(time.asMilliseconds()) << std::endl; - while (window.pollEvent(event)) { + while (game.gameWindow.pollEvent(event)) { switch (event.type) { case sf::Event::Closed: //game.backgroundMusic.stop(); - window.close(); + game.gameWindow.close(); break; case sf::Event::MouseMoved: @@ -67,14 +66,14 @@ int main() case sf::Event::MouseButtonPressed: game.gameEvents.mousePressed = true; - mousePos = sf::Mouse::getPosition(window); + mousePos = sf::Mouse::getPosition(game.gameWindow); mousePosF = sf::Vector2f(static_cast(mousePos.x), static_cast(mousePos.y)); game.gameEvents.pressedPos = mousePosF; break; case sf::Event::MouseButtonReleased: game.gameEvents.mouseReleased = true; - mousePos = sf::Mouse::getPosition(window); + mousePos = sf::Mouse::getPosition(game.gameWindow); mousePosF = sf::Vector2f(static_cast(mousePos.x), static_cast(mousePos.y)); game.gameEvents.releasedPos = mousePosF; break; @@ -83,13 +82,13 @@ int main() break; } } - window.clear(); - currentActivityEnum = currentActivity->executeActivity(game); + game.gameWindow.clear(); + currentActivityEnum = currentActivity->executeActivity(); if (currentActivityEnum != oldActivityEnum) { - currentActivity = std::move(setCurrentActivity(game, currentActivityEnum)); + currentActivity = std::move(setCurrentActivity(currentActivityEnum)); oldActivityEnum = currentActivityEnum; } - window.display(); + game.gameWindow.display(); game.gameEvents.mousePressed = false; game.gameEvents.mouseReleased = false; game.gameEvents.mouseMoved = false;