Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0 C#2.0 C#3.0 C# C#4.0 XNA
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.
 
        // -- Create the Chunk.
        /// <summary>
        /// Creates the Chunk at the Cooardinate and returns the string..
        /// </summary>
        /// <param name="?"></param>
        private string CreateChunk(int ChunkX, int ChunkY)
        {
           
            // -- Return String.
            string _ReturnString = "";
 
            // -- Create a stream writer.
            StreamWriter _Writer = new StreamWriter(Path + ChunkX + "_" + ChunkY + ".txt");
 
            // -- Initialize.
            for (int i = 0; i < World.ChunkSize; i++)
                for (int j = 0; j < World.ChunkSize; j++)
                    _ReturnString += (char)BlockID.BLOCK_GRASS ;
 
            // -- Write the world bytes.
            for (int i = 0; i < ChunkSize; i++)
            {
                for (int j = 0; j < ChunkSize; j++)
                {
                    // -- Block.
                    char Block;
                    if (ChunkY > 0 || (ChunkY == 0 && j > ChunkSize / 2))
                    {
                        Block = (char)BlockID.BLOCK_GRASS;
                    }
                    else
                        Block = (char)BlockID.BLOCK_AIR;
 
                    // -- Add Return.
                    _ReturnString = ReplaceAtIndex(i * j, Block, _ReturnString);
 

                }
            }
 
            // -- Write.
            _Writer.Write(_ReturnString);
 
            // -- Close the Writer.
            _Writer.Flush();
            _Writer.Close();
 
            // -- Return.
            return _ReturnString;
        }
 
// -- Get a block and also join a string.
 
        // -- Change a character.
        /// <summary>
        /// Changes a specific character in a string.
        /// </summary>
        /// <param name="i"></param>
        /// <param name="value"></param>
        /// <param name="word"></param>
        /// <returns></returns>
        static string ReplaceAtIndex(int i, char value, string word)
        {
            // -- Create an array of characters out of the string.
            char[] _Characters = word.ToCharArray();
 
            // -- Change the character
            _Characters[i] = value;
 
            // -- Return.
            return string.Join("", _Characters);
        }
 

        // -- Get a block.
        /// <summary>
        /// Returns the block given from the arguments. Note: Also returns air if out of bounds.
        /// </summary>
        /// <param name="BlockX">X for the block.</param>
        /// <param name="BlockY">Y for the block.</param>
        /// <param name="Chunk">The chunk to check in.</param>
        /// <returns></returns>
        public static char GetBlock(int BlockX, int BlockY, string Chunk)
        {
            // -- Block.
            char _Block = (char)BlockID.BLOCK_AIR;
 

            // -- Get the block.
            _Block = Chunk[BlockX * BlockY];
 
            // -- Return.
            return _Block;
        }
 
 

// -- And most importantly the drawing.
// -- Draw World.
        /// <summary>
        /// Draws the World on the Graphics given as parameter.
        /// </summary>
        /// <param name="g"></param>
        public void DrawWorld()
        {
            // -- Loop through the chunkX.
            for (int i = 0; i < World.ChunkSize; i++)
            {
                // -- Loop through the chunkY.
                for (int j = 0; j < World.ChunkSize; j++)
                {
                    // -- Get the Block.
                    char Block = (char)World.GetBlock(i, j, ThePlayer.LoadedChunks[5]);
 
                    // -- Color.
                    Color toDraw = Color.Transparent;
 
                    // -- Get the Right Color.
                    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;
                    }
                    
 
                    // -- Draw the Block.
                    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 ! :(
Posted 24-Dec-12 4:51am
Yvar Birx2.4K
Edited 24-Dec-12 7:19am
v2
Comments
Sergey Alexandrovich Kryukov at 24-Dec-12 12:38pm
   
Help with what? Is there a question here? A problem?
—SA
Yvar Birx at 24-Dec-12 12:51pm
   
Yes, I am generating 'flat grass' and the blocks should be below half of the room, although they are spazzing all over the Room. So the problem is 'reading' out the blocks. THAT is the problem.
austinbox at 24-Dec-12 15:02pm
   
Seems a bit fishy that you have two loops with the same variable names, wouldn't that alone givew you an error?
Yvar Birx at 24-Dec-12 15:07pm
   
No, the thing is, they are even sizes. The Chunk has to be 20 by 20. And I am not going to make two variables valueing 20. :P
Yvar Birx at 24-Dec-12 15:13pm
   
What do you exactly mean ? (Thanks for helping me out btw)
 
The first two for loops make sure the string has the length it needs to have, otherwise I can't change the character.
 
The next two loops Actually generate the landscape.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I think your code could be simplified a little. I'm guessting that I was the x-axis and J was the y-axis
 
for (int i = 0; i < World.ChunkSize * World.ChunkSize; i++)
     _ReturnString += (char)BlockID.BLOCK_AIR;
 
// -- Write the world bytes.
for (int y = 0; y < ChunkSize; y++)
{
     for (int x = 0; x < ChunkSize; x++)
     {
          // -- Block.
         char Block = (char)BlockID.BLOCK_AIR;
         if (y < ChunkSize / 2)
              Block = (char)BlockID.BLOCK_GRASS;
         // -- Add Return.
         _ReturnString = ReplaceAtIndex(x * y, Block, _ReturnString);
     }
}
 
Try that.
  Permalink  
v2
Comments
Yvar Birx at 24-Dec-12 15:22pm
   
This, made it a lot more compact and easy thanks, unfortunatly, reading it out still seems to be messed up, am I using the right calculation(I am using the right chunk don't worry about that).
 
Pic:
 
http://tinypic.com/r/xm99ah/6
austinbox at 24-Dec-12 15:26pm
   
It's got to be your reading, I'm working on it...
 
EDIT: I wouldn't suggest storing data in a one-dimensional array.
Loading is alot easier swhen you use a multi-dimensional array.
I would suggest redoing all of your drawing/loading with this pseudocode.
 
Block[,] blocks = new Block[width, height];
for (int x = 0; x < blocks.GetLength(0); x++)
{
for (int y = 0; y < blocks.GetLength(1); y++)
{//Add filling code here ex. blocks[x, y] = BLOCKID
}
}
 
//And to get a blockid
int blockid = blocks[x, y];
 
Yvar Birx at 24-Dec-12 15:34pm
   
Well, that is the problem. I have 9 chunks loaded in, whenever the player goes into another chunk the chunks are reloaded. This is ThePlayer.LoadedChunks. It's an array of strings, and I can't make an array out of an array. :L
Yvar Birx at 24-Dec-12 15:35pm
   
I could transform the string into an array though, the chunk the player is in, to draw. ?
Yvar Birx at 24-Dec-12 15:36pm
   
I also store those chunk strings in a file. ;l
Yvar Birx at 24-Dec-12 15:52pm
   
Nevermind, you were right, I found a way. Thanks so much !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Gihan Liyanage 332
1 Sergey Alexandrovich Kryukov 330
2 ClimerChinna 222
3 OriginalGriff 206
4 Richard MacCutchan 170
0 Sergey Alexandrovich Kryukov 8,363
1 OriginalGriff 7,112
2 CPallini 2,598
3 Richard MacCutchan 2,025
4 Abhinav S 1,788


Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 24 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100