/// <summary>Loads the TMX map and textures if loadTextures is true at the given path.</summary> public static TmxMap Load(string path, bool loadTextures) { var map = new TmxMap(); JObject jMap; if (!File.Exists(path)) throw new FileNotFoundException("Could not load TMX map, file not found.", path); using (var fs = File.OpenText(path)) { string json = fs.ReadToEnd(); jMap = JsonConvert.DeserializeObject<JObject>(json); } int version = jMap["version"].Value<int>(); if (version != Version) throw new NotImplementedException("Implementation for TMX file version \"" + version + "\" is not supported. Supported version is \"" + Version + "\"."); map.Width = jMap["width"].Value<int>(); map.Height = jMap["height"].Value<int>(); map.TileWidth = jMap["tilewidth"].Value<int>(); map.TileHeight = jMap["tileheight"].Value<int>(); map.Orientation = parseOrientation(jMap["orientation"].Value<string>()); map.BackgroundColor = Utility.HexToColor(jMap["backgroundcolor"].Value<string>()); var jMapProperties = jMap["properties"]; if (jMapProperties != null) map.Properties = ParseTmxProperties(jMapProperties.Value<JObject>()); else map.Properties = new TmxProperties(); var jTilesetArray = jMap["tilesets"].Value<JArray>(); map.Tilesets = new TmxTilesetCollection(); for (int i = 0; i < jTilesetArray.Count; ++i) { var tileset = new TmxTileset(); map.Tilesets.Add(tileset); var jTileset = jTilesetArray[i].Value<JObject>(); tileset.FirstGid = jTileset["firstgid"].Value<uint>(); tileset.Margin = jTileset["margin"].Value<int>(); tileset.Spacing = jTileset["spacing"].Value<int>(); tileset.Name = jTileset["name"].Value<string>(); tileset.TileWidth = jTileset["tilewidth"].Value<int>(); tileset.TileHeight = jTileset["tileheight"].Value<int>(); tileset.ImageWidth = jTileset["imagewidth"].Value<int>(); tileset.ImageHeight = jTileset["imageheight"].Value<int>(); tileset.ImageSource = jTileset["image"].Value<string>(); var jProperties = jTileset["properties"]; if (jProperties != null) tileset.Properties = ParseTmxProperties(jProperties.Value<JObject>()); else tileset.Properties = new TmxProperties(); if (loadTextures) { var texturePath = Path.Combine(Path.GetDirectoryName(path), jTileset["image"].Value<string>()); tileset.Texture = Utility.LoadTexture2D(texturePath, false); } tileset.InitializeTiles(); } var jLayerArray = jMap["layers"].Value<JArray>(); map.Layers = new TmxLayerCollection(); for (int i = 0; i < jLayerArray.Count; ++i) { var layer = new TmxLayer(); map.Layers.Add(layer); var jLayer = jLayerArray[i].Value<JObject>(); layer.Width = jLayer["width"].Value<int>(); layer.Height = jLayer["height"].Value<int>(); layer.Name = jLayer["name"].Value<string>(); layer.Opacity = jLayer["opacity"].Value<float>(); layer.Type = parseLayerType(jLayer["type"].Value<string>()); layer.Visible = jLayer["visible"].Value<bool>(); layer.XOffset = jLayer["x"].Value<int>(); layer.YOffset = jLayer["y"].Value<int>(); var jProperties = jLayer["properties"]; if (jProperties != null) layer.Properties = ParseTmxProperties(jProperties.Value<JObject>()); else layer.Properties = new TmxProperties(); switch (layer.Type) { case TmxLayerType.Tile: var jData = jLayer["data"].Value<JArray>(); layer.Data = new uint[jData.Count]; for (int j = 0; j < jData.Count; ++j) layer.Data[j] = jData[j].Value<uint>(); break; case TmxLayerType.Object: var jObjects = jLayer["objects"].Value<JArray>(); layer.Objects = new TmxObjectCollection(); for (int j = 0; j < jObjects.Count; ++j) { var jObject = jObjects[j].Value<JObject>(); var tmxObject = new TmxObject(); layer.Objects.Add(tmxObject); tmxObject.Name = jObject["name"].Value<string>(); tmxObject.Width = jObject["width"].Value<int>(); tmxObject.Height = jObject["height"].Value<int>(); tmxObject.X = jObject["x"].Value<int>(); tmxObject.Y = jObject["y"].Value<int>(); tmxObject.Type = jObject["type"].Value<string>(); tmxObject.Visible = jObject["visible"].Value<bool>(); var properties = jObject["properties"]; if (properties != null) tmxObject.Properties = ParseTmxProperties(properties.Value<JObject>()); } break; case TmxLayerType.Image: throw new NotImplementedException(); } } return map; }
/// <summary>Loads the TMX map and textures if loadTextures is true at the given path.</summary> public static TmxMap Load(string path, bool loadTextures) { var map = new TmxMap(); JObject jMap; if (!File.Exists(path)) { throw new FileNotFoundException("Could not load TMX map, file not found.", path); } using (var fs = File.OpenText(path)) { string json = fs.ReadToEnd(); jMap = JsonConvert.DeserializeObject <JObject>(json); } int version = jMap["version"].Value <int>(); if (version != Version) { throw new NotImplementedException("Implementation for TMX file version \"" + version + "\" is not supported. Supported version is \"" + Version + "\"."); } map.Width = jMap["width"].Value <int>(); map.Height = jMap["height"].Value <int>(); map.TileWidth = jMap["tilewidth"].Value <int>(); map.TileHeight = jMap["tileheight"].Value <int>(); map.Orientation = parseOrientation(jMap["orientation"].Value <string>()); map.BackgroundColor = Utility.HexToColor(jMap["backgroundcolor"].Value <string>()); var jMapProperties = jMap["properties"]; if (jMapProperties != null) { map.Properties = ParseTmxProperties(jMapProperties.Value <JObject>()); } else { map.Properties = new TmxProperties(); } var jTilesetArray = jMap["tilesets"].Value <JArray>(); map.Tilesets = new TmxTilesetCollection(); for (int i = 0; i < jTilesetArray.Count; ++i) { var tileset = new TmxTileset(); map.Tilesets.Add(tileset); var jTileset = jTilesetArray[i].Value <JObject>(); tileset.FirstGid = jTileset["firstgid"].Value <uint>(); tileset.Margin = jTileset["margin"].Value <int>(); tileset.Spacing = jTileset["spacing"].Value <int>(); tileset.Name = jTileset["name"].Value <string>(); tileset.TileWidth = jTileset["tilewidth"].Value <int>(); tileset.TileHeight = jTileset["tileheight"].Value <int>(); tileset.ImageWidth = jTileset["imagewidth"].Value <int>(); tileset.ImageHeight = jTileset["imageheight"].Value <int>(); tileset.ImageSource = jTileset["image"].Value <string>(); var jProperties = jTileset["properties"]; if (jProperties != null) { tileset.Properties = ParseTmxProperties(jProperties.Value <JObject>()); } else { tileset.Properties = new TmxProperties(); } if (loadTextures) { var texturePath = Path.Combine(Path.GetDirectoryName(path), jTileset["image"].Value <string>()); tileset.Texture = Utility.LoadTexture2D(texturePath, false); } tileset.InitializeTiles(); } var jLayerArray = jMap["layers"].Value <JArray>(); map.Layers = new TmxLayerCollection(); for (int i = 0; i < jLayerArray.Count; ++i) { var layer = new TmxLayer(); map.Layers.Add(layer); var jLayer = jLayerArray[i].Value <JObject>(); layer.Width = jLayer["width"].Value <int>(); layer.Height = jLayer["height"].Value <int>(); layer.Name = jLayer["name"].Value <string>(); layer.Opacity = jLayer["opacity"].Value <float>(); layer.Type = parseLayerType(jLayer["type"].Value <string>()); layer.Visible = jLayer["visible"].Value <bool>(); layer.XOffset = jLayer["x"].Value <int>(); layer.YOffset = jLayer["y"].Value <int>(); var jProperties = jLayer["properties"]; if (jProperties != null) { layer.Properties = ParseTmxProperties(jProperties.Value <JObject>()); } else { layer.Properties = new TmxProperties(); } switch (layer.Type) { case TmxLayerType.Tile: var jData = jLayer["data"].Value <JArray>(); layer.Data = new uint[jData.Count]; for (int j = 0; j < jData.Count; ++j) { layer.Data[j] = jData[j].Value <uint>(); } break; case TmxLayerType.Object: var jObjects = jLayer["objects"].Value <JArray>(); layer.Objects = new TmxObjectCollection(); for (int j = 0; j < jObjects.Count; ++j) { var jObject = jObjects[j].Value <JObject>(); var tmxObject = new TmxObject(); layer.Objects.Add(tmxObject); tmxObject.Name = jObject["name"].Value <string>(); tmxObject.Width = jObject["width"].Value <int>(); tmxObject.Height = jObject["height"].Value <int>(); tmxObject.X = jObject["x"].Value <int>(); tmxObject.Y = jObject["y"].Value <int>(); tmxObject.Type = jObject["type"].Value <string>(); tmxObject.Visible = jObject["visible"].Value <bool>(); var properties = jObject["properties"]; if (properties != null) { tmxObject.Properties = ParseTmxProperties(properties.Value <JObject>()); } } break; case TmxLayerType.Image: throw new NotImplementedException(); } } return(map); }