Guest User

Untitled

a guest
Dec 18th, 2014
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.97 KB | None | 0 0
  1. #ifdef SFML_STATIC
  2. #pragma comment(lib, "glew.lib")
  3. #pragma comment(lib, "freetype.lib")
  4. #pragma comment(lib, "jpeg.lib")
  5. #pragma comment(lib, "opengl32.lib")
  6. #pragma comment(lib, "winmm.lib")
  7. #pragma comment(lib, "gdi32.lib")  
  8. #endif // SFML_STATIC
  9.  
  10. #include <SFML/Graphics.hpp>
  11. #include <iostream>
  12. #include <cmath>
  13. using namespace std;
  14.  
  15. class GameObject
  16. {
  17. public:
  18.     enum Object_type
  19.     {
  20.         player,
  21.         tile
  22.     };
  23.     ~GameObject()
  24.     {
  25.     }
  26.  
  27.     virtual void Update()
  28.     {
  29.         GetLastPosition();
  30.     }
  31.  
  32.     void Draw(sf::RenderWindow& Window)
  33.     {
  34.         Window.draw(shape);
  35.     }
  36.     sf::RectangleShape shape;
  37.  
  38.     Object_type object_type;
  39.     sf::Vector2f velocity;
  40.     sf::Vector2f lastPosition;
  41.     sf::Vector2f lastvelocity;
  42.     int loopcounter;
  43.     bool canjump;
  44.     float speed;
  45.  
  46. protected:
  47.     void GetLastPosition()
  48.     {
  49.         loopcounter++;
  50.         if (loopcounter == 2)
  51.         {
  52.             lastPosition = shape.getPosition();
  53.             lastvelocity = velocity;
  54.             loopcounter = 0;
  55.         }
  56.     }
  57. };
  58.  
  59. class Player : public GameObject
  60. {
  61. public:
  62.     Player()
  63.     {
  64.         object_type = player;
  65.         speed = .1;
  66.         loopcounter = 0;
  67.         canjump = false;
  68.  
  69.         shape.setSize(sf::Vector2f(75, 75));
  70.         shape.setFillColor(sf::Color::Blue);
  71.  
  72.     }
  73.  
  74.     ~Player()
  75.     {
  76.     }
  77.  
  78.     void Update()
  79.     {
  80.         GetLastPosition();
  81.         shape.move(velocity.x, velocity.y);
  82.     }
  83. };
  84.  
  85. class Tile : public GameObject
  86. {
  87. public:
  88.     enum Tile_type
  89.     {
  90.         ground
  91.     };
  92.     Tile(sf::Vector2f new_pos, Tile_type new_type)
  93.     {
  94.         tile_type = new_type;
  95.         object_type = tile;
  96.  
  97.         shape.setSize(sf::Vector2f(125, 125));
  98.         shape.setPosition(new_pos);
  99.  
  100.         if (tile_type == ground)
  101.         {
  102.             shape.setFillColor(sf::Color::Green);
  103.         }
  104.     }
  105.     ~Tile()
  106.     {
  107.  
  108.     };
  109.  
  110.     Tile_type tile_type;
  111.  
  112. };
  113.  
  114. class Camera
  115. {
  116. public:
  117.     Camera(Player* player)
  118.     {
  119.         view.setSize(sf::Vector2f(1024, 768));
  120.         view.setCenter(player->shape.getPosition());
  121.     }
  122.     ~Camera()
  123.     {
  124.  
  125.     }
  126.  
  127.     void Update(Player* player)
  128.     {
  129.         view.setCenter(player->shape.getPosition());
  130.     }
  131.     void Draw(sf::RenderWindow& Window)
  132.     {
  133.         Window.setView(view);
  134.     }
  135.     sf::View view;
  136.  
  137. };
  138.  
  139. void MovePlayer(Player* player)
  140. {
  141.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  142.         player->shape.move(-player->speed, 0);
  143.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
  144.         player->shape.move(player->speed, 0);
  145.  
  146.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && player->canjump)
  147.     {
  148.         player->velocity.y -= .2;
  149.         player->canjump = false;
  150.     }
  151. }
  152.  
  153. void CheckCollision(std::vector<GameObject*>& objects)
  154. {
  155.     for (auto it = objects.begin(); it != objects.end(); it++)
  156.     {
  157.         if ((*it)->object_type != GameObject::tile)
  158.         {
  159.             if ((*it)->lastPosition != (*it)->shape.getPosition())
  160.             {
  161.                 int iteratorindex1 = std::distance(objects.begin(), it);
  162.                 for (auto it2 = objects.begin(); it2 != objects.end(); it2++)
  163.                 {
  164.                     int iteratorindex2 = std::distance(objects.begin(), it2);
  165.                     if ((*it)->shape.getGlobalBounds().intersects((*it2)->shape.getGlobalBounds()) && iteratorindex1 != iteratorindex2)
  166.                     {
  167.                         //checks left
  168.                         if ((*it)->lastPosition.x > (*it)->shape.getPosition().x)
  169.                             (*it)->shape.move((*it)->speed, 0);
  170.                         //checks right
  171.                         if ((*it)->lastPosition.x < (*it)->shape.getPosition().x)
  172.                             (*it)->shape.move(-(*it)->speed, 0);
  173.  
  174.                         //checks bottom
  175.                         if ((*it)->lastPosition.y < (*it)->shape.getPosition().y)
  176.                         {
  177.                             if((*it)->shape.getPosition().x > (*it2)->shape.getPosition().x &&
  178.                                 (*it)->shape.getPosition().x < (*it2)->shape.getPosition().x + (*it2)->shape.getGlobalBounds().width)
  179.                             {
  180.                                 (*it)->shape.move(0, -(*it)->velocity.y);
  181.                                 (*it)->velocity.y = 0;
  182.                                 (*it)->canjump = true;
  183.                             }
  184.                             if ((*it)->shape.getPosition().x + (*it)->shape.getGlobalBounds().width >(*it2)->shape.getPosition().x &&
  185.                                 (*it)->shape.getPosition().x + (*it)->shape.getGlobalBounds().width < (*it2)->shape.getPosition().x + (*it2)->shape.getGlobalBounds().width)
  186.                             {
  187.                                 (*it)->shape.move(0, -(*it)->velocity.y);
  188.                                 (*it)->velocity.y = 0;
  189.                                 (*it)->canjump = true;
  190.                             }
  191.  
  192.                         }
  193.                         //checks top
  194.                     }
  195.                 }
  196.             }
  197.         }
  198.     }
  199. }
  200.  
  201. const int map_w = 20;
  202. const int map_h = 10;
  203. const sf::Vector2f startingPoint = sf::Vector2f(1, 1);
  204.  
  205. int tile_map[map_h][map_w] =
  206. {
  207.     { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
  208.     { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
  209.     { 1, 0, 2, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
  210.     { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0 },
  211.     { 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0 },
  212.     { 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 },
  213.     { 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0 },
  214.     { 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0 },
  215.     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0 },
  216.     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
  217. };
  218.  
  219.  
  220.  
  221. int main()
  222. {
  223.     sf::RenderWindow window(sf::VideoMode(1024, 768), "");
  224.  
  225.     sf::Vector2f gravity(0, .0001);
  226.    
  227.     std::vector<GameObject*> objects;
  228.  
  229.     Player * player = new Player;
  230.     Camera camera(player);
  231.  
  232.     for (int i = 0; i < map_h; i++)
  233.     {
  234.         for (int x = 0; x < map_w; x++)
  235.         {
  236.             if (tile_map[i][x] == 1)
  237.             {
  238.                 Tile * tile = new Tile(sf::Vector2f(startingPoint.x * x * 125, startingPoint.y * i * 125), Tile::ground);
  239.                 objects.push_back(tile);
  240.             }
  241.             if (tile_map[i][x] == 2)
  242.             {
  243.                 player->shape.setPosition(sf::Vector2f(startingPoint.x * x * 125, startingPoint.y * i * 125));
  244.                 objects.push_back(player);
  245.             }
  246.         }
  247.     }
  248.  
  249.    
  250.  
  251.     while (window.isOpen())
  252.     {
  253.         sf::Event event;
  254.         while (window.pollEvent(event))
  255.         {
  256.             if (event.type == sf::Event::Closed)
  257.                 window.close();
  258.         }
  259.         window.clear();
  260.  
  261.         MovePlayer(player);
  262.  
  263.         player->velocity.y += gravity.y;
  264.  
  265.         CheckCollision(objects);
  266.         camera.Update(player);
  267.         for (auto it = objects.begin(); it != objects.end(); it++)
  268.         {
  269.             (*it)->Update();
  270.             (*it)->Draw(window);
  271.         }
  272.         camera.Draw(window);
  273.  
  274.         window.display();
  275.     }
  276.  
  277.     delete player;
  278.  
  279.  
  280.     return 0;
  281. }
Advertisement
Add Comment
Please, Sign In to add comment