コード例 #1
0
ファイル: Map.cs プロジェクト: patriksvensson/ld29
 public void AddLayer(int id, Layer layer)
 {
     if (_layers.Length < id + 1)
     {
         Array.Resize(ref _layers, id + 1);
     }
     _layers[id] = layer;
 }
コード例 #2
0
ファイル: Map.cs プロジェクト: patriksvensson/ld29
        public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Camera camera, Layer layer)
        {
            // Calculate the first visible cell index.
            float cameraCellX = (int)camera.Position.X / 16;
            float cameraCellY = (int)camera.Position.Y / 16;
            var firstIndex = (int)cameraCellX + (_size.Width * (int)cameraCellY);

            // Calculate stuff that we will need later.
            var horizontalCells = camera.ScreenSize.Width / 16;
            var verticalCells = camera.ScreenSize.Height / 16;
            var mapCellCount = _size.Width * _size.Height;

            for (var y = 0; y < verticalCells + 1; y++)
            {
                // How much should we increment the index position with?
                var verticalIncrement = y * _size.Width;

                for (var x = 0; x < horizontalCells + 1; x++)
                {
                    var index = firstIndex + verticalIncrement + x;

                    if (index < 0)
                    {
                        continue;
                    }

                    if (index >= mapCellCount)
                    {
                        return;
                    }

                    if (layer.Tiles[index] != null)
                    {
                        var tx = index % _size.Width * 16;
                        var ty = index / _size.Width * 16;
                        var position = new Vector2(tx, ty);
                        position = camera.Transform(position);

                        // Get the source rectangle and the texture.
                        var sourceRectangle = layer.Tiles[index].GetRectangle();
                        var texture = layer.Tiles[index].Tileset.Texture;

                        // Draw the tile.
                        spriteBatch.Draw(texture, position, sourceRectangle, layer.Color);
                    }
                }
            }
        }