/// <summary> /// Retrieve each map layer and forward it to the appropriate function. /// </summary> /// <param name="data">The map's data to be parsed.</param> void ParseLayers(JSONNode data) { // Retrieve layers property (also provided by Tiled maps). int layersCount = TiledMapParser.GetLayersCount(); Debug.Log("Amount of layers is: " + layersCount); // Create the moveable matrix. This is used to check what tile is moveable (also used to calculate Shortest Path). moveableTilesMatrix = new bool[mapWidth, mapHeight]; // Create the moveable matrix. This is used to check what is the tile weight (used to calculate Shortest Path). tilesWeightMatrix = new int[mapWidth, mapHeight]; // Process each layer. for (int i = 0; i < layersCount; i++) { /** * SECOND, we need to set the layer we are parsing. We already set the data we are processing in SetupRootNode(), * now we set the layer to be worked. */ TiledMapParser.SetupCurrentLayer(i); // Retrieve layer property (also provided by Tiled maps). string layerName = TiledMapParser.GetLayerName(); Debug.Log("Parsing layer: \"" + layerName + "\""); switch (layerName) { case "TilesLayer0": case "TilesLayer1": case "TilesLayer2": Debug.Log("Parse tiles layer: " + layerName); ParseTileLayer(i); break; case "TilesObjectsLayer0": // This is much as ParseTileLayer(). We check if there is objects in each map position. In positive case, we // retrieve the moveable and weight property. These are "tile objects" and don't interact with the characters. //ParseTileObjectLayer(i); break; case "ObjectsLayer": Debug.Log("Parse objects layer: " + layerName); ParseObjects(i); break; case "EventsLayer": // Much same as ParseObjects(). //Debug.Log("Parse events layer: " + EventsLayerIdx); //EventsParser.ParseLayer(i); break; default: Assert.IsTrue(false, "Received unhandled layer. Name: " + layerName + " - ID: " + i); break; } } }
/// <summary> /// Parse objects from the map. /// </summary> /// <param name="tilesedIdx">The corresponding tileset index.</param> public void ParseObjects(int tilesedIdx) { /** * Set tileset index for this layer. */ TiledMapParser.SetupCurrentTileset(tilesedIdx); // Assert layer name. Assert.AreEqual<string>("ObjectsLayer", TiledMapParser.GetLayerName()); // "objects" is also a special layer from tile that we can create to spawn objects. int objCount = TiledMapParser.GetLayerObjectsCount(); for (int objIdx = 0; objIdx < objCount; objIdx++) { /** * WHILE parsing objects, we have the object index in our JSON data file, but we must find the corresponding * tileset identifier for the object in order to retrieve its properties. */ int tileID = TiledMapParser.GetLayerObjectTileID(objIdx); // Some shared properties provided by Tiled. int objRot = TiledMapParser.GetObjectRotation(objIdx); // FindObjectRealX/Y allows to find the corresponding X and Y position of the object in the Unity coordinates. // The object is put in the center of the tile. int objPosX = TiledMapParser.FindObjectRealX(objIdx, mapWidth, tileWidth); int objPosY = TiledMapParser.FindObjectRealY(objIdx, mapWidth, tileWidth); Debug.Log("Spawn: " + tileID + " pos X: " + objPosX + " - pos Y: " + objPosY + " - obj rot: " + objRot); /** This is very specific stuff. I want to show two things here: * * Here, we can parse a string property directly as an Enumerator. * * ObjectType objType = (ObjectType)Enum.Parse(typeof(ObjectType), TiledMapParser.GetTileStringProperty(tileID, ObjectTypeProperty)); */ /** Here, we are retrieving a unique property from the object. This property does NOT come from the tileset, but from object's data. * Unique properties are set directly in the object in the map. It's useful to set custom properties for each object (as the kind of * items a chest will drop when opened). * */ int objAmount = TiledMapParser.GetObjectIntUniqueProperty(objIdx, "Amount"); Debug.Log("Object amount: " + objAmount); } Debug.Log("Total parsed objects: " + objCount); }