Advertisement
joker546645

Walking professor_no_hat

Mar 1st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include<chrono>
  3.  
  4. class Animation
  5. {
  6. public:
  7.     Animation ( ) = default;
  8.     Animation ( int x, int y, int width, int height )
  9.     {
  10.         texture.loadFromFile ( "professor_walk_cycle_no_hat.png" );
  11.         for (int i = 0; i < nFrames; i++)
  12.         {
  13.             frames[i] = { x + i * width,y ,width ,height };
  14.         }
  15.     }
  16.     void ApplyToSprite ( sf::Sprite& s, bool isMove ) const
  17.     {
  18.         s.setTexture ( texture );
  19.         if (isMove == true)
  20.             s.setTextureRect ( frames[iFrame] );
  21.         else
  22.             s.setTextureRect ( frames[0] );
  23.     }
  24.     void Update ( float dt )
  25.     {
  26.         time += dt;
  27.         while (time >= holdTime)
  28.         {
  29.             time -= holdTime;
  30.             Advance ( );
  31.         }
  32.     }
  33. private:
  34.     void Advance ( )
  35.     {
  36.         if (++iFrame >= nFrames)
  37.         {
  38.             iFrame = 1;
  39.         }
  40.     }
  41. private:
  42.     static constexpr int nFrames = 9;
  43.     static constexpr float holdTime = 0.08f;
  44.     sf::Texture texture;
  45.     sf::IntRect frames[nFrames];
  46.     int iFrame = 0;
  47.     float time = 0.0f;
  48. };
  49.  
  50. class Character
  51. {
  52. private:
  53.     enum class AnimationIndex
  54.     {
  55.         WalkingUp,
  56.         WalkingDown,
  57.         WalkingLeft,
  58.         WalkingRight,
  59.         Count
  60.     };
  61. public:
  62.     Character ( const sf::Vector2f& pos )
  63.         :
  64.         pos ( pos )
  65.     {
  66.         sprite.setTextureRect ( { 0,0,64,64 } );
  67.         animations[int ( AnimationIndex::WalkingUp )] = Animation ( 0, 0, 64, 64 );
  68.         animations[int ( AnimationIndex::WalkingDown )] = Animation ( 0, 128, 64, 64 );
  69.         animations[int ( AnimationIndex::WalkingLeft )] = Animation ( 0, 64, 64, 64 );
  70.         animations[int ( AnimationIndex::WalkingRight )] = Animation ( 0, 192, 64, 64 );
  71.     }
  72.     void Draw ( sf::RenderTarget& rt ) const
  73.     {
  74.         rt.draw ( sprite );
  75.     }
  76.     void SetDirection ( const sf::Vector2f& dir )
  77.     {
  78.         vel = dir * speed;
  79.         if (dir.x > 0.0f)
  80.         {
  81.             curAnimation = AnimationIndex::WalkingRight;
  82.         }
  83.         else if (dir.x < 0.0f)
  84.         {
  85.             curAnimation = AnimationIndex::WalkingLeft;
  86.         }
  87.         else if (dir.y < 0.0f)
  88.         {
  89.             curAnimation = AnimationIndex::WalkingUp;
  90.         }
  91.         else if (dir.y > 0.0f)
  92.         {
  93.             curAnimation = AnimationIndex::WalkingDown;
  94.         }
  95.     }
  96.     void Update ( float dt )
  97.     {
  98.         pos += vel * dt;
  99.         animations[int ( curAnimation )].Update ( dt );
  100.         if(vel.x == 0 && vel.y == 0)
  101.             animations[int ( curAnimation )].ApplyToSprite ( sprite, false );
  102.         else
  103.             animations[int ( curAnimation )].ApplyToSprite ( sprite, true );
  104.         sprite.setPosition ( pos );
  105.     }
  106. private:
  107.     static constexpr float speed = 100.0f;
  108.     sf::Vector2f pos;
  109.     sf::Vector2f vel = {0.0f,0.0f};
  110.     sf::Sprite sprite;
  111.     Animation animations[int ( AnimationIndex::Count )];
  112.     AnimationIndex curAnimation = AnimationIndex::WalkingDown;
  113. };
  114.  
  115.  
  116. int main()
  117. {
  118.     // Create the main window
  119.     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
  120.    
  121.     Character fucker ( { 100.0f,100.0f } );
  122.  
  123.     // timepoint 4 delta time measurement
  124.     auto tp = std::chrono::steady_clock::now ( );
  125.  
  126.  
  127.     // Start the game loop
  128.     while (window.isOpen())
  129.     {
  130.         // Process events
  131.         sf::Event event;
  132.         while (window.pollEvent(event))
  133.         {
  134.             // Close window: exit
  135.             if (event.type == sf::Event::Closed)
  136.                 window.close();
  137.         }
  138.  
  139.         // get dt
  140.         float dt;
  141.         {
  142.             const auto new_tp = std::chrono::steady_clock::now ( );
  143.             dt = std::chrono::duration<float> ( new_tp - tp ).count ( );
  144.             tp = new_tp;
  145.         }
  146.  
  147.  
  148.  
  149.         // handle input
  150.         sf::Vector2f dir = { 0.0f,0.0f };
  151.         if (sf::Keyboard::isKeyPressed ( sf::Keyboard::Up )) {
  152.             dir.y -= 1.0;
  153.         }
  154.         if (sf::Keyboard::isKeyPressed ( sf::Keyboard::Down )) {
  155.             dir.y += 1.0;
  156.         }
  157.         if (sf::Keyboard::isKeyPressed ( sf::Keyboard::Left )) {
  158.             dir.x -= 1.0;
  159.         }
  160.         if (sf::Keyboard::isKeyPressed ( sf::Keyboard::Right )) {
  161.             dir.x += 1.0;
  162.         }
  163.         fucker.SetDirection ( dir );
  164.  
  165.         //update fucker
  166.         fucker.Update ( dt );
  167.  
  168.  
  169.  
  170.  
  171.         // Clear screen
  172.         window.clear();
  173.         // Draw the sprite
  174.         fucker.Draw ( window );
  175.         // Update the window
  176.         window.display();
  177.     }
  178.     return EXIT_SUCCESS;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement