Advertisement
Felanpro

my beautiful world (update)

Apr 25th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.05 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4.  
  5. using namespace std;
  6.  
  7. unsigned int width = 500;
  8. unsigned int height = 500;
  9.  
  10. unsigned int direction = 0;
  11. int view_direction_adder_x;
  12. int view_direction_adder_y;
  13.  
  14. int main()
  15. {
  16.     int map[10][30] =
  17.     {
  18.         {2, 3, 2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 2, 2, 2, 3, 2, 3, 3, 3, 2, 2, 2, 2},
  19.         {2, 2, 2, 2, 2, 2, 2, 3, 4, 4, 4, 2, 2, 2, 2, 2, 2, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 2, 2},
  20.         {2, 2, 2, 3, 2, 3, 2, 2, 4, 4, 4, 2, 2, 3, 2, 3, 2, 2, 2, 2, 2, 2, 2, 3, 2, 3, 2, 2, 2, 2},
  21.         {2, 2, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2},
  22.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  23.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  24.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  25.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  26.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
  27.         {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
  28.     };
  29.  
  30.     sf::Vector2f view_direction(0, 0);
  31.  
  32.     sf::RenderWindow window(sf::VideoMode(width, height), "In Progress");
  33.     window.setVerticalSyncEnabled(true);
  34.     window.setFramerateLimit(70);
  35.  
  36.     sf::Texture sprite_sheet;
  37.     sprite_sheet.loadFromFile("mainguy.png");
  38.  
  39.     sf::Sprite sprite;
  40.     sprite.setTexture(sprite_sheet);
  41.     sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
  42.     sprite.setPosition(width/2, (height/2)); //Initial position of the sprite
  43.     sprite.setOrigin(41 / 2, 36 / 2);
  44.  
  45.     sf::RectangleShape sky(sf::Vector2f(50, 50));
  46.     sky.setFillColor(sf::Color::Cyan);
  47.  
  48.     sf::RectangleShape ground(sf::Vector2f(50, 50));
  49.     ground.setFillColor(sf::Color::Green);
  50.  
  51.     sf::RectangleShape cloud(sf::Vector2f(50, 50));
  52.     cloud.setFillColor(sf::Color::White);
  53.  
  54.     sf::RectangleShape building(sf::Vector2f(50, 50));
  55.     building.setFillColor(sf::Color::Cyan);
  56.  
  57.     //Don't give this object any color
  58.     sf::RectangleShape tester(sf::Vector2f(50, 50));
  59.  
  60.     sf::Time time = sf::seconds(0);
  61.     sf::Clock clock;
  62.  
  63.     sf::Event event;
  64.     while (window.isOpen())
  65.     {
  66.         view_direction_adder_x = 0;
  67.         view_direction_adder_y = 0;
  68.  
  69.         while (window.pollEvent(event))
  70.         {
  71.             if (event.type == sf::Event::Closed)
  72.             {
  73.                 window.close();
  74.             }
  75.         }
  76.  
  77.         //Handle other things
  78.  
  79.         //Gather input
  80.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  81.         {
  82.             if (direction != 1)
  83.             {
  84.                 clock.restart();
  85.             }
  86.             cout << "Walking right" << endl;
  87.             direction = 1;
  88.         }
  89.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  90.         {
  91.             if (direction != 2)
  92.             {
  93.                 clock.restart();
  94.             }
  95.             cout << "Walking left" << endl;
  96.             direction = 2;
  97.         }
  98.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  99.         {
  100.             if (direction != 3)
  101.             {
  102.                 clock.restart();
  103.             }
  104.             cout << "Walking up" << endl;
  105.             direction = 3;
  106.         }
  107.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  108.         {
  109.             if (direction != 4)
  110.             {
  111.                 clock.restart();
  112.             }
  113.             cout << "Walking down" << endl;
  114.             direction = 4;
  115.         }
  116.         else
  117.         {
  118.             cout << "not moving" << endl;
  119.             direction = 0;
  120.         }
  121.  
  122.         //Execute animations
  123.         if (direction == 1)
  124.         {
  125.             //sprite.move(4, 0);
  126.             time = clock.getElapsedTime();
  127.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  128.             {
  129.                 sprite.setTextureRect(sf::IntRect(0, 36, 41, 36));
  130.             }
  131.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  132.             {
  133.                 sprite.setTextureRect(sf::IntRect(41, 36, 41, 36));
  134.             }
  135.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  136.             {
  137.                 sprite.setTextureRect(sf::IntRect(41 * 2, 36, 41, 36));
  138.             }
  139.             else if (time.asSeconds() > 1.5)
  140.             {
  141.                 clock.restart();
  142.             }
  143.             view_direction_adder_x += -10;
  144.         }
  145.         else if (direction == 2)
  146.         {
  147.             //sprite.move(-4, 0);
  148.             time = clock.getElapsedTime();
  149.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  150.             {
  151.                 sprite.setTextureRect(sf::IntRect(0, 36 * 3, 41, 36));
  152.             }
  153.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  154.             {
  155.                 sprite.setTextureRect(sf::IntRect(41, 36 * 3, 41, 36));
  156.             }
  157.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  158.             {
  159.                 sprite.setTextureRect(sf::IntRect(41 * 2, 36 * 3, 41, 36));
  160.             }
  161.             else if (time.asSeconds() > 1.5)
  162.             {
  163.                 clock.restart();
  164.             }
  165.             view_direction_adder_x += 10;
  166.         }
  167.         else if (direction == 3)
  168.         {
  169.             //sprite.move(0, -4);
  170.             time = clock.getElapsedTime();
  171.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  172.             {
  173.                 sprite.setTextureRect(sf::IntRect(41, 36 * 2, 41, 36));
  174.             }
  175.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  176.             {
  177.                 sprite.setTextureRect(sf::IntRect(0, 36 * 2, 41, 36));
  178.             }
  179.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  180.             {
  181.                 sprite.setTextureRect(sf::IntRect(41 * 2, 36 * 2, 41, 36));
  182.             }
  183.             else if (time.asSeconds() > 1.5)
  184.             {
  185.                 clock.restart();
  186.             }
  187.             view_direction_adder_y += 10;
  188.         }
  189.         else if (direction == 4)
  190.         {
  191.             //sprite.move(0, 4);
  192.             time = clock.getElapsedTime();
  193.             if (time.asSeconds() >= 0 && time.asSeconds() <= .5)
  194.             {
  195.                 sprite.setTextureRect(sf::IntRect(0, 0, 41, 36));
  196.             }
  197.             else if (time.asSeconds() > .5 && time.asSeconds() <= 1)
  198.             {
  199.                 sprite.setTextureRect(sf::IntRect(41 * 2, 0, 41, 36));
  200.             }
  201.             else if (time.asSeconds() > 1 && time.asSeconds() <= 1.5)
  202.             {
  203.                 sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
  204.             }
  205.             else if (time.asSeconds() > 1.5)
  206.             {
  207.                 clock.restart();
  208.             }
  209.             view_direction_adder_y += -10;
  210.         }
  211.         else if (direction == 0)
  212.         {
  213.             sprite.setTextureRect(sf::IntRect(41, 0, 41, 36));
  214.         }
  215.  
  216.         //Check collision/Height limit
  217.         /*
  218.         if (sprite.getPosition().y < 200)
  219.         {
  220.             sprite.setPosition(sprite.getPosition().x, 200);
  221.         }
  222.         */
  223.  
  224.         for (int mapY = 0; mapY < 10; mapY++)
  225.         {
  226.             for (int mapX = 0; mapX < 30; mapX++)
  227.             {
  228.                 sf::FloatRect tester_boundingBox = tester.getGlobalBounds();
  229.                 if (map[mapY][mapX] == 1)
  230.                 {
  231.                     tester.setPosition(mapX * 50, mapY * 50);
  232.                     tester.move(view_direction);
  233.                     if (tester_boundingBox.intersects(sprite.getGlobalBounds()))
  234.                         cout << "Player is on ground" << endl;
  235.                 }
  236.                 else if (map[mapY][mapX] == 2)
  237.                 {
  238.                     tester.setPosition(mapX * 50, mapY * 50);
  239.                     tester.move(view_direction);
  240.                     if (tester_boundingBox.intersects(sprite.getGlobalBounds()))
  241.                     {
  242.                         cout << "Player is on sky" << endl;
  243.                         if (direction == 3)
  244.                         {
  245.                             sprite.setPosition(sprite.getPosition().x, tester.getPosition().y + 54);
  246.                             view_direction_adder_x = 0;
  247.                             view_direction_adder_y = 0;
  248.                         }
  249.                     }
  250.                 }
  251.                 else if (map[mapY][mapX] == 3)
  252.                 {
  253.                     tester.setPosition(mapX * 50, mapY * 50);
  254.                     tester.move(view_direction);
  255.                     if (tester_boundingBox.intersects(sprite.getGlobalBounds()))
  256.                         cout << "Player is on cloud" << endl;
  257.                 }
  258.             }
  259.         }
  260.  
  261.         view_direction.x += view_direction_adder_x;
  262.         view_direction.y += view_direction_adder_y;
  263.  
  264.         window.clear(sf::Color::Black); //Clear
  265.  
  266.         //Here we draw everything
  267.         for (int mapY = 0; mapY < 10; mapY++)
  268.         {
  269.             for (int mapX = 0; mapX < 30; mapX++)
  270.             {
  271.                 if (map[mapY][mapX] == 1)
  272.                 {
  273.                     ground.setPosition(mapX * 50, mapY * 50);
  274.                     ground.move(view_direction);
  275.                     window.draw(ground);
  276.                 }
  277.                 else if (map[mapY][mapX] == 2)
  278.                 {
  279.                     sky.setPosition(mapX * 50, mapY * 50);
  280.                     sky.move(view_direction);
  281.                     window.draw(sky);
  282.                 }
  283.                 else if (map[mapY][mapX] == 3)
  284.                 {
  285.                     cloud.setPosition(mapX * 50, mapY * 50);
  286.                     cloud.move(view_direction);
  287.                     window.draw(cloud);
  288.                 }
  289.                 else if (map[mapY][mapX] == 3)
  290.                 {
  291.                     building.setPosition(mapX * 50, mapY * 50);
  292.                     building.move(view_direction);
  293.                     window.draw(building);
  294.                 }
  295.             }
  296.         }
  297.  
  298.         window.draw(sprite);
  299.  
  300.         window.display(); //Display
  301.  
  302.         //Display important calculations
  303.         //cout << "view_direction.y: " << view_direction.y << " " << "view_direction.x: " << view_direction.x << endl;
  304.     }
  305.  
  306.     int pause; cin >> pause;
  307.     return 0;
  308. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement