Skip to content

Commit

Permalink
Revert "Finished score"
Browse files Browse the repository at this point in the history
This reverts commit f9f3031.
  • Loading branch information
LxtteDev committed Feb 25, 2023
1 parent f9f3031 commit 3c40f54
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 55 deletions.
7 changes: 3 additions & 4 deletions src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Game::Game(sf::Vector2f size):
mBackground(Background(mWindow)),
mPlayer(Player(mWindow)),
mBall(Ball(mWindow)),
mEnemy(Enemy(mWindow, mBall)),
mScore(Score(mWindow, mBall))
mScore(Score(mWindow)),
mEnemy(Enemy(mWindow, mBall))
{
std::cout << "Creating game" << std::endl;

Expand Down Expand Up @@ -36,10 +36,9 @@ 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);
Expand Down
59 changes: 18 additions & 41 deletions src/score/score.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,31 @@
#include "score.h"

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::Score(sf::RenderWindow &window): mWindow(window), playerText(sf::Text()), enemyText(sf::Text()) {
sf::Font arial;
arial.loadFromFile("content/arial.ttf");

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::Resize();

Score::mWindow.draw(Score::playerText);
}

void Score::updateScore(int player, int enemy) {
Score::playerScore += player;
Score::enemyScore += enemy;
}

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);

Score::playerText.setString("00");
Score::enemyText.setString("00");
}

void Score::draw(float deltaTime) {
// 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);
// Score::mWindow.draw(Score::enemyText);
}
14 changes: 4 additions & 10 deletions src/score/score.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include "../ball/ball.h"

#ifndef SCORE_H_
#define SCORE_H_

class Score {
public:
Score(sf::RenderWindow& window, Ball& ball);
Score(sf::RenderWindow& window);

void updateScore(int player, int enemey);
void Resize();
void draw(float deltaTime);
private:
Ball& mBall;
sf::RenderWindow& mWindow;
sf::Font font;

sf::Vector2f playerPosition;
sf::Vector2f enemyPosition;

bool canScore = 1;
sf::Text& playerText;
int playerScore = 0;

sf::Text& enemyText;
int enemyScore = 0;
};

Expand Down

0 comments on commit 3c40f54

Please sign in to comment.