/// <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> /// Adds a cell data changeset to this terrain object type. /// </summary> /// <param name="changeset">The changeset operation to add.</param> public void AddCellDataChangeset(ICellDataChangeSet changeset) { if (changeset == null) { throw new ArgumentNullException("changeset"); } if (changeset.Tileset != this.tileset) { throw new TileSetException("The given ICellDataChangeSet is in another TileSet!"); } if (this.tileset.IsFinalized) { throw new InvalidOperationException("TileSet already finalized!"); } this.cellDataChangesets.Add(changeset); }
/// <summary> /// Load a cell data changeset from the given XML element. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="tileset">The tileset being loaded.</param> /// <returns>The loaded cell data changeset.</returns> private static ICellDataChangeSet LoadCellDataChangeSet(XElement fromElem, TileSet tileset) { ICellDataChangeSet retObj = null; ICellDataModifier modifier = null; /// Load the name of the target field. XAttribute fieldAttr = fromElem.Attribute(XmlTileSetConstants.CELLDATACHANGESET_FIELD_ATTR); if (fieldAttr == null) { throw new TileSetException("Field name not defined for a data changeset element!"); } if (fieldAttr.Value == XmlTileSetConstants.CELLDATA_ISWALKABLE_NAME) { modifier = new WalkabilityFlagModifier(XmlHelper.LoadBool(fromElem.Value)); } else if (fieldAttr.Value == XmlTileSetConstants.CELLDATA_ISBUILDABLE_NAME) { modifier = new BuildabilityFlagModifier(XmlHelper.LoadBool(fromElem.Value)); } else if (fieldAttr.Value == XmlTileSetConstants.CELLDATA_GROUNDLEVEL_NAME) { modifier = new GroundLevelModifier(XmlHelper.LoadInt(fromElem.Value)); } if (modifier == null) { throw new TileSetException("Unexpected field name defined for a data changeset element!"); } switch (fromElem.Name.LocalName) { case XmlTileSetConstants.CELLDATACHANGESET_ALL_ELEM: retObj = new CellDataChangeSetBase(modifier, tileset); break; case XmlTileSetConstants.CELLDATACHANGESET_CELL_ELEM: XAttribute cellAttr = fromElem.Attribute(XmlTileSetConstants.CELLDATACHANGESET_CELL_CELL_ATTR); if (cellAttr == null) { throw new TileSetException("Cell not defined for a cell data changeset element!"); } retObj = new CellChangeSet(XmlHelper.LoadIntVector(cellAttr.Value), modifier, tileset); break; case XmlTileSetConstants.CELLDATACHANGESET_COL_ELEM: XAttribute colIndexAttr = fromElem.Attribute(XmlTileSetConstants.CELLDATACHANGESET_COL_INDEX_ATTR); if (colIndexAttr == null) { throw new TileSetException("Column not defined for a column data changeset element!"); } retObj = new ColumnChangeSet(XmlHelper.LoadInt(colIndexAttr.Value), modifier, tileset); break; case XmlTileSetConstants.CELLDATACHANGESET_QUARTER_ELEM: XAttribute quarterAttr = fromElem.Attribute(XmlTileSetConstants.CELLDATACHANGESET_QUARTER_WHICH_ATTR); if (quarterAttr == null) { throw new TileSetException("Quarter not defined for a quarter data changeset element!"); } MapDirection quarter; if (!EnumMap <MapDirection, string> .TryDemap(quarterAttr.Value, out quarter)) { throw new TileSetException(string.Format("Unexpected quarter '{0}' defined for quarter data changeset!", quarterAttr.Value)); } retObj = new IsoQuarterChangeSet(quarter, modifier, tileset); break; case XmlTileSetConstants.CELLDATACHANGESET_RECT_ELEM: XAttribute rectAttr = fromElem.Attribute(XmlTileSetConstants.CELLDATACHANGESET_RECT_RECT_ATTR); if (rectAttr == null) { throw new TileSetException("Rectangle not defined for a rectangle data changeset element!"); } retObj = new RectangleChangeSet(XmlHelper.LoadIntRectangle(rectAttr.Value), modifier, tileset); break; case XmlTileSetConstants.CELLDATACHANGESET_ROW_ELEM: XAttribute rowIndexAttr = fromElem.Attribute(XmlTileSetConstants.CELLDATACHANGESET_ROW_INDEX_ATTR); if (rowIndexAttr == null) { throw new TileSetException("Row not defined for a row data changeset element!"); } retObj = new RowChangeSet(XmlHelper.LoadInt(rowIndexAttr.Value), modifier, tileset); break; default: throw new TileSetException(string.Format("Unexpected data changeset element '{0}'!", fromElem.Name)); } if (retObj == null) { throw new TileSetException("Unable to load cell data changeset!"); } return(retObj); }
/// <summary> /// Loads a terrain object definition 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 LoadTerrainObject(XElement fromElem, TileSet tileset) { XAttribute nameAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_NAME_ATTR); XAttribute imageAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_IMAGE_ATTR); XAttribute quadSizeAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_QUADSIZE_ATTR); XAttribute transpColorAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_TRANSPCOLOR_ATTR); if (nameAttr == null) { throw new TileSetException("Name not defined for terrain object!"); } if (imageAttr == null) { throw new TileSetException("Image not defined for terrain object!"); } if (quadSizeAttr == null) { throw new TileSetException("Quadratic size not defined for terrain object!"); } if (transpColorAttr == null) { throw new TileSetException("Transparent color not defined for terrain object!"); } /// Read the image data. string imagePath = Path.Combine(tmpImageDir, imageAttr.Value); byte[] imageData = File.ReadAllBytes(imagePath); tileset.CreateTerrainObjectType(nameAttr.Value, imageData, XmlHelper.LoadIntVector(quadSizeAttr.Value), XmlHelper.LoadColor(transpColorAttr.Value)); TerrainObjectType terrainObj = tileset.GetTerrainObjectTypeImpl(nameAttr.Value); /// Apply the defined area exclusions. foreach (XElement excludeAreaElem in fromElem.Elements(XmlTileSetConstants.TERRAINOBJ_EXCLUDEAREA_ELEM)) { XAttribute rectAttr = excludeAreaElem.Attribute(XmlTileSetConstants.TERRAINOBJ_EXCLUDEAREA_RECT_ATTR); if (rectAttr == null) { throw new TileSetException("The rectangle of the excluded area not defined!"); } terrainObj.ExcludeArea(XmlHelper.LoadIntRectangle(rectAttr.Value)); } /// Load the constraints and the cell data changesets. foreach (XElement childElem in fromElem.Elements()) { if (childElem.Name.LocalName == XmlTileSetConstants.TERRAINOBJ_TILECONSTRAINT_ELEM) { ITerrainObjectConstraint constraint = LoadTileConstraint(childElem, terrainObj, tileset); terrainObj.AddConstraint(constraint); } else if (childElem.Name.LocalName != XmlTileSetConstants.TERRAINOBJ_EXCLUDEAREA_ELEM) { ICellDataChangeSet changeset = LoadCellDataChangeSet(childElem, tileset); terrainObj.AddCellDataChangeset(changeset); } /// TODO: loading other constraint types can take place here! } }