示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="StaggeredNeighbours"/> class.
 /// </summary>
 /// <param name="tileMapLayer">The tile map layer.</param>
 /// <param name="center">The center.</param>
 /// <param name="staggerAxis">The stagger axis.</param>
 /// <param name="staggerIndex">Index of the stagger.</param>
 public StaggeredNeighbours(TiledMapLayer tileMapLayer, LayerTile center, TiledMapStaggerAxisType staggerAxis, TiledMapStaggerIndexType staggerIndex)
     : base(tileMapLayer, center)
 {
     this.staggerAxis  = staggerAxis;
     this.staggerIndex = staggerIndex;
 }
示例#2
0
        /// <summary>
        /// Fill VertexBuffer with a tile
        /// </summary>
        /// <param name="tileset">The tileset.</param>
        /// <param name="tile">The tile information.</param>
        /// <param name="tileIndex">Current tileId</param>
        private void FillTile(Tileset tileset, LayerTile tile, int tileIndex)
        {
            int textureWidth  = tileset.Image.Width;
            int textureHeight = tileset.Image.Height;

            var rect = TiledMapUtils.GetRectangleTileByID(tileset, tile.Id);

            RectangleF tileRectangle = new RectangleF(
                rect.X / (float)textureWidth,
                rect.Y / (float)textureHeight,
                rect.Width / (float)textureWidth,
                rect.Height / (float)textureHeight);

            int vertexId = tileIndex * verticesPerTile;

            Vector2 position;

            this.tiledMap.GetTilePosition(tile.X, tile.Y, tileset, out position);
            tile.LocalPosition = position;

            var textCoord0 = new Vector2(tileRectangle.X, tileRectangle.Y);
            var textCoord1 = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y);
            var textCoord2 = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y + tileRectangle.Height);
            var textCoord3 = new Vector2(tileRectangle.X, tileRectangle.Y + tileRectangle.Height);

            #region Flip calculation
            if (tile.HorizontalFlip)
            {
                var texCoordAux = textCoord0;
                textCoord0 = textCoord1;
                textCoord1 = texCoordAux;

                texCoordAux = textCoord2;
                textCoord2  = textCoord3;
                textCoord3  = texCoordAux;
            }

            if (tile.VerticalFlip)
            {
                var texCoordAux = textCoord0;
                textCoord0 = textCoord3;
                textCoord3 = texCoordAux;

                texCoordAux = textCoord2;
                textCoord2  = textCoord1;
                textCoord1  = texCoordAux;
            }

            if (tile.DiagonalFlip)
            {
                var texCoordAux = textCoord0;
                textCoord0 = textCoord2;
                textCoord2 = texCoordAux;
            }
            #endregion

            // Vertex 0
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = textCoord0;
            vertexId++;

            // Vertex 1
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = textCoord1;
            vertexId++;

            // Vertex 2
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = textCoord2;
            vertexId++;

            // Vertex 3
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = textCoord3;
            vertexId++;
        }
        private void UpdateTile(LayerTile layerTile, Entity tileEntity)
        {
            tileEntity.IsVisible = layerTile != null;

            if (layerTile != null)
            {
                tileEntity.FindComponent<Transform2D>().LocalPosition = layerTile.LocalPosition;
            }
        }
        /// <summary>
        /// Initializes this component
        /// </summary>
        protected override void Initialize()
        {
            base.Initialize();
            this.tiledMap = this.Owner.Parent.FindComponent<TiledMap>();

            this.tileTable = new LayerTile[this.tiledMap.Width, this.tiledMap.Height];

            for (int i = 0; i < this.tmxLayer.Tiles.Count; i++)
            {
                var tmxTile = this.tmxLayer.Tiles[i];

                Tileset selectedTileset = null;

                if (tmxTile.Gid > 0)
                {
                    foreach (var tileset in this.tiledMap.Tilesets)
                    {
                        if (tmxTile.Gid <= tileset.LastGid)
                        {
                            selectedTileset = tileset;
                            break;
                        }

                    }
                }

                LayerTile tile = new LayerTile(tmxTile, selectedTileset);
                this.tiles.Add(tile);

                int x = i % this.tiledMap.Width;
                int y = i / this.tiledMap.Width;
                this.tileTable[x, y] = tile;
            }
        }
        /// <summary>
        /// Fill VertexBuffer with a tile
        /// </summary>
        /// <param name="tileset">The tileset.</param>
        /// <param name="tile">The tile information.</param>
        /// <param name="tileIndex">Current tileId</param>
        private void FillTile(Tileset tileset, LayerTile tile, int tileIndex)
        {
            int textureWidth = tileset.Image.Width;
            int textureHeight = tileset.Image.Height;
            int tilesetTileWidth = tileset.TileWidth;
            int tilesetTileHeight = tileset.TileHeight;
            int spacing = tileset.Spacing;
            int margin = tileset.Margin;
            int tileGid = tile.Gid;
            int sheetIndex = tileGid - tileset.FirstGid;

            int rectangleX = (sheetIndex % tileset.XTilesCount);
            int rectangleY = ((sheetIndex - rectangleX) / tileset.XTilesCount);

            Rectangle rect = new Rectangle(
                margin + (tilesetTileWidth + spacing) * rectangleX,
                margin + (tilesetTileHeight + spacing) * rectangleY,
                tilesetTileWidth,
                tilesetTileHeight);

            RectangleF tileRectangle = new RectangleF(
                rect.X / (float)textureWidth,
                rect.Y / (float)textureHeight,
                rect.Width / (float)textureWidth,
                rect.Height / (float)textureHeight);

            int vertexId = tileIndex * verticesPerTile;

            Vector2 position;
            this.GetTilePosition(tile, tileset, out position);

            #region Flip calculation - Original version
            /*
            float texCoordXStart, texCoordXEnd;
            float texCoordYStart, texCoordYEnd;

            if (tile.HorizontalFlip)
            {
                texCoordXStart = tileRectangle.X + tileRectangle.Width;
                texCoordXEnd = tileRectangle.X;
                texCoordYStart = tileRectangle.Y;
                texCoordYEnd = tileRectangle.Y + tileRectangle.Height;
            }
            else if (tile.VerticalFlip)
            {
                texCoordXStart = tileRectangle.X;
                texCoordXEnd = tileRectangle.X + tileRectangle.Width;
                texCoordYStart = tileRectangle.Y + tileRectangle.Height;
                texCoordYEnd = tileRectangle.Y;

            }
            else if (tile.DiagonalFlip)
            {
                texCoordXStart = tileRectangle.X + tileRectangle.Width;
                texCoordXEnd = tileRectangle.X;
                texCoordYStart = tileRectangle.Y + tileRectangle.Height;
                texCoordYEnd = tileRectangle.Y;
            }
            else
            {
                texCoordXStart = tileRectangle.X;
                texCoordXEnd = tileRectangle.X + tileRectangle.Width;
                texCoordYStart = tileRectangle.Y;
                texCoordYEnd = tileRectangle.Y + tileRectangle.Height;
            }

            // Vertex 0
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXStart, texCoordYStart);
            vertexId++;

            // Vertex 1
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXEnd, texCoordYStart);
            vertexId++;

            // Vertex 2
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXEnd, texCoordYEnd);
            vertexId++;

            // Vertex 3
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXStart, texCoordYEnd);
            vertexId++;
            */
            #endregion Flip calculation - Original version

            #region Flip calculation - Fixed version
            var topLeft = new Vector2(tileRectangle.X, tileRectangle.Y);
            var topRight = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y);
            var bottomRight = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y + tileRectangle.Height);
            var bottomLeft = new Vector2(tileRectangle.X, tileRectangle.Y + tileRectangle.Height);

            if (tile.DiagonalFlip)
            {
                var temp = topRight;
                topRight = bottomLeft;
                bottomLeft = temp;
            }

            if (tile.HorizontalFlip)
            {
                var temp = topLeft;
                topLeft = topRight;
                topRight = temp;
                temp = bottomRight;
                bottomRight = bottomLeft;
                bottomLeft = temp;
            }

            if (tile.VerticalFlip)
            {
                var temp = topLeft;
                topLeft = bottomLeft;
                bottomLeft = temp;
                temp = topRight;
                topRight = bottomRight;
                bottomRight = temp;
            }

            // Vertex 0
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = topLeft;
            vertexId++;

            // Vertex 1
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = topRight;
            vertexId++;

            // Vertex 2
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = bottomRight;
            vertexId++;

            // Vertex 3
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = bottomLeft;
            vertexId++;
            #endregion Flip calculation - Fixed version
        }
        /// <summary>
        /// Fill VertexBuffer with a tile
        /// </summary>
        /// <param name="tileset">The tileset.</param>
        /// <param name="tile">The tile information.</param>
        /// <param name="tileIndex">Current tileId</param>
        private void FillTile(Tileset tileset, LayerTile tile, int tileIndex)
        {
            int textureWidth = tileset.Image.Width;
            int textureHeight = tileset.Image.Height;

            var rect = TiledMapUtils.GetRectangleTileByID(tileset, tile.Id);

            RectangleF tileRectangle = new RectangleF(
                rect.X / (float)textureWidth,
                rect.Y / (float)textureHeight,
                rect.Width / (float)textureWidth,
                rect.Height / (float)textureHeight);

            int vertexId = tileIndex * verticesPerTile;

            Vector2 position;
            this.tiledMap.GetTilePosition(tile.X, tile.Y, tileset, out position);
            tile.LocalPosition = position;

            var textCoord0 = new Vector2(tileRectangle.X, tileRectangle.Y);
            var textCoord1 = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y);
            var textCoord2 = new Vector2(tileRectangle.X + tileRectangle.Width, tileRectangle.Y + tileRectangle.Height);
            var textCoord3 = new Vector2(tileRectangle.X, tileRectangle.Y + tileRectangle.Height);

            #region Flip calculation
            if (tile.HorizontalFlip)
            {
                var texCoordAux = textCoord0;
                textCoord0 = textCoord1;
                textCoord1 = texCoordAux;

                texCoordAux = textCoord2;
                textCoord2 = textCoord3;
                textCoord3 = texCoordAux;
            }

            if (tile.VerticalFlip)
            {
                var texCoordAux = textCoord0;
                textCoord0 = textCoord3;
                textCoord3 = texCoordAux;

                texCoordAux = textCoord2;
                textCoord2 = textCoord1;
                textCoord1 = texCoordAux;

            }

            if (tile.DiagonalFlip)
            {
                var texCoordAux = textCoord0;
                textCoord0 = textCoord2;
                textCoord2 = texCoordAux;
            }
            #endregion

            // Vertex 0
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = textCoord0;
            vertexId++;

            // Vertex 1
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = textCoord1;
            vertexId++;

            // Vertex 2
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = textCoord2;
            vertexId++;

            // Vertex 3
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color = Color.White;
            this.vertices[vertexId].TexCoord = textCoord3;
            vertexId++;
        }
示例#7
0
        /// <summary>
        /// Gets a <see cref="NeighboursCollection"/> that contains the neighbour of the spefied tile.
        /// </summary>
        /// <param name="tile">The tile.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">The tile argument can not be null</exception>
        public NeighboursCollection GetNeighboursFromTile(LayerTile tile)
        {
            if (tile == null)
            {
                throw new ArgumentNullException("The tile argument can not be null");
            }

            if (!this.isLayerLoaded)
            {
                return null;
            }

            switch (tiledMap.Orientation)
            {
                case TiledMapOrientationType.Orthogonal:
                    return new OrthogonalNeighbours(this, tile);

                case TiledMapOrientationType.Isometric:
                    return new IsometricNeighbours(this, tile);

                case TiledMapOrientationType.Staggered:
                    return new StaggeredNeighbours(this, tile, tiledMap.StaggerAxis, tiledMap.StaggerIndex);

                case TiledMapOrientationType.Hexagonal:
                    return new HexagonalNeighbours(this, tile, tiledMap.StaggerAxis, tiledMap.StaggerIndex);

                default:
                    return null;
            }
        }
示例#8
0
        /// <summary>
        /// Refresh layer meshes.
        /// </summary>
        private void RefreshMeshes()
        {
            this.meshes.Clear();

            // If the tiles count is changed, clear the previous buffers an init a new ones
            int newNTiles = this.tiledMapLayer.Tiles.Count();

            if (newNTiles != this.nTiles)
            {
                this.RemoveBuffers();
                this.nTiles = newNTiles;

                // Vertices
                this.vertices     = new VertexPositionColorTexture[this.nTiles * verticesPerTile];
                this.vertexBuffer = new DynamicVertexBuffer(VertexPositionColorTexture.VertexFormat);

                // Indices
                ushort[] indices = new ushort[this.nTiles * indicesPerTile];
                for (int i = 0; i < this.nTiles; i++)
                {
                    indices[i * 6]       = (ushort)(i * 4);
                    indices[(i * 6) + 1] = (ushort)((i * 4) + 1);
                    indices[(i * 6) + 2] = (ushort)((i * 4) + 2);
                    indices[(i * 6) + 3] = (ushort)((i * 4) + 2);
                    indices[(i * 6) + 4] = (ushort)((i * 4) + 3);
                    indices[(i * 6) + 5] = (ushort)(i * 4);
                }
                this.indexBuffer = new IndexBuffer(indices);
            }

            int     tileIndex         = 0;
            int     startIndex        = 0;
            int     currentIndexCount = 0;
            Tileset currentTileset    = null;

            for (int i = 0; i < this.tiledMap.Height; i++)
            {
                for (int j = 0; j < this.tiledMap.Width; j++)
                {
                    int x, y;
                    this.GetCellRenderOrderByIndex(j, i, out x, out y);

                    LayerTile tile = this.tiledMapLayer.GetLayerTileByMapCoordinates(x, y);

                    if (tile == null || tile.Gid <= 0 || tile.Tileset == null || tile.Tileset.Image == null)
                    {
                        continue;
                    }

                    Tileset tileset = tile.Tileset;

                    if (currentTileset == null)
                    {
                        currentTileset = tileset;
                    }
                    else if (tileset != currentTileset)
                    {
                        this.NewMesh(startIndex, currentIndexCount, currentTileset.Image);

                        startIndex        = tileIndex;
                        currentIndexCount = 0;
                        currentTileset    = tileset;
                    }

                    this.FillTile(currentTileset, tile, tileIndex);

                    tileIndex++;
                    currentIndexCount++;
                }
            }

            if (currentTileset != null)
            {
                this.vertexBuffer.SetData(this.vertices);
                this.GraphicsDevice.BindVertexBuffer(this.vertexBuffer);

                this.NewMesh(startIndex, currentIndexCount, currentTileset.Image);
            }
        }
示例#9
0
 public void MoveToTile(LayerTile tile)
 {
     this.MoveToTileCoords(tile.X, tile.Y);
 }
示例#10
0
        protected override void Initialize()
        {
            base.Initialize();

            // Generate the Adjacency Matrix based on the TiledMapLayer
            this.groundTiledLayer = this.tiledMap.TileLayers["Ground"];

            this.pathFinder.AdjacencyMatrix = this.GetAdjacencyMatrixFromTiledMapLayer(this.groundTiledLayer, this.weightsByTileId);

            // TODO: NOT NECESSARY
            this.pathFindingAlgorithm.AdjacencyMatrix = this.pathFinder.AdjacencyMatrix;

            this.currentTile = this.groundTiledLayer.GetLayerTileByMapCoordinates(2, 3);

            // Move to starting tile
            this.SetTransformPosition(this.currentTile.LocalPosition);
        }
示例#11
0
        protected override void Update(TimeSpan gameTime)
        {
            if (this.nextTile != null)
            {
                this.elapsed += gameTime;
                var amount = Math.Min(1, this.elapsed.TotalSeconds / 0.5f);

                this.SetTransformPosition(Vector2.Lerp(this.currentTile.LocalPosition, this.nextTile.LocalPosition, (float)amount));

                if (amount >= 1)
                {
                    this.currentTile = nextTile;
                    this.nextTile = null;

                    if (this.pathToFollow.Count == 0)
                    {
                        this.UpdateCurrentAnimation();
                    }
                }
            }
            else if (this.pathToFollow.Count > 0)
            {
                this.nextTile = this.pathToFollow.First();
                this.pathToFollow.Remove(this.nextTile);
                this.elapsed = TimeSpan.Zero;

                this.UpdateCurrentAnimation();
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="OrthogonalNeighbours"/> class.
 /// </summary>
 /// <param name="tileMapLayer">The tile map layer.</param>
 /// <param name="center">The center.</param>
 public OrthogonalNeighbours(TiledMapLayer tileMapLayer, LayerTile center)
     : base(tileMapLayer, center)
 {
 }
        /// <summary>
        /// Obtains the tile position
        /// </summary>
        /// <param name="tile">The tile</param>
        /// <param name="tileset">The tileset</param>
        /// <param name="position">The tile position</param>
        private void GetTilePosition(LayerTile tile, Tileset tileset, out Vector2 position)
        {
            switch (this.tiledMap.Orientation)
            {
                case TiledMapOrientationType.Orthogonal:
                    position = new Vector2(
                        tile.X * this.tiledMap.TileWidth,
                        tile.Y * this.tiledMap.TileHeight);
                    break;

                case TiledMapOrientationType.Isometric:
                    position = new Vector2(
                        ((tile.X - tile.Y) * this.tiledMap.TileWidth * 0.5f) + (this.tiledMap.Height * this.tiledMap.TileWidth * 0.5f) - this.tiledMap.TileWidth * 0.5f,
                        ((tile.X + tile.Y) * this.tiledMap.TileHeight * 0.5f));
                    break;

                case TiledMapOrientationType.Staggered:
                    position = new Vector2(
                        (tile.X + ((tile.Y % 2) * 0.5f)) * this.tiledMap.TileWidth,
                        tile.Y * 0.5f * this.tiledMap.TileHeight
                        );

                    break;
                default:
                    position = Vector2.Zero;
                    break;
            }

            position.Y = position.Y + this.tiledMap.TileHeight - tileset.TileHeight;
        }
示例#14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="OrthogonalNeighbours"/> class.
 /// </summary>
 /// <param name="tileMapLayer">The tile map layer.</param>
 /// <param name="center">The center.</param>
 public OrthogonalNeighbours(TiledMapLayer tileMapLayer, LayerTile center)
     : base(tileMapLayer, center)
 {
 }
示例#15
0
        /// <summary>
        /// Initialize Layer
        /// </summary>
        private void LoadLayer()
        {
            this.tiles = new List<LayerTile>();

            if(this.Owner.Parent == null)
            {
                return;
            }

            this.tiledMap = this.Owner.Parent.FindComponent<TiledMap>();

            if (this.tiledMap != null && this.tiledMap.TmxMap != null)
            {
                this.tmxLayer = this.tiledMap.TmxMap.Layers.FirstOrDefault(l => l.Name == this.tmxLayerName);

                if (this.tmxLayer != null)
                {
                    this.tileTable = new LayerTile[this.tiledMap.Width, this.tiledMap.Height];

                    for (int i = 0; i < this.tmxLayer.Tiles.Count; i++)
                    {
                        var tmxTile = this.tmxLayer.Tiles[i];

                        Tileset selectedTileset = null;

                        if (tmxTile.Gid > 0)
                        {
                            foreach (var tileset in this.tiledMap.Tilesets)
                            {
                                if (tmxTile.Gid <= tileset.LastGid)
                                {
                                    selectedTileset = tileset;
                                    break;
                                }
                            }
                        }

                        LayerTile tile = new LayerTile(tmxTile, selectedTileset);
                        this.tiles.Add(tile);

                        Vector2 tileLocalPosition;
                        this.tiledMap.GetTilePosition(tile.X, tile.Y, selectedTileset, out tileLocalPosition);
                        tile.LocalPosition = tileLocalPosition;

                        int x = i % this.tiledMap.Width;
                        int y = i / this.tiledMap.Width;
                        this.tileTable[x, y] = tile;
                    }

                    this.isLayerLoaded = true;
                    this.NeedRefresh = true;
                }
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="IsometricNeighbours"/> class.
 /// </summary>
 /// <param name="tileMapLayer">The tile map layer.</param>
 /// <param name="center">The center.</param>
 public IsometricNeighbours(TiledMapLayer tileMapLayer, LayerTile center)
     : base(tileMapLayer, center)
 {
 }
        /// <summary>
        /// Refresh layer meshes.
        /// </summary>
        private void RefreshMeshes()
        {
            this.meshes.Clear();

            // If the tiles count is changed, clear the previous buffers an init a new ones
            int newNTiles = this.tiledMapLayer.Tiles.Count();

            if (newNTiles != this.nTiles)
            {
                this.RemoveBuffers();
                this.nTiles = newNTiles;

                var bufferCount = (int)Math.Ceiling((float)newNTiles / MaxTilesPerBuffer);

                this.vertices     = new VertexPositionColorTexture[bufferCount][];
                this.vertexBuffer = new DynamicVertexBuffer[bufferCount];
                this.indexBuffer  = new IndexBuffer[bufferCount];

                for (int j = 0; j < bufferCount; j++)
                {
                    int nBufferTiles = Math.Min(MaxTilesPerBuffer, this.nTiles - (j * MaxTilesPerBuffer));

                    // Vertices
                    this.vertices[j]     = new VertexPositionColorTexture[nBufferTiles * VerticesPerTile];
                    this.vertexBuffer[j] = new DynamicVertexBuffer(VertexPositionColorTexture.VertexFormat);

                    // Indices
                    ushort[] indices = new ushort[nBufferTiles * IndicesPerTile];
                    for (int i = 0; i < nBufferTiles; i++)
                    {
                        indices[i * 6]       = (ushort)(i * 4);
                        indices[(i * 6) + 1] = (ushort)((i * 4) + 1);
                        indices[(i * 6) + 2] = (ushort)((i * 4) + 2);
                        indices[(i * 6) + 3] = (ushort)((i * 4) + 2);
                        indices[(i * 6) + 4] = (ushort)((i * 4) + 3);
                        indices[(i * 6) + 5] = (ushort)(i * 4);
                    }

                    this.indexBuffer[j] = new IndexBuffer(indices);
                }
            }

            int     tileIndex         = 0;
            int     startIndex        = 0;
            int     currentIndexCount = 0;
            int     bufferIndex       = 0;
            var     boundingBox       = new BoundingBox(new Vector3(float.MaxValue), new Vector3(float.MinValue));
            Tileset currentTileset    = null;

            for (int i = 0; i < this.tiledMap.Height; i++)
            {
                for (int j = 0; j < this.tiledMap.Width; j++)
                {
                    int x, y;
                    this.GetCellRenderOrderByIndex(j, i, out x, out y);

                    LayerTile tile = this.tiledMapLayer.GetLayerTileByMapCoordinates(x, y);

                    if (tile == null || tile.Id < 0 || tile.Tileset == null || tile.Tileset.Image == null)
                    {
                        continue;
                    }

                    Tileset tileset = tile.Tileset;

                    if (currentTileset == null)
                    {
                        currentTileset = tileset;
                    }
                    else if (tileset != currentTileset)
                    {
                        this.NewMesh(startIndex, currentIndexCount, bufferIndex, currentTileset.Image, ref boundingBox);

                        startIndex        = tileIndex;
                        currentIndexCount = 0;
                        currentTileset    = tileset;
                        boundingBox       = new BoundingBox(new Vector3(float.MaxValue), new Vector3(float.MinValue));
                    }

                    this.FillTile(currentTileset, tile, bufferIndex, tileIndex, ref boundingBox);

                    tileIndex++;
                    currentIndexCount++;

                    if (tileIndex == MaxTilesPerBuffer)
                    {
                        this.vertexBuffer[bufferIndex].SetData(this.vertices[bufferIndex]);
                        this.GraphicsDevice.BindVertexBuffer(this.vertexBuffer[bufferIndex]);

                        this.NewMesh(startIndex, currentIndexCount, bufferIndex, currentTileset.Image, ref boundingBox);

                        tileIndex         = 0;
                        startIndex        = 0;
                        currentIndexCount = 0;
                        currentTileset    = null;
                        boundingBox       = new BoundingBox(new Vector3(float.MaxValue), new Vector3(float.MinValue));
                        bufferIndex++;
                    }
                }
            }

            if (currentTileset != null)
            {
                this.vertexBuffer[bufferIndex].SetData(this.vertices[bufferIndex]);
                this.GraphicsDevice.BindVertexBuffer(this.vertexBuffer[bufferIndex]);

                this.NewMesh(startIndex, currentIndexCount, bufferIndex, currentTileset.Image, ref boundingBox);
            }

            this.transform2D.Rectangle = this.tiledMap.CalcRectangle();
        }
示例#18
0
        /// <summary>
        /// Fill VertexBuffer with a tile
        /// </summary>
        /// <param name="tileset">The tileset.</param>
        /// <param name="tile">The tile information.</param>
        /// <param name="tileIndex">Current tileId</param>
        private void FillTile(Tileset tileset, LayerTile tile, int tileIndex)
        {
            int textureWidth      = tileset.Image.Width;
            int textureHeight     = tileset.Image.Height;
            int tilesetTileWidth  = tileset.TileWidth;
            int tilesetTileHeight = tileset.TileHeight;
            int spacing           = tileset.Spacing;
            int margin            = tileset.Margin;
            int tileGid           = tile.Gid;
            int sheetIndex        = tileGid - tileset.FirstGid;

            int rectangleX = (sheetIndex % tileset.XTilesCount);
            int rectangleY = ((sheetIndex - rectangleX) / tileset.XTilesCount);

            Rectangle rect = new Rectangle(
                margin + (tilesetTileWidth + spacing) * rectangleX,
                margin + (tilesetTileHeight + spacing) * rectangleY,
                tilesetTileWidth,
                tilesetTileHeight);

            RectangleF tileRectangle = new RectangleF(
                rect.X / (float)textureWidth,
                rect.Y / (float)textureHeight,
                rect.Width / (float)textureWidth,
                rect.Height / (float)textureHeight);

            int vertexId = tileIndex * verticesPerTile;

            Vector2 position;

            this.GetTilePosition(tile, tileset, out position);

            float texCoordXStart, texCoordXEnd;
            float texCoordYStart, texCoordYEnd;

            #region Flip calculation
            if (tile.HorizontalFlip)
            {
                texCoordXStart = tileRectangle.X + tileRectangle.Width;
                texCoordXEnd   = tileRectangle.X;
                texCoordYStart = tileRectangle.Y;
                texCoordYEnd   = tileRectangle.Y + tileRectangle.Height;
            }
            else if (tile.VerticalFlip)
            {
                texCoordXStart = tileRectangle.X;
                texCoordXEnd   = tileRectangle.X + tileRectangle.Width;
                texCoordYStart = tileRectangle.Y + tileRectangle.Height;
                texCoordYEnd   = tileRectangle.Y;
            }
            else if (tile.DiagonalFlip)
            {
                texCoordXStart = tileRectangle.X + tileRectangle.Width;
                texCoordXEnd   = tileRectangle.X;
                texCoordYStart = tileRectangle.Y + tileRectangle.Height;
                texCoordYEnd   = tileRectangle.Y;
            }
            else
            {
                texCoordXStart = tileRectangle.X;
                texCoordXEnd   = tileRectangle.X + tileRectangle.Width;
                texCoordYStart = tileRectangle.Y;
                texCoordYEnd   = tileRectangle.Y + tileRectangle.Height;
            }
            #endregion

            // Vertex 0
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXStart, texCoordYStart);
            vertexId++;

            // Vertex 1
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXEnd, texCoordYStart);
            vertexId++;

            // Vertex 2
            this.vertices[vertexId].Position = new Vector3(position.X + tileset.TileWidth, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXEnd, texCoordYEnd);
            vertexId++;

            // Vertex 3
            this.vertices[vertexId].Position = new Vector3(position.X, position.Y + tileset.TileHeight, 0);
            this.vertices[vertexId].Color    = Color.White;
            this.vertices[vertexId].TexCoord = new Vector2(texCoordXStart, texCoordYEnd);
            vertexId++;
        }
示例#19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="IsometricNeighbours"/> class.
 /// </summary>
 /// <param name="tileMapLayer">The tile map layer.</param>
 /// <param name="center">The center.</param>
 public IsometricNeighbours(TiledMapLayer tileMapLayer, LayerTile center)
     : base(tileMapLayer, center)
 {
 }