TiledLib.Map.Map C# (CSharp) Method

Map() public method

public Map ( Microsoft.Xna.Framework.Content.ContentManager content, string mapName ) : System
content Microsoft.Xna.Framework.Content.ContentManager
mapName string
return System
        public Map(ContentManager content, string mapName)
        {
            string mapPath = AppDomain.CurrentDomain.BaseDirectory +
                "Content/" +
                mapName +
                ".tmx";

            TmxMap tmx = new TmxMap(mapPath);

            Version = new Version(tmx.Version);

            switch(tmx.Orientation)
            {
                case TmxMap.OrientationType.Isometric:
                    Orientation = Orientation.Isometric;
                    break;
                case TmxMap.OrientationType.Orthogonal:
                    Orientation = Orientation.Orthogonal;
                    break;
                case TmxMap.OrientationType.Staggered:
                    throw new Exception(
                        "TiledLib doesn't support maps with Staggered " +
                        "orientation.");
            }

            Width = tmx.Width;
            Height = tmx.Height;
            TileWidth = tmx.TileWidth;
            TileHeight = tmx.TileHeight;

            Properties = new PropertyCollection();
            foreach(KeyValuePair<string, string> kvp in tmx.Properties)
            {
                Properties.Add(kvp);
            }

            // Create a list for our tiles
            List<Tile> tiles = new List<Tile>();
            Tiles = new Collection<Tile>(tiles);

            // Read in each TileSet
            foreach (TmxTileset ts in tmx.Tilesets)
            {
                string tilesetName = ts.Name;
                Texture2D texture = content.Load<Texture2D>(ts.Image.Source);

                bool collisionSet = ts.Properties.ContainsKey("CollisionSet") &&
                    ts.Properties["CollisionSet"] == "True" ?
                    true : false;

                /* TODO:  Add this intelligence to TiledSharp.
                 * If the texture is bigger than a individual tile, infer all the
                 * additional tiles that must be created.
                 */

                if (texture.Width > ts.TileWidth || texture.Height > ts.TileHeight)
                {
                    // Deconstruct tileset to individual tiles
                    int id = 0;
                    for (int y = 0; y < texture.Height / ts.TileHeight; y++)
                    {
                        for (int x = 0; x < texture.Width / ts.TileWidth; x++)
                        {
                            Rectangle source = new Rectangle(
                                x * ts.TileWidth,
                                y * ts.TileHeight,
                                ts.TileWidth,
                                ts.TileHeight
                            );

                            Color[] collisionData = null;
                            bool[] collisionBitData = null;
                            PropertyCollection props = new PropertyCollection();

                            TmxTilesetTile tsTile = ts.Tiles.Find(i => i.Id == id);
                            if(tsTile != null)
                            {
                                foreach (KeyValuePair<string, string> kvp in
                                    tsTile.Properties)
                                {
                                    props.Add(kvp);

                                    // Inherit tilesets collision
                                    bool collidable = collisionSet;
                                    if (kvp.Key == "CollisionSet")
                                    {
                                        // Allow override per tile
                                        if (kvp.Value == "True")
                                        {
                                            collidable = true;
                                        } else
                                        {
                                            collidable = false;
                                        }
                                    }
                                    if (collidable)
                                    {
                                        int numOfBytes = ts.TileWidth * ts.TileHeight;
                                        collisionData = new Color[numOfBytes];
                                        collisionBitData = new bool[numOfBytes];

                                        texture.GetData<Color>(
                                            0,
                                            source,
                                            collisionData,
                                            0,
                                            numOfBytes
                                        );

                                        for (int col = 0; col < numOfBytes; col++)
                                        {
                                            if (collisionData[col].A > 0)
                                            {
                                                collisionBitData[col] = true;
                                            }
                                        }
                                        collisionData = null;
                                    }
                                }
                            }

                            Tile t = new Tile(
                                texture,
                                source,
                                props,
                                collisionBitData
                            );

                            while (id >= tiles.Count)
                            {
                                tiles.Add(null);
                            }
                            tiles.Insert(id + ts.FirstGid, t);
                            id++;
                        }
                    }
                }
            }

            // Process Map Items (layers, objectgroups, etc.)
            List<Layer> layers = new List<Layer>();
            Layers = new Collection<Layer>(layers);

            foreach(TmxLayer l in tmx.Layers)
            {
                Layer layer = null;

                PropertyCollection props = new PropertyCollection();
                foreach(KeyValuePair<string, string> kvp in l.Properties)
                {
                    props.Add(kvp);
                }

                layer = new TileLayer(
                    l.Name,
                    tmx.Width,  // As of TiledQT always same as layer.
                    tmx.Height, // As of TiledQT always same as layer.
                    l.Visible,
                    (float) l.Opacity,
                    props,
                    this,
                    l.Tiles
                );
                layers.Add(layer);
                namedLayers.Add(l.Name, layer);
            }

            foreach(TmxObjectGroup og in tmx.ObjectGroups)
            {
                Layer layer = null;

                PropertyCollection props = new PropertyCollection();
                foreach(KeyValuePair<string, string> kvp in og.Properties)
                {
                    props.Add(kvp);
                }

                List<MapObject> objects = new List<MapObject>();

                // read in all of our objects
                foreach(TmxObjectGroup.TmxObject i in og.Objects)
                {
                    Rectangle objLoc = new Rectangle(
                        i.X,
                        i.Y,
                        i.Width,
                        i.Height
                    );

                    List<Point> objPoints = new List<Point>();
                    if (i.Points != null)
                    {
                        foreach (Tuple<int, int> tuple in i.Points)
                        {
                            objPoints.Add(new Point(tuple.Item1, tuple.Item2));
                        }
                    }

                    PropertyCollection objProps = new PropertyCollection();
                    foreach(KeyValuePair<string, string> kvp in i.Properties)
                    {
                        objProps.Add(kvp);
                    }

                    objects.Add(
                        new MapObject(
                            i.Name,
                            i.Type,
                            objLoc,
                            objPoints,
                            objProps)
                    );
                }

                layer = new MapObjectLayer(
                    og.Name,
                    tmx.Width,
                    tmx.Height,
                    og.Visible,
                    (float) og.Opacity,
                    props,
                    objects
                );

                layers.Add(layer);
                namedLayers.Add(og.Name, layer);
            }
        }

Same methods

Map::Map ( Microsoft.Xna.Framework.Content.ContentReader reader ) : System