Advertisement
venik2405

course.dashboard

Dec 20th, 2021
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.16 KB | None | 0 0
  1. #include "dashboard.h"
  2.  
  3. void Dashboard::initVariables()
  4. {
  5.     this->gear = 1;
  6.     this->gearCount = 5;
  7.     this->speed = 0;
  8.     this->rpm = 800;
  9.     this->maxNitro = 5;
  10. }
  11.  
  12. void Dashboard::initFont()
  13. {
  14.     if (!this->font.loadFromFile("Fonts/PixellettersFull.ttf"))
  15.         std::cout << "ERROR IN FONT INITIALIZATION";
  16. }
  17.  
  18. void Dashboard::initText()
  19. {
  20.     this->speedText.setFont(this->font);
  21.     this->speedText.setFillColor(sf::Color::White);
  22.     this->speedText.setCharacterSize(70);
  23.     this->speedText.setString("TEST");
  24.     //
  25.     this->coinsText.setFont(this->font);
  26.     this->coinsText.setFillColor(sf::Color::Yellow);
  27.     this->coinsText.setCharacterSize(110);
  28.     this->coinsText.setString("Coins");
  29. }
  30.  
  31. void Dashboard::initTextures()
  32. {
  33.     if (!this->speedo_texture.loadFromFile("Textures/speed_4.png"))
  34.         std::cout << "Failed to load speedo texture";
  35.     this->speedo.setTexture(this->speedo_texture);
  36.     this->speedo.setScale(1.2f, 1.2f);
  37.     if (!this->row_texture.loadFromFile("Textures/row_2.png"))
  38.         std::cout << "Failed to load row texture";
  39.     this->rpm_row.setTexture(this->row_texture);
  40.     this->rpm_row.setScale(1.5f, 1.5f);
  41.     this->rpm_row.setOrigin(80.f, 15.f);
  42.     this->speed_row.setTexture(this->row_texture);
  43.     this->speed_row.setScale(1.5f, 1.5f);
  44.     this->speed_row.setOrigin(80.f, 15.f);
  45.     if (!this->nitro_texture.loadFromFile("Textures/nitro_2.png"))
  46.         std::cout << "Failed to load nitro texture";
  47.     this->nitro_spr.setTexture(this->nitro_texture);
  48.     this->nitro_spr.setScale(1.2f, 1.2f);
  49.     this->nitro_spr.setTextureRect(IntRect(0, 0, 248, 65));
  50.  
  51.     if (!this->brick_texture.loadFromFile("Textures/gear.png"))
  52.         std::cout << "Failed to load gear texture";
  53.     this->brick.setTexture(this->brick_texture);
  54.     this->brick.setScale(1.2f, 1.2f);
  55.  
  56.     if (!this->butt_texture.loadFromFile("Textures/butt.png"))
  57.         std::cout << "Failed to load button texture";
  58.     this->butt.setTexture(this->butt_texture);
  59.     this->butt.setScale(1.1f, 1.1f);
  60.  
  61.     if (!this->black_rect_text.loadFromFile("Textures/hp.png"))
  62.         std::cout << "Failed to load hp_closer texture";
  63.     this->black_rect.setTexture(this->black_rect_text);
  64.     this->black_rect.setRotation(180.f);
  65. }
  66.  
  67. void Dashboard::initPositions()
  68. {
  69.     this->speedo.setPosition(-218.f, 730.f);
  70.     this->rpm_row.setPosition(750.f, 900.f);
  71.     this->rpm_row.setRotation(235);
  72.     this->speed_row.setPosition(1165.f, 900.f);
  73.     this->speed_row.setRotation(220);
  74.     this->speedText.setPosition(895, 988);
  75.     this->coinsText.setPosition(1700, 950);
  76.     this->brick.setPosition(417.f, 989.f);
  77.     this->butt.setPosition(917.f, 810.f);
  78.     this->nitro_spr.setPosition(75.f, 992.f);
  79.     this->black_rect.setPosition(1580.f, 1080.f);
  80. }
  81.  
  82. Dashboard::Dashboard()
  83. {
  84.     this->initTextures();
  85.     this->initVariables();
  86.     this->initFont();
  87.     this->initText();
  88.     this->initPositions();
  89. }
  90.  
  91. void Dashboard::updateGui()
  92. {
  93.     std::stringstream ssSpeed;
  94.     ssSpeed << this->speed;
  95.     this->speedText.setString(ssSpeed.str());
  96.     std::stringstream ssCoins;
  97.     ssCoins << "X" << this->coins;
  98.     this->coinsText.setString(ssCoins.str());
  99. }
  100.  
  101. void Dashboard::updateGear()
  102. {
  103.     this->brick.setPosition(417.f + (gear - 1) * 9, 989.f + (gear - 1) * 17);
  104. }
  105.  
  106. void Dashboard::updateHp()
  107. {
  108.     this->black_rect.setScale(- ( hp - 3) * 16.f, 1.4f);
  109. }
  110.  
  111. void Dashboard::updateNitro()
  112. {
  113.     this->nitro_spr.setTextureRect(IntRect(0.f, 0.f, 248.f * (nitro / 100), 65.f));
  114. }
  115.  
  116. void Dashboard::update(int speed, int rpm, int gear, int coins, float nitro, int hp)
  117. {
  118.     this->updateGear();
  119.     this->rpm_row.setRotation(260 + 180.f * (rpm / 9000.f));
  120.     this->speed_row.setRotation(220 + 180.f * (speed / 205.f));
  121.     this->speed = speed;
  122.     this->rpm = rpm;
  123.     this->gear = gear;
  124.     this->coins = coins;
  125.     this->nitro = nitro;
  126.     this->hp = hp;
  127.     this->updateNitro();
  128.     this->updateGui();
  129.     this->updateHp();
  130. }
  131.  
  132. const bool Dashboard::renderButt()
  133. {
  134.     if (this->rpm > 8000)
  135.     {
  136.         return true;
  137.     }
  138.     return false;
  139. }
  140.  
  141.  
  142. void Dashboard::render(sf::RenderTarget* target)
  143. {
  144.     target->draw(this->speedo);
  145.     target->draw(this->rpm_row);
  146.     target->draw(this->speed_row);
  147.     target->draw(this->speedText);
  148.     target->draw(this->coinsText);
  149.     target->draw(this->brick);
  150.     target->draw(this->black_rect);
  151.     if (renderButt()) target->draw(this->butt);
  152.     target->draw(this->nitro_spr);
  153. }
  154.  
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement