daily pastebin goal
9%
SHARE
TWEET

Untitled

a guest Nov 26th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "maker.h"
  2.  
  3. bool MapMaker::load(std::string filename, std::list<Object *> &objects) {
  4.  
  5.     Json::Value root;
  6.     Json::Reader reader;
  7.  
  8.     std::ifstream file(filename);
  9.  
  10.     bool parsingSuccessful = reader.parse(file, root);
  11.     if(!parsingSuccessful)
  12.         return false;
  13.  
  14.     TileSize tileSize;
  15.     tileSize.x = root["tilesets"][0u]["tilewidth"].asInt();
  16.     tileSize.y = root["tilesets"][0u]["tileheight"].asInt();
  17.     tileSize.s = root["tilesets"][0u]["spacing"].asInt();
  18.  
  19.     for(Json::Value &layer: root["layers"]) {
  20.         if(layer["name"].asString() != "objects")
  21.             loadLayer(layer, objects, tileSize);
  22.         else
  23.             loadObjects(root, layer, objects, tileSize);
  24.     }
  25.  
  26.     sf::Texture *tileset = new sf::Texture();
  27.     tileset->loadFromFile("resources/maps/" + root["tilesets"][0u]["image"].asString());
  28.  
  29.     for(Object *object: objects) {
  30.         object->texture = tileset;
  31.     }
  32.  
  33.     return true;
  34. }
  35.  
  36. void MapMaker::loadLayer(Json::Value &layer, std::list<Object *> &objects, TileSize tileSize) {
  37.  
  38.     MapLayer *tmp = new MapLayer(tileSize);
  39.  
  40.     tmp->width = layer["width"].asInt();
  41.     tmp->height = layer["height"].asInt();
  42.  
  43.     memset(tmp->tileMap, 0, sizeof(tmp->tileMap));
  44.  
  45.     for(size_t i = 0; i < layer["data"].size(); ++i)
  46.         tmp->tileMap[i] = layer["data"][(int)i].asInt();
  47.  
  48.     tmp->isCollisionLayer = layer["properties"]["Collision"].asBool();
  49.     tmp->isDestructibleLayer = layer["properties"]["Destructible"].asBool();
  50.  
  51.     objects.push_back(tmp);
  52. }
  53.  
  54. void MapMaker::loadObjects(Json::Value &root, Json::Value &layer, std::list<Object *> &objects, TileSize tileSize) {
  55.  
  56.     for(Json::Value &object: layer["objects"]) {
  57.         Sprite *sprite = new Sprite(tileSize);
  58.  
  59.         sprite->x = object["x"].asInt();
  60.         sprite->y = object["y"].asInt();
  61.         sprite->id = object["gid"].asInt();
  62.  
  63.         Json::Value &tileInfo = root["tilesets"][0u]["tiles"][std::to_string(sprite->id - 1)];
  64.         sprite->frame = 0;
  65.         sprite->frameCount = tileInfo["animation"].size();
  66.         sprite->frameDuration = tileInfo["animation"][0u]["duration"].asInt();
  67.  
  68.         objects.push_back(sprite);
  69.     }
  70. }
RAW Paste Data
Top