Exemplo n.º 1
0
 /// <summary>
 /// Gets the <see cref="T:MB2D.Tiles.Tile"/> at the specified x y.
 /// </summary>
 /// <param name="x">The x coordinate.</param>
 /// <param name="y">The y coordinate.</param>
 public Tile this[int x, int y]
 {
     get
     {
         var pos = MBMath.WrapGrid(x, y, _width, _height);
         return(_tiles[pos.X, pos.Y]);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Indexes a world-based coordinate into the collision grid
        /// </summary>
        /// <returns>The grid-based position.</returns>
        /// <param name="position">World-based position to index.</param>
        public Point IndexOf(Point position)
        {
            var pos = MBMath.WrapGrid(
                position.X / _cellSize,
                position.Y / _cellSize,
                _grid.ColCount, _grid.RowCount
                );

            return(new Point
            {
                X = pos.X,
                Y = pos.Y
            });
        }
Exemplo n.º 3
0
        /// <summary>
        /// Draws the tile map to the specified SpriteBatch, wrapping the rendering
        /// when the camera reaches the bounds of the map.
        /// </summary>
        /// <param name="spriteBatch">Sprite batch to draw to.</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            var cameraBounds = MBGame.Camera.BoundingRectangle;

            // Get coordinates to ensure drawing only happens within the
            // bounds of the camera
            var startX = (int)(cameraBounds.Position.X / TileSize.X);
            var startY = (int)(cameraBounds.Position.Y / TileSize.Y);
            var endX   = startX + (int)(cameraBounds.Width / TileSize.X);
            var endY   = startY + (int)(cameraBounds.Height / TileSize.Y);

            // Draw the map
            for (int x = startX - TileSize.X; x < endX + TileSize.X; x++)
            {
                for (int y = startY - TileSize.Y; y < endY + TileSize.Y; y++)
                {
                    // Get the position of the current tile to draw
                    // after wrapping it if it goes out of bounds of the array
                    var drawPos = MBMath.WrapGrid(x, y, _width, _height);
                    // Current tiles bounding rect
                    var drawView = new Rectangle(
                        x * TileSize.X,
                        y * TileSize.Y,
                        TileSize.X + _offset,
                        TileSize.Y + _offset
                        );

                    // Cull the tile
                    if (cameraBounds.Intersects(drawView))
                    {
                        var tile       = _tiles[drawPos.X, drawPos.Y];
                        var tileSource = GetTile(tile.ID);


                        spriteBatch.Draw(
                            texture: Texture,
                            destinationRectangle: drawView,
                            sourceRectangle: tileSource,
                            scale: _scale
                            );
                    }
                }
            }
        }