Advertisement
Guest User

Untitled

a guest
Dec 26th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 27.98 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. #include <vector>
  6. #include <math.h>
  7. #include <utility>
  8. #include "terrain.h"
  9. #include "player.h"
  10. #include "digidraw.h"
  11. #include "drawing.h"
  12. #include "map.h"
  13. #include "npc.h"
  14.  
  15. std::pair<bool, Terrain> isColliding(Player, std::vector<Terrain>);
  16. std::pair<bool, Terrain> isColliding(Drawing, std::vector<Terrain>);
  17. std::pair<bool, Drawing> isColliding(Player, std::map<ObjectName, Drawing>);
  18. std::pair<bool, Drawing> isColliding(Drawing, std::map<ObjectName, Drawing>);
  19. std::pair<bool, Terrain> isColliding(sf::Vertex, std::vector<Terrain>);
  20. bool pointCollide(sf::Vertex, Terrain);
  21. void gravitate(Map, Player);
  22. void movement(sf::Vector2f, Player, Map, std::map<ObjectName, Drawing>);
  23.  
  24. int main()
  25. {
  26.     std::fstream fs;
  27.     fs.open("/home/sir-lulzalot/Desktop/results.txt", std::fstream::out);
  28.  
  29.     sf::Clock theClock;
  30.     sf::Time theTime;
  31.     int32_t firstTap = 0, secondTap = 0;
  32.  
  33.     bool doubleSpeed = false;
  34.     bool flight = false;
  35.     bool doubleFall = false;
  36.     bool hadJumped = false;
  37.     bool canJump = true;
  38.     bool jumping = false;
  39.     sf::Vector2f jumpStart(0.0f,-3.0f);
  40.  
  41.     std::setw(2);
  42.     std::setprecision(5);
  43.  
  44.     sf::RenderWindow window(sf::VideoMode(1000, 500), "SFML works!");
  45.  
  46.     Map thisMap;
  47.  
  48.     sf::Vector2f vect(35.0f, 50.0f);
  49.     sf::Vector2f vectL(1000.0f,2.0f);
  50.     sf::Vector2f vectL2(1000.0f, 30.0f);
  51.     Terrain land(250, 400, vectL2);
  52.     Terrain ceiling(250, 310, vectL2);
  53.     Terrain test(200, 400, vect);
  54.     Drawing wall;
  55.     std::vector<Terrain> theMap;
  56.     std::map<ObjectName, Drawing> theDrawings;
  57.     Player character(300, 50, vect);
  58.     std::pair<bool, Terrain> collisionResult;
  59.     std::pair<bool, Drawing> collisionResultDrawn;
  60.     std::pair<bool, Terrain> drawingCollision;
  61.     std::pair<bool, Drawing> drawingToDrawingCollision;
  62.  
  63.     sf::Vertex thePoint;
  64.  
  65.     sf::Vertex pIQ, pIQ2;
  66.  
  67.     DigiDraw theDrawing[3];
  68.     int drawIterator = 0;
  69.  
  70.     theMap.push_back(land);
  71.     theMap.push_back(test);
  72.     theMap.push_back(ceiling);
  73.  
  74.     thisMap.addTerrain(land);
  75.     thisMap.addTerrain(test);
  76.     thisMap.addTerrain(ceiling);
  77.  
  78.     while (window.isOpen())
  79.     {
  80.         sf::Event event;
  81.         while (window.pollEvent(event))
  82.         {
  83.             switch (event.type){
  84.                 case sf::Event::Closed:
  85.                     window.close();
  86.                     break;
  87.                 case sf::Event::KeyReleased:
  88.                     if(event.key.code == sf::Keyboard::Return){
  89.                         fs << "Char Position:" << character.getObject().getPosition().x << " " << character.getObject().getPosition().y << "\n";
  90.                         fs << "Wall Position: " << wall.getObject().getPosition().x << " " << wall.getObject().getPosition().y << "\n";
  91.                     }
  92.                     if(event.key.code == sf::Keyboard::Up)
  93.                         flight = false;
  94.                     if(event.key.code == sf::Keyboard::Space){
  95.                         jumpStart.y = -3.0f;
  96.                         flight = false;
  97.                         hadJumped = true;
  98.                     }
  99.                     if(event.key.code == sf::Keyboard::Left || event.key.code == sf::Keyboard::Right){
  100.                         doubleSpeed = false;
  101.                         theTime = theClock.getElapsedTime();
  102.                         firstTap = theTime.asMilliseconds();
  103.                     }
  104.                     if(event.key.code == sf::Keyboard::LShift){
  105.                         if(theDrawing[0].analyzeArray() == ObjectName::Wall){
  106.                             if(theDrawings.find(ObjectName::Wall) != theDrawings.end())
  107.                                 theDrawings.erase(theDrawings.find(ObjectName::Wall));
  108.                             if(theDrawing[0].getAnalysis().getVertexCount() >= 2){
  109.                                 pIQ = theDrawing[0].getAnalysis()[0];
  110.                                 drawingCollision = isColliding(pIQ, theMap);
  111.                                 if(drawingCollision.first){
  112.                                     std::cout << "I resize the bottom!" << std::endl;
  113.                                     pIQ = theDrawing[0].resetPoint(pIQ.position.x, (drawingCollision.second.getTop()-0.1f), 0);
  114.                                 }
  115.                                 pIQ2 = theDrawing[0].getAnalysis()[(theDrawing[0].getAnalysis().getVertexCount()-1)];
  116.                                 drawingCollision = isColliding(pIQ2, theMap);
  117.                                 if(drawingCollision.first){
  118.                                     std::cout << "I resize the top!" << std::endl;
  119.                                     pIQ2 = theDrawing[0].resetPoint(pIQ2.position.x, (drawingCollision.second.getBottom()+0.1f) , (theDrawing[0].getAnalysis().getVertexCount()-1));
  120.                                 }
  121.                                 wall.theObject.setPosition((pIQ2.position.x-20), pIQ2.position.y);
  122.                                 wall.theObject.setSize(sf::Vector2f(40, (pIQ.position.y - pIQ2.position.y)));
  123.                                 wall.resizeTerrain();
  124.                                 wall.setName(ObjectName::Wall);
  125.                                 theDrawings[ObjectName::Wall] = wall;
  126.                                 theDrawings[ObjectName::Wall].gravityShift(0.1f);
  127.                             }
  128.                         }
  129.                        else if(theDrawing[0].analyzeArray() == ObjectName::Fan){
  130.                            std::cout << "Somehow you were going to summon a Fan." << std::endl;
  131.                        }
  132.                         theDrawing[0].emptyArray();
  133.                         theDrawing[1].emptyArray();
  134.                         theDrawing[2].emptyArray();
  135.                         drawIterator = 0;
  136.                     }
  137.                     break;
  138.                 case sf::Event::MouseButtonReleased:
  139.                 if(event.mouseButton.button == sf::Mouse::Left){
  140.                     drawIterator++;
  141.                 }
  142.                     break;
  143.                 default:
  144.                     break;
  145.             }
  146.         }
  147.         thePoint.position.x = (float)sf::Mouse::getPosition(window).x;
  148.         thePoint.position.y = (float)sf::Mouse::getPosition(window).y;
  149.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)){
  150.             if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && drawIterator <= 2){
  151.                 theDrawing[drawIterator].fillAnalysis(thePoint);
  152.                 theDrawing[drawIterator].fillArray(thePoint);
  153.             }
  154.         }
  155.         else{
  156.             if(!flight){
  157.                 if(doubleFall){
  158.                     if(character.getGravityY() <= 6.0f)
  159.                         character.gravityShift(character.getGravityY() + 0.2f);
  160.                 }
  161.                 else
  162.                     if(character.getGravityY() <= 3.0f)
  163.                         character.gravityShift(character.getGravityY() + 0.1f);
  164.                 character.theObject.move(character.getGravity());
  165.                 character.update();
  166.                 collisionResult = isColliding(character, theMap);
  167.                 if(!theDrawings.empty())
  168.                     collisionResultDrawn = isColliding(character, theDrawings);
  169.                 if(collisionResult.first)
  170.                     hadJumped = false;
  171.                 else if(!theDrawings.empty())
  172.                     if(collisionResultDrawn.first)
  173.                         hadJumped = false;
  174.                 if(collisionResult.first && !doubleFall){
  175.                     character.theObject.setPosition(character.theObject.getPosition().x,
  176.                     (collisionResult.second.getObject().getPosition().y-0.1f)-character.theObject.getSize().y);
  177.                     character.gravityShift(0.1f);
  178.                     character.update();
  179.                     if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  180.                         canJump = true;
  181.                 }
  182.                 else if(collisionResult.first && doubleFall){
  183.                     character.theObject.setPosition(character.theObject.getPosition().x,
  184.                     (collisionResult.second.getObject().getPosition().y-0.1f)-character.theObject.getSize().y);
  185.                     character.gravityShift(0.1f);
  186.                     doubleFall = false;
  187.                     character.update();
  188.                     if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  189.                         canJump = true;
  190.                 }
  191.                 else if(!theDrawings.empty()){
  192.                     if(collisionResultDrawn.first && !doubleFall){
  193.                         character.theObject.setPosition(character.theObject.getPosition().x,
  194.                         (collisionResultDrawn.second.getObject().getPosition().y-0.1f)-character.theObject.getSize().y);
  195.                         character.gravityShift(0.1f);
  196.                         character.update();
  197.                         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  198.                             canJump = true;
  199.                     }
  200.                     if(collisionResultDrawn.first && doubleFall){
  201.                         character.theObject.setPosition(character.theObject.getPosition().x,
  202.                         (collisionResultDrawn.second.getObject().getPosition().y-0.1f)-character.theObject.getSize().y);
  203.                         character.gravityShift(0.1f);
  204.                         doubleFall = false;
  205.                         character.update();
  206.                         if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  207.                             canJump = true;
  208.                     }
  209.                 }
  210.                 else
  211.                     canJump = false;
  212.             }
  213.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
  214.                 theTime = theClock.getElapsedTime();
  215.                 secondTap = theTime.asMilliseconds();
  216.                 if(secondTap - firstTap <= 300)
  217.                     doubleSpeed = true;
  218.                 if(doubleSpeed)
  219.                     character.theObject.move(-2.00f, 0);
  220.                 else
  221.                     character.theObject.move(-1.00f,0);
  222.                 character.update();
  223.                 collisionResult = isColliding(character, theMap);
  224.                 if(!theDrawings.empty())
  225.                     collisionResultDrawn = isColliding(character, theDrawings);
  226.                 if(collisionResult.first){
  227.                     character.theObject.setPosition((collisionResult.second.getObject().getPosition().x+0.1f)+collisionResult.second.getObject().getSize().x,
  228.                                                     character.getObject().getPosition().y);
  229.                     character.update();
  230.                 }
  231.                 else if(!theDrawings.empty())
  232.                     if(collisionResultDrawn.first){
  233.                         if(doubleSpeed && canJump && !hadJumped){
  234.                             theDrawings[collisionResultDrawn.second.getName()].theObject.move(-2.00f, 0);
  235.                             theDrawings[collisionResultDrawn.second.getName()].resizeTerrain();
  236.                         }
  237.                         else{
  238.                         character.theObject.setPosition((collisionResultDrawn.second.getObject().getPosition().x+0.1f)+collisionResultDrawn.second.getObject().getSize().x,
  239.                                                         character.getObject().getPosition().y);
  240.                         character.update();
  241.                         }
  242.                     }
  243.             }
  244.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
  245.                 theTime = theClock.getElapsedTime();
  246.                 secondTap = theTime.asMilliseconds();
  247.                 if(secondTap - firstTap <= 300)
  248.                     doubleSpeed = true;
  249.                 if(doubleSpeed)
  250.                     character.theObject.move(2.00f, 0);
  251.                 else
  252.                     character.theObject.move(1.00f, 0);
  253.                 character.update();
  254.                 collisionResult = isColliding(character, theMap);
  255.                 if(!theDrawings.empty())
  256.                     collisionResultDrawn = isColliding(character, theDrawings);
  257.                 if(collisionResult.first){
  258.                     character.theObject.setPosition((collisionResult.second.getObject().getPosition().x-0.1f)-character.theObject.getSize().x,
  259.                                                     character.getObject().getPosition().y);
  260.                     character.update();
  261.                 }
  262.                 else if(!theDrawings.empty())
  263.                     if(collisionResultDrawn.first){
  264.                         if(doubleSpeed && canJump && !hadJumped){
  265.                             theDrawings[collisionResultDrawn.second.getName()].theObject.move(2.00f, 0);
  266.                             theDrawings[collisionResultDrawn.second.getName()].resizeTerrain();
  267.                         }
  268.                         else{
  269.                             character.theObject.setPosition((collisionResultDrawn.second.getObject().getPosition().x-0.1f)-character.theObject.getSize().x,
  270.                                                             character.getObject().getPosition().y);
  271.                             character.update();
  272.                         }
  273.                     }
  274.             }
  275.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
  276.                 flight = true;
  277.                 movement(sf::Vector2f(0, -1.00f), character, thisMap, theDrawings);
  278.                 /*
  279.                 character.theObject.move(0,-1.00f);
  280.                 character.update();
  281.                 collisionResult = isColliding(character, theMap);
  282.                 if(!theDrawings.empty())
  283.                     collisionResultDrawn = isColliding(character, theDrawings);
  284.                 if(collisionResult.first){
  285.                     character.theObject.setPosition(character.theObject.getPosition().x,
  286.                     (collisionResult.second.getObject().getPosition().y+0.1f)+collisionResult.second.getObject().getSize().y);
  287.                     character.update();
  288.                 }
  289.                 else if(!theDrawings.empty())
  290.                     if(collisionResultDrawn.first){
  291.                         character.theObject.setPosition(character.theObject.getPosition().x,
  292.                         (collisionResultDrawn.second.getObject().getPosition().y+0.1f)+collisionResultDrawn.second.getObject().getSize().y);
  293.                         character.update();
  294.                     }
  295.                     */
  296.             }
  297.             if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
  298.                 if(!isColliding(character, theMap).first)
  299.                     doubleFall = true;
  300.             }
  301.             if((sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && canJump) || (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && jumping)){
  302.                 canJump = false;
  303.                 jumping = true;
  304.                 if(jumpStart.y < 0 && !hadJumped)
  305.                     flight = true;
  306.                 else{
  307.                     flight = false;
  308.                     jumping = false;
  309.                 }
  310.                 if(flight && !hadJumped){
  311.                     character.theObject.move(jumpStart);
  312.                     character.update();
  313.                     collisionResult = isColliding(character, theMap);
  314.                     if(!theDrawings.empty())
  315.                         collisionResultDrawn = isColliding(character, theDrawings);
  316.                     if(collisionResult.first){
  317.                         if(jumpStart.y < 0){
  318.                             character.theObject.setPosition(character.theObject.getPosition().x,
  319.                             (collisionResult.second.getObject().getPosition().y+1)+collisionResult.second.getObject().getSize().y);
  320.                             jumpStart.y = 0.0f;
  321.                             flight = false;
  322.                             jumping = false;
  323.                         }
  324.                         else
  325.                             character.getObject().move(jumpStart);
  326.                         character.update();
  327.                     }
  328.                     else if(!theDrawings.empty())
  329.                         if(collisionResultDrawn.first){
  330.                             if(jumpStart.y < 0){
  331.                                 character.theObject.setPosition(character.theObject.getPosition().x,
  332.                                 (collisionResultDrawn.second.getObject().getPosition().y+1)+collisionResultDrawn.second.getObject().getSize().y);
  333.                                 jumpStart.y = 0.0f;
  334.                                 flight = false;
  335.                             }
  336.                         else
  337.                             character.getObject().move(jumpStart);
  338.                         character.update();
  339.                     }
  340.                     if(jumpStart.y < 0.0f)
  341.                         jumpStart.y += 0.1f;
  342.                 }
  343.             }
  344.             if(theDrawings.find(ObjectName::Wall) != theDrawings.end()){
  345.                 drawingCollision = isColliding(theDrawings[ObjectName::Wall], theMap);
  346.                 theDrawings[ObjectName::Wall].theObject.move(theDrawings[ObjectName::Wall].getGravity());
  347.                 if(drawingCollision.first){
  348.                     theDrawings[ObjectName::Wall].theObject.setPosition(theDrawings[ObjectName::Wall].theObject.getPosition().x,
  349.                                                                        (drawingCollision.second.getObject().getPosition().y-0.1f)-theDrawings[ObjectName::Wall].theObject.getSize().y);
  350.                     theDrawings[ObjectName::Wall].resizeTerrain();
  351.                     theDrawings[ObjectName::Wall].gravityShift(0.1f);
  352.                 }
  353.                 else{
  354.                     theDrawings[ObjectName::Wall].resizeTerrain();
  355.                     theDrawings[ObjectName::Wall].gravityShift(theDrawings[ObjectName::Wall].getGravityY() + 0.1f);
  356.                 }
  357.             }
  358.         }
  359.         if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
  360.             window.close();
  361.         if(character.collision(test))
  362.             std::cout << "Collision!" << std::endl;
  363.         if(character.collision(land))
  364.             std::cout << "Collision!" << std::endl;
  365.  
  366.         character.update();
  367.         window.clear();
  368.         if(sf::Keyboard::LShift){
  369.             window.draw(character.getObject());
  370.             window.draw(land.getObject());
  371.             window.draw(test.getObject());
  372.             window.draw(ceiling.getObject());
  373.             if(!theDrawings.empty())
  374.                 window.draw(theDrawings[ObjectName::Wall].getObject());
  375.         }
  376.         window.draw(theDrawing[0].getArray());
  377.         window.draw(theDrawing[0].getAnalysis());
  378.         window.draw(theDrawing[1].getArray());
  379.         window.draw(theDrawing[1].getAnalysis());
  380.         window.draw(theDrawing[2].getArray());
  381.         window.draw(theDrawing[2].getAnalysis());
  382.         window.display();
  383.     }
  384.     fs.close();
  385.     return 0;
  386. }
  387.  
  388. std::pair<bool, Terrain> isColliding(Player thePlayer, std::vector<Terrain> theMap){
  389.     bool wouldCollide = false;
  390.     Terrain theTerrain;
  391.     for(std::vector<Terrain>::iterator i = theMap.begin(); i != theMap.end(); i++)
  392.         if(thePlayer.collision(*i)){
  393.             wouldCollide = true;
  394.             theTerrain = *i;
  395.         }
  396.     return std::make_pair(wouldCollide, theTerrain);
  397. }
  398.  
  399. std::pair<bool, Terrain> isColliding(Drawing theDrawing, std::vector<Terrain> theMap){
  400.     bool wouldCollide = false;
  401.     Terrain theTerrain;
  402.     for(std::vector<Terrain>::iterator i = theMap.begin(); i != theMap.end(); i++)
  403.         if(theDrawing.collision(*i)){
  404.             wouldCollide = true;
  405.             theTerrain = *i;
  406.         }
  407.     return std::make_pair(wouldCollide, theTerrain);
  408. }
  409.  
  410. std::pair<bool, Drawing> isColliding(Player thePlayer, std::map<ObjectName, Drawing> theDrawings){
  411.     bool wouldCollide = false;
  412.     Drawing theObject;
  413.     if(theDrawings.find(ObjectName::Wall) != theDrawings.end())
  414.         if(thePlayer.collision(theDrawings[ObjectName::Wall])){
  415.             wouldCollide = true;
  416.             theObject = theDrawings[ObjectName::Wall];
  417.         }
  418.     return std::make_pair(wouldCollide, theObject);
  419. }
  420.  
  421. std::pair<bool, Terrain> isColliding(sf::Vertex thePoint, std::vector<Terrain> theMap){
  422.     bool wouldCollide = false;
  423.     Terrain theObject;
  424.     for(std::vector<Terrain>::iterator i = theMap.begin(); i != theMap.end(); i++)
  425.         if(pointCollide(thePoint, *i)){
  426.             wouldCollide = true;
  427.             theObject = *i;
  428.         }
  429.     return std::make_pair(wouldCollide, theObject);
  430. }
  431.  
  432. bool pointCollide(sf::Vertex thePoint, Terrain theTerrain){
  433.     if((thePoint.position.x + 20) < theTerrain.getLeft() || (thePoint.position.x - 20) > theTerrain.getRight() ||
  434.         thePoint.position.y > theTerrain.getBottom() || thePoint.position.y < theTerrain.getTop())
  435.     {
  436.         return false;
  437.     }
  438.     return true;
  439. }
  440.  
  441. void gravitate(Map &theMap, Player &thePlayer, std::map<ObjectName, Drawing> &theDrawings){
  442.     std::vector<Terrain> thisTerrain = theMap.getTerrain();
  443.     std::vector<NPC> thisPopulace = theMap.getNPCs();
  444.     for(std::vector<Terrain>::iterator i = thisTerrain.begin(); i != thisTerrain.end(); i++){
  445.         i->gravitate();
  446.     }
  447.     for(std::vector<NPC>::iterator i = thisPopulace.begin(); i != thisPopulace.end(); i++){
  448.         i->gravitate();
  449.     }
  450.     theMap.setTerrain(thisTerrain);
  451.     theMap.setNPCs(thisPopulace);
  452.     thePlayer.gravitate();
  453.     if(theDrawings.find(ObjectName::Wall) != theDrawings.end())
  454.         theDrawings[ObjectName::Wall].gravitate();
  455. }
  456.  
  457. void movement(sf::Vector2f theVector, Player &thePlayer, Map &theMap, std::map<ObjectName, Drawing> theDrawings){
  458.     std::vector<Terrain> theTerrain = theMap.getTerrain();
  459.     std::vector<NPC> thePopulace = theMap.getNPCs();
  460.     //Moving down.
  461.     if(theVector.x == 0 && theVector.y > 0){
  462.         thePlayer.movement(theVector);
  463.         thePlayer.update();
  464.         //All terrain checks.
  465.         for(std::vector<Terrain>::iterator i = theTerrain.begin(); i != theTerrain.end(); i++){
  466.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  467.                 thePlayer.setPosition(sf::Vector2f(thePlayer.getObject().getPosition().x, (i->getTop()-0.1f)));
  468.                 thePlayer.update();
  469.             }
  470.         }
  471.         //All NPC checks.
  472.         for(std::vector<NPC>::iterator i = thePopulace.begin(); i != thePopulace.end(); i++){
  473.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  474.                 thePlayer.setPosition(sf::Vector2f(thePlayer.getObject().getPosition().x, (i->getTop()-0.1f)));
  475.                 thePlayer.update();
  476.             }
  477.         }
  478.         //Individual drawing checks.
  479.         if(theDrawings.find(ObjectName::Wall) != theDrawings.end()){
  480.             if(thePlayer.collision(Solid(theDrawings[ObjectName::Wall].getTop(), theDrawings[ObjectName::Wall].getBottom(),
  481.                                          theDrawings[ObjectName::Wall].getLeft(), theDrawings[ObjectName::Wall].getRight())) &&
  482.                theDrawings[ObjectName::Wall].getSolidarity()){
  483.                 thePlayer.setPosition(sf::Vector2f(thePlayer.getObject().getPosition().x, (theDrawings[ObjectName::Wall].getTop()-0.1f)));
  484.                 thePlayer.update();
  485.             }
  486.         }
  487.     }
  488.     //Moving up.
  489.     else if(theVector.x == 0 && theVector.y < 0){
  490.         thePlayer.movement(theVector);
  491.         thePlayer.update();
  492.         //All terrain checks.
  493.         for(std::vector<Terrain>::iterator i = theTerrain.begin(); i != theTerrain.end(); i++){
  494.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  495.                 thePlayer.setPosition(sf::Vector2f(thePlayer.getObject().getPosition().x, (i->getBottom()+0.1f)));
  496.                 thePlayer.update();
  497.             }
  498.         }
  499.         //All NPC checks.
  500.         for(std::vector<NPC>::iterator i = thePopulace.begin(); i != thePopulace.end(); i++){
  501.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  502.                 thePlayer.setPosition(sf::Vector2f(thePlayer.getObject().getPosition().x, (i->getBottom()+0.1f)));
  503.                 thePlayer.update();
  504.             }
  505.         }
  506.         //Individual drawing checks.
  507.         if(theDrawings.find(ObjectName::Wall) != theDrawings.end()){
  508.             if(thePlayer.collision(Solid(theDrawings[ObjectName::Wall].getTop(), theDrawings[ObjectName::Wall].getBottom(),
  509.                                          theDrawings[ObjectName::Wall].getLeft(), theDrawings[ObjectName::Wall].getRight())) &&
  510.                theDrawings[ObjectName::Wall].getSolidarity()){
  511.                 thePlayer.setPosition(sf::Vector2f(thePlayer.getObject().getPosition().x, (theDrawings[ObjectName::Wall].getBottom()+0.1f)));
  512.                 thePlayer.update();
  513.             }
  514.         }
  515.     }
  516.     //Moving right.
  517.     else if(theVector.x > 0 && theVector.y == 0){
  518.         thePlayer.movement(theVector);
  519.         thePlayer.update();
  520.         //All terrain checks.
  521.         for(std::vector<Terrain>::iterator i = theTerrain.begin(); i != theTerrain.end(); i++){
  522.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  523.                 thePlayer.setPosition(sf::Vector2f((i->getLeft()-0.1f), thePlayer.getObject().getPosition().y));
  524.                 thePlayer.update();
  525.             }
  526.         }
  527.         //All NPC checks.
  528.         for(std::vector<NPC>::iterator i = thePopulace.begin(); i != thePopulace.end(); i++){
  529.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  530.                 thePlayer.setPosition(sf::Vector2f((i->getLeft()-0.1f), thePlayer.getObject().getPosition().y));
  531.                 thePlayer.update();
  532.             }
  533.         }
  534.         //Individual drawing checks.
  535.         if(theDrawings.find(ObjectName::Wall) != theDrawings.end()){
  536.             if(thePlayer.collision(Solid(theDrawings[ObjectName::Wall].getTop(), theDrawings[ObjectName::Wall].getBottom(),
  537.                                          theDrawings[ObjectName::Wall].getLeft(), theDrawings[ObjectName::Wall].getRight())) &&
  538.                theDrawings[ObjectName::Wall].getSolidarity()){
  539.                 thePlayer.setPosition(sf::Vector2f((theDrawings[ObjectName::Wall].getLeft()-0.1f), thePlayer.getObject().getPosition().y));
  540.                 thePlayer.update();
  541.             }
  542.         }
  543.     }
  544.     //Moving left.
  545.     else if(theVector.x < 0 && theVector.y == 0){
  546.         thePlayer.movement(theVector);
  547.         thePlayer.update();
  548.         //All terrain checks.
  549.         for(std::vector<Terrain>::iterator i = theTerrain.begin(); i != theTerrain.end(); i++){
  550.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  551.                 thePlayer.setPosition(sf::Vector2f((i->getRight()+0.1f), thePlayer.getObject().getPosition().y));
  552.                 thePlayer.update();
  553.             }
  554.         }
  555.         //All NPC checks.
  556.         for(std::vector<NPC>::iterator i = thePopulace.begin(); i != thePopulace.end(); i++){
  557.             if(thePlayer.collision(Solid(i->getTop(), i->getBottom(), i->getLeft(), i->getRight())) && i->getSolidarity()){
  558.                 thePlayer.setPosition(sf::Vector2f((i->getRight()+0.1f), thePlayer.getObject().getPosition().y));
  559.                 thePlayer.update();
  560.             }
  561.         }
  562.         //Individual drawing checks.
  563.         if(theDrawings.find(ObjectName::Wall) != theDrawings.end()){
  564.             if(thePlayer.collision(Solid(theDrawings[ObjectName::Wall].getTop(), theDrawings[ObjectName::Wall].getBottom(),
  565.                                          theDrawings[ObjectName::Wall].getLeft(), theDrawings[ObjectName::Wall].getRight())) &&
  566.                theDrawings[ObjectName::Wall].getSolidarity()){
  567.                 thePlayer.setPosition(sf::Vector2f((theDrawings[ObjectName::Wall].getRight()+0.1f), thePlayer.getObject().getPosition().y));
  568.                 thePlayer.update();
  569.             }
  570.         }
  571.     }
  572. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement