/// <summary> /// Creates a mixed tile type for the given terrain types. /// </summary> /// <param name="terrainTypeA">The name of the first terrain type.</param> /// <param name="terrainTypeB">The name of the second terrain type.</param> /// <param name="combination">The combination of the terrain types in the new mixed tile type.</param> /// <remarks>Terrain type A must be the parent of terrain type B.</remarks> public void CreateMixedTileType(string terrainTypeA, string terrainTypeB, TerrainCombination combination) { if (this.isFinalized) { throw new InvalidOperationException("It is not possible to create new tile type for a finalized TileSet!"); } if (terrainTypeA == null) { throw new ArgumentNullException("terrainTypeA"); } if (terrainTypeB == null) { throw new ArgumentNullException("terrainTypeB"); } if (combination == TerrainCombination.Simple) { throw new ArgumentException("combination", "Invalid combination for a mixed tile type!"); } Tuple <string, string, TerrainCombination> key = new Tuple <string, string, TerrainCombination>(terrainTypeA, terrainTypeB, combination); if (this.mixedTileTypes.ContainsKey(key)) { throw new TileSetException(string.Format("Mixed tile type for terrain types '{0}' and '{1}' with combination '{2}' already exists!", terrainTypeA, terrainTypeB, combination)); } IsoTileType newTile = new IsoTileType(terrainTypeA, terrainTypeB, combination, this); this.mixedTileTypes.Add(key, newTile); }
/// <summary> /// Loads a tile variant from the given XML element. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tile">The tile type being loaded.</param> /// <param name="tileset">The tileset being loaded.</param> private static void LoadVariant(XElement fromElem, IsoTileType tile, TileSet tileset) { XAttribute imageAttr = fromElem.Attribute(XmlTileSetConstants.VARIANT_IMAGE_ATTR); XAttribute transpColorAttr = fromElem.Attribute(XmlTileSetConstants.VARIANT_TRANSPCOLOR_ATTR); if (imageAttr == null) { throw new TileSetException("Image not defined for tile variant!"); } if (transpColorAttr == null) { throw new TileSetException("Transparent color not defined for tile variant!"); } /// Read the image data. string imagePath = Path.Combine(tmpImageDir, imageAttr.Value); byte[] imageData = File.ReadAllBytes(imagePath); /// Create the new TileVariant object and add it to the tile type. IsoTileVariant newVariant = new IsoTileVariant(imageData, XmlHelper.LoadColor(transpColorAttr.Value), tileset); tile.AddVariant(newVariant); /// Load the cell data changesets. foreach (XElement childElem in fromElem.Elements()) { ICellDataChangeSet changeset = LoadCellDataChangeSet(childElem, tileset); newVariant.AddCellDataChangeset(changeset); } /// Register the variant to the tileset. tileset.RegisterVariant(newVariant); }
/// <summary> /// Loads a mixed tile type from the XML element into the given tileset. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tileset">The TileSet to load to.</param> private static void LoadMixedTile(XElement fromElem, TileSet tileset) { XAttribute terrainAAttr = fromElem.Attribute(XmlTileSetConstants.MIXEDTILE_TERRAINA_ATTR); XAttribute terrainBAttr = fromElem.Attribute(XmlTileSetConstants.MIXEDTILE_TERRAINB_ATTR); XAttribute combinationAttr = fromElem.Attribute(XmlTileSetConstants.MIXEDTILE_COMBINATION_ATTR); if (terrainAAttr == null) { throw new TileSetException("Terrain type A not defined for mixed tile!"); } if (terrainBAttr == null) { throw new TileSetException("Terrain type B not defined for mixed tile!"); } if (combinationAttr == null) { throw new TileSetException("Terrain combination not defined for mixed tile!"); } TerrainCombination combination; if (!EnumMap <TerrainCombination, string> .TryDemap(combinationAttr.Value, out combination)) { throw new TileSetException(string.Format("Unexpected terrain combination '{0}' defined for mixed tile.", combinationAttr.Value)); } tileset.CreateMixedTileType(terrainAAttr.Value, terrainBAttr.Value, combination); IsoTileType tile = tileset.GetIsoTileTypeImpl(terrainAAttr.Value, terrainBAttr.Value, combination); LoadVariants(fromElem, tile, tileset); }
/// <summary> /// Loads a simple tile type from the XML element into the given tileset. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tileset">The TileSet to load to.</param> private static void LoadSimpleTile(XElement fromElem, TileSet tileset) { XAttribute terrainAttr = fromElem.Attribute(XmlTileSetConstants.SIMPLETILE_TERRAIN_ATTR); if (terrainAttr == null) { throw new TileSetException("Terrain type not defined for simple tile!"); } tileset.CreateSimpleTileType(terrainAttr.Value); IsoTileType tile = tileset.GetIsoTileTypeImpl(terrainAttr.Value); LoadVariants(fromElem, tile, tileset); }
/// <summary> /// Loads a conditional branch of a tile type from the given XML element. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tile">The tile type being loaded.</param> /// <param name="tileset">The tileset being loaded.</param> private static void LoadBranch(XElement fromElem, IsoTileType tile, TileSet tileset) { /// Check whether the branch element has exactly 2 child elements. XElement conditionElem = null; XElement actionElem = null; int i = 0; foreach (XElement child in fromElem.Elements()) { if (i == 0) { conditionElem = child; } else if (i == 1) { actionElem = child; } else { throw new TileSetException("Unexpected nodes in conditional branch!"); } i++; } if (i != 2) { throw new TileSetException("Missing nodes in conditional branch!"); } /// Check the action element. if (actionElem.Name != XmlTileSetConstants.THEN_ELEM) { throw new TileSetException(string.Format("Unexpected node '{0}' at conditional branch!", actionElem.Name)); } /// Load the condition and start defining the conditional branch. IIsoTileCondition condition = LoadCondition(conditionElem, tileset); tile.BeginConditionalBranch(condition); /// Load the variants of the conditional branch. foreach (XElement variantElem in actionElem.Elements(XmlTileSetConstants.VARIANT_ELEM)) { LoadVariant(variantElem, tile, tileset); } /// Close the conditional branch. tile.EndConditionalBranch(); }
/// <summary> /// Loads the variants of a tile type from the XML element into the given tileset. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tile">The tile type to load to.</param> /// <param name="tileset">The tileset of the tile type.</param> private static void LoadVariants(XElement fromElem, IsoTileType tile, TileSet tileset) { TileReadStatus status = TileReadStatus.None; foreach (XElement childElem in fromElem.Elements()) { if (status == TileReadStatus.None) { /// We are in the initial read status. if (childElem.Name == XmlTileSetConstants.VARIANT_ELEM) { /// No conditional expression at the current tile type. status = TileReadStatus.NoCondition; LoadVariant(childElem, tile, tileset); } else if (childElem.Name == XmlTileSetConstants.IF_ELEM) { /// Beginning of the conditional branch. status = TileReadStatus.ConditionalBranch; LoadBranch(childElem, tile, tileset); } else { /// Other XML elements not allowed at this read status. throw new TileSetException(string.Format("Unexpected node '{0}'!", childElem.Name)); } } else if (status == TileReadStatus.NoCondition) { /// We are in the read status where only variant elements are allowed. if (childElem.Name == XmlTileSetConstants.VARIANT_ELEM) { LoadVariant(childElem, tile, tileset); } else { /// Other XML elements not allowed at this read status. throw new TileSetException(string.Format("Unexpected node '{0}'!", childElem.Name)); } } else if (status == TileReadStatus.ConditionalBranch) { /// We are in the read status where only conditional branch and default branch elements are allowed. if (childElem.Name == XmlTileSetConstants.ELSEIF_ELEM) { /// Load the conditional branch. LoadBranch(childElem, tile, tileset); } else if (childElem.Name == XmlTileSetConstants.ELSE_ELEM) { /// Load the default branch. status = TileReadStatus.DefaultBranch; foreach (XElement varElem in childElem.Elements(XmlTileSetConstants.VARIANT_ELEM)) { LoadVariant(varElem, tile, tileset); } } else { /// Other XML elements not allowed at this read status. throw new TileSetException(string.Format("Unexpected node '{0}'!", childElem.Name)); } } else { /// Other XML elements not allowed. throw new TileSetException(string.Format("Unexpected node '{0}' after default branch!", childElem.Name)); } } }