Advertisement
venik2405

course.land

Dec 20th, 2021
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 KB | None | 0 0
  1. #include "Land.h"
  2.  
  3. void Land::initTextures()
  4. {
  5.     //Road
  6.     if (!this->road_texture.loadFromFile("Textures/road_10.png"))
  7.         std::cout << "Failed to load road texture";
  8.     this->road_sprite1.setTexture(road_texture);
  9.     this->road_sprite2.setTexture(road_texture);
  10.  
  11.     //Mountains
  12.     if (!this->mount_texture.loadFromFile("Textures/mount_1.png"))
  13.         std::cout << "Failed to load mountain texture";
  14.     this->mount_sprite1.setTexture(mount_texture);
  15.     this->mount_sprite1.setScale(1.6, 1);
  16.     this->mount_sprite2.setTexture(mount_texture);
  17.     this->mount_sprite2.setScale(1.6, 1);
  18.  
  19.     //Trees
  20.     if (!this->tree_texture.loadFromFile("Textures/trees_4.png"))
  21.         std::cout << "Failed to load trees texture";
  22.     this->tree_sprite1.setTexture(tree_texture);
  23.     this->tree_sprite2.setTexture(tree_texture);
  24.     this->tree_sprite1.setScale(3, 2);
  25.     this->tree_sprite2.setScale(3, 2);
  26.  
  27.     if (!this->sky_texture.loadFromFile("Textures/sky_2.png"))
  28.         std::cout << "Failed to load sky texture";
  29.     this->sky_spr1.setTexture(sky_texture);
  30.     this->sky_spr2.setTexture(sky_texture);
  31.     this->sky_spr1.setScale(1, 1);
  32.     this->sky_spr2.setScale(1, 1);
  33. }
  34.  
  35. void Land::initVariables()
  36. {
  37.     isMovingDown = false;
  38.     isMovingUp = false;
  39.     prevLine = 3;
  40. }
  41.  
  42. void Land::updateScrolling()
  43. {
  44.     //Road
  45.     if (road_sprite2.getPosition().x < 0) {
  46.         this->road_sprite1.setPosition(road_sprite2.getPosition().x + 1920.f, road_sprite2.getPosition().y);
  47.     }
  48.     if (road_sprite1.getPosition().x < 0) {
  49.         this->road_sprite2.setPosition(road_sprite1.getPosition().x + 1920.f, road_sprite1.getPosition().y);
  50.     }
  51.     if (road_sprite1.getPosition().x > 0) {
  52.         this->road_sprite2.setPosition(road_sprite1.getPosition().x - 1920.f, road_sprite1.getPosition().y);
  53.     }
  54.     if (road_sprite2.getPosition().x > 0) {
  55.         this->road_sprite1.setPosition(road_sprite2.getPosition().x - 1920.f, road_sprite2.getPosition().y);
  56.     }
  57.  
  58.     //Mountains
  59.     if (mount_sprite2.getPosition().x < 0) {
  60.         this->mount_sprite1.setPosition(mount_sprite2.getPosition().x + 1780.f, mount_sprite2.getPosition().y);
  61.     }
  62.     if (mount_sprite1.getPosition().x < 0) {
  63.         this->mount_sprite2.setPosition(mount_sprite1.getPosition().x + 1780.f, mount_sprite1.getPosition().y);
  64.     }
  65.     if (mount_sprite2.getPosition().x > 0) {
  66.         this->mount_sprite1.setPosition(mount_sprite2.getPosition().x - 1780, mount_sprite2.getPosition().y);
  67.     }
  68.     if (mount_sprite1.getPosition().x > 0) {
  69.         this->mount_sprite2.setPosition(mount_sprite1.getPosition().x - 1780, mount_sprite1.getPosition().y);
  70.     }
  71.  
  72.     //Trees
  73.     if (tree_sprite2.getPosition().x < 0) {
  74.         this->tree_sprite1.setPosition(tree_sprite2.getPosition().x + 1780.f, tree_sprite2.getPosition().y);
  75.     }
  76.     if (tree_sprite1.getPosition().x < 0) {
  77.         this->tree_sprite2.setPosition(tree_sprite1.getPosition().x + 1780.f, tree_sprite1.getPosition().y);
  78.     }
  79.     if (tree_sprite2.getPosition().x > 0) {
  80.         this->tree_sprite1.setPosition(tree_sprite2.getPosition().x - 1780.f, tree_sprite2.getPosition().y);
  81.     }
  82.     if (tree_sprite1.getPosition().x > 0) {
  83.         this->tree_sprite2.setPosition(tree_sprite1.getPosition().x - 1780.f, tree_sprite1.getPosition().y);
  84.     }
  85.     //Sky
  86.     if (sky_spr2.getPosition().x < 0) {
  87.         this->sky_spr1.setPosition(sky_spr1.getPosition().x + 3000.f, sky_spr1.getPosition().y);
  88.     }
  89.     if (sky_spr1.getPosition().x < 0) {
  90.         this->sky_spr2.setPosition(sky_spr1.getPosition().x + 3000.f, sky_spr1.getPosition().y);
  91.     }
  92.     if (sky_spr2.getPosition().x > 0) {
  93.         this->sky_spr1.setPosition(sky_spr2.getPosition().x - 3000.f, sky_spr2.getPosition().y);
  94.     }
  95.     if (sky_spr1.getPosition().x > 0) {
  96.         this->sky_spr2.setPosition(sky_spr1.getPosition().x - 3000.f , sky_spr1.getPosition().y);
  97.     }
  98. }
  99.  
  100. void Land::initPositions()
  101. {
  102.     this->road_sprite1.setPosition(0.f, -300.f);
  103.     this->road_sprite2.setPosition(2030.f, -300.f);
  104.     this->mount_sprite1.setPosition(0.f, 110.f);
  105.     this->mount_sprite2.setPosition(1750.f, 110.f);
  106.     this->tree_sprite1.setPosition(0.f, 257.f);
  107.     this->tree_sprite2.setPosition(1800.f, 257.f);
  108.     this->sky_spr1.setPosition(0.f, -150.f);
  109.     this->sky_spr2.setPosition(3000.f, -150.f);
  110.     this->tree_sprite2.setPosition(1800.f, 257.f);
  111. }
  112.  
  113.  
  114.  
  115. Land::Land()
  116. {
  117.     this->initTextures();
  118.     this->initPositions();
  119.     this->initVariables();
  120. }
  121.  
  122.  
  123.  
  124. const int Land::getPosY() const
  125. {
  126.     return this->road_sprite1.getPosition().y;
  127. }
  128.  
  129. void Land::moveView(float speedX, float speedY)
  130. {
  131.     //Move
  132.     this->road_sprite1.move(speedX * 0.5, speedY);
  133.     this->road_sprite2.move(speedX * 0.5, speedY);
  134.     this->mount_sprite1.move(speedX * 0.02, speedY);
  135.     this->mount_sprite2.move(speedX * 0.02, speedY);
  136.     this->tree_sprite1.move(speedX * 0.2, speedY);
  137.     this->tree_sprite2.move(speedX * 0.2, speedY);
  138.     this->sky_spr1.move(speedX * 0.01, speedY);
  139.     this->sky_spr2.move(speedX * 0.01, speedY);
  140.     this->updateScrolling();
  141. }
  142.  
  143. void Land::render(sf::RenderTarget* target)
  144. {
  145.     //Road
  146.     target->draw(this->road_sprite2);
  147.     target->draw(this->road_sprite1);
  148.     //Sky
  149.     target->draw(this->sky_spr1);
  150.     target->draw(this->sky_spr2);
  151.     //Mountains
  152.     target->draw(this->mount_sprite1);
  153.     target->draw(this->mount_sprite2);
  154.     //Trees
  155.     target->draw(this->tree_sprite1);
  156.     target->draw(this->tree_sprite2);
  157. }
  158.  
  159. void Land::update(float speedX, float speedY, int line)
  160. {
  161.     this->line = line;
  162.     this->checkLines();
  163.     this->speed = speedX;
  164.     this->moveView(speedX, speedY);
  165.     this->moveVertically();
  166. }
  167.  
  168. void Land::moveVertically()
  169. {
  170.     float speedY;
  171.     speedY = this->speed * 0.1f;
  172.     if (speedY < -5.f)
  173.         speedY = - 5.f;
  174.     if (isMovingUp) {
  175.         this->moveView(0.f, -speedY);
  176.         if (this->getPos().y + 625 > this->line*110)
  177.             isMovingUp = false;
  178.     }
  179.     else
  180.         if (isMovingDown) {
  181.             this->moveView(0.f, speedY);
  182.             if (this->getPos().y + 620 < this->line * 110)
  183.                 isMovingDown = false;
  184.         }
  185. }
  186.  
  187. void Land::checkLines()
  188. {
  189.     if ((this->line > prevLine)) {
  190.         prevLine = line;
  191.         isMovingUp = true;
  192.     }
  193.     if ((this->line < prevLine)) {
  194.         prevLine = line;
  195.         isMovingDown = true;
  196.     }
  197. }
  198.  
  199. const::sf::Vector2f Land::getPos() const
  200. {
  201.     return this->road_sprite1.getPosition();
  202. }
  203.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement