Advertisement
venik2405

course.enemy

Dec 20th, 2021
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. #include "Enemy.h"
  2.  
  3. void Enemy::initTextures()
  4. {
  5.     float scaleFrameX = 0;
  6.     float scaleFrameY = 0;
  7.     float scaleWheel = 0;
  8.     switch (this->type) {
  9.     case 1:
  10.         if (!this->texture_frame.loadFromFile("Textures/lancer_1.png"))
  11.             std::cout << "Failed to load player`s frame texture";
  12.         if (!this->wheel_texture.loadFromFile("Textures/green_wheel3.png"))
  13.             std::cout << "Failed to load player`s wheels texture";
  14.         scaleFrameX = 0.7;
  15.         scaleFrameY = 0.7;
  16.         scaleWheel = 1.1;
  17.         this->left_wh.setOrigin(24.f, 24.f);
  18.         this->right_wh.setOrigin(24.f, 24.f);
  19.         break;
  20.     case 2:
  21.         if (!this->texture_frame.loadFromFile("Textures/aston_2.png"))
  22.             std::cout << "Failed to load player`s frame texture";
  23.         if (!this->wheel_texture.loadFromFile("Textures/red_wheel.png"))
  24.             std::cout << "Failed to load player`s wheels texture";
  25.         scaleFrameX = 1.f;
  26.         scaleFrameY = 1.f;
  27.         scaleWheel = 0.7;
  28.         this->left_wh.setOrigin(46, 46);
  29.         this->right_wh.setOrigin(46, 46);
  30.         break;
  31.     case 3:
  32.         if (!this->texture_frame.loadFromFile("Textures/bmw_e60.png"))
  33.             std::cout << "Failed to load player`s frame texture";
  34.         if (!this->wheel_texture.loadFromFile("Textures/red_wheel.png"))
  35.             std::cout << "Failed to load player`s wheels texture";
  36.         scaleFrameX = 0.7;
  37.         scaleFrameY = 0.7;
  38.         scaleWheel = 0.7;
  39.         this->left_wh.setOrigin(46, 46);
  40.         this->right_wh.setOrigin(46, 46);
  41.         break;
  42.     }
  43.     this->frame.setTexture(this->texture_frame);
  44.     this->frame.setScale(scaleFrameX, scaleFrameY);
  45.     //Wheels
  46.     this->left_wh.setTexture(this->wheel_texture);
  47.     this->right_wh.setTexture(this->wheel_texture);
  48.     this->left_wh.setScale(scaleWheel, scaleWheel);
  49.     this->right_wh.setScale(scaleWheel, scaleWheel);
  50. }
  51.  
  52. void Enemy::initVariables(int sp, int y, int type)
  53. {
  54.     this->speed = sp - rand() % 7;
  55.     if (speed < 10) speed = 70;
  56.     this->type = type;
  57.     this->damage = 1;
  58.     this->y = y;
  59.     this->line = (y - 450) / 110;
  60.     switch (this->type) {
  61.     case (1):
  62.         this->leftWheelPosX = 124.f;
  63.         this->leftWheelPosY = 118.f;
  64.         this->rightWheelPosX = 375.f;
  65.         this->rightWheelPosY = 118.f;
  66.         break;
  67.     case (2):
  68.         this->leftWheelPosX = 88.f;
  69.         this->leftWheelPosY = 88.f;
  70.         this->rightWheelPosX = 345.f;
  71.         this->rightWheelPosY = 88.f;
  72.         break;
  73.     case (3):
  74.         this->leftWheelPosX = 98.f;
  75.         this->leftWheelPosY = 97.f;
  76.         this->rightWheelPosX = 350.f;
  77.         this->rightWheelPosY = 97.f;
  78.         break;
  79.     }
  80. }
  81.  
  82.  
  83. const sf::FloatRect Enemy::getBounds() const
  84. {
  85.     return this->frame.getGlobalBounds();
  86. }
  87.  
  88. const sf::Sprite Enemy::getShape() const
  89. {
  90.     return this->frame;
  91. }
  92.  
  93. const int Enemy::getLine() const
  94. {
  95.     return this->line;
  96. }
  97.  
  98. void Enemy::crash()
  99. {
  100.     this->speed = 5;
  101. }
  102.  
  103. void Enemy::changeSpeed(int speed)
  104. {
  105.     this->speed = speed;
  106. }
  107.  
  108. float Enemy::getSpeed()
  109. {
  110.     return this->speed;
  111. }
  112.  
  113. void Enemy::boost()
  114. {
  115.     this->frame.setPosition(this->frame.getPosition().x + 40, this->frame.getPosition().y);
  116. }
  117.  
  118. void Enemy::updateWheels()
  119. {
  120.     this->left_wh.setPosition(this->frame.getPosition().x + leftWheelPosX, this->frame.getPosition().y + leftWheelPosY);
  121.     this->right_wh.setPosition(this->frame.getPosition().x + rightWheelPosX, this->frame.getPosition().y + rightWheelPosY);
  122. }
  123.  
  124. Enemy::Enemy(float x, float y, int sp, int type)
  125. {
  126.     this->initVariables(sp, y, type);
  127.     this->initTextures();
  128.     this->frame.setPosition(x, y);
  129. }
  130.  
  131. void Enemy::updatePos(int posY) {
  132.     this->frame.setPosition(this->frame.getPosition().x, posY);
  133. }
  134.  
  135. void Enemy::update(int sp, int posY)
  136. {
  137.     this->frame.move(- (sp - this->speed), 0.f);
  138.     this->updatePos(posY + 300 + y);
  139.     this->updateWheels();
  140.     this->left_wh.rotate(this->speed * 0.6);
  141.     this->right_wh.rotate(this->speed * 0.6);
  142. }
  143.  
  144. void Enemy::render(sf::RenderTarget* target)
  145. {
  146.     target->draw(this->frame);
  147.     target->draw(this->left_wh);
  148.     target->draw(this->right_wh);
  149. }
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement