/// <summary> /// Reads the TMX file related to an Area. /// </summary> /// <param name="area">The area.</param> /// <param name="data">The data.</param> /// <exception cref="System.Exception">No or faulty 'Type' property set for tileLayer. Accepted values are 'Low' and 'High'.</exception> private void ReadTMX(ref Area area, DataSet data) { TileSetBox tileSets = new TileSetBox(); foreach(DataRow setData in data.Tables["tileset"].Rows) { tileSets.Add(setData); } area.TileSets = tileSets; #region Read TileLayers foreach(DataRow layerData in data.Tables["layer"].Rows) { try { if (layerData["visible"].ToString().Equals("0")) continue; } catch { } Point layerDims = new Point(int.Parse(layerData["width"].ToString()), int.Parse(layerData["height"].ToString())); int[,] tileGrid = new int[layerDims.X, layerDims.Y]; int x = 0; int y = 0; foreach (DataRow mapData in layerData.GetChildRows("layer_data")) { foreach (DataRow tileData in mapData.GetChildRows("data_tile")) { tileGrid[x, y] = int.Parse(tileData["gid"].ToString()); if (x < layerDims.X - 1) ++x; else { x = 0; ++y; } } } TileLayer tileLayer = new TileLayer(tileGrid, area); foreach (DataRow properties in layerData.GetChildRows("layer_properties")) { foreach (DataRow propertyData in properties.GetChildRows("properties_property")) { if (propertyData["name"].Equals("Type") || propertyData["name"].Equals("type")) { if (propertyData["value"].Equals("Low") || propertyData["value"].Equals("low")) { area.AddLowLayer(tileLayer); continue; } if (propertyData["value"].Equals("High") || propertyData["value"].Equals("high")) { area.AddHighLayer(tileLayer); continue; } throw new Exception("No or faulty 'Type' property set for tileLayer. Accepted values are 'Low' and 'High'."); } } } } #endregion #region Read Objectgroups EntityBuilder.BeginArea(area); foreach (DataRow groupData in data.Tables["objectgroup"].Rows) { try { if (groupData["visible"].ToString().Equals("0")) continue; } catch { } switch (XMLParser.ValueOfProperty( groupData.GetChildRows("objectgroup_properties")[0], "Type")) { case "Entities": ReadEntityLayer(ref area, groupData); break; case "RoamZones": ReadRoamZones(groupData); break; } } EntityBuilder.EndArea(); #endregion }
public Entity CreateEntity(TileSetBox tilesetBox, DataRow data) { Entity entity = CreateEntity( tilesetBox.TypeOf( int.Parse( data["gid"].ToString() ))); entity.Position = new Vector2( int.Parse(data["x"].ToString()) + tilesetBox.DimensionsOf(int.Parse(data["gid"].ToString())).X * .5f, int.Parse(data["y"].ToString())); // Handle special types #region Chest //Creates a standard Dialog providing the item specified in the Tiled editor if (entity.Categorie.Equals("Chest")) { Conversation con = new Conversation("default"); //try //{ con.Add(new ItemContainer( SwitchItemType(data.GetChildRows("object_properties")[0]))); //} //catch { throw new Exception("A Chest is missing an Item."); } con.Add(new KillSpeaker()); List<Conversation> cons = new List<Conversation>(); cons.Add(con); entity.AddModule(new Dialog(cons)); } #endregion #region Door if (entity.Categorie.Equals("Door")) { entity.AddModule(new OpenDoorOnCollide(data)); } #endregion #region JumpPoint if (entity.Categorie.Equals("JumpPoint")) { DataRow jumpProps = data.GetChildRows("object_properties")[0]; entity.AddModule(new JumpPoint( XMLParser.ValueOfProperty(jumpProps, "ID"), XMLParser.ValueOfProperty(jumpProps, "Target"))); _jumpPoints.Add(entity); } #endregion #region Check for demanded zone try { string zoneID = XMLParser.ValueOfProperty(data.GetChildRows("object_properties")[0], "ZoneID"); // Check if the entity relates to a Zone if (zoneID != null) { entity.SetZone(FindMatch(int.Parse(zoneID))); } } catch { } #endregion return entity; }