-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
57 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup") | ||
#include <iostream> | ||
#include "game/game.h" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<sf::Texture&>(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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters