/// <summary> /// Creates a TerrainObjectType. /// </summary> /// <param name="name">The name of the TerrainObjectType.</param> /// <param name="imgData">The byte sequence that contains the image data of the TerrainObjectType.</param> /// <param name="quadSize">The size of the TerrainObjectType in quadratic tiles.</param> /// <param name="transparentColor">The transparent color of the TerrainObjectType.</param> public void CreateTerrainObjectType(string name, byte[] imgData, RCIntVector quadSize, RCColor transparentColor) { if (this.isFinalized) { throw new InvalidOperationException("It is not possible to create new TerrainObjectType for a finalized TileSet!"); } if (name == null) { throw new ArgumentNullException("name"); } if (this.terrainObjectTypes.ContainsKey(name)) { throw new TileSetException(string.Format("TerrainObjectType with name '{0}' already exists!", name)); } TerrainObjectType newTerrainObject = new TerrainObjectType(name, imgData, quadSize, transparentColor, this); newTerrainObject.SetIndex(this.allTerrainObjectList.Count); this.terrainObjectTypes.Add(name, newTerrainObject); this.allTerrainObjectList.Add(newTerrainObject); }
/// <summary> /// Load a tile-constraint for a terrain object from the given XML element. /// </summary> /// <param name="fromElem">The XML element to load from.</param> /// <param name="terrainObj">The terrain object.</param> /// <param name="tileset">The tileset being loaded.</param> /// <returns>The loaded tile-constraint.</returns> private static ITerrainObjectConstraint LoadTileConstraint(XElement fromElem, TerrainObjectType terrainObj, TileSet tileset) { /// Load the attributes of the constraint. XAttribute quadCoordsAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_TILECONSTRAINT_QUADCOORDS_ATTR); XAttribute terrainAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_TILECONSTRAINT_TERRAIN_ATTR); XAttribute terrainAAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_TILECONSTRAINT_TERRAINA_ATTR); XAttribute terrainBAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_TILECONSTRAINT_TERRAINB_ATTR); XAttribute combinationsAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINOBJ_TILECONSTRAINT_COMBINATIONS_ATTR); if (quadCoordsAttr == null) { throw new TileSetException("Quadratic coordinates not defined for tile constraint element!"); } if (terrainAttr != null && (terrainAAttr != null || terrainBAttr != null || combinationsAttr != null)) { throw new TileSetException("Invalid attributes defined for tile constraint on a simple tile!"); } if (terrainAttr == null && (terrainAAttr == null || terrainBAttr == null || combinationsAttr == null)) { throw new TileSetException("Invalid attributes defined for tile constraint on a mixed tile!"); } RCIntVector quadCoords = XmlHelper.LoadIntVector(quadCoordsAttr.Value); if (terrainObj.IsExcluded(quadCoords)) { throw new TileSetException(string.Format("TileConstraint at excluded coordinates {0} cannot be defined!", quadCoords)); } if (terrainAttr != null) { TerrainType terrain = tileset.GetTerrainTypeImpl(terrainAttr.Value); return(new IsoTileConstraint(quadCoords, terrain, tileset)); } else { TerrainType terrainA = tileset.GetTerrainTypeImpl(terrainAAttr.Value); TerrainType terrainB = tileset.GetTerrainTypeImpl(terrainBAttr.Value); List <TerrainCombination> combinations = new List <TerrainCombination>(); string[] combinationStrings = combinationsAttr.Value.Split(';'); if (combinationStrings.Length == 0) { throw new TileSetException("Terrain combination not defined for tile constraint on a mixed tile!"); } foreach (string combStr in combinationStrings) { TerrainCombination combination; if (!EnumMap <TerrainCombination, string> .TryDemap(combStr, out combination) || combination == TerrainCombination.Simple) { throw new TileSetException(string.Format("Unexpected terrain combination '{0}' defined for tile constraint!", combStr)); } combinations.Add(combination); } return(new IsoTileConstraint(quadCoords, terrainA, terrainB, combinations, tileset)); } }
/// <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! } }