diff --git a/CMakeLists.txt b/CMakeLists.txt index 289f72d..f70232e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,4 +27,4 @@ target_link_libraries(Pong sfml-graphics) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) -include(CPack) +include(CPack) \ No newline at end of file diff --git a/src/game/game.cpp b/src/game/game.cpp index fced75a..5490a21 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5,8 +5,8 @@ Game::Game(sf::Vector2f size): mBackground(Background(mWindow)), mPlayer(Player(mWindow)), mBall(Ball(mWindow)), - mScore(Score(mWindow)), - mEnemy(Enemy(mWindow, mBall)) + mEnemy(Enemy(mWindow, mBall)), + mScore(Score(mWindow, mBall)) { std::cout << "Creating game" << std::endl; @@ -36,9 +36,10 @@ void Game::display() { Game::mPlayer.Resize(); Game::mBall.Resize(); Game::mEnemy.Resize(); + Game::mScore.Resize(); } - // this->mWindow.clear(); + this->mWindow.clear(); Game::mBackground.Draw(this->deltaTime); Game::mPlayer.Draw(this->deltaTime); diff --git a/src/main.cpp b/src/main.cpp index 14ada6c..94b49ec 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup") #include #include "game/game.h" diff --git a/src/score/score.cpp b/src/score/score.cpp index 3390617..8fac72d 100644 --- a/src/score/score.cpp +++ b/src/score/score.cpp @@ -1,31 +1,54 @@ #include "score.h" -Score::Score(sf::RenderWindow &window): mWindow(window), playerText(sf::Text()), enemyText(sf::Text()) { - sf::Font arial; - arial.loadFromFile("content/arial.ttf"); +Score::Score(sf::RenderWindow &window, Ball& ball): mWindow(window), mBall(ball) { + Score::font.loadFromFile("content/font.ttf"); + const_cast(Score::font.getTexture(128)).setSmooth(false); - Score::playerText.setFont(arial); - Score::enemyText.setFont(arial); - - Score::playerText.setCharacterSize(60); - Score::enemyText.setCharacterSize(60); - - Score::playerText.setPosition(sf::Vector2f(0.0f, 0.0f)); - Score::enemyText.setPosition(sf::Vector2f(0.0f, 0.0f)); - Score::updateScore(0, 0); - - Score::mWindow.draw(Score::playerText); + Score::Resize(); } void Score::updateScore(int player, int enemy) { Score::playerScore += player; Score::enemyScore += enemy; - - Score::playerText.setString("00"); - Score::enemyText.setString("00"); +} + +void Score::Resize() { + sf::Vector2f windowSize(this->mWindow.getView().getSize()); + + Score::playerPosition = sf::Vector2f(windowSize.x / 2 - 10.0f, 21.0f); + Score::enemyPosition = sf::Vector2f(windowSize.x / 2 + 10.0f, 21.0f); } void Score::draw(float deltaTime) { - // Score::mWindow.draw(Score::enemyText); + // Update scores + sf::Vector2f windowSize(this->mWindow.getView().getSize()); + sf::Vector2f ballPosition = Score::mBall.getPosition(); + sf::Vector2f ballVelocity = Score::mBall.getVelocity(); + if (canScore) { + if (ballPosition.x <= 6 && ballVelocity.x < 0) { + Score::updateScore(1, 0); + canScore = 0; + } else if (ballPosition.x >= windowSize.x - 6 && ballVelocity.x > 0) { + Score::updateScore(0, 1); + canScore = 0; + } + } else { + float centre = windowSize.x / 2; + if (ballPosition.x >= centre - 15.0f && ballPosition.x <= centre + 15.05) + canScore = 1; + } + + // Draw text + std::string playerScore = std::to_string(this->playerScore); + std::string enemyScore = std::to_string(this->enemyScore); + + sf::Text player(enemyScore, Score::font, 64); + player.setOrigin(sf::Vector2f(player.getLocalBounds().width, 0)); + player.setPosition(Score::playerPosition); + Score::mWindow.draw(player); + + sf::Text enemy(playerScore, Score::font, 64); + enemy.setPosition(Score::enemyPosition); + Score::mWindow.draw(enemy); } \ No newline at end of file diff --git a/src/score/score.h b/src/score/score.h index d10c3e8..73c4d6f 100644 --- a/src/score/score.h +++ b/src/score/score.h @@ -1,22 +1,28 @@ #include #include +#include +#include "../ball/ball.h" #ifndef SCORE_H_ #define SCORE_H_ class Score { public: - Score(sf::RenderWindow& window); + Score(sf::RenderWindow& window, Ball& ball); void updateScore(int player, int enemey); + void Resize(); void draw(float deltaTime); private: + Ball& mBall; sf::RenderWindow& mWindow; + sf::Font font; - sf::Text& playerText; - int playerScore = 0; + sf::Vector2f playerPosition; + sf::Vector2f enemyPosition; - sf::Text& enemyText; + bool canScore = 1; + int playerScore = 0; int enemyScore = 0; };