Skip to content

Commit

Permalink
Merge pull request #43 from Ipagaxi/fight
Browse files Browse the repository at this point in the history
Fight
  • Loading branch information
Ipagaxi committed Mar 15, 2024
2 parents 9156cb8 + da9fc8c commit ff68ab2
Show file tree
Hide file tree
Showing 23 changed files with 223 additions and 100 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ file(GLOB source_files
"src/main/UIComponents/*.cpp"
"src/main/Actors/*.cpp"
"src/main/Animations/*.cpp"
"src/main/UI_Objects/*.cpp"
)
set(SOURCES ${source_files})

Expand Down
10 changes: 8 additions & 2 deletions src/include/Activities/FightActivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
#include "Activities/Activity.hpp"
#include "ActivityEnum.hpp"
#include "Animations/TextFadingManager.hpp"
#include "Animations/IncomingBanner.hpp"
#include "UIComponents/UIStats.hpp"
#include "UIEnemyOverview.hpp"
#include "UIPlayerOverview.hpp"
#include "UI_Objects/UIEnemyOverview.hpp"
#include "UI_Objects/UIPlayerOverview.hpp"
#include "Activities/MenuActivity.hpp"
#include "UIElements/UIColorPicker.hpp"
#include "UIElements/UIButton.hpp"
Expand All @@ -41,11 +42,16 @@ class FightActivity: public Activity {
UIEnemyOverview enemyOverview;
UIPlayerOverview playerOverview;
TextFadingManager textFadingManager;
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;

Enemy initEnemy();
void runPlayersTurn(GameState &gameState);
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
6 changes: 3 additions & 3 deletions src/include/Animations/TextFadingManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ enum AnimationPath {
class TextFading {
public:
TextFading();
TextFading(std::string text, sf::Vector2f pos, sf::Color textColor, int textSize, sf::Font _font, float _pixelPerMillSec);
TextFading(std::string text, sf::Vector2f pos, sf::Color textColor, int textSize, sf::Font _font);
sf::Font font;
sf::Text text;
int remainingVisibilty = 0;
int millSecToLive = 600;
AnimationPath animationPath = Right;
float pixelPerMillSec = 0.2;
float pixelPerMillSec = 0.15;
// Used for parabel computation
int pastMillSec = 0;
float initPosY;
Expand All @@ -44,7 +44,7 @@ class TextFading {
class TextFadingManager: Animation {
public:
void run(GameState &gameState);
void startAnimation(GameState &gameState, std::string text, sf::Vector2f pos, sf::Color textColor, int textSize, float pixelPerMillSec, AnimationPath animationPath);
void startAnimation(GameState &gameState, std::string text, sf::Vector2f pos, sf::Color textColor, int textSize, AnimationPath animationPath);
void updateAnimationState(GameState &gameState);
TextFading fadingText;
bool isRunning = false;
Expand Down
2 changes: 1 addition & 1 deletion src/include/GameState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class GameState {
sf::RenderWindow* gameWindow;
sf::Time elapsedTime;

Player player = Player("Ipagaxi", 20, 12, {100, 100, 100}, "default_actor_quer.png");
Player player = Player("Ipagaxi", 100, 12, {100, 100, 100}, "default_actor_quer.png");

GameState(sf::RenderWindow &window, ActivityEnum activity);

Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class UIEnemyOverview {
UIEnemyOverview(GameState &gameState, Enemy enemy);

Enemy creature;
UIColorPicker colorPicker = UIColorPicker("colorPIC_default.png", "border_square.png");
UIColorPicker colorPicker = UIColorPicker("colorPIC_default.png", "color_picker_border.png");
sf::Text pickedColorText;

void changeHealth(int value);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#ifndef UIPLAYEROVERVIEW_HPP
#define UIPLAYEROVERVIEW_HPP

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

#include "GameState.hpp"
#include "UIElements/UIBox.hpp"
#include "UIElements/UIBorderedImage.hpp"
#include "UIComponents/UIStats.hpp"


class UIPlayerOverview {
public:
UIPlayerOverview(GameState &gameState);

UIBorderedImage playerFrame;
Player player;

void changeHealth(int value);
void draw(sf::RenderWindow &gameWindow);

private:
UIBox backgroundBox = UIBox(sf::Color(51, 25, 0, 150), "borders/metal_border_900x900.png");
sf::Texture playerBackgroundTX;
sf::Sprite playerBackgroundSP;
UIStats statsComponent;
};

#ifndef UIPLAYEROVERVIEW_HPP
#define UIPLAYEROVERVIEW_HPP

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

#include "GameState.hpp"
#include "UIElements/UIBox.hpp"
#include "UIElements/UIBorderedImage.hpp"
#include "UIComponents/UIStats.hpp"


class UIPlayerOverview {
public:
UIPlayerOverview(GameState &gameState);

UIBorderedImage playerFrame;
Player player;

void changeHealth(int value);
void draw(sf::RenderWindow &gameWindow);

private:
UIBox backgroundBox = UIBox(sf::Color(51, 25, 0, 150), "borders/metal_border_900x900.png");
sf::Texture playerBackgroundTX;
sf::Sprite playerBackgroundSP;
UIStats statsComponent;
};

#endif
52 changes: 42 additions & 10 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 @@ -18,10 +18,24 @@ FightActivity::FightActivity(GameState &gameState) : playerStatsBox(gameState, g
std::mt19937 gen(randSeed());
std::uniform_int_distribution<int> dist(0, 1);
this->isPlayersTurn = dist(gen);

this->playersTurnTX.loadFromFile(RESOURCE_PATH "combat/turn_status_player.png");
this->enemiesTurnTX.loadFromFile(RESOURCE_PATH "combat/turn_status_enemy.png");

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

void FightActivity::runEnemiesTurn(GameState &gameState) {
if (!enemyDamageCalculated) {
this->turnIsChanging = false;
std::random_device randSeed;
std::mt19937 gen(randSeed());
int minDamage = int(0.75 * this->enemyOverview.creature.attackStrength);
Expand All @@ -33,32 +47,39 @@ void FightActivity::runEnemiesTurn(GameState &gameState) {
sf::FloatRect playerIconSize = this->playerOverview.playerFrame.getSize();
sf::Vector2f damagePos = sf::Vector2f(playerIconPos.x + (playerIconSize.width * 0.5), playerIconPos.y + (playerIconSize.height * 0.5));

this->textFadingManager.startAnimation(gameState, std::to_string(enemyDamage), damagePos, sf::Color::Yellow, gameState.gameWindow->getSize().y * 0.05, 0.15, AnimationPath::Parabel);
this->textFadingManager.startAnimation(gameState, std::to_string(enemyDamage), damagePos, sf::Color::Yellow, gameState.gameWindow->getSize().y * 0.05, AnimationPath::Parabel);
this->playerOverview.changeHealth(enemyDamage);
this->enemyDamageCalculated = true;
}
if (this->textFadingManager.fadingText.pastMillSec >= this->textFadingManager.fadingText.millSecToLive) {
this->textFadingManager.fadingText.pastMillSec = 0;
this->isPlayersTurn = (this->isPlayersTurn + 1) % 2;
this->turnSP.setTexture(this->playersTurnTX);
this->turnChangeBanner.setNewLabel("Your Turn");
this->turnIsChanging = true;
}
}

void FightActivity::runPlayersTurn(GameState &gameState) {
sf::Vector2f clickedPos;
if (this->enemyOverview.colorPicker.clickListener(gameState, clickedPos)) {
this->turnSP.setTexture(this->playersTurnTX);
this->pickedColor = this->enemyOverview.colorPicker.getPixelColor(clickedPos);
this->enemyOverview.updatePickedColorText("(" + std::to_string(pickedColor.r) + ", " + std::to_string(pickedColor.g) + ", " + std::to_string(pickedColor.b) + ")", this->pickedColor);
float attackMultiplier = this->calculateAttackMult();
//std::cout << "Attack Multiplier: " << std::to_string(attackMultiplier) << std::endl;
int damage = gameState.player.attackStrength * attackMultiplier;
//std::cout << "Damage: " << damage << std::endl;
this->textFadingManager.startAnimation(gameState, std::to_string(damage), clickedPos, sf::Color::Yellow, gameState.gameWindow->getSize().y * 0.05, 0.15, AnimationPath::Parabel);
this->textFadingManager.startAnimation(gameState, std::to_string(damage), clickedPos, sf::Color::Yellow, gameState.gameWindow->getSize().y * 0.05, AnimationPath::Parabel);
this->enemyOverview.changeHealth(damage);
}
if (this->textFadingManager.fadingText.pastMillSec >= this->textFadingManager.fadingText.millSecToLive) {
this->textFadingManager.fadingText.pastMillSec = 0;
this->isPlayersTurn = (this->isPlayersTurn + 1) % 2;
this->enemyDamageCalculated = false;
this->turnSP.setTexture(this->enemiesTurnTX);
this->turnChangeBanner.setNewLabel("Enemies Turn");
this->turnIsChanging = true;
}
}

Expand All @@ -75,6 +96,8 @@ void FightActivity::runFight(GameState &gameState) {
this->runDefeat(gameState);
} else if (this->enemyOverview.creature.health == 0) {
this->runVictory(gameState);
} else if (this->turnIsChanging) {
this->turnChangeBanner.updateAnimation(gameState, this->turnIsChanging);
} else if (this->isPlayersTurn) {
this->runPlayersTurn(gameState);
} else {
Expand All @@ -91,10 +114,14 @@ void FightActivity::executeActivity(GameState &gameState) {
this->runFight(gameState);

window->draw(this->backgroundSP);
window->draw(this->turnSP);
this->playerOverview.draw(*window);
this->enemyOverview.draw(*window);
this->exitButton.draw(*gameState.gameWindow);
this->textFadingManager.run(gameState);
if (this->turnIsChanging) {
this->turnChangeBanner.drawAnimation(*gameState.gameWindow);
}

if (this->exitButton.clickListener(gameState)) {
std::unique_ptr<MenuActivity> menu = std::make_unique<MenuActivity>(gameState);
Expand All @@ -112,30 +139,34 @@ Enemy FightActivity::initEnemy() {

switch (randomNum) {
case 0:
// Zucchini
randomEnemy.name = enemyNames[randomNum];
randomEnemy.attackStrength = (std::rand() % 5) + 8;
randomEnemy.health = (std::rand() % 30) + 120;
randomEnemy.health = (std::rand() % 30) + 50;
randomEnemy.defense = {std::rand() % 150, (std::rand() % 50) + 120, std::rand() % 100};
randomEnemy.picPath = "zucchini_demon_quer.png";
randomEnemy.colorPicPath = "colorPIC_" + std::to_string(randomNum) + ".png";
break;
case 1:
// Assel
randomEnemy.name = enemyNames[randomNum];
randomEnemy.attackStrength = (std::rand() % 3) + 3;
randomEnemy.health = (std::rand() % 20) + 160;
randomEnemy.defense = {std::rand() % 255, std::rand() % 255, std::rand() % 255};
randomEnemy.health = (std::rand() % 20) + 110;
randomEnemy.defense = {(std::rand() % 255), std::rand() % 255, std::rand() % 255};
randomEnemy.picPath = "assel_quer.png";
randomEnemy.colorPicPath = "colorPIC_" + std::to_string(randomNum) + ".png";
break;
case 2:
// Hamster
randomEnemy.name = enemyNames[randomNum];
randomEnemy.attackStrength = (std::rand() % 2) + 1;
randomEnemy.health = (std::rand() % 15) + 40;
randomEnemy.defense = {std::rand() % 100, std::rand() % 100, std::rand() % 100};
randomEnemy.health = (std::rand() % 15) + 20;
randomEnemy.defense = {(std::rand() % 100) + 100, (std::rand() % 50) + 40, (std::rand() % 100) + 100};
randomEnemy.picPath = "hamster_quer.png";
randomEnemy.colorPicPath = "colorPIC_" + std::to_string(randomNum) + ".png";
break;
case 3:
// Mantis Warrior
randomEnemy.name = enemyNames[randomNum];
randomEnemy.attackStrength = (std::rand() % 6) + 11;
randomEnemy.health = (std::rand() % 15) + 90;
Expand All @@ -144,10 +175,11 @@ Enemy FightActivity::initEnemy() {
randomEnemy.colorPicPath = "colorPIC_" + std::to_string(randomNum) + ".png";
break;
case 4:
// Flesh-Fungus
randomEnemy.name = enemyNames[randomNum];
randomEnemy.attackStrength = (std::rand() % 10) + 7;
randomEnemy.health = (std::rand() % 2) + 3;
randomEnemy.defense = {std::rand() % 50, std::rand() % 50, std::rand() % 50};
randomEnemy.health = (std::rand() % 2) + 20;
randomEnemy.defense = {std::rand() % 80, std::rand() % 250, std::rand() % 150};
randomEnemy.picPath = "hamster_fungus_quer.png";
randomEnemy.colorPicPath = "colorPIC_" + std::to_string(randomNum) + ".png";
break;
Expand Down
2 changes: 1 addition & 1 deletion src/main/Actors/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Enemy::Enemy() {
this->defense = {0, 0, 0};
this->picPath = "default_actor_quer.png";
this->colorPicPath = "color_test.png";
this->colorPicBorderPath = "borderMetal.png";
this->colorPicBorderPath = "color_picker_border.png";
}

Enemy::Enemy(std::string _name, int _health, int _attackStrength, RGB _defense, std::string _picPath, std::string _colorPicPath, std::string _colorPicBorderPath) {
Expand Down
Loading

0 comments on commit ff68ab2

Please sign in to comment.