Guest User

No Polymorphism

a guest
Jun 18th, 2017
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. class Armor
  2. {
  3. private:
  4.     float body_size;
  5.     sf::Color body_color;
  6.     int defense;
  7. public:
  8.     Armor(float body_r, sf::Color c, int d) : body_size(body_r), body_color(c), defense(d) {}
  9.     float& GetBodySize() { return body_size; }
  10.     sf::Color& GetBodyColor() { return body_color; }
  11.     int& GetDefense() { return defense; }
  12. };
  13.  
  14. class Turret
  15. {
  16. private:
  17.     float rotor_radius;
  18.     sf::Color rot_col;
  19.     sf::Vector2f barrel_size;
  20.     sf::Color barrel_col;
  21. public:
  22.     Turret(float rr, sf::Color rc, sf::Vector2f bs, sf::Color bc)
  23.     : rotor_radius(rr), rot_col(rc), barrel_size(bs), barrel_col(bc) {}
  24.     float& GetRotorSize() { return rotor_radius; }
  25.     sf::Color& GetRotorColor() { return rot_col; }
  26.     sf::Vector2f& GetBarrelSize() { return barrel_size; }
  27.     sf::Color& GetBarrelColor() { return barrel_col; }
  28. };
  29.  
  30. class Robot
  31. {
  32. private:
  33.     sf::CircleShape bot_body;
  34.     Armor bot_armor;
  35.     sf::CircleShape turret_body;
  36.     sf::RectangleShape barrel_body;
  37.     Turret bot_turret;
  38.     float barrel_angle;
  39.     sf::Clock barrel_clock;
  40.     float current_x;
  41.     float current_y;
  42.     float velocity_x;
  43.     float velocity_y;
  44.     float max_velocity;
  45.     ParticleSystem particles;
  46. protected:
  47.     void BotUpdate(sf::Time t)
  48.     {
  49.         bot_body.setPosition(sf::Vector2f(current_x, current_y));
  50.         turret_body.setPosition(sf::Vector2f(current_x, current_y));
  51.         sf::Color reskin = turret_body.getFillColor();
  52.         sf::Uint8 alpha = (sf::Uint8)std::rand() % 255;
  53.         if (alpha > 127)
  54.             if (sf::Uint8(reskin.g + 5) < reskin.g)
  55.                 reskin.g = 255;
  56.             else reskin.g += 5;
  57.         else
  58.             if (sf::Uint8(reskin.g - 5) > reskin.g)
  59.                 reskin.g = 0;
  60.             else reskin.g -= 5;
  61.         turret_body.setFillColor(reskin);
  62.         barrel_body.setPosition(sf::Vector2f(current_x, current_y));
  63.         barrel_body.setRotation(barrel_angle);
  64.         particles.setEmitter(GetCircumferencePoint(barrel_body.getPosition(), barrel_body.getSize().x, barrel_angle));
  65.         particles.update(t);
  66.        
  67.     }
  68. public:
  69.     Robot(Armor a, Turret t, float cx, float cy): bot_armor(a), bot_turret(t), current_x(cx), current_y(cy), particles(1000)
  70.     {
  71.         bot_body.setRadius(bot_armor.GetBodySize());
  72.         bot_body.setFillColor(bot_armor.GetBodyColor());
  73.         bot_body.setOrigin(sf::Vector2f(bot_body.getRadius(), bot_body.getRadius()));
  74.         bot_body.setPosition(sf::Vector2f(current_x, current_y));
  75.  
  76.         turret_body.setRadius(bot_turret.GetRotorSize());
  77.         turret_body.setFillColor(bot_turret.GetRotorColor());
  78.         turret_body.setOrigin(sf::Vector2f(turret_body.getRadius(), turret_body.getRadius()));
  79.         turret_body.setPosition(sf::Vector2f(current_x, current_y));
  80.  
  81.         barrel_body.setSize(bot_turret.GetBarrelSize());
  82.         barrel_body.setFillColor(bot_turret.GetBarrelColor());
  83.         barrel_body.setOrigin(sf::Vector2f(0, barrel_body.getSize().y / 2));
  84.         barrel_body.setPosition(sf::Vector2f(current_x, current_y));
  85.  
  86.         barrel_angle = 90;
  87.         velocity_x = 0;
  88.         velocity_y = 0;
  89.         max_velocity = 300;
  90.     }
  91.     void DrawBot(sf::RenderWindow* w)
  92.     {
  93.         w->draw(bot_body);
  94.         w->draw(turret_body);
  95.         w->draw(barrel_body);
  96.         w->draw(particles);
  97.     }
  98.     void Update(sf::Time t)
  99.     {
  100.         BotUpdate(t);
  101.     }
  102.     void AccelerateX(float x)
  103.     {
  104.         velocity_x += x;
  105.         if (velocity_x > max_velocity) velocity_x = max_velocity;
  106.         else if (velocity_x < -max_velocity) velocity_x = -max_velocity;
  107.     }
  108.     void AccelerateY(float y)
  109.     {
  110.         velocity_y += y;
  111.         if (velocity_y > max_velocity) velocity_y = max_velocity;
  112.         else if (velocity_y < -max_velocity) velocity_y = -max_velocity;
  113.     }
  114.     sf::Vector2f GetVelocity() { return sf::Vector2f(velocity_x, velocity_y); }
  115.     sf::Vector2f GetPosition() { return sf::Vector2f(current_x, current_y); }
  116.     void SetPosition(float x, float y)
  117.     {
  118.         current_x = x;
  119.         current_y = y;
  120.     }
  121.     bool IsUnderAcceleration() { return (velocity_x != 0 || velocity_y != 0); }
  122.     void SetVelocityX(float v)
  123.     {
  124.         velocity_x = v;
  125.     }
  126.     void SetVelocityY(float v)
  127.     {
  128.         velocity_y = v;
  129.     }
  130.  
  131.     float& GetMaxVelocity() { return max_velocity;  }
  132.     void BarrelRPMOffset(float offset)
  133.     {
  134.         if (barrel_angle + offset > 360) barrel_angle -= 360;
  135.         else if (barrel_angle + offset < 0) barrel_angle += 360;
  136.         barrel_angle += offset;
  137.     }
  138.     float GetBarrelAngle() { return barrel_angle; }
  139. };
Advertisement
Add Comment
Please, Sign In to add comment