/// <summary> /// Adds a constraint to this TerrainObjectType. Area exclusion is not possible after calling this method. /// </summary> /// <param name="constraint">The constraint to add.</param> public void AddConstraint(ITerrainObjectConstraint constraint) { if (constraint == null) { throw new ArgumentNullException("constraint"); } if (constraint.Tileset != this.tileset) { throw new TileSetException("The given ITerrainObjectConstraint is in another TileSet!"); } if (this.tileset.IsFinalized) { throw new InvalidOperationException("TileSet already finalized!"); } this.constraints.Add(constraint); this.areaCanBeExcluded = false; }
/// <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! } }