public void LoadChunk(int xx, int yy)
{
string chunkname = xx + "_" + yy;
if (File.Exists(Main.ChunkPath + "Chunks" + chunkname + ".chnk"))
{
//If the chunk is not loaded, Create a new chunk and begin loading it's tiles.
if (WorldChunks[xx, yy] == null)
{
WorldChunks[xx, yy] = new Chunk(xx, yy);
}
System.Diagnostics.Debug.WriteLine("Loading chunk [" + xx + "," + yy + "]");
Stream stream = File.Open(Main.ChunkPath + "Chunks" + chunkname + ".chnk", FileMode.Open);
StreamReader reader = new StreamReader(stream);
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
WorldChunks[xx, yy].myTiles = JsonConvert.DeserializeObject<byte[,]>(reader.ReadToEnd(), settings);
stream.Close();
System.Diagnostics.Debug.WriteLine("Chunk loaded.. [" + xx + "," + yy + "]");
int x, y;
for (x = 0; x < Main.ChunkSizeX; x++)
{
for (y = 0; y < Main.ChunkSizeY; y++)
{
byte _Type = WorldChunks[xx, yy].myTiles[x, y];
int tx = (xx * ChunkSizeX) + x;
int ty = (yy * ChunkSizeY) + y;
if (_Type > 0)
{
if (Tiles[tx, ty] == null && tx < MaxTilesX && ty < MaxTilesY)
{
Tiles[x + (xx * ChunkSizeX), (yy * ChunkSizeY) + y] = new Tile();
Tiles[(xx * ChunkSizeX) + x, (yy * ChunkSizeY) + y].Type = _Type;
if (ty > GROUND_LEVEL[tx] + 1)
{
//System.Diagnostics.Debug.Write("Below level:" + x + "|" + tx);
Tiles[tx, ty].HasBG = true;
}
}
else continue;
}
}
}
}
}