tranerius

1) SFML создание персонажа, анимации, карты

Apr 1st, 2018
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.78 KB | None | 0 0
  1. /*
  2.     level_1_97x97.png: https://image.ibb.co/caKMzx/level_1_97x97.png
  3.     uganda_textures_one_texture_144x164.png: https://image.ibb.co/h9YP2H/uganda_textures_one_texture_144x164.png
  4. */
  5.  
  6. #include <SFML/Graphics.hpp>
  7. #include <iostream>
  8. #include <string>
  9.  
  10. enum Direction {
  11.     Forward,
  12.     Backward,
  13.     Stand
  14. };
  15.  
  16. class Character {
  17. protected:
  18.     sf::Vector2u window_size;
  19.     std::string path_to_texture;
  20.     sf::Image image;
  21.     sf::Texture texture;
  22.     sf::Sprite sprite;
  23.     int texture_width, texture_height;
  24.     int texture_number;
  25.  
  26.     class Animation {
  27.     public:
  28.         double texture;
  29.         int texture_number;
  30.         int start_move_texture;
  31.         bool is_last = false;
  32.         Animation(int texture_number, int start_move_texture) {
  33.             this->texture_number = texture_number;
  34.             texture = start_move_texture;
  35.             this->start_move_texture = start_move_texture;
  36.         }
  37.         int GetCharacterTexture(double time) {
  38.             if (is_last) {
  39.                 texture = start_move_texture;
  40.                 is_last = false;
  41.             }
  42.             if (texture < start_move_texture) {
  43.                 texture = start_move_texture;
  44.             }
  45.             texture += (time / 100000);
  46.             if (texture >= texture_number) {
  47.                 texture = texture_number - 1;
  48.                 is_last = true;
  49.             }
  50.             return texture;
  51.         }
  52.     };
  53.  
  54.     Animation *character_animation;
  55.     sf::Clock clock;
  56.     double time = 1;
  57.     double character_speed = 1;
  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. public:
  66.     Character(std::string path_to_texture, int texture_width, int texture_height, int start_move_texture, sf::Vector2f start_position, sf::Vector2u window_size) {
  67.         this->path_to_texture = path_to_texture;
  68.         image.loadFromFile(path_to_texture);
  69.         image.createMaskFromColor(sf::Color(0, 14, 88));
  70.         texture.loadFromImage(image);
  71.         sprite.setTexture(texture);
  72.         sprite.setPosition(start_position);
  73.         character_dimensions.x = this->texture_width = texture_width;
  74.         character_dimensions.y = this->texture_height = texture_height;
  75.         this->window_size = window_size;
  76.         SetTextureForSprite(1, 1);
  77.         texture_number = image.getSize().x / texture_width;
  78.         character_animation = new Animation(texture_number, start_move_texture);
  79.     }
  80.     ~Character() {
  81.         if (character_animation != nullptr) {
  82.             delete character_animation;
  83.         }
  84.     }
  85.     virtual sf::Sprite GetSprite() const {
  86.         return sprite;
  87.     }
  88.     virtual void SetTextureForSprite(int x, int y) {
  89.         sprite.setTextureRect(sf::IntRect(x, y, texture_width, texture_height));
  90.     }
  91.     virtual void SetCharacterSpeed(double character_speed) {
  92.         if (character_speed >= 0) {
  93.             this->character_speed = character_speed;
  94.         }
  95.     }
  96.     virtual double GetCharacterSpeed() const {
  97.         return character_speed;
  98.     }
  99.     virtual void SetTime() {
  100.         time = clock.getElapsedTime().asMicroseconds();
  101.         clock.restart();
  102.     }
  103.     void CharacterDirection(Direction direction) {
  104.         if (direction == Direction::Forward) {
  105.             sprite.setOrigin(0, 0);
  106.             sprite.setScale(character_dimensions.x / texture_width, character_dimensions.y / texture_height);
  107.         }
  108.         else if (direction == Direction::Backward) {
  109.             sprite.setOrigin(texture_width, 0);
  110.             sprite.setScale(-character_dimensions.x / texture_width, character_dimensions.y / texture_height);
  111.         }
  112.     }
  113.     virtual void Move(Direction direction) {
  114.         if (direction != Direction::Stand) {
  115.             character_last_position = sprite.getPosition();
  116.             if (direction == Direction::Forward) {
  117.                 CharacterDirection(direction);
  118.                 sprite.move(character_speed*time * 0.00003, 0);
  119.             }
  120.             else if (direction == Direction::Backward) {
  121.                 CharacterDirection(direction);
  122.                 sprite.move(-(character_speed * time * 0.00003), 0);
  123.             }
  124.             sprite.setTextureRect(sf::IntRect((character_animation->GetCharacterTexture(time)*(texture_width + 2)) + 1, 1, texture_width, texture_height));
  125.             BehindTheMap();
  126.         }
  127.         else {
  128.             sprite.setTextureRect(sf::IntRect(1, 1, texture_width, texture_height));
  129.             character_animation->texture = 1;
  130.         }
  131.     }
  132.     virtual void SetCharacterScale(double width_div, double height_div) {
  133.         sprite.setScale(1 / width_div, 1 / height_div);
  134.         character_dimensions.x /= width_div;
  135.         character_dimensions.y /= height_div;
  136.     }
  137.     virtual sf::Vector2f GetCharacterDimensions() const {
  138.         return character_dimensions;
  139.     }
  140. };
  141.  
  142. class UgandaKnuckles : public Character {
  143. public:
  144.     UgandaKnuckles(std::string path_to_texture, int texture_width, int texture_height, int start_move_texture, sf::Vector2f start_position, sf::Vector2u window_size)
  145.         :Character(path_to_texture, texture_width, texture_height, start_move_texture, start_position, window_size) {}
  146. };
  147.  
  148. class Map {
  149. protected:
  150.     sf::Vector2u window_size;
  151.     std::string path_to_map_textures;
  152.     sf::Image image;
  153.     sf::Texture texture;
  154.     sf::Sprite sprite;
  155. public:
  156.     Map(std::string path_to_map_textures, sf::Vector2u window_size) {
  157.         this->path_to_map_textures = path_to_map_textures;
  158.         this->window_size = window_size;
  159.         image.loadFromFile(path_to_map_textures);
  160.         image.createMaskFromColor(sf::Color(0, 14, 88));
  161.         texture.loadFromImage(image);
  162.         sprite.setTexture(texture);
  163.     }
  164.     virtual void SetSprite(int x, int y, int texture_width, int texture_height) {
  165.         if (x < image.getSize().x && y < image.getSize().y) {
  166.             sprite.setTextureRect(sf::IntRect(x, y, texture_width, texture_height));
  167.         }
  168.     }
  169.     virtual sf::Sprite GetSprite() const {
  170.         return sprite;
  171.     }
  172.     virtual void SetPosition(double x, double y) {
  173.         sprite.setPosition(x, y);
  174.     }
  175.     int GetImageWidth() {
  176.         return image.getSize().x;
  177.     }
  178.     int GetImageHeight() {
  179.         return image.getSize().y;
  180.     }
  181. };
  182.  
  183. class Level1 : public Map {
  184. public:
  185.     Level1(std::string path_to_map_textures, sf::Vector2u window_size)
  186.         :Map(path_to_map_textures, window_size) {}
  187.  
  188. };
  189.  
  190. int main() {
  191.     setlocale(LC_ALL, "ru");
  192.     sf::RenderWindow window(sf::VideoMode(1000, 285), "Test");
  193.     sf::Event window_event;
  194.     Map *level1 = new Level1("level_1_97x97.png", window.getSize());
  195.     Character *main_character = new UgandaKnuckles("uganda_textures_one_texture=144x164.png", 144, 164, 2, sf::Vector2f(0, 135), window.getSize());
  196.     main_character->SetCharacterSpeed(7);
  197.     main_character->SetCharacterScale(2.5, 2.5);
  198.     bool stand = true;
  199.     //std::cout << "Размеры персонажа " << main_character->GetCharacterDimensions().x << "x" << main_character->GetCharacterDimensions().y << std::endl;
  200.     while (window.isOpen()) {
  201.         main_character->SetTime();
  202.         if (window.pollEvent(window_event)) {
  203.             if (window_event.type == sf::Event::Closed) {
  204.                 window.close();
  205.             }
  206.         }
  207.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
  208.             main_character->Move(Direction::Stand);
  209.         }
  210.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
  211.             main_character->Move(Direction::Forward);
  212.             stand = false;
  213.         }
  214.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
  215.             main_character->Move(Direction::Backward);
  216.             stand = false;
  217.         }
  218.         if (stand) {
  219.             main_character->Move(Direction::Stand);
  220.         }
  221.         stand = true;
  222.         window.clear();
  223.         level1->SetSprite(1, 1, 97, 97); //спрайт неба
  224.         for (int i = 0, x = -20; i != 11; ++i, x += 97) {
  225.             level1->SetPosition(x, 0);
  226.             window.draw(level1->GetSprite());
  227.             level1->SetPosition(x, 97);
  228.             window.draw(level1->GetSprite());
  229.         }
  230.         level1->SetSprite(98, 1, 144, 141); //спрайт солнца
  231.         level1->SetPosition(800, 30);
  232.         window.draw(level1->GetSprite());
  233.         level1->SetSprite(1, 98, 97, 97); //спрайт земли
  234.         for (int i = 0, x = -20; i != 11; ++i, x += 97) {
  235.             level1->SetPosition(x, 192);
  236.             window.draw(level1->GetSprite());
  237.         }
  238.         window.draw(main_character->GetSprite());
  239.         window.display();
  240.     }
  241.     delete level1;
  242.     delete main_character;
  243.     return 0;
  244. }
Advertisement
Add Comment
Please, Sign In to add comment