Search code examples
c#arraystile

C# read only multidimensional array and tilemaps


I made a tile map class, that only contains the data and methods to get or set tiles. Now I have the problem, that I want to make a tile map renderer. But I thing its not very fast to call the methods GetTile(int layerIndex, int x, int y); to iterate threw the tiles and draw them. So I want to give the possibility to get the full array with all data. In this case tiledMapData. But the problem is, that I want that someone cant change the values of the array, because I want make events for changing tiles for physics. How I can do this.

public class TiledMap
{
    public int LayerCount
    {
        get;
        private set;
    }
    public int TileCountX
    {
        get;
        private set;
    }
    public int TileCountY
    {
        get;
        private set;
    }
    public int TotalTileCount
    {
        get;
        private set;
    }

    private int[,] tiledMapData;


    public TiledMap(int layerCount, int tileCountX, int tileCountY)
    {
        LayerCount = layerCount;
        TileCountX = tileCountX;
        TileCountY = tileCountY;
        TotalTileCount = TileCountX * TileCountY;

        tiledMapData = new int[LayerCount, TotalTileCount];
        for (int layerIndex = 0; layerIndex < TotalTileCount; layerIndex++)
        {
            for (int tileIndex = 0; tileIndex < TotalTileCount; tileIndex++)
            {
                tiledMapData[layerIndex, tileIndex] = 0;
            }
        }
    }


    public int  GetTile(int layerIndex, int x, int y)
    {
        return tiledMapData[layerIndex, y * TileCountX + x];
    }
    public void SetTile(int layerIndex, int x, int y, int tileType)
    {
        tiledMapData[layerIndex, y * TileCountX + x] = tileType;
    }
}

And generally, how you would design a tilemap 100 x 100 tiles 16 bit graphics and destroy able/build able tiles, how handle physics player interaction and so on....? Maybe someone know good tutorials(The language is not important)

Thanks for helping me :)


Solution

  • Okay I have to reject this, it makes a difference event when not using Length directly.

            const int countX = 100;
            const int countY = 100;
            const int countLayer = 5;
            TiledMap tm = new TiledMap(countLayer, countX, countY);
    
            const int testRuns = 10000;
    
            Console.WriteLine("Run 1, start");
            DateTime start1 = DateTime.Now;
            for (int test = 0; test < testRuns; test++)
            {
                for (int i = 0; i < countLayer; i++)
                {
                    for (int j = 0; j < countX; j++)
                    {
                        for (int k = 0; k < countY; k++)
                        {
                            int data = tm.GetTile(i, j, k);
                            data++;
                        }
                    }
                }
            }
            DateTime end1 = DateTime.Now;
            Console.WriteLine(String.Format("Run 1, time: {0}", end1-start1));
    
            Console.WriteLine("Run 2, start");
            DateTime start2 = DateTime.Now;
            for (int test = 0; test < testRuns; test++)
            {
                for (int i = 0; i < countLayer; i++)
                {
                    for (int j = 0; j < countX; j++)
                    {
                        for (int k = 0; k < countY; k++)
                        {
                            int data = tm.DirectArray[i, k*countX + j];
                            data++;
                        }
                    }
                }
            }
            DateTime end2 = DateTime.Now;
            Console.WriteLine(String.Format("Run 1, time: {0}", end2 - start2));
    
            Console.ReadLine();
    

    The array access variant performed much better. Still not sure if it would when you use some kind of read only array. So make sure you check it out.