<commit_before>#include "scene.h"
#include "game.h"
#include "text_entity.h"
#include "welcome_state.h"

using namespace space_invaders;

inline std::string toScoreString(int score) {
  auto result = std::to_string(score);
  auto difference = (4 - result.size());
  if (difference >= 0) {
    result = "0000" + result;
  }
  return result.substr(result.length() - 4, 4);
}

Scene::Scene(Game& game) : mGame(game),
  mState(std::make_shared<WelcomeState>(game)),
  mScore1Caption(std::make_shared<TextEntity>(game)),
  mHiScoreCaption(std::make_shared<TextEntity>(game)),
  mScore2Caption(std::make_shared<TextEntity>(game)),
  mScore1Text(std::make_shared<TextEntity>(game)),
  mHiScoreText(std::make_shared<TextEntity>(game)),
  mScore2Text(std::make_shared<TextEntity>(game))
{
  // initialize the static caption for the 1st player score.
  mScore1Caption->setText("SCORE<1>");
  mScore1Caption->setX(125 - (mScore1Caption->getWidth() / 2));
  mScore1Caption->setY(40);

  // initialize the static caption for the high score.
  mHiScoreCaption->setText("HI-SCORE");
  mHiScoreCaption->setX(672 / 2 - (mHiScoreCaption->getWidth() / 2));
  mHiScoreCaption->setY(mScore1Caption->getY());

  // initialize the static caption for the 2nd player score.
  mScore2Caption->setText("SCORE<2>");
  mScore2Caption->setX(672 - 130 - (mScore2Caption->getWidth() / 2));
  mScore2Caption->setY(mScore1Caption->getY());

  // initialize the dynamic score value for the 1st player.
  mScore1Text->setText("0000");
  mScore1Text->setX(125 - (mScore1Text->getWidth() / 2));
  mScore1Text->setY(mScore1Caption->getY() + 35);

  // initialize the dynamic score value for the high score.
  mHiScoreText->setText("0000");
  mHiScoreText->setX(672 / 2 - (mHiScoreText->getWidth() / 2));
  mHiScoreText->setY(mScore1Text->getY());

  // initialize the dynamic score value for the 2nd player.
  mScore2Text->setText("0000");
  mScore2Text->setX(672 - 130 - (mScore2Text->getWidth() / 2));
  mScore2Text->setY(mScore1Text->getY());
}

void Scene::update(unsigned long dt)
{
  // TODO ensure player 1 score validity.
  // TODO ensure player 2 score validity.

  mScore1Caption->update(dt);
  mHiScoreCaption->update(dt);
  mScore2Caption->update(dt);

  mScore1Text->update(dt);
  mHiScoreText->update(dt);<commit_msg>Scene::update will now track the player scores and ensure that are synchronized with the player score indicators rendered on the screen.<commit_after>#include "scene.h"
#include "game.h"
#include "text_entity.h"
#include "welcome_state.h"

using namespace space_invaders;

inline std::string toScoreString(int score) {
  auto result = std::to_string(score);
  auto difference = (4 - result.size());
  if (difference >= 0) {
    result = "0000" + result;
  }
  return result.substr(result.length() - 4, 4);
}

Scene::Scene(Game& game) : mGame(game),
  mState(std::make_shared<WelcomeState>(game)),
  mScore1Caption(std::make_shared<TextEntity>(game)),
  mHiScoreCaption(std::make_shared<TextEntity>(game)),
  mScore2Caption(std::make_shared<TextEntity>(game)),
  mScore1Text(std::make_shared<TextEntity>(game)),
  mHiScoreText(std::make_shared<TextEntity>(game)),
  mScore2Text(std::make_shared<TextEntity>(game))
{
  // initialize the static caption for the 1st player score.
  mScore1Caption->setText("SCORE<1>");
  mScore1Caption->setX(125 - (mScore1Caption->getWidth() / 2));
  mScore1Caption->setY(40);

  // initialize the static caption for the high score.
  mHiScoreCaption->setText("HI-SCORE");
  mHiScoreCaption->setX(672 / 2 - (mHiScoreCaption->getWidth() / 2));
  mHiScoreCaption->setY(mScore1Caption->getY());

  // initialize the static caption for the 2nd player score.
  mScore2Caption->setText("SCORE<2>");
  mScore2Caption->setX(672 - 130 - (mScore2Caption->getWidth() / 2));
  mScore2Caption->setY(mScore1Caption->getY());

  // initialize the dynamic score value for the 1st player.
  mScore1Text->setText("0000");
  mScore1Text->setX(125 - (mScore1Text->getWidth() / 2));
  mScore1Text->setY(mScore1Caption->getY() + 35);

  // initialize the dynamic score value for the high score.
  mHiScoreText->setText("0000");
  mHiScoreText->setX(672 / 2 - (mHiScoreText->getWidth() / 2));
  mHiScoreText->setY(mScore1Text->getY());

  // initialize the dynamic score value for the 2nd player.
  mScore2Text->setText("0000");
  mScore2Text->setX(672 - 130 - (mScore2Text->getWidth() / 2));
  mScore2Text->setY(mScore1Text->getY());
}

void Scene::update(unsigned long dt)
{
  // get references to both player context containers.
  const auto& ctx1 = mGame.getPlayerContext1();
  const auto& ctx2 = mGame.getPlayerContext2();

  // ensure that player scores are up-to-date.
  mScore1Text->setText(toScoreString(ctx1.getScore()));
  mScore2Text->setText(toScoreString(ctx2.getScore()));

  mScore1Caption->update(dt);
  mHiScoreCaption->update(dt);
  mScore2Caption->update(dt);

  mScore1Text->update(dt);
  mHiScoreText->update(dt);
{'fraction_non_alphanumeric': 0.15626266720713416, 'fraction_numerical': 0.038508309687880014, 'mean_word_length': 5.153366583541147, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5902134', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1697, 'n_words': 332}