Advertisement
zgub4

Untitled

Apr 28th, 2015
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | None | 0 0
  1. #include "Map.h"
  2.  
  3.  
  4. Map::Map()
  5. {
  6.     ifstream lvlOne;
  7.     string input;
  8.  
  9.     lvlOne.open("level1.txt");
  10.  
  11.     if (lvlOne.fail()) {
  12.         perror("level1.txt");
  13.     }
  14.     else {
  15.         while (getline(lvlOne, input)) {
  16.             _levelData.push_back(input);
  17.         }
  18.     }
  19. }
  20.  
  21. void Map::printMap()
  22. {
  23.     for (int i = 0; i < _levelData.size(); i++) {
  24.         cout << _levelData[i] << endl;
  25.     }
  26. }
  27.  
  28. void Map::setPlayer(Player &p1)
  29. {
  30.     int x = p1.getXCords();
  31.     int y = p1.getYCords();
  32.     _levelData[y][x] = p1.getName();
  33. }
  34.  
  35. void Map::movePlayer(Player &p1)
  36. {
  37.     char input;
  38.  
  39.     int x = p1.getXCords();
  40.     int y = p1.getYCords();
  41.    
  42.     input = _getch();
  43.  
  44.     switch (input) {
  45.     case 'w':
  46.     case 'W':
  47.         if (collisionDetec(x, y - 1) == false) {
  48.             return;
  49.         }
  50.         else {
  51.             p1.moveY(-1);
  52.             y = p1.getYCords();
  53.         }
  54.         break;
  55.     case 's':
  56.     case 'S':
  57.         if (collisionDetec(x, y + 1) == false) {
  58.             return;
  59.         }
  60.         else {
  61.             p1.moveY(1);
  62.             y = p1.getYCords();
  63.         }
  64.         break;
  65.     case 'a':
  66.     case 'A':
  67.         if (collisionDetec(x - 1, y) == false) {
  68.             return;
  69.         }
  70.         else {
  71.             p1.moveX(-1);
  72.             x = p1.getXCords();
  73.         }
  74.         break;
  75.     case 'd':
  76.     case 'D':
  77.         if (collisionDetec(x + 1, y) == false) {
  78.             return;
  79.         }
  80.         else {
  81.             p1.moveX(1);
  82.             x = p1.getXCords();
  83.         }
  84.         break;
  85.     }
  86.  
  87.     _levelData[y][x] = p1.getName();
  88. }
  89.  
  90. bool Map::collisionDetec(int x, int y)
  91. {
  92.     if (_levelData[y][x] == '#') {
  93.         return false;
  94.     }
  95.    
  96.     return true;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement