#include "Game.h"
Game::Game(sf::Font const& font, std::int8_t difficulty) : window(), event(), playing(false), newRecord(false), clock(), saveHandler(), score(),
difficultyMode(difficulty), targetSide(50), errorsCount(0), target(0, 0, 50, 50),
t_background(), t_colors(), background(), tip(16),
newScoreMessage(Lang::L_newScore, font, 60), pressSPACEtoContinue(Lang::L_pressSPACE, font),
elapsedTimeText(Lang::L_time, font), elapsedTime("0", font), errorsText(Lang::L_errors, font), errors("0", font),
recordDisplay("Record : 0s 0e", font), difficultyDisplay(Lang::L_easy, font, 14)
{
switch(difficultyMode)
{
case 0:
break;
case 1:
target.height = 30;
target.width = 30;
targetSide = 30;
difficultyDisplay.setString("Normal");
break;
case 2:
target.height = 10;
target.width = 10;
targetSide = 10;
difficultyDisplay.setString(Lang::L_hard);
break;
default:
target.height = 1;
target.width = 1;
targetSide = 0;
difficultyDisplay.setString("Impossible");
}
score = saveHandler.getSave(difficulty);
t_background.loadFromFile("background.png");
t_colors.loadFromFile("colors.png");
background.setTexture(t_background, true);
tip.setPosition(756, 3);
tip.setTexture(&t_colors, true);
difficultyDisplay.setPosition(772 - difficultyDisplay.getLocalBounds().width / 2, 32);
elapsedTimeText.setPosition(10, 5);
elapsedTime.setPosition(10 + elapsedTimeText.getLocalBounds().width, 7);
errorsText.setPosition(250, 5);
errors.setPosition(250 + errorsText.getLocalBounds().width, 7);
recordDisplay.setPosition(500, 5);
newScoreMessage.setPosition((800 - newScoreMessage.getLocalBounds().width) / 2, 250);
pressSPACEtoContinue.setPosition((800 - pressSPACEtoContinue.getLocalBounds().width) / 2, 320);
}
//////////////////////////////////////////////////////////////////////////////
Game::~Game()
{
}
//////////////////////////////////////////////////////////////////////////////
void Game::launch()
{
window.create(sf::VideoMode(800, 600), "So close ! v1.0", sf::Style::Titlebar | sf::Style::Close);
window.setFramerateLimit(60);
setNewPosition();
while(window.isOpen())
{
if(playing)
{
calcTargetDistance();
float time(clock.getElapsedTime().asSeconds());
std::string timeStr(std::to_string(clock.getElapsedTime().asSeconds()));
if(time < 10)
timeStr.resize(3);
else
timeStr.resize(4);
elapsedTime.setString(timeStr);
errors.setString(std::to_string(errorsCount));
}
window.clear();
window.draw(background);
window.draw(tip);
window.draw(elapsedTimeText);
window.draw(elapsedTime);
window.draw(errorsText);
window.draw(errors);
window.draw(recordDisplay);
window.draw(difficultyDisplay);
if(newRecord)
window.draw(newScoreMessage);
if(!playing)
window.draw(pressSPACEtoContinue);
window.display();
while(window.pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseMoved:
if(playing)
calcTargetDistance();
break;
case sf::Event::MouseButtonReleased:
if(playing && event.mouseButton.button == sf::Mouse::Left)
{
if(checkTargetFound())
{
playing = false;
// the player found it ! you must do something.
}
else
errorsCount++;
}
break;
case sf::Event::KeyPressed:
if(event.key.code == sf::Keyboard::Space)
{
if(!playing)
{
playing = true;
errorsCount = 0;
clock.restart();
}
else
{
playing = false;
tip.setTextureRect(sf::IntRect(0, 0, 255, 1));
setNewPosition();
}
}
break;
default:
break;
}
}
}
}
//////////////////////////////////////////////////////////////////////////////
void Game::setNewPosition()
{
int x(797), y(546);
switch(difficultyMode)
{
case 0:
x-=20;
y-=20;
case 1:
x-=20;
x-=20;
case 2:
x-=10;
y-=10;
break;
default:
break;
}
target.top = rand() % x;
target.left = ( rand() % y ) + 53;
}
//////////////////////////////////////////////////////////////////////////////
void Game::calcTargetDistance()
{
short distance(0);
sf::Vector2i mouse(sf::Mouse::getPosition(window));
sf::Vector2i targetPos(target.height/2 + target.top, target.width / 2 + target.left);
sf::Vector2i diff(mouse - targetPos);
diff.x *= diff.x;
diff.y *= diff.y;
distance = sqrt(diff.x + diff.y);
if(distance < targetSide/2)
distance = 0;
else
distance -= targetSide/2;
if(distance > 255)
distance = 255;
tip.setTextureRect(sf::IntRect(distance, 0, 1, 1));
}
//////////////////////////////////////////////////////////////////////////////
bool Game::checkTargetFound()
{
sf::Vector2i mousePos(sf::Mouse::getPosition(window).y, sf::Mouse::getPosition(window).x); // on inverse x et y pour cause de bug de sf::Rect...
return target.contains(mousePos);
}