Skip to content

Commit

Permalink
changed color pick sound and refactored animation banner code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed Mar 15, 2024
1 parent 140d494 commit d824328
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 57 deletions.
6 changes: 2 additions & 4 deletions src/include/Activities/FightActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Activities/Activity.hpp"
#include "ActivityEnum.hpp"
#include "Animations/TextFadingManager.hpp"
#include "Animations/IncomingBanner.hpp"
#include "UIComponents/UIStats.hpp"
#include "UI_Objects/UIEnemyOverview.hpp"
#include "UI_Objects/UIPlayerOverview.hpp"
Expand Down Expand Up @@ -44,22 +45,19 @@ class FightActivity: public Activity {
sf::Texture playersTurnTX;
sf::Texture enemiesTurnTX;
sf::Sprite turnSP;
IncomingBanner turnChangeBanner;

sf::Color pickedColor;
int maxMultiplier = 2;
int isPlayersTurn;
bool enemyDamageCalculated = false;
bool turnIsChanging = true;
sf::RectangleShape turnBanner;
sf::Text turnBannerText;
sf::RectangleShape transparentLayer;

Enemy initEnemy();
void runPlayersTurn(GameState &gameState);
void runEnemiesTurn(GameState &gameState);
void runDefeat(GameState &gameState);
void runVictory(GameState &gameState);
void updateTurnChangeState(GameState &gameState);

// Compute damage multiplier
float mapInInterval(float value);
Expand Down
25 changes: 25 additions & 0 deletions src/include/Animations/IncomingBanner.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef INCOMINGBANNER_HPP
#define INCOMINGBANNER_HPP

#include <string>
#include <SFML/Graphics.hpp>

#include "GameState.hpp"

class IncomingBanner {
public:
IncomingBanner(GameState &GameState);
IncomingBanner(GameState &gameState, std::string label);

void setNewLabel(std::string newLabel);
void startAnimation();
void updateAnimation(GameState &gameState, bool &animationRuns);
void drawAnimation(sf::RenderWindow &gameWindow);

private:
sf::RectangleShape banner;
sf::Text bannerText;
void init(GameState &gameState);
};

#endif
3 changes: 2 additions & 1 deletion src/include/UIElements/UIColorPicker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ class UIColorPicker: public UIElement {
sf::Image colorIMG;

sf::SoundBuffer releaseSoundBuffer;
sf::Sound releaseSound;

bool pressed;

public:
UIColorPicker(std::string imagePath, std::string borderPath);
UIColorPicker(sf::Image image, std::string borderPath);

sf::Sound releaseSound;

void draw(sf::RenderWindow &window) override;
void setPosition(float x, float y) override;
sf::Vector2f getPosition() override;
Expand Down
58 changes: 7 additions & 51 deletions src/main/Activities/FightActivity.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "Activities/FightActivity.hpp"


FightActivity::FightActivity(GameState &gameState) : playerStatsBox(gameState, gameState.player), enemyOverview(gameState, initEnemy()), playerOverview(gameState) {
FightActivity::FightActivity(GameState &gameState) : playerStatsBox(gameState, gameState.player), enemyOverview(gameState, initEnemy()), playerOverview(gameState), turnChangeBanner(gameState) {
this->backgroundTX.loadFromFile(RESOURCE_PATH "backgrounds/background_fight.png");
this->backgroundSP.setTexture(this->backgroundTX);

Expand All @@ -24,51 +24,13 @@ FightActivity::FightActivity(GameState &gameState) : playerStatsBox(gameState, g

if (this->isPlayersTurn) {
this->turnSP.setTexture(this->playersTurnTX);
this->turnBannerText.setString("Your Turn");
this->turnChangeBanner.setNewLabel("Your Turn");
} else {
this->turnSP.setTexture(this->enemiesTurnTX);
this->turnBannerText.setString("Enemies Turn");
this->turnChangeBanner.setNewLabel("Enemies Turn");
}
sf::FloatRect turnStateSignSize = this->turnSP.getGlobalBounds();
this->turnSP.setPosition((windowSize.x - turnStateSignSize.width) * 0.5 , -2.0);

this->transparentLayer.setSize(static_cast<sf::Vector2f>(windowSize));
this->transparentLayer.setFillColor(sf::Color(40, 40, 40, 210));

this->turnBanner.setSize(sf::Vector2f(windowSize.x, windowSize.y * 0.2));
this->turnBanner.setFillColor(sf::Color(40, 40, 40, 210));

this->turnBannerText.setFont(gameState.mainFont);
this->turnBannerText.setCharacterSize(gameState.gameWindow->getSize().y*0.1);
this->turnBannerText.setFillColor(sf::Color::White);
sf::FloatRect textRec = this->turnBannerText.getGlobalBounds();
this->turnBannerText.setOrigin(textRec.width/2, textRec.height/2);
}

void FightActivity::updateTurnChangeState(GameState &gameState) {
int changeTimeMillSec = 1000;
int bannerMovementime = 300;
static int pastTimeInMillSec = 0;
static int pastMovementTime = 0;
float pastTimeRatio = std::min(pastMovementTime/static_cast<float>(bannerMovementime), 1.0f);
sf::Vector2f windowSize = static_cast<sf::Vector2f>(gameState.gameWindow->getSize());
sf::FloatRect turnBannerSize = this->turnBanner.getGlobalBounds();
sf::Vector2f turnBannerPos = sf::Vector2f(-windowSize.x + pastTimeRatio * windowSize.x, (windowSize.y - turnBannerSize.height) * 0.5f);
this->turnBanner.setPosition(turnBannerPos.x, turnBannerPos.y);
this->turnBannerText.setPosition(turnBannerPos.x + turnBannerSize.width * 0.5, turnBannerPos.y + turnBannerSize.height * 0.5);

pastTimeInMillSec += gameState.elapsedTime.asMilliseconds();
if (pastMovementTime < bannerMovementime) {
pastMovementTime += gameState.elapsedTime.asMilliseconds();
}
if (pastTimeInMillSec >= changeTimeMillSec) {
this->turnIsChanging = false;
turnBannerPos = sf::Vector2f(-windowSize.x, (windowSize.y - turnBannerSize.height) * 0.5f);
this->turnBanner.setPosition(turnBannerPos.x, turnBannerPos.y);
this->turnBannerText.setPosition(turnBannerPos.x + turnBannerSize.width * 0.5, turnBannerPos.y + turnBannerSize.height * 0.5);
pastTimeInMillSec = 0;
pastMovementTime = 0;
}
}

void FightActivity::runEnemiesTurn(GameState &gameState) {
Expand All @@ -93,9 +55,7 @@ void FightActivity::runEnemiesTurn(GameState &gameState) {
this->textFadingManager.fadingText.pastMillSec = 0;
this->isPlayersTurn = (this->isPlayersTurn + 1) % 2;
this->turnSP.setTexture(this->playersTurnTX);
this->turnBannerText.setString("Your Turn");
sf::FloatRect turnBannerTextSize = this->turnBannerText.getGlobalBounds();
this->turnBannerText.setOrigin(turnBannerTextSize.width * 0.5, turnBannerTextSize.height * 0.5);
this->turnChangeBanner.setNewLabel("Your Turn");
this->turnIsChanging = true;
}
}
Expand All @@ -118,9 +78,7 @@ void FightActivity::runPlayersTurn(GameState &gameState) {
this->isPlayersTurn = (this->isPlayersTurn + 1) % 2;
this->enemyDamageCalculated = false;
this->turnSP.setTexture(this->enemiesTurnTX);
this->turnBannerText.setString("Enemies Turn");
sf::FloatRect turnBannerTextSize = this->turnBannerText.getGlobalBounds();
this->turnBannerText.setOrigin(turnBannerTextSize.width * 0.5, turnBannerTextSize.height * 0.5);
this->turnChangeBanner.setNewLabel("Enemies Turn");
this->turnIsChanging = true;
}
}
Expand All @@ -139,7 +97,7 @@ void FightActivity::runFight(GameState &gameState) {
} else if (this->enemyOverview.creature.health == 0) {
this->runVictory(gameState);
} else if (this->turnIsChanging) {
this->updateTurnChangeState(gameState);
this->turnChangeBanner.updateAnimation(gameState, this->turnIsChanging);
} else if (this->isPlayersTurn) {
this->runPlayersTurn(gameState);
} else {
Expand All @@ -162,9 +120,7 @@ void FightActivity::executeActivity(GameState &gameState) {
this->exitButton.draw(*gameState.gameWindow);
this->textFadingManager.run(gameState);
if (this->turnIsChanging) {
//window->draw(this->transparentLayer);
window->draw(this->turnBanner);
window->draw(this->turnBannerText);
this->turnChangeBanner.drawAnimation(*gameState.gameWindow);
}

if (this->exitButton.clickListener(gameState)) {
Expand Down
60 changes: 60 additions & 0 deletions src/main/Animations/IncomingBanner.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "Animations/IncomingBanner.hpp"

void IncomingBanner::init(GameState &gameState) {
sf::Vector2u windowSize = gameState.gameWindow->getSize();

this->banner.setSize(sf::Vector2f(windowSize.x, windowSize.y * 0.2));
this->banner.setFillColor(sf::Color(40, 40, 40, 210));

this->bannerText.setFont(gameState.mainFont);
this->bannerText.setCharacterSize(gameState.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);
}

IncomingBanner::IncomingBanner(GameState &gameState) {
this->init(gameState);
}

IncomingBanner::IncomingBanner(GameState &gameState, std::string label) {
this->bannerText.setString(label);
this->init(gameState);
}

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);
}

void IncomingBanner::updateAnimation(GameState &gameState, bool &animationRuns) {
int changeTimeMillSec = 1000;
int bannerMovementime = 300;
static int pastTimeInMillSec = 0;
static int pastMovementTime = 0;
float pastTimeRatio = std::min(pastMovementTime/static_cast<float>(bannerMovementime), 1.0f);
sf::Vector2f windowSize = static_cast<sf::Vector2f>(gameState.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);

pastTimeInMillSec += gameState.elapsedTime.asMilliseconds();
if (pastMovementTime < bannerMovementime) {
pastMovementTime += gameState.elapsedTime.asMilliseconds();
}
if (pastTimeInMillSec >= changeTimeMillSec) {
animationRuns = 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);
pastTimeInMillSec = 0;
pastMovementTime = 0;
}
}

void IncomingBanner::drawAnimation(sf::RenderWindow &gameWindow) {
gameWindow.draw(this->banner);
gameWindow.draw(this->bannerText);
}
2 changes: 1 addition & 1 deletion src/main/UIElements/UIColorPicker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ UIColorPicker::UIColorPicker(std::string imagePath, std::string borderPath) {
// For a save small overlap
this->borderSP.scale(0.97, 0.97);

this->releaseSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/buttonAlpha2.wav");
this->releaseSoundBuffer.loadFromFile(RESOURCE_PATH "test_sounds/air_gun.wav");
this->releaseSound.setBuffer(this->releaseSoundBuffer);
}

Expand Down
Binary file added src/resources/test_sounds/air_gun.wav
Binary file not shown.
Binary file added src/resources/test_sounds/softair_3.wav
Binary file not shown.

0 comments on commit d824328

Please sign in to comment.