Advertisement
venik2405

course.player

Dec 20th, 2021
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.45 KB | None | 0 0
  1. #include "Player.h"
  2.  
  3. void Player::initVariables()
  4. {
  5.     this->moveTimerMax = 7.f;
  6.     this->moveTimerUp = moveTimerMax;
  7.     this->moveTimerDown = moveTimerMax;
  8.     this->timerCrashMax = 100.f;
  9.     this->timerCrash = timerCrashMax;
  10.     this->speed = 0;
  11.     this->hpMax = 3;
  12.     this->hp = hpMax;
  13.     this->coins = 0;
  14.     isNitring = false;
  15. }
  16.  
  17. void Player::updateTimer()
  18. {
  19.     this->moveTimerUp += 0.5f;
  20.     this->moveTimerDown += 0.5f;
  21.     this->timerCrash += 0.5f;
  22. }
  23.  
  24. bool Player::canMoveUp()
  25. {
  26.     if (this->moveTimerUp > moveTimerMax) {
  27.         this->moveTimerUp = 0.f;
  28.         return true;
  29.     }
  30.     return false;
  31. }
  32.  
  33. bool Player::canMoveDown()
  34. {
  35.     if (this->moveTimerDown > moveTimerMax) {
  36.         this->moveTimerDown = 0.f;
  37.         return true;
  38.     }
  39.     return false;
  40. }
  41.  
  42. bool Player::canGetDamage()
  43. {
  44.     if (this->timerCrash > timerCrashMax) {
  45.         this->timerCrash = 0.f;
  46.         return true;
  47.     }
  48.     return false;
  49. }
  50.  
  51. void Player::initTextures()
  52. {
  53.     //Frame
  54.     if (!this->frame_texture.loadFromFile("Textures/player4.png"))
  55.         std::cout << "Failed to load player`s frame texture";
  56.     this->frame.setTexture(this->frame_texture);
  57.     this->frame.setScale(0.7f, 0.7f);
  58.     //Wheels
  59.     if (!this->nitro_text.loadFromFile("Textures/fire_2.png"))
  60.         std::cout << "Failed to load nitro`s frame texture";
  61.     this->nitro_spr.setTexture(this->nitro_text);
  62.     this->nitro_spr.setScale(0.7f, 0.7f);
  63.  
  64.     if (!this->wheel_texture.loadFromFile("Textures/red_wheel.png"))
  65.         std::cout << "Failed to load player`s wheels texture";
  66.     this->left_wh.setTexture(this->wheel_texture);
  67.     this->right_wh.setTexture(this->wheel_texture);
  68.     this->left_wh.setScale(0.7f, 0.7f);
  69.     this->right_wh.setScale(0.7f, 0.7f);
  70.     this->left_wh.setOrigin(46.f, 46.f);
  71.     this->right_wh.setOrigin(46.f, 46.f);
  72. }
  73.  
  74. void Player::initMusic()
  75. {
  76.     if (!(this->buffer_earn.loadFromFile("Sounds/coin_3.ogg")))
  77.         std::cout << "Failed to load coin`s sound";
  78.     this->earn_sound.setBuffer(buffer_earn);
  79.     this->earn_sound.setVolume(100);
  80.     //
  81.     if (!(this->buffer_slip.loadFromFile("Sounds/slip.ogg")))
  82.         std::cout << "Failed to load slip`s sound";
  83.     this->slip_sound.setBuffer(buffer_slip);
  84.     this->slip_sound.setVolume(100);
  85.     //
  86.     if (!(this->nitro_bf.loadFromFile("Sounds/nitro.wav")))
  87.         std::cout << "Failed to load nitro sound";
  88.     this->nitro_s.setBuffer(nitro_bf);
  89.     this->nitro_s.setVolume(30);
  90.     //
  91.  
  92.     if (!(this->nitro_pick_buff.loadFromFile("Sounds/nitro_pick.wav")))
  93.         std::cout << "Failed to load nitro_pick sound";
  94.     this->nitro_pick.setBuffer(nitro_pick_buff);
  95.     this->nitro_pick.setVolume(100);
  96. }
  97.  
  98.  
  99. void Player::updateWindowBundsCollision(sf::RenderTarget* target)
  100. {
  101.     //Left
  102.     if (this->frame.getGlobalBounds().left <= 0.f)
  103.         this->frame.setPosition(0.f, this->frame.getGlobalBounds().top);
  104.     //Right
  105.     if (this->frame.getGlobalBounds().left +
  106.         this->frame.getGlobalBounds().width >= target->getSize().x)
  107.         this->frame.setPosition(target->getSize().x - this->frame.getGlobalBounds().width, this->frame.getGlobalBounds().top);
  108.     //Top
  109.     if (this->frame.getGlobalBounds().top <= 0.f)
  110.         this->frame.setPosition(this->frame.getGlobalBounds().left, 0.f);
  111.     //Down
  112.     if (this->frame.getGlobalBounds().top +
  113.         this->frame.getGlobalBounds().height >= target->getSize().y)
  114.         this->frame.setPosition(this->frame.getGlobalBounds().left, target->getSize().y - this->frame.getGlobalBounds().height);
  115. }
  116.  
  117. void Player::updateWheels()
  118. {
  119.     this->left_wh.setPosition(this->frame.getPosition().x + 102.f, this->frame.getPosition().y + 92.f);
  120.     this->right_wh.setPosition(this->frame.getPosition().x + 352.f, this->frame.getPosition().y + 92.f);
  121. }
  122.  
  123. const int Player::getSpeed() const
  124. {
  125.     return this->speed;
  126. }
  127.  
  128.  
  129. const sf::Sprite Player::getShape() const
  130. {
  131.     return this->frame;
  132. }
  133.  
  134. const int Player::getLine() const
  135. {
  136.     return this->engine.getLine();
  137. }
  138.  
  139. void Player::changeLine()
  140. {
  141.     int choice = rand() % 2;
  142.     slip_sound.play();
  143.     if (choice == 1)
  144.     {
  145.         for (int i = 0; i < choice + 1; i++)
  146.         {
  147.             engine.moveUp();
  148.             engine.moveUp();
  149.         }
  150.     }
  151.     else
  152.     {
  153.         for (int i = 0; i < choice + 1; i++)
  154.         {
  155.             engine.moveDown();
  156.             engine.moveDown();
  157.         }
  158.     }
  159. }
  160.  
  161. void Player::crash() {
  162.     this->engine.crash();
  163.     if (canGetDamage())
  164.         this->hp--;
  165. }
  166.  
  167.  
  168. void Player::getNitro()
  169. {
  170.     engine.getNitro();
  171.     nitro_pick.play();
  172.  
  173. }
  174.  
  175. void Player::mute()
  176. {
  177.     this->engine.voiceOff();
  178. }
  179.  
  180. void Player::updateInput()
  181. {
  182.     this->speed = engine.getSpeed();
  183.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
  184.         this->engine.brake();
  185.     }
  186.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
  187.         this->engine.gas();
  188.     }
  189.     else {
  190.         this->engine.Idle();
  191.     }
  192.  
  193.     if (Keyboard::isKeyPressed(Keyboard::W) && canMoveUp())
  194.     {
  195.         this->engine.moveUp();
  196.     }
  197.  
  198.  
  199.     if (Keyboard::isKeyPressed(Keyboard::S) && canMoveDown())
  200.     {
  201.         //действия, когда нажали
  202.         this->engine.moveDown();
  203.     }
  204.  
  205.     if (Keyboard::isKeyPressed(Keyboard::LShift))
  206.     {
  207.         this->engine.nitro();
  208.         isNitring = true;
  209.         if (engine.canNitro())
  210.         {
  211.             if (nitro_s.getStatus() == SoundSource::Status::Stopped)
  212.                 nitro_s.play();
  213.             isNitring = true;
  214.         }
  215.     }
  216.     else {
  217.         nitro_s.stop();
  218.         isNitring = false;
  219.     }
  220.  
  221.  
  222.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
  223.         this->engine.gearDown();
  224.     }
  225.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) {
  226.         this->engine.gearUp();
  227.     }
  228.     this->left_wh.rotate(this->speed * 0.6f);
  229.     this->right_wh.rotate(this->speed * 0.6f);
  230. }
  231.  
  232. Player::Player(float x, float y)
  233. {
  234.     this->initMusic();
  235.     this->frame.setPosition(45, 555);
  236.     this->left_wh.setPosition(x + 102.f, y + 92.f);
  237.     this->right_wh.setPosition(x + 352.f, y + 92.f);
  238.     this->nitro_spr.setPosition(frame.getPosition().x - 200, frame.getPosition().y + 100);
  239.     this->initVariables();
  240.     this->initTextures();
  241. }
  242.  
  243. Player::~Player()
  244. {
  245. }
  246.  
  247. void Player::getCoin()
  248. {
  249.     this->coins++;
  250.     earn_sound.play();
  251. }
  252.  
  253. const bool Player::getHp()
  254. {
  255.     return this->hp;
  256. }
  257.  
  258. void Player::updateDash()
  259. {
  260.     int sp = engine.getSpeed();
  261.     int rp = engine.getRpm();
  262.     int gr = engine.getGear();
  263.     int cn = this->coins;
  264.     int nit = engine.getNitroCount();
  265.     int hp = this->hp;
  266.     dash.update(sp, rp, gr, cn, nit, hp);
  267. }
  268.  
  269.  
  270.  
  271. void Player::update(sf::RenderTarget* target)
  272. {
  273.     this->updateTimer();
  274.     this->updateWindowBundsCollision(target);
  275.     this->updateInput();
  276.     this->engine.work();
  277.     this->updateWheels();
  278.     this->updateDash();
  279.  
  280. }
  281.  
  282.  
  283. void Player::render(sf::RenderTarget* target)
  284. {
  285.     this->engine.render(target);
  286.     target->draw(this->frame);
  287.     target->draw(this->left_wh);
  288.     target->draw(this->right_wh);
  289.     if (isNitring)
  290.         target->draw(nitro_spr);
  291.     dash.render(target);
  292. }
  293.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement