Advertisement
venik2405

course.menu

Dec 20th, 2021
1,059
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include "Menu.h"
  2.  
  3. void Menu::initTextures()
  4. {
  5.     if (!this->fone_texture.loadFromFile("Textures/fon_def2.png"))
  6.         std::cout << "Failed to load road texture";
  7.     if (!this->back_texture.loadFromFile("Textures/back.png"))
  8.         std::cout << "Failed to load back texture";
  9.     this->fone_spr.setTexture(back_texture);
  10.  
  11.     if (!this->butPlayText.loadFromFile("Textures/Buttons/but_play.png"))
  12.         std::cout << "Failed to load butPlayText texture";
  13.     if (!this->butPlayText_active.loadFromFile("Textures/Buttons/but_play_activate.png"))
  14.         std::cout << "Failed to load butPlayText_active texture";
  15.     this->butPlay_spr.setTexture(butPlayText);
  16.  
  17.     if (!this->butSetText.loadFromFile("Textures/Buttons/but_set.png"))
  18.         std::cout << "Failed to load butSetText texture";
  19.     if (!this->butSetText_active.loadFromFile("Textures/Buttons/but_set_activate.png"))
  20.         std::cout << "Failed to load butSetText_active texture";
  21.     this->butSet_spr.setTexture(butSetText);
  22.  
  23.     if (!this->butExitText.loadFromFile("Textures/Buttons/but_exit.png"))
  24.         std::cout << "Failed to load butExitText texture";
  25.     if (!this->butExitText_active.loadFromFile("Textures/Buttons/but_exit_activate.png"))
  26.         std::cout << "Failed to load butExitText_active texture";
  27.     this->butExit_spr.setTexture(butExitText);
  28.  
  29.     if (!this->set_text.loadFromFile("Textures/set.png"))
  30.         std::cout << "Failed to load set_text texture";
  31.     this->set_spr.setTexture(set_text);
  32.  
  33.     if (!(this->but_bf.loadFromFile("Sounds/but_sound.wav")))
  34.         std::cout << "Failed to load gear sound";
  35.     this->but_s.setBuffer(but_bf);
  36.     this->but_s.setVolume(100);
  37. }
  38.  
  39. //Texture butPlayText;
  40. //Sprite butPlay_spr;
  41. //Sprite butPlay_activate_spr;
  42. //
  43. //Texture butSetText;
  44. //Sprite butSet_spr;
  45. //Sprite butSet_activate_spr;
  46. //
  47. //Texture butExitText;
  48. //Sprite butExit_spr;
  49. //Sprite butExit_activate_spr;
  50.  
  51. void Menu::initVariables()
  52. {
  53.     this->flag = 0;
  54.     this->timer = 200;
  55.     this->timer = 200;
  56.     this->moveTimerMax = 5.f;
  57.     this->moveTimer = moveTimerMax;
  58.     this->choice = 3;
  59.     music.openFromFile("Sounds/menu.wav");
  60.     music.setLoop(true);
  61.     music.setVolume(30);
  62.     this->soundCooldownMax = 10.f;
  63.     this->soundCooldown = soundCooldownMax;
  64. }
  65.  
  66. void Menu::updateSoundCooldown()
  67. {
  68.     soundCooldown += 0.5f;
  69. }
  70.  
  71. void Menu::changeMusic()
  72. {
  73.     if (this->music.getStatus() == SoundSource::Status::Paused ||
  74.         this->music.getStatus() == SoundSource::Status::Stopped)
  75.         this->music.play();
  76.     else
  77.         this->music.pause();
  78. }
  79.  
  80. void Menu::initPositions()
  81. {
  82.  
  83.     this->fone_spr.setPosition(0.f, 0.f);
  84.     this->butPlay_spr.setPosition(1250.f, 455.f);
  85.     this->butSet_spr.setPosition(1250.f, 655.f);
  86.     this->butExit_spr.setPosition(1250.f, 855.f);
  87. }
  88.  
  89. void Menu::getTouches(sf::RenderWindow* window)
  90. {
  91.     this->moveTimer += 0.5f;
  92.     if (Keyboard::isKeyPressed(Keyboard::W) && canMoveUp())
  93.     {
  94.         this->choice++;
  95.  
  96.         if (this->choice > 3)
  97.         this->choice = 1;
  98.  
  99.         but_s.play();
  100.     }
  101.  
  102.  
  103.     if (Keyboard::isKeyPressed(Keyboard::S) && canMoveUp())
  104.     {
  105.         //действия, когда нажали
  106.         this->choice--;
  107.         if (this->choice < 1)
  108.             this->choice = 3;
  109.         but_s.play();
  110.     }
  111.  
  112.     if (Keyboard::isKeyPressed(Keyboard::Enter))
  113.     {
  114.         //действия, когда нажали
  115.             this->flag = choice;
  116.  
  117.         but_s.play();
  118.         if (flag == 3)
  119.         {
  120.             music.stop();
  121.         }
  122.     }
  123.  
  124.     if (Keyboard::isKeyPressed(Keyboard::G))
  125.         this->changeMusic();
  126. }
  127.  
  128. bool Menu::canMoveUp()
  129. {
  130.     if (this->moveTimer > moveTimerMax) {
  131.         this->moveTimer = 0.f;
  132.         return true;
  133.     }
  134.     return false;
  135. }
  136.  
  137. Menu::Menu()
  138. {
  139.     this->initTextures();
  140.     this->initPositions();
  141.     this->initVariables();
  142. }
  143.  
  144.  
  145. void Menu::run(sf::RenderTarget* target, sf::RenderWindow* window)
  146. {
  147.     this->render(target);
  148.     this->update(window);
  149. }
  150.  
  151. bool Menu::ChangeFon()
  152. {
  153.     if (timer < 0)
  154.     {
  155.         return true;
  156.     }
  157.     else {
  158.         timer--;
  159.     }
  160.     return false;
  161. }
  162.  
  163. bool Menu::needToStart()
  164. {
  165.     if (timerAfter < 0)
  166.     {
  167.         return false;///////////////////////////////////////////////
  168.     }
  169. }
  170.  
  171. void Menu::updateFon()
  172. {
  173.     this->fone_spr.setTexture(fone_texture);
  174. }
  175.  
  176. int Menu::getFlag()
  177. {
  178.     return flag;
  179. }
  180.  
  181. void Menu::updateButtons()
  182. {
  183.     butExit_spr.setTexture(butExitText);
  184.     butSet_spr.setTexture(butSetText);
  185.     butPlay_spr.setTexture(butPlayText);
  186.     switch (choice)
  187.     {
  188.     case 3:
  189.         butPlay_spr.setTexture(butPlayText_active);
  190.         break;
  191.     case 2:
  192.         butSet_spr.setTexture(butSetText_active);
  193.         break;
  194.     case 1:
  195.         butExit_spr.setTexture(butExitText_active);
  196.         break;
  197.     }
  198. }
  199.  
  200. void Menu::render(sf::RenderTarget* target)
  201. {
  202.     target->draw(fone_spr);
  203.     if (ChangeFon())
  204.     {
  205.         if (flag == 2)
  206.             target->draw(set_spr);
  207.         target->draw(butPlay_spr);
  208.         target->draw(butSet_spr);
  209.         target->draw(butExit_spr);
  210.     }
  211. }
  212.  
  213. void Menu::update(sf::RenderWindow* window)
  214. {
  215.     if (ChangeFon())
  216.     {
  217.         updateSoundCooldown();
  218.         updateFon();
  219.     }
  220.     this->getTouches(window);
  221.     this->updateButtons();
  222.  
  223. }
  224.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement