Ejemplo n.º 1
0
        /// <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));
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads the terrain tree from the given XML element into the given tileset.
        /// </summary>
        /// <param name="fromElem">The XML element to load from.</param>
        /// <param name="parent">
        /// The parent of the currently loaded TerrainType of null if the root is being loaded.
        /// </param>
        /// <param name="tileset">The TileSet to load to.</param>
        private static void LoadTerrainTree(XElement fromElem, TerrainType parent, TileSet tileset)
        {
            XAttribute nameAttr        = fromElem.Attribute(XmlTileSetConstants.TERRAINTYPE_NAME_ATTR);
            XAttribute transLengthAttr = fromElem.Attribute(XmlTileSetConstants.TERRAINTYPE_TRANSLENGTH_ATTR);

            if (nameAttr == null)
            {
                throw new TileSetException("TerrainType name not defined!");
            }
            if (parent == null && transLengthAttr != null)
            {
                throw new TileSetException("Transition length cannot be defined for the root TerrainType!");
            }

            /// Create the terrain type and add it to it's parent as a child.
            tileset.CreateTerrainType(nameAttr.Value);
            if (parent != null)
            {
                if (transLengthAttr != null)
                {
                    int transLength = XmlHelper.LoadInt(transLengthAttr.Value);
                    if (transLength < 0)
                    {
                        throw new TileSetException("Transition length must be non-negative!");
                    }
                    parent.AddChild(nameAttr.Value, transLength);
                }
                else
                {
                    parent.AddChild(nameAttr.Value);
                }
            }

            /// Process the child XML elements.
            TerrainType currentTerrain = tileset.GetTerrainTypeImpl(nameAttr.Value);

            foreach (XElement childTerrainElem in fromElem.Elements(XmlTileSetConstants.TERRAINTYPE_ELEM))
            {
                LoadTerrainTree(childTerrainElem, currentTerrain, tileset);
            }
        }