开发者

C#: Copy Section of 2D-Array

开发者 https://www.devze.com 2023-01-06 06:31 出处:网络
i\'m working on a Client-Server Game with a tilebased map. If the user changes the visible section of the map (i.e. scrolls in any direction) i query the server for info about the new section giving X

i'm working on a Client-Server Game with a tilebased map. If the user changes the visible section of the map (i.e. scrolls in any direction) i query the server for info about the new section giving X, Y, Width and Height.

The Map info is stored as 2D array

MapTile[,]

(MapTile is a simple Struct)

When the client requests a new section i want to read the section from the map tile array, for that i defined a method

public Map开发者_Go百科Tile[,] GetMapSection(Rectangle area)
{
  [... snip validation ...]
  MapTile[,] result = new MapTile[area.Width, area.Height];

  for (Int32 y = 0; y < area.Height; ++y)
  {
    for (Int32 x = 0; x < area.Width; ++x)
    {
       result[x, y] = this.map[x + area.X, y + area.Y];
    }
  }
  return result;
}

Is there an easier (=> easier to read, understand and maintain, not necessarily faster) way to get the section from the Array?


Not really. This seemes like a sensible and simple way to do it. The reason .NET doesn't provide any built-in method is that, unlike with a 1D array, there are a whole variety of different ways of copying/combining multidimensional arrays.

You're not really going to get faster, and I think it's perfectly maintainable. Seems like pretty fixed code anyway.


I found a link on a MS example for arraycopy with c#

Array copy using pointers

0

精彩评论

暂无评论...
验证码 换一张
取 消