Skip to content

Commit

Permalink
started adding changing color picke after pick
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed Apr 18, 2024
1 parent 73ee511 commit e20a3fa
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 20 deletions.
16 changes: 16 additions & 0 deletions src/include/FightStates/PlayersTurn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@
#include "FightStates/FightState.hpp"
#include "Game.hpp"
#include "Color.hpp"
#include "GenerateColorIMG.hpp"

enum PlayerPhase {
PICK_COLOR,
ANIMATE_ATTACK,
CHANGE_COLOR,
END_TURN
};

class PlayersTurn: public FightState {
public:
void run(Game &game, FightEnv &fightEnv) override;

private:
PlayerPhase playerPhase = PlayerPhase::PICK_COLOR;
bool colorPicked = false;
sf::Vector2f clickedPos;

void processAttack(FightEnv &fightEnv, Game &game);
void changeColoPickerImage(Game &game, FightEnv &fightEnv);

// Compute damage multiplier
float mapInInterval(float value);
float calculateSingleMultPart(Color color, FightEnv &fightEnv);
float calculateAttackMult(FightEnv &fightEnv);
// Metrics in file DamageMultMetrics.cpp
float sameColorMetric(Color color, FightEnv &fightEnv);
float counterColorMetric(Color color, FightEnv &fightEnv);
float tugOfWarMetric(Color color, FightEnv &fightEnv);
};
Expand Down
4 changes: 2 additions & 2 deletions src/include/GenerateColorIMG.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ void generateTexture() {
pixels[index+3] = 255;
}
}
int fileNum = std::rand() % 10;
//int fileNum = std::rand() % 10;
sf::Image colorIMG;
colorIMG.create(GEN_IMG_WIDTH, GEN_IMG_HEIGHT, pixels);
colorIMG.saveToFile(RESOURCE_PATH "color_textures/colorPIC_" + std::to_string(fileNum) + ".png");
colorIMG.saveToFile(RESOURCE_PATH "color_textures/colorPIC_new.png");
delete[] pixels;
}

Expand Down
2 changes: 1 addition & 1 deletion src/include/UIElements/UIColorPicker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class UIColorPicker: public UIElement {
sf::Texture borderTX;
sf::Sprite colorSP;
sf::Sprite borderSP;
sf::Image colorIMG;

sf::SoundBuffer releaseSoundBuffer;

Expand All @@ -26,6 +25,7 @@ class UIColorPicker: public UIElement {
UIColorPicker(std::string imagePath, std::string borderPath);
UIColorPicker(sf::Image image, std::string borderPath);

sf::Image colorIMG;
sf::Sound releaseSound;

void draw(sf::RenderWindow* window) override;
Expand Down
105 changes: 88 additions & 17 deletions src/main/FightStates/PlayersTurn.cpp
Original file line number Diff line number Diff line change
@@ -1,37 +1,108 @@
#include "FightStates/PlayersTurn.hpp"

void PlayersTurn::run(Game &game, FightEnv &fightEnv) {
sf::Vector2f clickedPos;
static bool colorPicked = false;
if (fightEnv.enemyOverview.colorPicker.clickListener(game.gameEvents, clickedPos) && !colorPicked) {
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.enemyOverview.changeHealth(damage);
fightEnv.newColorIMGNeeded = true;
switch (this->playerPhase) {
case PlayerPhase::PICK_COLOR:
if (fightEnv.enemyOverview.colorPicker.clickListener(game.gameEvents, 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.enemyOverview.changeHealth(damage);
fightEnv.newColorIMGNeeded = true;
this->playerPhase = PlayerPhase::ANIMATE_ATTACK;
}
break;
case PlayerPhase::ANIMATE_ATTACK:
this->processAttack(fightEnv, game);
break;
case PlayerPhase::CHANGE_COLOR:
break;
case PlayerPhase::END_TURN:
break;
}
}

void PlayersTurn::processAttack(FightEnv &fightEnv, Game &game) {
if (fightEnv.textFadingManager.fadingText.pastMillSec >= fightEnv.textFadingManager.fadingText.millSecToLive) {
fightEnv.textFadingManager.fadingText.pastMillSec = 0;
fightEnv.isPlayersTurn = (fightEnv.isPlayersTurn + 1) % 2;
fightEnv.enemyDamageCalculated = false;
fightEnv.turnSP.setTexture(fightEnv.enemiesTurnTX);
fightEnv.turnChangeBanner.setNewLabel("Enemies Turn");
colorPicked = false;
this->colorPicked = false;
fightEnv.turnIsChanging = true;
generateTexture();
this->playerPhase = PlayerPhase::CHANGE_COLOR;
}
}

void PlayersTurn::changeColoPickerImage(Game &game, FightEnv &fightEnv) {
static bool initialized = false;
static bool newColorImageSet = false;
static sf::Image currentColorImage = fightEnv.enemyOverview.colorPicker.colorIMG;
static sf::Image newColorImage;
if (!initialized) {
newColorImage.loadFromFile(RESOURCE_PATH "color_textures/colorPIC_new.png");
initialized = true;
}
sf::Uint8* pixels = new sf::Uint8[GEN_IMG_WIDTH*GEN_IMG_HEIGHT*4];
for (int y = 0; y < GEN_IMG_HEIGHT; ++y)
{
for (int x = 0; x < GEN_IMG_WIDTH; ++x)
{
const double red = redPerlin.octave2D_01((x * fx), (y * fy), octaves, persistens) * maxColor;
const double green = greenPerlin.octave2D_01((x * fx), (y * fy), octaves, persistens) * maxColor;
const double blue = bluePerlin.octave2D_01((x * fx), (y * fy), octaves, persistens) * maxColor;

int index = (y * GEN_IMG_WIDTH + x) *4;
pixels[index] = red;
pixels[index+1] = green;
pixels[index+2] = blue;
pixels[index+3] = 255;
}
}
if (newColorImageSet) {
newColorImageSet = true;
this->playerPhase = PlayerPhase::END_TURN;
}
}

float PlayersTurn::mapInInterval(float value) {
return (2.f/3) * (value*value) + (1.f/3) * value;
}

// This metric rewards for picking same as defense
float PlayersTurn::sameColorMetric(Color color, FightEnv &fightEnv) {
int pickedColorValue;
int defenseColorValue;
switch (color) {
case RED:
pickedColorValue = fightEnv.pickedColor.r;
defenseColorValue = fightEnv.enemyOverview.creature.defense.red;
break;
case GREEN:
pickedColorValue = fightEnv.pickedColor.g;
defenseColorValue = fightEnv.enemyOverview.creature.defense.green;
break;
case BLUE:
pickedColorValue = fightEnv.pickedColor.b;
defenseColorValue = fightEnv.enemyOverview.creature.defense.blue;
break;
default:
break;
}
int deviationFromOptimal = std::max(pickedColorValue - defenseColorValue, 0);
float effectiveness = 1 - (deviationFromOptimal/ 250.f);
return effectiveness;
}

//This metric rewards when hitting the exact counter colors
float PlayersTurn::counterColorMetric(Color color, FightEnv &fightEnv) {
int pickedColorValue;
Expand Down Expand Up @@ -92,7 +163,7 @@ float PlayersTurn::tugOfWarMetric(Color color, FightEnv &fightEnv) {

float PlayersTurn::calculateSingleMultPart(Color color, FightEnv &fightEnv) {
// Here is decided with which metric to calculated the multiplier portion
float calulatedPortion = this->tugOfWarMetric(color, fightEnv);
float calulatedPortion = this->sameColorMetric(color, fightEnv);
return mapInInterval(calulatedPortion);
}

Expand Down

0 comments on commit e20a3fa

Please sign in to comment.