tranerius

2) 1 + гравитация

Apr 3rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.02 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. enum Direction {
  6.     Forward = 1,
  7.     Backward = -1,
  8.     Up = -1,
  9.     Down = 1,
  10.     Stand = 0
  11. };
  12.  
  13. class Character {
  14. protected:
  15.     sf::Vector2u window_size;
  16.     std::string path_to_texture;
  17.     sf::Image image;
  18.     sf::Texture texture;
  19.     sf::Sprite sprite;
  20.     int texture_width, texture_height;
  21.     int texture_number;
  22.     class Animation {
  23.     public:
  24.         double texture;
  25.         int texture_number;
  26.         int start_move_texture;
  27.         bool is_last = false;
  28.         Animation(int texture_number, int start_move_texture) {
  29.             this->texture_number = texture_number;
  30.             texture = start_move_texture;
  31.             this->start_move_texture = start_move_texture;
  32.         }
  33.         int GetCharacterTexture(double time) {
  34.             if (is_last) {
  35.                 texture = start_move_texture;
  36.                 is_last = false;
  37.             }
  38.             if (texture < start_move_texture) {
  39.                 texture = start_move_texture;
  40.             }
  41.             texture += (time / 100000);
  42.             if (texture >= texture_number) {
  43.                 texture = texture_number - 1;
  44.                 is_last = true;
  45.             }
  46.             return texture;
  47.         }
  48.     };
  49.     Animation *character_animation;
  50.     sf::Clock clock;
  51.     double time = 1; //время 1 итерации цикла while (window.isOpen()) в функции main
  52.     double character_speed = 1;
  53.     double character_weight = 1;
  54.     bool jump = false; //при нажатии стрелки вверх = true
  55.     bool gravity_to_ground = false; //при start_jump_momentum = 0 gravity_to_ground = true
  56.     double before_jump_y_position, jump_momentum;
  57.     sf::Clock jump_time;
  58.     sf::Vector2f character_last_position;
  59.     sf::Vector2f character_dimensions; //габариты персонажа
  60.     void BehindTheMap() {
  61.         if (sprite.getPosition().x > window_size.x - character_dimensions.x || sprite.getPosition().x < 0) {
  62.             sprite.setPosition(character_last_position.x, character_last_position.y);
  63.         }
  64.     }
  65.     void Jump() {
  66.         if (!gravity_to_ground) {
  67.             if (jump_momentum > 0) {
  68.                 jump_momentum -= 9.8 * jump_time.getElapsedTime().asMicroseconds() * 0.00001 * character_weight;
  69.                 sprite.move(0, -jump_momentum* 0.000025);
  70.             }
  71.             else {
  72.                 gravity_to_ground = true;
  73.                 jump_momentum = 0;
  74.                 jump_time.restart();
  75.             }
  76.         }
  77.         else {
  78.             if (before_jump_y_position > sprite.getPosition().y) {
  79.                 jump_momentum += 9.8 * jump_time.getElapsedTime().asMicroseconds() * 0.00001 * character_weight;
  80.                 sprite.move(0, jump_momentum*0.000025);
  81.             }
  82.             else {
  83.                 gravity_to_ground = false;
  84.                 jump = false;
  85.                 jump_momentum = 0;
  86.                 sprite.setPosition(sprite.getPosition().x, before_jump_y_position);
  87.             }
  88.         }
  89.     }
  90. public:
  91.     Character(std::string path_to_texture, int texture_width, int texture_height, int start_move_texture, sf::Vector2f start_position, sf::Vector2u window_size) {
  92.         this->path_to_texture = path_to_texture;
  93.         image.loadFromFile(path_to_texture);
  94.         image.createMaskFromColor(sf::Color(0, 14, 88));
  95.         texture.loadFromImage(image);
  96.         sprite.setTexture(texture);
  97.         sprite.setPosition(start_position);
  98.         character_dimensions.x = this->texture_width = texture_width;
  99.         character_dimensions.y = this->texture_height = texture_height;
  100.         this->window_size = window_size;
  101.         SetTextureForSprite(1, 1);
  102.         texture_number = image.getSize().x / texture_width;
  103.         character_animation = new Animation(texture_number, start_move_texture);
  104.     }
  105.     ~Character() {
  106.         if (character_animation != nullptr) {
  107.             delete character_animation;
  108.         }
  109.     }
  110.     virtual sf::Sprite GetSprite() const {
  111.         return sprite;
  112.     }
  113.     virtual void SetTextureForSprite(int x, int y) {
  114.         sprite.setTextureRect(sf::IntRect(x, y, texture_width, texture_height));
  115.     }
  116.     virtual void SetCharacterSpeed(double character_speed) {
  117.         if (character_speed >= 0) {
  118.             this->character_speed = character_speed / character_weight;
  119.         }
  120.     }
  121.     virtual double GetCharacterSpeed() const {
  122.         return character_speed;
  123.     }
  124.     virtual void SetCharacterWeight(double character_weight) {
  125.         if (character_weight > 0) {
  126.             this->character_weight = character_weight / 70;
  127.             character_speed /= this->character_weight;
  128.         }
  129.     }
  130.     virtual double GetCharacterWeight() const {
  131.         return character_weight;
  132.     }
  133.     virtual void SetTime() {
  134.         if (jump) {
  135.             Jump();
  136.         }
  137.         time = clock.getElapsedTime().asMicroseconds();
  138.         clock.restart();
  139.     }
  140.     void CharacterDirection(Direction direction) {
  141.         if (direction == Direction::Forward) {
  142.             sprite.setOrigin(0, 0);
  143.             sprite.setScale(character_dimensions.x / texture_width, character_dimensions.y / texture_height);
  144.         }
  145.         else if (direction == Direction::Backward) {
  146.             sprite.setOrigin(texture_width, 0);
  147.             sprite.setScale(-character_dimensions.x / texture_width, character_dimensions.y / texture_height);
  148.         }
  149.     }
  150.     virtual void Move(Direction horizontal_direction, Direction vertical_direction = Direction::Stand) {
  151.         if (horizontal_direction || vertical_direction != Stand) {
  152.             character_last_position = sprite.getPosition();
  153.             if (horizontal_direction == Forward || Backward) {
  154.                 CharacterDirection(horizontal_direction);
  155.             }
  156.             if (vertical_direction == Up) {
  157.                 if (!jump) {
  158.                     before_jump_y_position = sprite.getPosition().y;
  159.                     jump_momentum = character_speed * 1000 * character_weight;  
  160.                     jump_time.restart();
  161.                     jump = true;
  162.                 }
  163.             }
  164.             sprite.move(horizontal_direction*character_speed*time * 0.000025, 0);
  165.             sprite.setTextureRect(sf::IntRect((character_animation->GetCharacterTexture(time * (character_speed / 7))*(texture_width + 2)) + 1, 1, texture_width, texture_height));
  166.             BehindTheMap();
  167.         }
  168.         else {
  169.             sprite.setTextureRect(sf::IntRect(1, 1, texture_width, texture_height));
  170.             character_animation->texture = 1;
  171.         }
  172.     }
  173.     virtual void SetCharacterScale(double width_div, double height_div) {
  174.         sprite.setScale(1 / width_div, 1 / height_div);
  175.         character_dimensions.x /= width_div;
  176.         character_dimensions.y /= height_div;
  177.     }
  178.     virtual sf::Vector2f GetCharacterDimensions() const {
  179.         return character_dimensions;
  180.     }
  181. };
  182.  
  183. class UgandaKnuckles : public Character {
  184. public:
  185.     UgandaKnuckles(std::string path_to_texture, int texture_width, int texture_height, int start_move_texture, sf::Vector2f start_position, sf::Vector2u window_size)
  186.         :Character(path_to_texture, texture_width, texture_height, start_move_texture, start_position, window_size) {}
  187. };
  188.  
  189. class Map {
  190. protected:
  191.     sf::Vector2u window_size;
  192.     std::string path_to_map_textures;
  193.     sf::Image image;
  194.     sf::Texture texture;
  195.     sf::Sprite sprite;
  196. public:
  197.     Map(std::string path_to_map_textures, sf::Vector2u window_size) {
  198.         this->path_to_map_textures = path_to_map_textures;
  199.         this->window_size = window_size;
  200.         image.loadFromFile(path_to_map_textures);
  201.         image.createMaskFromColor(sf::Color(0, 14, 88));
  202.         texture.loadFromImage(image);
  203.         sprite.setTexture(texture);
  204.     }
  205.     virtual void SetSprite(int x, int y, int texture_width, int texture_height) {
  206.         if (x < image.getSize().x && y < image.getSize().y) {
  207.             sprite.setTextureRect(sf::IntRect(x, y, texture_width, texture_height));
  208.         }
  209.     }
  210.     virtual sf::Sprite GetSprite() const {
  211.         return sprite;
  212.     }
  213.     virtual void SetPosition(double x, double y) {
  214.         sprite.setPosition(x, y);
  215.     }
  216.     int GetImageWidth() {
  217.         return image.getSize().x;
  218.     }
  219.     int GetImageHeight() {
  220.         return image.getSize().y;
  221.     }
  222. };
  223.  
  224. class Level1 : public Map {
  225. public:
  226.     Level1(std::string path_to_map_textures, sf::Vector2u window_size)
  227.         :Map(path_to_map_textures, window_size) {}
  228.  
  229. };
  230.  
  231. void CharacterManagement(Character*&object, bool&stand) {
  232.     if ((sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down))) {
  233.         object->Move(Direction::Stand);
  234.     }
  235.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
  236.         object->Move(Direction::Forward, Direction::Up);
  237.         stand = false;
  238.     }
  239.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
  240.         object->Move(Direction::Backward, Direction::Up);
  241.         stand = false;
  242.     }
  243.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
  244.         object->Move(Direction::Forward);
  245.         stand = false;
  246.     }
  247.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
  248.         object->Move(Direction::Backward);
  249.         stand = false;
  250.     }
  251.     else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
  252.         object->Move(Direction::Stand, Direction::Up);
  253.         stand = false;
  254.     }
  255. }
  256.  
  257. int main() {
  258.     setlocale(LC_ALL, "ru");
  259.     sf::RenderWindow window(sf::VideoMode(1000, 285), "Uganda adventure");
  260.     sf::Event window_event;
  261.     Map *level1 = new Level1("level_1_97x97.png", window.getSize());
  262.     Character *main_character = new UgandaKnuckles("uganda_textures_one_texture=144x164.png", 144, 164, 2, sf::Vector2f(0, 135), window.getSize());
  263.     main_character->SetCharacterSpeed(7); //7 - средняя скорость
  264.     main_character->SetCharacterWeight(70); //70 - средний вес
  265.     main_character->SetCharacterScale(2.5, 2.5);
  266.     bool stand = true;
  267.     //std::cout << "Размеры персонажа " << main_character->GetCharacterDimensions().x << "x" << main_character->GetCharacterDimensions().y << std::endl;
  268.     while (window.isOpen()) {
  269.         main_character->SetTime();
  270.         if (window.pollEvent(window_event)) {
  271.             if (window_event.type == sf::Event::Closed) {
  272.                 window.close();
  273.             }
  274.         }
  275.         CharacterManagement(main_character, stand);
  276.         if (stand) {
  277.             main_character->Move(Direction::Stand);
  278.         }
  279.         stand = true;
  280.         window.clear();
  281.         level1->SetSprite(1, 1, 97, 97); //спрайт неба
  282.         for (int i = 0, x = -20; i != 11; ++i, x += 97) {
  283.             level1->SetPosition(x, 0);
  284.             window.draw(level1->GetSprite());
  285.             level1->SetPosition(x, 97);
  286.             window.draw(level1->GetSprite());
  287.         }
  288.         level1->SetSprite(98, 1, 144, 141); //спрайт солнца
  289.         level1->SetPosition(800, 30);
  290.         window.draw(level1->GetSprite());
  291.         level1->SetSprite(1, 98, 97, 97); //спрайт земли
  292.         for (int i = 0, x = -20; i != 11; ++i, x += 97) {
  293.             level1->SetPosition(x, 192);
  294.             window.draw(level1->GetSprite());
  295.         }
  296.         window.draw(main_character->GetSprite());
  297.         window.display();
  298.     }
  299.     delete level1;
  300.     delete main_character;
  301.     return 0;
  302. }
Advertisement
Add Comment
Please, Sign In to add comment