Skip to content

Instantly share code, notes, and snippets.

@demonixis
Created November 2, 2012 10:18
Show Gist options
  • Save demonixis/3999959 to your computer and use it in GitHub Desktop.
Save demonixis/3999959 to your computer and use it in GitHub Desktop.
2D Array to 1D Array
[Serializable]
public class Level
{
public int Id { get; protected set; }
public Point3 Position { get; set; }
public Size3 Sizes { get; set; }
public string WallTexture { get; protected set; }
public string GroundTexture { get; protected set; }
public string TopTexture { get; protected set; }
public int Width { get; protected set; }
public int Depth { get; protected set; }
public int[] Tiles { get; protected set; }
public Level(int id)
{
Id = id;
}
public void SetTiles2D(int[,] tiles)
{
Width = tiles.GetLength(0);
Depth = tiles.GetLength(1);
int[] tiles1D = new int[Width * Depth];
Tiles = new int[Width * Depth];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Depth; y++)
{
Tiles[x + y * Width] = tiles[x, y];
}
}
}
public int[,] GetTiles2D()
{
int[,] tiles2D = new int[Width, Depth];
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Depth; y++)
{
tiles2D[x, y] = Tiles[x + y * Width];
}
}
return tiles2D;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment