SHARE
TWEET

Untitled

a guest Dec 9th, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void save(string fname){
  2.     std::ofstream gameFile; //calling the file GameFile
  3.     gameFile.open(fname);
  4.     //Time
  5.     std::time_t result = std::time(nullptr);
  6.     gameFile << std::asctime(std::localtime(&result));
  7.     //Current Room player is in
  8.     gameFile << currentState->getCurrentRoom()->getName()->data();
  9.     gameFile << "\n";
  10.     //Current Inventory
  11.     for (GameObject *i:State::inventory) {
  12.         gameFile << i->getKeyword()->data();
  13.         gameFile << ",";
  14.     }
  15.     gameFile << "\n";
  16.     //Rooms Inventory
  17.     for (Room* i:Room::rooms){
  18.         gameFile << i->getName()->data();
  19.         int size = i->objects.size();
  20.         int count = 0;
  21.         gameFile << "\n";
  22.         if (size != 0) {
  23.             for(GameObject* a: i->objects){
  24.                 gameFile << a->getKeyword()->data();
  25.                 if (count != size -1) {
  26.                     gameFile << ",";
  27.                 }
  28.                 count ++;
  29.             }
  30.         } else {
  31.             gameFile << "null";
  32.         }
  33.         gameFile << "\n";
  34.     }
  35.     gameFile.close();
  36.     std::cout << "The file has been saved as " + fname + "\n";
  37.  
  38. }
  39. void loadGameState(string fName) {
  40.     std::ifstream MyFile("Saves/" + fName);
  41.     string line;
  42.  
  43.     //TIMESTAMP
  44.     getline(MyFile, line);
  45.     std::cout << "Timestamp of save: " + line << std::endl;
  46.  
  47.     //CURRENT ROOM
  48.     getline(MyFile, line);
  49.  
  50.     for(Room* iter: Room::rooms){
  51.         string key = iter->getName()->data();
  52.  
  53.         if (key.compare(0, ' ', line) == 0) {
  54.             currentState->goTo(iter);
  55.         }
  56.     }
  57.  
  58.     //INVENTORY
  59.     getline(MyFile, line);
  60.  
  61.     string intermediate;
  62.     std::stringstream temp(line);
  63.  
  64.     State::inventory.clear();
  65.  
  66.     while (std::getline(temp, intermediate, ',')) {
  67.  
  68.         for (GameObject* iter: objList) {
  69.             if (intermediate.compare(0, ' ', *iter->getKeyword()) == 0) {
  70.                 State::inventory.push_back(iter);
  71.             }
  72.         }
  73.     }
  74.  
  75.     //ROOM OBJECTS
  76.     string strName;
  77.     string strList;
  78.  
  79.     for (Room* z: Room::rooms) {
  80.         z->clearRoom();
  81.     }
  82.  
  83.     while (!MyFile.eof()) {
  84.         getline(MyFile, strName);
  85.         getline(MyFile, strList);
  86.  
  87.         std::vector<string> tokens;
  88.         std::stringstream store(strList);
  89.  
  90.         while (std::getline(store, intermediate, ',')) {
  91.             for (Room *x: Room::rooms) {
  92.                 if (strName.compare(0, ' ', *x->getName()) == 0) {
  93.                     for (GameObject *y: objList) {
  94.                         if (intermediate.compare(0, ' ', y->getKeyword()->data()) == 0) {
  95.                             x->addObject(y);
  96.                         }
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     MyFile.close();
  103. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top