xna 2d瓷砖地图编辑器仅绘制屏幕上的内容

时间:2011-07-20 02:59:48

标签: c# map xna drawing tiles

好的,我有一个2d Tile Map编辑器,我正在xna c#中工作。

在Draw方法中,我循环(使用'for'循环)我的2维拼贴数组,以便 我的地图每帧更新并绘制所有图块。

我的问题是,你如何只画出屏幕上看到的瓷砖。

还有一种更好的方法来绘制平铺贴图(而不是更新每一帧)。

2 个答案:

答案 0 :(得分:1)

在平台游戏演示中我用可见的瓷砖进行了计算,然后只绘制了那些瓷砖。我相信你必须将它们包含在每次绘制的绘制方法中。

这是一个片段(这只是从左到右滚动所以没有计算垂直范围)。这跟踪摄像机位置以计算它。

编辑::添加第二种方法显示它如何根据存储在玩家对象中的玩家位置更新相机位置。

    private void DrawTiles(SpriteBatch spriteBatch)
    {
        // Calculate the visible range of tiles.
        int left = (int)Math.Floor(cameraPosition / Tile.Width);
        int right = left + spriteBatch.GraphicsDevice.Viewport.Width / Tile.Width;
        right = Math.Min(right, Width - 1);
        // For each tile position
        for (int y = 0; y < Height; ++y)
        {
            for (int x = left; x <= right; ++x)
            {
                // If there is a visible tile in that position
                Texture2D texture = tiles[x, y].Texture;
                if (texture != null)
                {
                    // Draw it in screen space.
                    Vector2 position = new Vector2(x, y) * Tile.Size;
                    spriteBatch.Draw(texture, position, Color.White);
                }
            }
        }
    }`


    private void ScrollCamera(Viewport viewport)
    {
        const float ViewMargin = 0.35f;

        // Calculate the edges of the screen.
        float marginWidth = viewport.Width * ViewMargin;
        float marginLeft = cameraPosition + marginWidth;
        float marginRight = cameraPosition + viewport.Width - marginWidth;

        // Calculate how far to scroll when the player is near the edges of the screen.
        float cameraMovement = 0.0f;
        if (Player.Position.X < marginLeft)
            cameraMovement = Player.Position.X - marginLeft;
        else if (Player.Position.X > marginRight)
            cameraMovement = Player.Position.X - marginRight;

        // Update the camera position, but prevent scrolling off the ends of the level.
        float maxCameraPosition = Tile.Width * Width - viewport.Width;
        cameraPosition = MathHelper.Clamp(cameraPosition + cameraMovement, 0.0f, maxCameraPosition);
    }

答案 1 :(得分:0)

int MapSizeX = 20;   
int MapSizeY = 20;
int LeftCornerX = 0;       //the position of the Tile in the 2Darray that is going
int LeftCornerY = 0;       //to be drawn in the left corner of the screen.
int ScreenSizeX = 10;
int ScreenSizeY = 10;

public Tiles[,] tiles = new Tile[MapSizeX, MapSizeY]; //list of all Tiles

//then you can draw it like this....

int counterX = 0;  //represents the position on screen
int counterY = 0;

// y and x inside the for loops represents the position in tiles

for(int y = LeftCornerY; y < MapSizeY <  y++)
{
    for(int x = LeftCornerX; y < MapSizeX <  x++)
    {
         if(counterX  < ScreenSizeX && counterY < ScreenSizeY)
         {
              tiles[x, y].draw(tiles[counterX , counterY]);
         }
         counterX ++;
         //when you do like this you draw the tiles you want
         //at the position you want. In the draw method you just
         // drawn the tile you want at the position of the tile you 
         // send as in parameter to the draw method.
    }
    counterY++;
    counterX = 0;
}

然后你只需要增加LeftCorner变量来绘制地图的另一部分