Advertisement
venik2405

course.engine

Dec 20th, 2021
956
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.36 KB | None | 0 0
  1. #include "Engine.h"
  2.  
  3.  
  4. void Engine::updateNitro()
  5. {
  6.     if (isNitring) {
  7.         nitroCounter -= 0.5f;
  8.         if (nitroCounter < nitroCounterMax)
  9.         {
  10.             isNitring = false;
  11.             boost = 50;
  12.             addSpeed = 0;
  13.         }
  14.     }
  15. }
  16.  
  17. void Engine::initSounds()
  18. {
  19.     if (!(this->gear_bf.loadFromFile("Sounds/Engine/gear_2.wav")))
  20.         std::cout << "Failed to load gear sound";
  21.     this->gear_s.setBuffer(gear_bf);
  22.     this->gear_s.setVolume(30);
  23.    
  24.     if (!(this->work_bf.loadFromFile("Sounds/Engine/engine_7.wav")))
  25.         std::cout << "Failed to load engine sound";
  26.     this->work_s.setBuffer(work_bf);
  27.     this->work_s.setLoop(true);
  28.     this->work_s.setVolume(20);
  29. }
  30.  
  31.  
  32. void Engine::updateSound()
  33. {
  34.     if (work_s.getStatus() == SoundSource::Status::Stopped)
  35.         work_s.play();
  36.     this->work_s.setPitch((this->rpm + (float)(gear * 500.f)) / 4000.f);
  37.     this->work_s.setVolume(this->rpm / 80.f);
  38.     if (work_s.getPitch() < 0.7f)
  39.         this->work_s.setPitch(0.7f);
  40.     if (work_s.getVolume() < 20)
  41.         this->work_s.setVolume(20);
  42. }
  43.  
  44. void Engine::initVariables()
  45. {
  46.     this->nitroCounter = 0;
  47.     this->nitroCounterMax = 100;
  48.     this->gear = 1;
  49.     this->gearCount = 5;
  50.     this->speed = 0;
  51.     this->rpm = 800;
  52.     this->maxRpm = 8500;
  53.     this->line = 3;
  54.     this->isNitring = false;
  55.     this->boost = 50;
  56.     this->addSpeed = 0;
  57.     this->left = sf::Time(milliseconds(0));
  58.     this->left = sf::Time(milliseconds(50));
  59. }
  60.  
  61. Engine::Engine()
  62. {
  63.     this->initVariables();
  64.     this->initSounds();
  65. }
  66.  
  67. void Engine::voiceOff()
  68. {
  69.     this->work_s.stop();
  70. }
  71.  
  72. bool Engine::canNitro()
  73. {
  74.     if (this->nitroCounter > 0)
  75.         return true;
  76.     return false;
  77. }
  78.  
  79. float Engine::getNitroCount()
  80. {
  81.     return this->nitroCounter;
  82. }
  83.  
  84. void Engine::getNitro()
  85. {
  86.     this->nitroCounter += 25;
  87.     if (this->nitroCounter > 100)
  88.         this->nitroCounter = 100;
  89. }
  90.  
  91. void Engine::nitro()
  92. {
  93.     if (canNitro())
  94.     {
  95.         this->boost = 150;
  96.         addSpeed = 20;
  97.         this->isNitring = true;
  98.     }
  99. }
  100.  
  101. void Engine::brake()
  102. {
  103.     if (rpm > 800)
  104.         rpm -= 100 / gear;
  105.     else
  106.         if (rpm > 300)
  107.             rpm = 200;
  108. }
  109.  
  110. void Engine::gas()
  111. {
  112.     if (rpm < maxRpm)
  113.         rpm += boost/(gear);
  114. }
  115.  
  116. void Engine::Idle()
  117. {
  118.     if (rpm == 200) rpm = 800;
  119.     if (rpm > 800)
  120.         rpm -= 20 / gear;
  121. }
  122.  
  123. void Engine::work()
  124. {
  125.     this->speed = (int)(((float)rpm / (float)maxRpm ) * ((float)maxSpeedPerGear[gear - 1] + addSpeed));
  126.     this->updateNitro();
  127.     this->updateSound();
  128. }
  129.  
  130. void Engine::crash()
  131. {
  132.     this->rpm = 2500;
  133. }
  134.  
  135. void Engine::gearDown()
  136. {
  137.     if ((gear > 1) && (rpm < 4000))
  138.     {
  139.         this->gear--;
  140.         this->rpm = (int)((float)maxRpm * (float)speed / (float)maxSpeedPerGear[gear - 1]);
  141.         if (rpm < 800) rpm = 800;
  142.         if (rpm > maxRpm) rpm = maxRpm;
  143.         gear_s.play();
  144.     }
  145. }
  146.  
  147. void Engine::gearUp()
  148. {
  149.     if ((gear < gearCount) && (rpm > 8000))
  150.     {
  151.         this->gear++;
  152.         this->rpm = (int)((float)maxRpm * (float)speed / (float)maxSpeedPerGear[gear - 1]);
  153.         if (rpm < 800) rpm = 800;
  154.         if (rpm > maxRpm) rpm = maxRpm;
  155.         gear_s.play();
  156.     }
  157. }
  158.  
  159. void Engine::moveUp()
  160. {
  161.     if ((this->line != 4) ) {
  162.         this->line++;
  163.     }
  164. }
  165.  
  166. void Engine::moveDown()
  167. {
  168.     if ((this->line != 1)) {
  169.         this->line--;
  170.     }
  171. }
  172.  
  173.  
  174. const int Engine::getSpeed() const
  175. {
  176.     return this->speed;
  177. }
  178.  
  179. const int Engine::getLine() const
  180. {
  181.     return this -> line;
  182. }
  183.  
  184. const int Engine::getGear() const
  185. {
  186.     return this->gear;
  187. }
  188.  
  189. const int Engine::getRpm() const
  190. {
  191.     return this->rpm;
  192. }
  193.  
  194. void Engine::updateView() {
  195. }
  196.  
  197. void Engine::render(sf::RenderTarget* target)
  198. {
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement