int main(int argc, char **argv) { string sourceFile("SourceData.json"); WtmSourceDataSet* wtmSourceDataSet = WtmJsonLoader::loadWtmSourceDataSet(sourceFile); Network* network = NetworkCreator::create(wtmSourceDataSet); string worldFile("world2.json"); World* world = createWorldFromJsonFile(worldFile, argc, argv); WtmActor wtmActor(network); world->addActor(wtmActor, Coords(6, 6), Directions::Top); MovingActor movingActorA(Directions::Top); world->addActor(movingActorA, Coords(8, 0), Directions::Right); MovingActor movingActorB(Directions::Right); world->addActor(movingActorB, Coords(9, 5), Directions::Right); MovingActor movingActorC(Directions::Right); world->addActor(movingActorC, Coords(3, 11), Directions::Right); MovingActor movingActorD(Directions::Right); world->addActor(movingActorD, Coords(2, 3), Directions::Right); world->startWorldLoop(); ///*----------------------------------------------------------------------------------*/ //string wtmNetworkFile("DestData.json"); //Network* network = WtmJsonLoader::loadNetwork(wtmNetworkFile); //string worldFile("world2.json"); //World* world = createWorldFromJsonFile(worldFile, argc, argv); //MovingActor movingActor(Directions::Top); //world->addActor(movingActor, Coords(7, 7), Directions::Right); //WtmActor wtmActor(network); //world->addActor(wtmActor, Coords(6, 9), Directions::Top); //world->startWorldLoop(); ///*----------------------------------------------------------------------------------*/ //string destFile("DestData.json"); //WtmJsonLoader::saveNetwork(destFile, wtmActor.network); return 0; }
void WorldHandler::loadWorld(int worldNum){ if(!typeLoaded){ this->loadTileTypes(); } if(!itemLoaded){ this->loadItemTypes(); } xMapSize = 78; yMapSize = 318; map.reserve(xMapSize*yMapSize); xMapSize = 0; yMapSize = 0; const char seperator = ','; std::ifstream worldFile("data/saves/world"+std::to_string(worldNum)+".csv", std::ios::in); //declare a file stream if (!worldFile.is_open()) //checks if the file is open, if not load a template { std::cout << "World file not found, loading a template" << std::endl; worldFile = std::ifstream("data/templateWorld.csv", std::ios::in); //declare a new file stream of the template world } std::string str; while (getline(worldFile, str)){ //loop through and push all the ints to a vector std::string read; for(int i = 0; i <= str.length(); i++){ //find seperators and split the integers //this allows for >1 digit ints // i >= str.length finds the end of the line if(str[i] == seperator || i >= str.length()){ xMapSize++; int tempInt = atoi(read.c_str()); // turn into an int read.clear(); // clear the read String for next time //if the tempInt is incorrect or invalid if(tempInt > tiles.size() || tempInt < 0){ std::cout << "World failed to load, invalid tile ID"; return; } map.push_back(&tiles[tempInt]); }else{ read += str[i]; } } //count how many lines there are yMapSize++; } //xMapSize will end up being xSize*number of lines //so fix that xMapSize = xMapSize/yMapSize; worldLoaded = true; std::cout << "World loaded" << std::endl; }
void Player::loadPlayerData(int num) { const char seperator = ','; std::ifstream worldFile("data/saves/player"+std::to_string(num)+".csv", std::ios::in); //declare a file stream if (worldFile.is_open()) //checks if the file is open?? { std::string str; int linenumber = 0; while (getline(worldFile, str)) { //if it's a / just ignore the entire line if(str[0] == '/') { continue; } //loop through and do appropriate things with the data std::vector<int> readIntegers; // this will hold the data std::string read; for(int i = 0; i <= str.length(); i++) { if(str[i] == seperator || i >= str.length()) { int tempInt = atoi(read.c_str()); read.clear(); readIntegers.push_back(tempInt); } else { read += str[i]; } } linenumber++; if(linenumber == 1) { editPosition().x = readIntegers[0]; editPosition().y = readIntegers[1]; } else if(linenumber == 2) { editScene().x = readIntegers[0]; editScene().y = readIntegers[1]; } else if(linenumber >2) { inv.pickup(worldHandler->getItems()[readIntegers[0]], readIntegers[1]); } } std::cout << "Player data loaded" << std::endl; } else { std::cout << "No player data found, using blank" << std::endl; } //offset the world worldHandler->offSetby(getScene().x, getScene().y, true); }
void WorldHandler::loadItemTypes(){ const char seperator = ','; std::ifstream worldFile("data/items.csv", std::ios::in); //declare a file stream if (worldFile.is_open()) //checks if the file is open?? { std::string str; while (getline(worldFile, str)){ //if it's a / just ignore the entire line if(str[0] == '/'){ continue; } //loop through and push all the tiles into a vector std::vector<int> readIntegers; // this will hold the data std::string read; for(int i = 0; i <= str.length(); i++){ //find seperators and split the integers //this allows for >1 digit ints // i >= str.length finds the end of the line if(str[i] == seperator || i >= str.length()){ int tempInt = atoi(read.c_str()); read.clear(); readIntegers.push_back(tempInt); }else{ read += str[i]; } } //now push the integers in items.push_back(new Item(readIntegers[0], readIntegers[1], readIntegers[2], readIntegers[3])); } typeLoaded = true; std::cout << "Items loaded" << std::endl; }else{ typeLoaded = false; std::cout << "Item file failed to load" << std::endl; } }