Skip to content

Instantly share code, notes, and snippets.

@AngryAnt
Created November 4, 2011 11:13
Show Gist options
  • Select an option

  • Save AngryAnt/1339115 to your computer and use it in GitHub Desktop.

Select an option

Save AngryAnt/1339115 to your computer and use it in GitHub Desktop.
Example of tile level construction in C# - overly simplified, quite useless and completely untested
public Tile tilePrefabs[];
public int width = 1, height = 1;
public Vector2 tileSize = new Vector2 (1.0f, 1.0f);
Tile tiles[];
void Start ()
{
tiles = new Tile[width * height];
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
SetTile (
x,
y,
Instantiate (
tilePrefabs [Random.Range (0, tilePrefabs.length)],
transform.position + transform.right * tileSize.x * x + transform.up * -1.0f * tileSize.y * y,
transform.rotation
)
);
}
}
}
public Tile GetTile (int x, int y)
{
ValidateCoordinates (x, y);
return tiles[y * width + x];
}
public void SetTile (int x, int y, Tile tile)
{
ValidateCoordinates (x, y);
tiles[y * width + x] = tile;
}
void ValidateCoordinates (int x, int y)
{
if (x >= width || y >= height)
{
throw new System.ArgumentException (string.Format ("Tile {0}, {1} is out of bounds ({2} x {3})!", x, y, width, height));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment