绘制一个基于区块的地图区块、地图

2023-09-06 06:39:10 作者:非我薄情

该脚本绘制控件,英雄,表面和图:

This script draws the controls, hero, surface and the map:

public void render(Canvas canvas) {
        canvas.drawColor(Color.TRANSPARENT);

        Drawable myImage;

        int tileWidth = 50;
        int tileHeight = 50;

        int rowBaseX = 0;
        int rowBaseY = 0;

        int[][] board = new int[][] {
                {0,0,0,0,0,0,2,0,0,0,},
                {0,0,0,0,0,2,2,0,0,0,},
                {0,0,0,0,0,2,0,0,0,0,},
                {0,0,0,0,0,2,0,0,0,0,},
                {0,0,0,2,2,2,0,0,0,0,},
                {0,0,0,2,0,0,0,0,0,0,},
                {0,0,0,2,0,0,0,0,0,0,},
                {0,0,2,2,0,0,0,0,0,0,},
                {0,0,2,0,0,0,0,0,0,0,},
                {0,0,2,0,0,0,0,0,0,0,}
                };
        int mapWidth = 10;
        int mapHeight = 10;

        for (int row = 0; row < mapHeight; row++)
        {

        for (int col = 0; col < mapWidth; col++)
        {
        Resources res = this.getContext().getResources();

        switch(board[row][col])
        {
        case 0:
        myImage = res.getDrawable(R.drawable.tile1);
        break;
        case 1:
        myImage = res.getDrawable(R.drawable.tile2);
        break;
        default:
        myImage = res.getDrawable(R.drawable.tile3);
        break;
        }

        int curL = rowBaseX + (col * tileWidth);
        int curU = rowBaseY + (row * tileHeight);
        int curR = curL + tileWidth;
        int curD = curU + tileHeight;

        myImage.setBounds(curL,curU,curR,curD);
        myImage.draw(canvas);
        }
        }

        droid.draw(canvas);
        butt.draw(canvas);
        butt1.draw(canvas);
        butt2.draw(canvas);
        butt3.draw(canvas);
        buttz.draw(canvas);
        buttz1.draw(canvas);
        buttz2.draw(canvas);
        buttz3.draw(canvas);
        buttx.draw(canvas);
    }

有一种英雄,其具有当玩家移动他与对照重绘,和所有其他可绘制还具有被重绘。的问题是,在绘制地图是一个漫长的过程,所以更大的地图我创建,较慢的主人公的动作,因为地图的每个瓦片具有待涂漆。有没有办法把所有的瓷砖在其他方法的一个位图,并绘制在画布的方法,一个位图?

There is a hero, which has to be redrawn when player moves him with the controls, and all other drawables also has to be redrawn. The problem is that drawing a map is a long process, so the bigger map i create, the slower hero moves, because every tile of the map has to be painted. Is there a way to put all the tiles to a one bitmap in other method and draw that one bitmap in the canvas method?

推荐答案

最好的办法是只绘制地图的部分是在屏幕上可见。这样,无论多么大的整体地图成为该图的绘制是永远不变的。既然你是在一个网格系统,您可以轻松地找出哪些细胞的主人公是:

The best option is to only draw the portion of your map that is visible on the screen. That way no matter how big the overall map becomes the drawing of that map is always constant. Since you're on a grid system you can easily figure out which cell the hero is in:

heroGridX = hero.x % mapWidth;
heroGridY = hero.y % mapHeight;

从那里,你可以计算出有多少细胞周围的球员,你希望使用的宽度和画布的高度和网格单元格的宽度和高度的大小不变来绘制:

From there you can calculate how many cells around the player you want to draw by using the width and height of the canvas and the constant size of your grid cell's width and height:

leftGrid = heroGridX - (canvas.getWidth() / tileWidth) / 2;
topGrid = heroGridY - (canvas.getHeight() / tileHeight) / 2;
rightGrid = heroGridX + (canvas.getWidth() / tileWidth) / 2;
bottomGrid = heroGridY + (canvas.getHeight() / tileHeight) / 2;

您可以使用一个数据结构来存储这些值的独立英雄的,只有移动它们一旦球员得到接近边缘滚动地图。这样主人公上下移动,而不滚动地图,直到他们获得X或Y像素瓦片的边缘。取而代之的是呈现例程中计算这些。

You could use a data structure to store these values independent of the hero and only move them once the player gets close to an edge to scroll the map. That way the hero moves up and down without scrolling the map until they get X or Y pixels to the edge of a tile. Instead of calculating these inside the rendering routine.

这将使用更少的内存不是整个地图绘制成一个大的位图。绘制成一个大的位图是交易更多的内存使用量较少的CPU时间。由于地图变大也是如此,以绘制地图所需的内存。该算法仅仅保持绘制地图恒定的,因为你的屏幕大小在运行时不改变。而且,相较于其他选项,内存使用率不作为你的地图变得更大发展的任何较大的(它生长相比非常小,以吸引更多的瓷砖在画布)。这一个重要的事实是,如果你没有得到一个更大的屏幕(比如一个片与电话)。该算法将扩大妥善也因此玩家将看到更多的地图周围的地形。

This will use far less memory than drawing the entire map into one large bitmap. Drawing into a large bitmap is trading more memory usage for less CPU time. As your map grows larger so does the memory needed to draw that map. This algorithm merely keeps drawing the map a constant because the size of your screen doesn't change at runtime. And, in comparison to the other option your memory usage doesn't grow any larger as your map grows larger (it grows very small in comparison to drawing more tiles in a canvas). One important fact about this is that if you did get a larger screen (say a tablet vs a phone). This algorithm will scale up properly too so the player will see more of the surrounding terrain of the map.