2014-02-10 15 views
0

我做了一個瓦片貼圖類,它只包含獲取或設置貼圖的數據和方法。 現在我遇到了這個問題,我想製作一個瓷磚地圖渲染器。 但我不太快,它調用方法GetTile(int layerIndex,int x,int y);迭代拋出瓦片並繪製它們。 所以我想給出所有數據全部數組的可能性。 在這種情況下tiledMapData。 但問題是,我希望有人不能改變數組的值,因爲我想讓事件改變瓷磚的物理。 我如何做到這一點。C#只讀多維數組和瓦片圖

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; 
    } 
} 

而且一般,你會如何設計一個tilemap的100×100瓦16個圖形和破壞能/建造能夠瓷磚,如何處理物理玩家互動等....? 也許有人知道好的教程(語言並不重要)

謝謝你幫助我:)

+0

你可以簡單地複製數據並返回重複? –

+0

你可以使用jaggard數組http://msdn.microsoft.com/en-us/library/2s05feca.aspx – Thanigainathan

+0

由於在撤銷的答案中指出了@toATwork的數組優化,我猜這個直接訪問是最高性能的。我猜測(沒有測試)即使是多維的ReadOnlyCollection也不能與純粹的aray性能相匹配。 – ZoolWay

回答

2

好吧,我已經拒絕這一點,不能直接使用長度時,有差別的事件。

 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(); 

陣列訪問變體表現要好得多。仍然不確定是否會使用某種只讀陣列。所以一定要檢查一下。

+1

這完全不符合!如果你用for循環直接訪問一個數組(鋸齒狀但不是多維),例如'for(int i = 0; i toATwork

+0

你不需要'長度'。上面的對象具有固定的圖層數X和Y.我正在執行一個性能檢查項目...... – ZoolWay

+1

我做了一次性能檢查 - 並對文檔進行了細緻的閱讀。使用數組和長度運算符,他們進行一些內部優化 - 如前所述,例如,在循環開始時只檢查一次邊界,而不是循環中的每次訪問。 – toATwork