|
| 1 | +#include "leaderboardscene.hpp" |
| 2 | +#include "constants.hpp" |
| 3 | +#include "operators.hpp" |
| 4 | + |
| 5 | +LeaderboardScene::LeaderboardScene() : Scene(SCENE_WIDTH, SCENE_HEIGHT) { |
| 6 | + state = LeaderboardState::INSERT_NAME; |
| 7 | + playerName = ""; |
| 8 | + playerScore = 0; |
| 9 | + font = TextureManager::getFont(); |
| 10 | +} |
| 11 | + |
| 12 | +void LeaderboardScene::activate() { |
| 13 | + return; |
| 14 | +} |
| 15 | + |
| 16 | +void LeaderboardScene::deactivate() { |
| 17 | + return; |
| 18 | +} |
| 19 | + |
| 20 | +SceneID LeaderboardScene::getID() const { |
| 21 | + return SceneID::LEADERBOARD; |
| 22 | +} |
| 23 | + |
| 24 | +SceneID LeaderboardScene::getNextSceneID() const { |
| 25 | + return SceneID::LEADERBOARD; |
| 26 | +} |
| 27 | + |
| 28 | +void LeaderboardScene::processText(const sf::Event &event) { |
| 29 | + // TODO: Write your solution here |
| 30 | +} |
| 31 | + |
| 32 | +void LeaderboardScene::processBackspace() { |
| 33 | + // TODO: Write your solution here |
| 34 | +} |
| 35 | + |
| 36 | +void LeaderboardScene::processEnter() { |
| 37 | + // TODO: Write your solution here |
| 38 | +} |
| 39 | + |
| 40 | +void LeaderboardScene::processEvent(const sf::Event &event) { |
| 41 | + if (state == LeaderboardState::INSERT_NAME) { |
| 42 | + if (event.type == sf::Event::TextEntered) { |
| 43 | + processText(event); |
| 44 | + } else if (event.type == sf::Event::KeyPressed) { |
| 45 | + if (event.key.code == sf::Keyboard::BackSpace) { |
| 46 | + processBackspace(); |
| 47 | + } else if (event.key.code == sf::Keyboard::Enter) { |
| 48 | + processEnter(); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +void LeaderboardScene::update(sf::Time delta) { |
| 55 | + return; |
| 56 | +} |
| 57 | + |
| 58 | +void LeaderboardScene::draw(sf::RenderWindow &window, TextureManager &textureManager) { |
| 59 | + window.clear(sf::Color::Black); |
| 60 | + if (state == LeaderboardState::INSERT_NAME) { |
| 61 | + drawInsertNameScreen(window); |
| 62 | + } else if (state == LeaderboardState::SHOW_LEADERBOARD) { |
| 63 | + drawLeaderboard(window); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +void LeaderboardScene::drawInsertNameScreen(sf::RenderWindow &window) { |
| 68 | + sf::RectangleShape inputBox(sf::Vector2f(INPUTBOX_WIDTH, INPUTBOX_HEIGHT)); |
| 69 | + inputBox.setSize(sf::Vector2f(INPUTBOX_WIDTH, INPUTBOX_HEIGHT)); |
| 70 | + inputBox.setFillColor(sf::Color::Black); |
| 71 | + inputBox.setOutlineColor(sf::Color::White); |
| 72 | + inputBox.setOutlineThickness(2); |
| 73 | + inputBox.setPosition(SCENE_WIDTH / 2 - inputBox.getSize().x / 2, SCENE_HEIGHT / 2 - inputBox.getSize().y / 2); |
| 74 | + |
| 75 | + sf::Text prompt; |
| 76 | + prompt.setFont(font); |
| 77 | + prompt.setString("Insert Name"); |
| 78 | + prompt.setCharacterSize(INPUTNAME_FONT_SIZE); |
| 79 | + prompt.setFillColor(sf::Color::White); |
| 80 | + prompt.setPosition(SCENE_WIDTH / 2 - prompt.getGlobalBounds().width / 2, SCENE_HEIGHT / 2 - inputBox.getSize().y / 2 - 100); |
| 81 | + |
| 82 | + sf::Text nameText; |
| 83 | + nameText.setFont(font); |
| 84 | + nameText.setString(playerName); |
| 85 | + nameText.setCharacterSize(INPUTNAME_FONT_SIZE); |
| 86 | + nameText.setFillColor(sf::Color::White); |
| 87 | + |
| 88 | + sf::FloatRect textBounds = nameText.getLocalBounds(); |
| 89 | + float textHeight = textBounds.height; |
| 90 | + float textYOffset = textBounds.top; |
| 91 | + float inputBoxCenterY = inputBox.getPosition().y + inputBox.getSize().y / 2; |
| 92 | + nameText.setPosition(SCENE_WIDTH / 2 - textBounds.width / 2, inputBoxCenterY - textHeight / 2 - textYOffset); |
| 93 | + |
| 94 | + window.draw(inputBox); |
| 95 | + window.draw(prompt); |
| 96 | + window.draw(nameText); |
| 97 | +} |
| 98 | + |
| 99 | +void LeaderboardScene::drawLeaderboard(sf::RenderWindow &window) { |
| 100 | + sf::Text header = createText("LEADERBOARD", font, HEADER_FONT_SIZE, HEADER_X, HEADER_Y); |
| 101 | + window.draw(header); |
| 102 | + |
| 103 | + sf::Text posHeader = createText("POS", font, LEADERBOARD_FONT_SIZE, POS_HEADER_X, POS_HEADER_Y); |
| 104 | + window.draw(posHeader); |
| 105 | + |
| 106 | + sf::Text nameHeader = createText("NAME", font, LEADERBOARD_FONT_SIZE, NAME_HEADER_X, NAME_HEADER_Y); |
| 107 | + window.draw(nameHeader); |
| 108 | + |
| 109 | + sf::Text scoreHeader = createText("SCORE", font, LEADERBOARD_FONT_SIZE, SCORE_HEADER_X, SCORE_HEADER_Y); |
| 110 | + window.draw(scoreHeader); |
| 111 | + |
| 112 | + unsigned int NUMBER_OF_ROWS = leaderboard.getScores().size(); |
| 113 | + float LEADERBOARD_HEIGHT = ROW_HEIGHT * (NUMBER_OF_ROWS + 1); // One additional row for the header |
| 114 | + |
| 115 | + // Draw horizontal lines |
| 116 | + sf::RectangleShape line(sf::Vector2f(LEADERBOARD_WIDTH, 2)); |
| 117 | + line.setFillColor(sf::Color::White); |
| 118 | + for (unsigned int i = 0; i <= NUMBER_OF_ROWS + 1; ++i) { |
| 119 | + line.setPosition(LEADERBOARD_X, LEADERBOARD_Y + i * ROW_HEIGHT); |
| 120 | + window.draw(line); |
| 121 | + } |
| 122 | + |
| 123 | + // Draw vertical lines |
| 124 | + sf::RectangleShape line1(sf::Vector2f(2, LEADERBOARD_HEIGHT)); |
| 125 | + line1.setPosition(LEADERBOARD_X, LEADERBOARD_Y); |
| 126 | + window.draw(line1); |
| 127 | + sf::RectangleShape line2(sf::Vector2f(2, LEADERBOARD_HEIGHT)); |
| 128 | + line2.setPosition(LEADERBOARD_X + 50, LEADERBOARD_Y); |
| 129 | + window.draw(line2); |
| 130 | + sf::RectangleShape line3(sf::Vector2f(2, LEADERBOARD_HEIGHT)); |
| 131 | + line3.setPosition(LEADERBOARD_X + 450, LEADERBOARD_Y); |
| 132 | + window.draw(line3); |
| 133 | + sf::RectangleShape line4(sf::Vector2f(2, LEADERBOARD_HEIGHT)); |
| 134 | + line4.setPosition(LEADERBOARD_X + 600, LEADERBOARD_Y); |
| 135 | + window.draw(line4); |
| 136 | + |
| 137 | + // Draw contents of the leaderboard |
| 138 | + auto scores = leaderboard.getScores(); |
| 139 | + for (std::size_t i = 0; i < scores.size(); ++i) { |
| 140 | + auto it = std::next(scores.begin(), i); |
| 141 | + |
| 142 | + sf::Text pos; |
| 143 | + if (i != 9) { |
| 144 | + pos = createText(std::to_string(i + 1), font, LEADERBOARD_FONT_SIZE, LEADERBOARD_X + TWO_DIGIT_POS_OFFSET, LEADERBOARD_Y + ROW_HEIGHT * (i + 1) + BORDER_OFFSET); |
| 145 | + } else { |
| 146 | + pos = createText(std::to_string(i + 1), font, LEADERBOARD_FONT_SIZE, LEADERBOARD_X + ONE_DIGIT_POS_OFFSET, LEADERBOARD_Y + ROW_HEIGHT * (i + 1) + BORDER_OFFSET); |
| 147 | + } |
| 148 | + window.draw(pos); |
| 149 | + |
| 150 | + sf::Text name = createText(it->second, font, LEADERBOARD_FONT_SIZE, LEADERBOARD_X + NAME_X_OFFSET, LEADERBOARD_Y + ROW_HEIGHT * (i + 1) + BORDER_OFFSET); |
| 151 | + window.draw(name); |
| 152 | + |
| 153 | + sf::Text score; |
| 154 | + if (std::to_string(it->first).length() == 1) { |
| 155 | + score = createText(std::to_string(it->first), font, LEADERBOARD_FONT_SIZE, LEADERBOARD_X + ONE_DIGIT_SCORE_OFFSET, LEADERBOARD_Y + ROW_HEIGHT * (i + 1) + BORDER_OFFSET); |
| 156 | + } else if (std::to_string(it->first).length() == 4) { |
| 157 | + score = createText(std::to_string(it->first), font, LEADERBOARD_FONT_SIZE, LEADERBOARD_X + FOUR_DIGIT_SCORE_OFFSET, LEADERBOARD_Y + ROW_HEIGHT * (i + 1) + BORDER_OFFSET); |
| 158 | + } else { |
| 159 | + score = createText(std::to_string(it->first), font, LEADERBOARD_FONT_SIZE, LEADERBOARD_X + FIVE_DIGIT_SCORE_OFFSET, LEADERBOARD_Y + ROW_HEIGHT * (i + 1) + BORDER_OFFSET); |
| 160 | + } |
| 161 | + window.draw(score); |
| 162 | + } |
| 163 | +} |
0 commit comments