Hello Code Project,
I have been facing a problem for the last three days. I have been trying to fix it myself no luck. Basically, I am creating a flat grass map by only generating air unless the (Y cooardinate) is below half of the size of the view(ChunkSize).
Here is the code:
// -- Generating the Landscape.
private string CreateChunk(int ChunkX, int ChunkY)
{
string _ReturnString = "";
StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");
for (int i = 0; i < World.ChunkSize; i++)
for (int j = 0; j < World.ChunkSize; j++)
_ReturnString += (char)BlockID.BLOCK_GRASS ;
for (int i = 0; i < ChunkSize; i++)
{
for (int j = 0; j < ChunkSize; j++)
{
char Block;
if (ChunkY > 0 || (ChunkY == 0 && j > ChunkSize / 2))
{
Block = (char)BlockID.BLOCK_GRASS;
}
else
Block = (char)BlockID.BLOCK_AIR;
_ReturnString = ReplaceAtIndex(i * j, Block, _ReturnString);
}
}
_Writer.Write(_ReturnString);
_Writer.Flush();
_Writer.Close();
return _ReturnString;
}
// -- Get a block and also join a string.
static string ReplaceAtIndex(int i, char value, string word)
{
char[] _Characters = word.ToCharArray();
_Characters[i] = value;
return string.Join("", _Characters);
}
public static char GetBlock(int BlockX, int BlockY, string Chunk)
{
char _Block = (char)BlockID.BLOCK_AIR;
_Block = Chunk[BlockX * BlockY];
return _Block;
}
// -- And most importantly the drawing.
public void DrawWorld()
{
for (int i = 0; i < World.ChunkSize; i++)
{
for (int j = 0; j < World.ChunkSize; j++)
{
char Block = (char)World.GetBlock(i, j, ThePlayer.LoadedChunks[5]);
Color toDraw = Color.Transparent;
switch (Block)
{
case (char)BlockID.BLOCK_AIR:
break;
case (char)BlockID.BLOCK_DIRT:
toDraw = Color.Brown;
break;
case (char)BlockID.BLOCK_GRASS:
toDraw = Color.Green;
break;
}
int Size = BlockID.BlockSize;
int X = i == 0 ? 0 : i * Size;
int Y = j == 0 ? 0 : j * Size;
Rectangle _Rec = new Rectangle(X, Y, BlockID.BlockSize, BlockID.BlockSize);
spriteBatch.Draw(ImageBlock, _Rec, toDraw);
}
}
}
And if you are wondering what the result is:
http://i46.tinypic.com/de0daq.jpg[
^]
Edit: For people who do not understand the problem. The problem is that all the green blocks(ID.GrasS) should be under the Height / 2 of the room and now they are all over the place, but I do not understand how I am reading this out wrong ?.
Please help ! :(