Advertisement
venik2405

course.game

Dec 20th, 2021
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.45 KB | None | 0 0
  1. #include "Game.h"
  2. #include "Enemy.h"
  3.  
  4. void Game::checkMenu()
  5. {
  6.     int choice = menu.getFlag();
  7.     if (choice == 3)
  8.     {
  9.         gameWorking = true;
  10.     }
  11.  
  12.     if (choice == 1)
  13.         this->window->close();
  14. }
  15.  
  16. void Game::changeMusic()
  17. {
  18.     if (this->music.getStatus() == SoundSource::Status::Paused ||
  19.         this->music.getStatus() == SoundSource::Status::Stopped)
  20.         this->music.play();
  21.     else
  22.         this->music.pause();
  23. }
  24.  
  25. void Game::initTextures()
  26. {
  27.     if (!this->end_texture.loadFromFile("Textures/end_2.png"))
  28.         std::cout << "Failed to load player`s frame texture";
  29.     this->end_spr.setTexture(this->end_texture);
  30.     this->end_spr.setScale(1.f, 1.f);
  31. }
  32.  
  33. void Game::initVariables()
  34. {
  35.     this->endGame = false;
  36.     menuRunning = true;
  37.     this->soundCooldownMax = 10.f;
  38.     this->soundCooldown = soundCooldownMax;
  39. }
  40.  
  41. void Game::initWindow()
  42. {
  43.     this->videomode = sf::VideoMode(1920, 1080);
  44.     this->window = new sf::RenderWindow(this->videomode, "Drug_race", sf::Style::Close | sf::Style::Titlebar |sf::Style::Fullscreen);
  45.     this->window->setFramerateLimit(144);
  46.     this->window->setVerticalSyncEnabled(true);
  47. }
  48.  
  49. void Game::initEnemies()
  50. {
  51.     this->spawnTimerMax = 30.f;
  52.     this->spawnTimer = 0;
  53. }
  54.  
  55. void Game::initSounds()
  56. {
  57.     if (!(this->music.openFromFile("Sounds/snoop.ogg")))
  58.         std::cout << "Failed to load a main music";
  59.     this->music.setVolume(50);
  60.     this->music.setLoop(true);
  61. //  this->music.play();
  62.     if (!(this->contact_buff.loadFromFile("Sounds/contact.wav")))
  63.         std::cout << "Failed to load contact sound";
  64.     this->contact_sound.setBuffer(contact_buff);
  65.     this->contact_sound.setVolume(100);
  66.  
  67.     if (!(this->buffer_crash.loadFromFile("Sounds/crash_1.ogg")))
  68.         std::cout << "Failed to load crash sound";
  69.     this->crash_sound.setBuffer(buffer_crash);
  70.     this->crash_sound.setVolume(100);
  71.  
  72. }
  73.  
  74. void Game::updateEnemies(int speed, int posY)
  75. {
  76.     this->spawnTimer += 0.1f;
  77.     if (this->spawnTimer > this->spawnTimerMax)
  78.     {
  79.         this->enemies.push_back(new Enemy(2500, 450 + (rand() % 4) * 110, this->player.getSpeed(), rand() % 3 + 1));
  80.         this->spawnTimer = 0.f;
  81.     }
  82.     for (int i = 0; i < this->enemies.size(); ++i)
  83.     {
  84.         this->enemies[i]->update(speed, posY);
  85.  
  86.         //Remove enemy at the buttom
  87.         if ((this->enemies[i]->getBounds().left < -10000) || (this->enemies[i]->getBounds().left > 5000))
  88.         {
  89.             this->enemies.erase(this->enemies.begin() + i);
  90.         }
  91.     }
  92. }
  93.  
  94. void Game::run()
  95. {
  96.     while (this->window->isOpen())
  97.     {
  98.         this->render();
  99.         this->update();
  100.     }
  101. }
  102.  
  103. const bool Game::getEndGame() const
  104. {
  105.     return this->endGame;
  106. }
  107.  
  108. void Game::mute()
  109. {
  110.     this->player.mute();
  111.     this->music.pause();
  112. }
  113.  
  114. Game::Game()
  115. {
  116.     initSounds();
  117.     initTextures();
  118.     initVariables();
  119.     initWindow();
  120.     initEnemies();
  121.     initCoins();
  122. }
  123.  
  124. Game::~Game()
  125. {
  126.     delete this->window;
  127. }
  128.  
  129. void Game::pollEvents()
  130. {
  131.     while (this->window->pollEvent(this->sfmlEvent)) {
  132.         switch (this->sfmlEvent.type)
  133.         {
  134.         case sf::Event::Closed:
  135.             this->window->close();
  136.             break;
  137.         case sf::Event::KeyPressed:
  138.             if (this->sfmlEvent.key.code == sf::Keyboard::Escape)
  139.                 this->window->close();
  140.  
  141.             if (this->sfmlEvent.key.code == sf::Keyboard::F)
  142.                 this->changeMusic();
  143.             break;
  144.         }
  145.     }
  146. }
  147.  
  148. void Game::updatePlayer()
  149. {
  150.     this->player.update(this->window);
  151. }
  152.  
  153. sf::RenderWindow* Game::getWindow()
  154. {
  155.     return this->window;
  156. }
  157.  
  158. void Game::renderGame()
  159. {
  160.     this->land.render(this->window);
  161.     for (auto* coin : this->coins)
  162.     {
  163.         coin->render(this->window);
  164.     }
  165.     for (auto* enemy : this->enemies)
  166.     {
  167.         if (enemy->getLine() == 0)
  168.             enemy->render(this->window);
  169.     }
  170.     for (auto* enemy : this->enemies)
  171.     {
  172.         if (enemy->getLine() == 1)
  173.             enemy->render(this->window);
  174.     }
  175.     for (auto* enemy : this->enemies)
  176.     {
  177.         if (enemy->getLine() == 2)
  178.             enemy->render(this->window);
  179.     }
  180.     for (auto* enemy : this->enemies)
  181.     {
  182.         if (enemy->getLine() == 3)
  183.             enemy->render(this->window);
  184.     }
  185.     this->player.render(this->window);
  186.     if (this->endGame == true)
  187.         this->window->draw(this->end_spr);
  188. }
  189.  
  190. void Game::render()
  191. {
  192.     this->window->clear();
  193.     if (this->menu.getFlag() != 3)
  194.         this->menu.render(this->window);
  195.     else
  196.         this->renderGame();
  197.     this->window->display();
  198. }
  199.  
  200. void Game::crash(int i)
  201. {
  202.     if (this->player.getShape().getGlobalBounds().left > this->enemies[i]->getBounds().left) {
  203.         this->enemies[i]->crash();
  204.     }
  205.     this->player.crash();
  206.     this->crash_sound.play();
  207.     if (this->player.getHp() < 1)
  208.     {
  209.         this->endGame = true;
  210.     }
  211. }
  212.  
  213. void Game::updateSoundCooldown()
  214. {
  215.     soundCooldown += 0.5f;
  216. }
  217.  
  218. void Game::initCoins()
  219. {
  220.     this->coinTimerMax = 8.f;
  221.     this->coinTimer = 0;
  222. }
  223.  
  224. void Game::updateCoins(int speed, int posY)
  225. {
  226.     this->coinTimer += 0.1f;
  227.     if (this->coinTimer > this->coinTimerMax && coins.size() < 5)
  228.     {
  229.         int choice = rand() % 100 + 1;
  230.         int type = 1;
  231.         if (choice > 80 && choice < 90)
  232.             type = 2;
  233.         else if (choice > 89)
  234.             type = 3;
  235.         this->coins.push_back(new Coin(2000, 450 + (rand() % 4) * 110, this->player.getSpeed(), type));
  236.         this->coinTimer = 0.f;
  237.     }
  238.     for (int i = 0; i < this->coins.size(); ++i)
  239.     {
  240.         this->coins[i]->update(speed, posY);
  241.  
  242.         //Remove enemy at the buttom
  243.         if (this->coins[i]->getBounds().left < -400)
  244.         {
  245.             this->coins.erase(this->coins.begin() + i);
  246.         }
  247.     }
  248. }
  249.  
  250. void Game::updateCollision()
  251. {
  252.     int speed;
  253.     for (size_t i = 0; i < this->enemies.size(); i++)
  254.     {
  255.         if (abs( this->player.getShape().getGlobalBounds().top - this->enemies[i]->getBounds().top) < 20 &&
  256.             abs(this->player.getShape().getGlobalBounds().left - this->enemies[i]->getBounds().left) < 440)
  257.         {
  258.             this->crash(i);
  259.         }
  260.         for (size_t j = 0; j < this->enemies.size(); j++)
  261.         {
  262.             if (i != j)
  263.             {
  264.                 if (abs(this->enemies[i]->getBounds().top - this->enemies[j]->getBounds().top) < 20 &&
  265.                     abs(this->enemies[i]->getBounds().left - this->enemies[j]->getBounds().left) < 440)
  266.                 {
  267.                     if (this->enemies[i]->getBounds().left > 0 && this->enemies[i]->getBounds().left < 1920)
  268.                         contact_sound.play();
  269.                     if (this->enemies[i]->getSpeed() > this->enemies[j]->getSpeed())
  270.                     {
  271.                         int speed = this->enemies[i]->getSpeed();
  272.                         this->enemies[i]->changeSpeed(this->enemies[j]->getSpeed());
  273.                         this->enemies[j]->changeSpeed(speed);
  274.                         this->enemies[j]->boost();
  275.                     }
  276.                     else
  277.                     {
  278.                         int speed = this->enemies[j]->getSpeed();
  279.                         this->enemies[j]->changeSpeed(this->enemies[i]->getSpeed());
  280.                         this->enemies[i]->changeSpeed(speed);
  281.                         this->enemies[i]->boost();
  282.                     }
  283.                 }
  284.             }
  285.         }
  286.     }
  287.     for (size_t i = 0; i < this->coins.size(); i++)
  288.     {
  289.         if (abs(this->player.getShape().getGlobalBounds().top - this->coins[i]->getBounds().top) < 20 &&
  290.             abs(this->player.getShape().getGlobalBounds().left - this->coins[i]->getBounds().left) < 440)
  291.         {
  292.             switch (coins[i]->getType())
  293.             {
  294.             case 1:
  295.                 this->player.getCoin();
  296.                 break;
  297.             case 2:
  298.                 this->player.getNitro();
  299.                 break;
  300.             case 3:
  301.                 this->player.changeLine();
  302.                 break;
  303.             }
  304.             coins.erase(coins.begin() + i);
  305.         }
  306.     }
  307.     //Check the collision
  308. }
  309.  
  310. void Game::updateGame()
  311. {
  312.     if (!this->endGame)
  313.     {
  314.         this->updateSoundCooldown();
  315.         this->updatePlayer();
  316.         this->updateCoins(player.getSpeed(), land.getPos().y);
  317.         this->land.update(-this->player.getSpeed(), 0, player.getLine());
  318.         this->updateEnemies(player.getSpeed(), land.getPos().y);
  319.         this->updateCollision();
  320.     }
  321.     else {
  322.         this->mute();
  323.     }
  324. }
  325.  
  326. void Game::updateMenu()
  327. {
  328.     menu.run(window, window);
  329. }
  330.  
  331. void Game::update()
  332. {
  333.     this->pollEvents();
  334.     checkMenu();
  335.     if (!gameWorking)
  336.         this->updateMenu();
  337.     else
  338.         this->updateGame();
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement