private void LoadTileSets(string path)
        {
            m_TmxMap = new TmxMap(path);

            int mapWidth     = m_TmxMap.Width;
            int mapHeight    = m_TmxMap.Height;
            int tileWidth    = m_TmxMap.TileWidth;
            int tileHeight   = m_TmxMap.TileHeight;
            int tileSetCount = m_TmxMap.Tilesets.Count;

            m_Tilemap.Rows    = (ushort)mapHeight;
            m_Tilemap.Columns = (ushort)mapWidth;


            for (int i = 0; i < tileSetCount; i++)
            {
                TmxTileset tileSet = m_TmxMap.Tilesets[i];

                int imageWidth  = tileSet.Image.Width.Value;
                int imageHeight = tileSet.Image.Height.Value;
                int columns     = tileSet.Columns.Value;
                int rows        = tileSet.TileCount.Value / columns;

                string tileSetSource = tileSet.Image.Source;
                int    firstGID      = tileSet.FirstGid;

                float rowOffset    = tileSet.TileHeight / (float)imageHeight;
                float columnOffset = tileSet.TileWidth / (float)imageWidth;

                //TODO: Texture Test
                m_TextureTest = TextureUtils.LoadTexture_PNG(tileSetSource, imageWidth, imageHeight, false, TextureFormat.ARGB32, true);


                //MAPPING UV COORDINATES and RECT TO GID
                //Starts from Top-Left Corner of the TileSet which is the First GId
                //and then proceeds to the Bottom-Right Corner of the tileSet

                int Gid = firstGID;
                int key = 0;
                Dictionary <int, TmxTilesetTile> m_TmxTiles = tileSet.Tiles;

                for (int y = rows - 1; y >= 0; y--)
                {
                    for (int x = 0; x < columns; x++)
                    {
                        Vector2 bottomLeft  = new Vector2(x * columnOffset, y * rowOffset);
                        Vector2 topLeft     = new Vector2(x * columnOffset, (y + 1) * rowOffset);
                        Vector2 bottomRight = new Vector2((x + 1) * columnOffset, y * rowOffset);
                        Vector2 topRight    = new Vector2((x + 1) * columnOffset, (y + 1) * rowOffset);

                        //TODO:! Save FrameData According to Texture Atlas
                        FrameData m_FrameData = new FrameData(new Vector2[] { bottomLeft, topLeft, bottomRight, topRight },
                                                              new Rect(x * columnOffset, y * rowOffset, columnOffset, rowOffset));

                        m_GidToFrameData.Add(Gid, m_FrameData);
                        m_GidToPenalty.Add(Gid, (short)int.Parse(m_TmxTiles[key].Properties["Penalty"]));

                        Gid += 1;
                        key += 1;
                    }
                }

                m_TmxTiles = null;



                //MAPPING ANIMATION FRAMES TO GID
                //Starts from Zero => So both Tile ID and frame.Id is GID - 1
                //Only GIDs start from 1

                if (tileSet.Tiles.Keys != null)
                {
                    int[] keys = tileSet.Tiles.Keys.ToArray();
                    for (int j = 0; j < keys.Length; j++)
                    {
                        int frameCount = tileSet.Tiles[keys[j]].AnimationFrames.Count;
                        if (frameCount > 0)
                        {
                            int baseGID      = tileSet.Tiles[keys[j]].Id + 1;
                            int currentFrame = 0;

                            KeyFrame[] m_KeyFrames     = new KeyFrame[frameCount];
                            var        animationFrames = tileSet.Tiles[keys[j]].AnimationFrames;
                            foreach (var frame in animationFrames)
                            {
                                m_KeyFrames[currentFrame] = new KeyFrame(m_GidToFrameData[frame.Id + 1], (ushort)frame.Duration);
                                currentFrame += 1;
                            }
                            m_BaseGidToAnimation.Add(baseGID, new FrameAnimation(m_KeyFrames));
                        }
                    }
                }

                tileSet = null;
            }
        }
예제 #2
0
        public TileEngine(int mapWidth, int mapHeight, Collection <TmxLayerTile> tiles, TmxTileset tileset, TmxList <TmxObjectGroup> objectGroups)
        {
            Map = new byte[mapWidth, mapHeight];

            foreach (var tile in tiles)
            {
                Map[tile.X, tile.Y] = (byte)tile.Gid;
            }

            TilesetName      = tileset.Name;
            TileWidth        = tileset.TileWidth;
            TileHeight       = tileset.TileHeight;
            TileMapTilesWide = mapWidth;
            TileMapTilesHigh = mapHeight;

            CollidableGids = new List <int>();
            foreach (var tile in tileset.Tiles)
            {
                if (tile.Value.ObjectGroups != null)
                {
                    CollidableGids.Add(tile.Value.Id);
                }
            }
        }
            public void Build()
            {
                Dictionary <string, List <Vertex> > vertices = new Dictionary <string, List <Vertex> >();

                foreach (var tile in layer.Tiles)
                {
                    if (tile.Gid == 0)
                    {
                        continue;
                    }

                    // Find the corresponding tileset
                    TmxTileset tileset = null;
                    foreach (var ts in map.Tilesets)
                    {
                        if (tile.Gid >= ts.FirstGid && tile.Gid < ts.FirstGid + ts.TileCount)
                        {
                            tileset = ts;
                            break;
                        }
                    }
                    if (tileset == null)
                    {
                        continue;
                    }

                    if (!vertices.ContainsKey(tileset.Image.Source))
                    {
                        vertices.Add(tileset.Image.Source, new List <Vertex>());
                    }
                    var vertexList = vertices[tileset.Image.Source];

                    // Prepare the vertices
                    Vertex tl = new Vertex(new Vector2f((float)(layer.OffsetX ?? 0) + tile.X * tileset.TileWidth, (float)(layer.OffsetY ?? 0) + tile.Y * tileset.TileHeight));
                    Vertex bl = new Vertex(new Vector2f(tl.Position.X, tl.Position.Y + tileset.TileHeight));
                    Vertex br = new Vertex(new Vector2f(tl.Position.X + tileset.TileWidth, tl.Position.Y + tileset.TileHeight));
                    Vertex tr = new Vertex(new Vector2f(tl.Position.X + tileset.TileWidth, tl.Position.Y));

                    // Find the tile in the tileset
                    int            lid    = tile.Gid - tileset.FirstGid;
                    TmxTilesetTile tsTile = null;
                    foreach (var tsTileCheck in tileset.Tiles)
                    {
                        if (tsTileCheck.Id == lid)
                        {
                            tsTile = tsTileCheck;
                            break;
                        }
                    }

                    // Do animations
                    if (tsTile != null && tsTile.AnimationFrames != null && tsTile.AnimationFrames.Count != 0)
                    {
                        var anim = new AnimIndex();
                        anim.whichTex     = tileset.Image.Source;
                        anim.vertexOffset = vertexList.Count;
                        anim.tile         = tile;
                        anim.tileset      = tileset;
                        anim.tsTile       = tsTile;
                        anim.animInfo     = tsTile.AnimationFrames;
                        anims.Add(anim);
                        lid = GetAnimationFrame(tsTile.AnimationFrames);
                    }

                    // Do texture coordinates
                    UpdateTexCoords(tile, tileset, lid, ref tl, ref bl, ref br, ref tr);

                    // Add vertices to the layer for rendering
                    vertexList.Add(tl);
                    vertexList.Add(bl);
                    vertexList.Add(br);
                    vertexList.Add(tr);
                }

                this.vertices.Clear();
                foreach (var vertexList in vertices)
                {
                    this.vertices.Add(vertexList.Key, vertexList.Value.ToArray());
                }
            }
예제 #4
0
 public Tileset(TmxTileset tileset)
 {
     _tileset     = tileset;
     _tilesAcross = _tileset.Image.Width / _tileset.TileWidth;
     _tilesDown   = _tileset.Image.Height / _tileset.TileHeight;
 }
예제 #5
0
 // Get the frame of a tile relative to its tileset
 public static int GetTileFrame(int tileGid, TmxTileset tileset)
 {
     return(tileGid - tileset.FirstGid);
 }
예제 #6
0
        /// <summary>
        /// Draw the map using the specified renderer.
        /// </summary>
        /// <param name="renderer">The renderer to use to draw the map.</param>
        public override void Render(Renderer renderer)
        {
            // Flush the buffer.
            renderer.RenderFlush();

            // Check if anything is loaded.
            if (TiledMap == null || !_loaded)
            {
                return;
            }

            // layer - The map layer currently drawing.
            // t - The tile currently drawing from [layer].
            //      tId - The id of the tile within all tilesets combined.
            //      tRect - The location [t] should be drawn to.
            // ts - The tileset of [t].
            //      tsId - The id of [ts] within the collection.
            //      tsOffset - The [tId] within the scope of the current tileset. An id and image within the map containing the ti.
            // ti - The tile image, within the [ts] which represents the image of [tsOffset].
            //      tiUv - The rectangle where the [ti] is located within the [ts] texture.

            // Go through all map layers.
            foreach (TmxLayer layer in TiledMap.Layers)
            {
                // Skip the layer if not visible.
                if (!layer.Visible)
                {
                    continue;
                }

                // Go through all tiles on the layer.
                for (int t = 0; t < layer.Tiles.Count; t++)
                {
                    // Get the id of the tile.
                    int tId = layer.Tiles[t].Gid;

                    // If the tile is empty skip it.
                    if (tId == 0)
                    {
                        continue;
                    }

                    // Find which tileset the tId belongs in.
                    Rectangle  tiUv = GetUvFromTileImageId(tId, out int tsId);
                    TmxTileset ts   = TiledMap.Tilesets[tsId];

                    // Get tile properties.
                    int tX = t % TiledMap.Width * TiledMap.TileWidth;
                    int tY = (int)((float)Math.Floor(t / (double)TiledMap.Width) * TiledMap.TileHeight);

                    // Get the location of the tile.
                    Rectangle tRect = new Rectangle(tX, tY, ts.TileWidth, ts.TileHeight);

                    // Modify for map size.
                    float ratioDifferenceX = Width / (TiledMap.TileWidth * TiledMap.Width);
                    float ratioDifferenceY = Height / (TiledMap.TileHeight * TiledMap.Height);
                    tRect.Width  *= ratioDifferenceX;
                    tRect.Height *= ratioDifferenceY;
                    tRect.X      *= ratioDifferenceX;
                    tRect.Y      *= ratioDifferenceY;

                    // Check if visible rectangle exists.
                    renderer.RenderQueue(tRect.LocationZ(0), tRect.Size, new Color(255, 255, 255, (int)(layer.Opacity * 255)), Tilesets[tsId], tiUv);
                }
            }

            // Flush the rendered tiles.
            renderer.RenderFlush();
        }